001/*license*\ 002 Codelet: Copyright (C) 2014, Jeff Epstein (aliteralmind __DASH__ github __AT__ yahoo __DOT__ com) 003 004 This software is dual-licensed under the: 005 - Lesser General Public License (LGPL) version 3.0 or, at your option, any later version; 006 - Apache Software License (ASL) version 2.0. 007 008 Either license may be applied at your discretion. More information may be found at 009 - http://en.wikipedia.org/wiki/Multi-licensing. 010 011 The text of both licenses is available in the root directory of this project, under the names "LICENSE_lgpl-3.0.txt" and "LICENSE_asl-2.0.txt". The latest copies may be downloaded at: 012 - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 013 - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 014\*license*/ 015package com.github.aliteralmind.codelet.alter; 016 import com.github.xbn.analyze.alter.AlterationRequired; 017 import com.github.aliteralmind.codelet.CodeletInstance; 018 import com.github.aliteralmind.codelet.TagletTextUtil; 019 import com.github.xbn.linefilter.alter.NewTextLineAltererFor; 020 import com.github.xbn.linefilter.alter.TextLineAlterer; 021 import com.github.xbn.analyze.validate.NewValidResultFilterFor; 022 import com.github.xbn.analyze.validate.ValidResultFilter; 023 import com.github.xbn.regexutil.NewPatternFor; 024 import com.github.xbn.regexutil.ReplacedInEachInput; 025 import com.github.xbn.util.JavaRegexes; 026 import java.util.regex.Matcher; 027 import java.util.regex.Pattern; 028/** 029 <p>Convenience functions for creating line alterers tailored for codelets.</p> 030 031 @see com.github.xbn.linefilter.alter.TextLineAlterer 032 @see com.github.xbn.linefilter.alter.NewTextLineAltererFor 033 * @since 0.1.0 034 * @author Copyright (C) 2014, Jeff Epstein ({@code aliteralmind __DASH__ github __AT__ yahoo __DOT__ com}), dual-licensed under the LGPL (version 3.0 or later) or the ASL (version 2.0). See source code for details. <a href="http://codelet.aliteralmind.com">{@code http://codelet.aliteralmind.com}</a>, <a href="https://github.com/aliteralmind/codelet">{@code https://github.com/aliteralmind/codelet}</a> 035 **/ 036public class NewLineAltererFor { 037 /** 038 <p>Eliminates all references to a package. Use in conjunction with {@link #eliminatePackageLine(Appendable, Appendable) eliminatePackageLine}.</p> 039 040 * @return 041 042<blockquote><pre>{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor NewTextLineAltererFor}.{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor#replacement(AlterationRequired, Pattern, String, ReplacedInEachInput, Appendable, ValidResultFilter) replacement}({@link com.github.xbn.analyze.alter.AlterationRequired}.{@link com.github.xbn.analyze.alter.AlterationRequired#YES YES}, 043 {@link com.github.xbn.regexutil.NewPatternFor NewPatternFor}.{@link com.github.xbn.regexutil.NewPatternFor#literal(String) literal}({@link com.github.aliteralmind.codelet.TagletTextUtil TagletTextUtil}.{@link com.github.aliteralmind.codelet.TagletTextUtil#getExamplePackageName(CodeletInstance) getExamplePackageName}(instance) + "."), 044 "", {@link com.github.xbn.regexutil.ReplacedInEachInput ReplacedInEachInput}.{@link com.github.xbn.regexutil.ReplacedInEachInput#ALL ALL}, 045 dbgRplcr_ifNonNull, 046 null)</pre></blockquote> 047 */ 048 public static final TextLineAlterer elimPackageReferences(CodeletInstance instance, Appendable dbgRplcr_ifNonNull) { 049 return NewTextLineAltererFor.replacement(AlterationRequired.YES, 050 NewPatternFor.literal(TagletTextUtil.getExamplePackageName(instance) + "."), 051 "", ReplacedInEachInput.ALL, 052 dbgRplcr_ifNonNull, 053 null); 054 } 055 /** 056 <p>Eliminates the package declaration line.</p> 057 058 * @return 059<blockquote><pre>{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor NewTextLineAltererFor}.{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor#text(Pattern, ValidResultFilter, Appendable) text}(packageLinePtrn, firstOccuranceFilter, 060 dbgPkgLnVldtr_ifNonNull)</pre></blockquote> 061 Where {@code packageLinePtrn} is 062 <br/> <code>{@link java.util.regex.Pattern Pattern}.{@link java.util.regex.Pattern#compile(String) compile}({@link com.github.xbn.util.JavaRegexes JavaRegexes}.{@link com.github.xbn.util.JavaRegexes#PACKAGE_DECL_ONE_LINE_NO_CMTS PACKAGE_DECL_ONE_LINE_NO_CMTS})</code> 063 <br/>and {@code firstOccuranceFilter} is a 064 <br/> <code>{@link com.github.xbn.analyze.validate.NewValidResultFilterFor NewValidResultFilterFor}.{@link com.github.xbn.analyze.validate.NewValidResultFilterFor#inUnchangedOutFalse(int, int, String, String, Appendable) inUnchangedOutFalse}(1, 1, null, null, dbgEveryLine_ifNonNull);</code> 065 * @see #elimPackageReferences(CodeletInstance, Appendable) elimPackageReferences 066 */ 067 public static final TextLineAlterer eliminatePackageLine(Appendable dbgEveryLine_ifNonNull, Appendable dbgPkgLnVldtr_ifNonNull) { 068 Pattern packageLinePtrn = Pattern.compile(JavaRegexes.PACKAGE_DECL_ONE_LINE_NO_CMTS); 069 ValidResultFilter firstOccuranceFilter = NewValidResultFilterFor. 070 inUnchangedOutFalse(1, 1, null, null, dbgEveryLine_ifNonNull); 071 072 return NewTextLineAltererFor.textValidateOnly(packageLinePtrn, firstOccuranceFilter, 073 dbgPkgLnVldtr_ifNonNull); 074 } 075 private static final Matcher INDENT_MTCHR = Pattern.compile("\\^[ \\\\t]+").matcher(""); 076 /** 077 <p>Eliminates indentation from every line, if desired.</p> 078 079 * @param indentRegexToElim_emptyStrIfNone The regular expression representing the indentation to eliminate. May not be {@code null}. Must begin with a line-start anchor {@code '^'} and otherwise contain only spaces ({@code ' '}) and tabs {@code '\t'}. 080 * @return If {@code indentRegexToElim_emptyStrIfNone.length()} is<ul> 081 <li>{@code 0}: {@code null}.</li> 082 <li>One or greater: 083<blockquote><pre>{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor NewTextLineAltererFor}.{@link com.github.xbn.linefilter.alter.NewTextLineAltererFor#replacement(AlterationRequired, Pattern, String, ReplacedInEachInput, Appendable, ValidResultFilter) replacement}({@link com.github.xbn.analyze.alter.AlterationRequired}.{@link com.github.xbn.analyze.alter.AlterationRequired#YES YES}, 084 {@link java.util.regex.Pattern Pattern}.{@link java.util.regex.Pattern#compile(String) compile}(indentRegexToElim_emptyStrIfNone), "", 085 {@link com.github.xbn.regexutil.ReplacedInEachInput ReplacedInEachInput}.{@link com.github.xbn.regexutil.ReplacedInEachInput#FIRST FIRST}, debugDest_ifNonNull, null)</pre></blockquote></li> 086 </ul> 087 */ 088 public static final TextLineAlterer eliminateIndentationOrNull(String indentRegexToElim_emptyStrIfNone, Appendable debugDest_ifNonNull) { 089 if(indentRegexToElim_emptyStrIfNone.length() == 0) { 090 return null; 091 } 092 093 if(!INDENT_MTCHR.reset(indentRegexToElim_emptyStrIfNone).matches()) { 094 throw new IllegalArgumentException("Invalid indentation regex: indentRegexToElim_emptyStrIfNone=\"" + indentRegexToElim_emptyStrIfNone + "\""); 095 } 096 097 return NewTextLineAltererFor.replacement(AlterationRequired.YES, 098 Pattern.compile(indentRegexToElim_emptyStrIfNone), "", 099 ReplacedInEachInput.FIRST, debugDest_ifNonNull, null); 100 } 101 private NewLineAltererFor() { 102 throw new IllegalStateException("Do not instantiate"); 103 } 104}