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