001package org.apache.maven.doxia.sink.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.doxia.logging.Log; 023import org.apache.maven.doxia.logging.SystemStreamLog; 024import org.apache.maven.doxia.markup.Markup; 025import org.apache.maven.doxia.sink.Sink; 026 027/** 028 * An abstract base class that defines some convenience methods for sinks. 029 * 030 * @author ltheussl 031 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 032 * @since 1.1 033 */ 034public abstract class AbstractSink 035 implements Sink, Markup 036{ 037 private Log logger; 038 039 /** {@inheritDoc} */ 040 public void enableLogging( Log log ) 041 { 042 this.logger = log; 043 } 044 045 /** 046 * Returns a logger for this sink. 047 * If no logger has been configured, a new SystemStreamLog is returned. 048 * 049 * @return Log 050 */ 051 protected Log getLog() 052 { 053 if ( logger == null ) 054 { 055 logger = new SystemStreamLog(); 056 } 057 058 return logger; 059 } 060 061 /** 062 * Parses the given String and replaces all occurrences of 063 * '\n', '\r' and '\r\n' with the system EOL. All Sinks should 064 * make sure that text output is filtered through this method. 065 * 066 * @param text the text to scan. 067 * May be null in which case null is returned. 068 * @return a String that contains only System EOLs. 069 */ 070 protected static String unifyEOLs( String text ) 071 { 072 if ( text == null ) 073 { 074 return null; 075 } 076 077 int length = text.length(); 078 079 StringBuilder buffer = new StringBuilder( length ); 080 081 for ( int i = 0; i < length; i++ ) 082 { 083 if ( text.charAt( i ) == '\r' ) 084 { 085 if ( ( i + 1 ) < length && text.charAt( i + 1 ) == '\n' ) 086 { 087 i++; 088 } 089 090 buffer.append( EOL ); 091 } 092 else if ( text.charAt( i ) == '\n' ) 093 { 094 buffer.append( EOL ); 095 } 096 else 097 { 098 buffer.append( text.charAt( i ) ); 099 } 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}