001 /*
002 * Copyright (c) 2009 The JOMC Project
003 * Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions
008 * are met:
009 *
010 * o Redistributions of source code must retain the above copyright
011 * notice, this list of conditions and the following disclaimer.
012 *
013 * o Redistributions in binary form must reproduce the above copyright
014 * notice, this list of conditions and the following disclaimer in
015 * the documentation and/or other materials provided with the
016 * distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
027 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 *
030 * $Id: LineEditor.java 1869 2010-05-25 16:20:20Z schulte2005 $
031 *
032 */
033 package org.jomc.util;
034
035 import java.io.BufferedReader;
036 import java.io.IOException;
037 import java.io.StringReader;
038
039 /**
040 * Interface to line based editing.
041 *
042 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
043 * @version $Id: LineEditor.java 1869 2010-05-25 16:20:20Z schulte2005 $
044 *
045 * @see #edit(java.lang.String)
046 */
047 public class LineEditor
048 {
049
050 /** Editor to chain. */
051 private LineEditor editor;
052
053 /** Line separator. */
054 private String lineSeparator;
055
056 /** Creates a new {@code LineEditor} instance. */
057 public LineEditor()
058 {
059 this( null, null );
060 }
061
062 /**
063 * Creates a new {@code LineEditor} instance taking a string to use for separating lines.
064 *
065 * @param lineSeparator String to use for separating lines.
066 */
067 public LineEditor( final String lineSeparator )
068 {
069 this( null, lineSeparator );
070 }
071
072 /**
073 * Creates a new {@code LineEditor} instance taking an editor to chain.
074 *
075 * @param editor The editor to chain.
076 */
077 public LineEditor( final LineEditor editor )
078 {
079 this( editor, null );
080 }
081
082 /**
083 * Creates a new {@code LineEditor} instance taking an editor to chain and a string to use for separating lines.
084 *
085 * @param editor The editor to chain.
086 * @param lineSeparator String to use for separating lines.
087 */
088 public LineEditor( final LineEditor editor, final String lineSeparator )
089 {
090 super();
091 this.editor = editor;
092 this.lineSeparator = lineSeparator;
093 }
094
095 /**
096 * Gets the line separator of the editor.
097 *
098 * @return The line separator of the editor.
099 */
100 public final String getLineSeparator()
101 {
102 if ( this.lineSeparator == null )
103 {
104 this.lineSeparator = System.getProperty( "line.separator", "\n" );
105 }
106
107 return this.lineSeparator;
108 }
109
110 /**
111 * Edits text.
112 * <p>This method splits the given string into lines and passes every line to method {@code editLine} in order of
113 * occurrence. On end of input, method {@code editLine} is called with a {@code null} argument.</p>
114 *
115 * @param text The text to edit or {@code null}.
116 *
117 * @return The edited text or {@code null}.
118 *
119 * @throws IOException if editing fails.
120 */
121 public final String edit( final String text ) throws IOException
122 {
123 String edited = text;
124
125 if ( text != null )
126 {
127 final BufferedReader reader = new BufferedReader( new StringReader( text ) );
128 final StringBuilder buf = new StringBuilder();
129
130 String line = null;
131 while ( ( line = reader.readLine() ) != null )
132 {
133 final String replacement = this.editLine( line );
134 if ( replacement != null )
135 {
136 buf.append( replacement ).append( this.getLineSeparator() );
137 }
138 }
139
140 final String replacement = this.editLine( null );
141 if ( replacement != null )
142 {
143 buf.append( replacement );
144 }
145
146 edited = buf.toString();
147
148 if ( this.editor != null )
149 {
150 edited = this.editor.edit( edited );
151 }
152 }
153
154 return edited;
155 }
156
157 /**
158 * Edits a line.
159 *
160 * @param line The line to edit or {@code null} indicating the end of input.
161 *
162 * @return The string to replace {@code line} with, or {@code null} to replace {@code line} with nothing.
163 *
164 * @throws IOException if editing fails.
165 */
166 protected String editLine( final String line ) throws IOException
167 {
168 return line;
169 }
170
171 }