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