1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: LineEditor.java 891 2009-11-02 03:40:00Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.util; |
34 | |
35 | import java.io.BufferedReader; |
36 | import java.io.IOException; |
37 | import java.io.StringReader; |
38 | |
39 | /** |
40 | * Interface to line based editing. |
41 | * |
42 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
43 | * @version $Id: LineEditor.java 891 2009-11-02 03:40:00Z schulte2005 $ |
44 | * |
45 | * @see #edit(java.lang.String) |
46 | */ |
47 | public class LineEditor |
48 | { |
49 | |
50 | /** Editor to chain. */ |
51 | private LineEditor editor; |
52 | |
53 | /** Line separator. */ |
54 | private String lineSeparator; |
55 | |
56 | /** Creates a new {@code LineEditor} instance. */ |
57 | public LineEditor() |
58 | { |
59 | super(); |
60 | } |
61 | |
62 | /** |
63 | * Creates a new {@code LineEditor} instance taking a string to use for separating lines. |
64 | * |
65 | * @param lineSeparator String to use for separating lines. |
66 | */ |
67 | public LineEditor( final String lineSeparator ) |
68 | { |
69 | super(); |
70 | this.lineSeparator = lineSeparator; |
71 | } |
72 | |
73 | /** |
74 | * Creates a new {@code LineEditor} instance taking an editor to chain. |
75 | * |
76 | * @param editor The editor to chain. |
77 | */ |
78 | public LineEditor( final LineEditor editor ) |
79 | { |
80 | super(); |
81 | this.editor = editor; |
82 | } |
83 | |
84 | /** |
85 | * Creates a new {@code LineEditor} instance taking an editor to chain and a string to use for separating lines. |
86 | * |
87 | * @param editor The editor to chain. |
88 | * @param lineSeparator String to use for separating lines. |
89 | */ |
90 | public LineEditor( final LineEditor editor, final String lineSeparator ) |
91 | { |
92 | super(); |
93 | this.editor = editor; |
94 | this.lineSeparator = lineSeparator; |
95 | } |
96 | |
97 | /** |
98 | * Gets the line separator of the editor. |
99 | * |
100 | * @return The line separator of the editor. |
101 | */ |
102 | public final String getLineSeparator() |
103 | { |
104 | if ( this.lineSeparator == null ) |
105 | { |
106 | this.lineSeparator = System.getProperty( "line.separator" ); |
107 | } |
108 | |
109 | return this.lineSeparator; |
110 | } |
111 | |
112 | /** |
113 | * Edits text. |
114 | * <p>This method splits the given string into lines and passes every line to method {@code editLine} in order of |
115 | * occurrence. On end of input, method {@code editLine} is called with a {@code null} argument.</p> |
116 | * |
117 | * @param text The text to edit or {@code null}. |
118 | * |
119 | * @return The edited text or {@code null}. |
120 | * |
121 | * @throws IOException if editing fails. |
122 | */ |
123 | public final String edit( final String text ) throws IOException |
124 | { |
125 | String edited = text; |
126 | |
127 | if ( text != null ) |
128 | { |
129 | final BufferedReader reader = new BufferedReader( new StringReader( text ) ); |
130 | final StringBuilder buf = new StringBuilder(); |
131 | |
132 | String line = null; |
133 | while ( ( line = reader.readLine() ) != null ) |
134 | { |
135 | final String replacement = this.editLine( line ); |
136 | if ( replacement != null ) |
137 | { |
138 | buf.append( replacement ).append( this.getLineSeparator() ); |
139 | } |
140 | } |
141 | |
142 | final String replacement = this.editLine( null ); |
143 | if ( replacement != null ) |
144 | { |
145 | buf.append( replacement ); |
146 | } |
147 | |
148 | edited = buf.toString(); |
149 | |
150 | if ( this.editor != null ) |
151 | { |
152 | edited = this.editor.edit( edited ); |
153 | } |
154 | } |
155 | |
156 | return edited; |
157 | } |
158 | |
159 | /** |
160 | * Edits a line. |
161 | * |
162 | * @param line The line to edit or {@code null} indicating the end of input. |
163 | * |
164 | * @return The string to replace {@code line} with, or {@code null} to replace {@code line} with nothing. |
165 | * |
166 | * @throws IOException if editing fails. |
167 | */ |
168 | protected String editLine( final String line ) throws IOException |
169 | { |
170 | return line; |
171 | } |
172 | |
173 | } |