1 package org.apache.maven.doxia.sink;
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
26 /**
27 * An abstract base class that defines some convenience methods for sinks.
28 *
29 * @author ltheussl
30 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31 * @version $Id: AbstractSink.java 1185112 2011-10-17 11:33:00Z ltheussl $
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 *
69 * @return a String that contains only System EOLs.
70 */
71 protected static String unifyEOLs( String text )
72 {
73 if ( text == null )
74 {
75 return null;
76 }
77
78 int length = text.length();
79
80 StringBuilder buffer = new StringBuilder( length );
81
82 for ( int i = 0; i < length; i++ )
83 {
84 if ( text.charAt( i ) == '\r' )
85 {
86 if ( ( i + 1 ) < length && text.charAt( i + 1 ) == '\n' )
87 {
88 i++;
89 }
90
91 buffer.append( EOL );
92 }
93 else if ( text.charAt( i ) == '\n' )
94 {
95 buffer.append( EOL );
96 }
97 else
98 {
99 buffer.append( text.charAt( i ) );
100 }
101 }
102
103 return buffer.toString();
104 }
105
106 /**
107 * This is called in {@link #head()} or in {@link #close()}, and can be used
108 * to set the sink into a clear state so it can be re-used.
109 *
110 * @since 1.1.2
111 */
112 protected void init()
113 {
114 // nop
115 }
116 }