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 * @version $Id$ 033 * @since 1.1 034 */ 035public abstract class AbstractSink 036 implements Sink, Markup 037{ 038 private Log logger; 039 040 /** {@inheritDoc} */ 041 public void enableLogging( Log log ) 042 { 043 this.logger = log; 044 } 045 046 /** 047 * Returns a logger for this sink. 048 * If no logger has been configured, a new SystemStreamLog is returned. 049 * 050 * @return Log 051 */ 052 protected Log getLog() 053 { 054 if ( logger == null ) 055 { 056 logger = new SystemStreamLog(); 057 } 058 059 return logger; 060 } 061 062 /** 063 * Parses the given String and replaces all occurrences of 064 * '\n', '\r' and '\r\n' with the system EOL. All Sinks should 065 * make sure that text output is filtered through this method. 066 * 067 * @param text the text to scan. 068 * May be null in which case null is returned. 069 * 070 * @return a String that contains only System EOLs. 071 */ 072 protected static String unifyEOLs( String text ) 073 { 074 if ( text == null ) 075 { 076 return null; 077 } 078 079 int length = text.length(); 080 081 StringBuilder buffer = new StringBuilder( length ); 082 083 for ( int i = 0; i < length; i++ ) 084 { 085 if ( text.charAt( i ) == '\r' ) 086 { 087 if ( ( i + 1 ) < length && text.charAt( i + 1 ) == '\n' ) 088 { 089 i++; 090 } 091 092 buffer.append( EOL ); 093 } 094 else if ( text.charAt( i ) == '\n' ) 095 { 096 buffer.append( EOL ); 097 } 098 else 099 { 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}