001package org.apache.maven.doxia.sink; 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; 025 026/** 027 * An abstract base class that defines some convenience methods for sinks. 028 * 029 * @author ltheussl 030 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 031 * @version $Id$ 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 * 069 * @return a String that contains only System EOLs. 070 */ 071 protected static String unifyEOLs( String text ) 072 { 073 if ( text == null ) 074 { 075 return null; 076 } 077 078 int length = text.length(); 079 080 StringBuilder buffer = new StringBuilder( length ); 081 082 for ( int i = 0; i < length; i++ ) 083 { 084 if ( text.charAt( i ) == '\r' ) 085 { 086 if ( ( i + 1 ) < length && text.charAt( i + 1 ) == '\n' ) 087 { 088 i++; 089 } 090 091 buffer.append( EOL ); 092 } 093 else if ( text.charAt( i ) == '\n' ) 094 { 095 buffer.append( EOL ); 096 } 097 else 098 { 099 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}