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: TrailingWhitespaceEditor.java 891 2009-11-02 03:40:00Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.util; |
34 | |
35 | /** |
36 | * {@code LineEditor} removing trailing whitespace. |
37 | * |
38 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
39 | * @version $Id: TrailingWhitespaceEditor.java 891 2009-11-02 03:40:00Z schulte2005 $ |
40 | * |
41 | * @see #edit(java.lang.String) |
42 | */ |
43 | public final class TrailingWhitespaceEditor extends LineEditor |
44 | { |
45 | |
46 | /** Creates a new {@code TrailingWhitespaceEditor} instance. */ |
47 | public TrailingWhitespaceEditor() |
48 | { |
49 | super(); |
50 | } |
51 | |
52 | /** |
53 | * Creates a new {@code TrailingWhitespaceEditor} instance taking a string to use for separating lines. |
54 | * |
55 | * @param lineSeparator String to use for separating lines. |
56 | */ |
57 | public TrailingWhitespaceEditor( final String lineSeparator ) |
58 | { |
59 | super( lineSeparator ); |
60 | } |
61 | |
62 | /** |
63 | * Creates a new {@code TrailingWhitespaceEditor} instance taking an editor to chain. |
64 | * |
65 | * @param editor The editor to chain. |
66 | */ |
67 | public TrailingWhitespaceEditor( final LineEditor editor ) |
68 | { |
69 | super( editor ); |
70 | } |
71 | |
72 | /** |
73 | * Creates a new {@code TrailingWhitespaceEditor} instance taking an editor to chain and a string to use for separating lines. |
74 | * |
75 | * @param editor The editor to chain. |
76 | * @param lineSeparator String to use for separating lines. |
77 | */ |
78 | public TrailingWhitespaceEditor( final LineEditor editor, final String lineSeparator ) |
79 | { |
80 | super( editor, lineSeparator ); |
81 | } |
82 | |
83 | @Override |
84 | protected String editLine( final String line ) |
85 | { |
86 | String replacement = line; |
87 | |
88 | if ( line != null ) |
89 | { |
90 | StringBuilder whitespace = null; |
91 | boolean sawWhitespace = false; |
92 | final StringBuilder buf = new StringBuilder( line.length() ); |
93 | final char[] chars = line.toCharArray(); |
94 | |
95 | for ( int i = 0; i < chars.length; i++ ) |
96 | { |
97 | if ( Character.isWhitespace( chars[i] ) ) |
98 | { |
99 | if ( whitespace == null ) |
100 | { |
101 | whitespace = new StringBuilder(); |
102 | } |
103 | |
104 | whitespace.append( chars[i] ); |
105 | sawWhitespace = true; |
106 | } |
107 | else |
108 | { |
109 | if ( sawWhitespace ) |
110 | { |
111 | buf.append( whitespace ); |
112 | sawWhitespace = false; |
113 | whitespace = null; |
114 | } |
115 | buf.append( chars[i] ); |
116 | } |
117 | } |
118 | |
119 | replacement = buf.toString(); |
120 | } |
121 | |
122 | return replacement; |
123 | } |
124 | |
125 | } |