View Javadoc
1   package org.apache.maven.shared.filtering;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.FilterReader;
23  import java.io.Reader;
24  import java.util.LinkedHashSet;
25  
26  import org.codehaus.plexus.interpolation.multi.DelimiterSpecification;
27  
28  /**
29   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
30   */
31  public abstract class AbstractFilterReaderLineEnding
32      extends FilterReader
33  {
34  
35      private String escapeString;
36  
37      /**
38       * using escape or not.
39       */
40      protected boolean useEscape = false;
41  
42      /**
43       * if true escapeString will be preserved \{foo} -> \{foo}
44       */
45      private boolean preserveEscapeString = false;
46  
47      protected LinkedHashSet<DelimiterSpecification> delimiters = new LinkedHashSet<>();
48  
49      /**
50       * must always be bigger than escape string plus delimiters, but doesn't need to be exact
51       */
52      // CHECKSTYLE_OFF: MagicNumber
53      protected int markLength = 255;
54      // CHECKSTYLE_ON: MagicNumber
55  
56      protected AbstractFilterReaderLineEnding( Reader in )
57      {
58          super( in );
59      }
60  
61      /**
62       * @return the escapce string.
63       */
64      public String getEscapeString()
65      {
66          return escapeString;
67      }
68  
69      /**
70       * @param escapeString Set the value of the escape string.
71       */
72      public void setEscapeString( String escapeString )
73      {
74          // TODO NPE if escapeString is null ?
75          if ( escapeString != null && escapeString.length() >= 1 )
76          {
77              this.escapeString = escapeString;
78              this.useEscape = true;
79              calculateMarkLength();
80          }
81      }
82  
83      /**
84       * @return state of preserve escape string.
85       */
86      public boolean isPreserveEscapeString()
87      {
88          return preserveEscapeString;
89      }
90  
91      /**
92       * @param preserveEscapeString preserve escape string {@code true} or {@code false}.
93       */
94      public void setPreserveEscapeString( boolean preserveEscapeString )
95      {
96          this.preserveEscapeString = preserveEscapeString;
97      }
98  
99      protected void calculateMarkLength()
100     {
101         // CHECKSTYLE_OFF: MagicNumber
102         markLength = 255;
103         // CHECKSTYLE_ON: MagicNumber
104 
105         if ( escapeString != null )
106         {
107             markLength += escapeString.length();
108         }
109         for ( DelimiterSpecification spec : delimiters )
110         {
111             markLength += spec.getBegin().length();
112             markLength += spec.getEnd().length();
113 
114         }
115     }
116 }