View Javadoc
1   package org.apache.maven.doxia.sink.impl;
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 org.apache.maven.doxia.logging.Log;
23  import org.apache.maven.doxia.logging.SystemStreamLog;
24  import org.apache.maven.doxia.markup.Markup;
25  import org.apache.maven.doxia.sink.Sink;
26  
27  /**
28   * An abstract base class that defines some convenience methods for sinks.
29   *
30   * @author ltheussl
31   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   * @since 1.1
33   */
34  public abstract class AbstractSink
35      implements Sink, Markup
36  {
37      private Log logger;
38  
39      /** {@inheritDoc} */
40      public void enableLogging( Log log )
41      {
42          this.logger = log;
43      }
44  
45      /**
46       * Returns a logger for this sink.
47       * If no logger has been configured, a new SystemStreamLog is returned.
48       *
49       * @return Log
50       */
51      protected Log getLog()
52      {
53          if ( logger == null )
54          {
55              logger = new SystemStreamLog();
56          }
57  
58          return logger;
59      }
60  
61       /**
62        * Parses the given String and replaces all occurrences of
63        * '\n', '\r' and '\r\n' with the system EOL. All Sinks should
64        * make sure that text output is filtered through this method.
65        *
66        * @param text the text to scan.
67        *      May be null in which case null is returned.
68        * @return a String that contains only System EOLs.
69        */
70       protected static String unifyEOLs( String text )
71       {
72          if ( text == null )
73          {
74              return null;
75          }
76  
77          int length = text.length();
78  
79          StringBuilder buffer = new StringBuilder( length );
80  
81          for ( int i = 0; i < length; i++ )
82          {
83              if ( text.charAt( i ) == '\r' )
84              {
85                  if ( ( i + 1 ) < length && text.charAt( i + 1 ) == '\n' )
86                  {
87                      i++;
88                  }
89  
90                  buffer.append( EOL );
91              }
92              else if ( text.charAt( i ) == '\n' )
93              {
94                  buffer.append( EOL );
95              }
96              else
97              {
98                  buffer.append( text.charAt( i ) );
99              }
100         }
101 
102         return buffer.toString();
103     }
104 
105      /**
106       * This is called in {@link #head()} or in {@link #close()}, and can be used
107       * to set the sink into a clear state so it can be re-used.
108       *
109       * @since 1.1.2
110       */
111      protected void init()
112      {
113          // nop
114      }
115 }