1 /*
2 * Copyright (C) 2005 Christian Schulte <cs@schulte.it>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $JOMC: TrailingWhitespaceEditor.java 5091 2016-04-04 15:40:17Z schulte $
29 *
30 */
31 package org.jomc.util;
32
33 /**
34 * {@code LineEditor} removing trailing whitespace.
35 *
36 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
37 * @version $JOMC: TrailingWhitespaceEditor.java 5091 2016-04-04 15:40:17Z schulte $
38 *
39 * @see #edit(java.lang.String)
40 */
41 public final class TrailingWhitespaceEditor extends LineEditor
42 {
43
44 /**
45 * Creates a new {@code TrailingWhitespaceEditor} instance.
46 */
47 public TrailingWhitespaceEditor()
48 {
49 this( null, null );
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 this( null, 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 this( editor, null );
70 }
71
72 /**
73 * Creates a new {@code TrailingWhitespaceEditor} instance taking an editor to chain and a string to use for
74 * separating lines.
75 *
76 * @param editor The editor to chain.
77 * @param lineSeparator String to use for separating lines.
78 */
79 public TrailingWhitespaceEditor( final LineEditor editor, final String lineSeparator )
80 {
81 super( editor, lineSeparator );
82 }
83
84 /**
85 * {@inheritDoc}
86 * <p>
87 * This method returns {@code line} with any trailing whitespace characters removed.
88 * </p>
89 *
90 * @see Character#isWhitespace(char)
91 */
92 @Override
93 protected String editLine( final String line )
94 {
95 String replacement = line;
96
97 if ( line != null )
98 {
99 StringBuilder whitespace = null;
100 boolean sawWhitespace = false;
101 final StringBuilder buf = new StringBuilder( line.length() );
102 final char[] chars = line.toCharArray();
103
104 for ( int i = 0; i < chars.length; i++ )
105 {
106 if ( Character.isWhitespace( chars[i] ) )
107 {
108 if ( whitespace == null )
109 {
110 whitespace = new StringBuilder( line.length() );
111 }
112
113 whitespace.append( chars[i] );
114 sawWhitespace = true;
115 }
116 else
117 {
118 if ( sawWhitespace )
119 {
120 buf.append( whitespace );
121 sawWhitespace = false;
122 whitespace = null;
123 }
124 buf.append( chars[i] );
125 }
126 }
127
128 replacement = buf.toString();
129 }
130
131 return replacement;
132 }
133
134 }