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      protected int markLength = 128;
53  
54      protected AbstractFilterReaderLineEnding( Reader in )
55      {
56          super( in );
57      }
58  
59      /**
60       * @return the escapce string.
61       */
62      public String getEscapeString()
63      {
64          return escapeString;
65      }
66  
67      /**
68       * @param escapeString Set the value of the escape string.
69       */
70      public void setEscapeString( String escapeString )
71      {
72          // TODO NPE if escapeString is null ?
73          if ( escapeString != null && escapeString.length() >= 1 )
74          {
75              this.escapeString = escapeString;
76              this.useEscape = escapeString != null && escapeString.length() >= 1;
77              calculateMarkLength();
78          }
79      }
80  
81      /**
82       * @return state of preserve escape string.
83       */
84      public boolean isPreserveEscapeString()
85      {
86          return preserveEscapeString;
87      }
88  
89      /**
90       * @param preserveEscapeString preserve escape string {@code true} or {@code false}.
91       */
92      public void setPreserveEscapeString( boolean preserveEscapeString )
93      {
94          this.preserveEscapeString = preserveEscapeString;
95      }
96  
97      protected void calculateMarkLength()
98      {
99          markLength = 128;
100 
101         if ( escapeString != null )
102         {
103 
104             markLength += escapeString.length();
105 
106         }
107         for ( DelimiterSpecification spec : delimiters )
108         {
109             markLength += spec.getBegin().length();
110             markLength += spec.getEnd().length();
111 
112         }
113     }
114 }