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: TrailingWhitespaceEditor.java 1869 2010-05-25 16:20:20Z schulte2005 $
031 *
032 */
033 package org.jomc.util;
034
035 /**
036 * {@code LineEditor} removing trailing whitespace.
037 *
038 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
039 * @version $Id: TrailingWhitespaceEditor.java 1869 2010-05-25 16:20:20Z schulte2005 $
040 *
041 * @see #edit(java.lang.String)
042 */
043 public final class TrailingWhitespaceEditor extends LineEditor
044 {
045
046 /** Creates a new {@code TrailingWhitespaceEditor} instance. */
047 public TrailingWhitespaceEditor()
048 {
049 this( null, null );
050 }
051
052 /**
053 * Creates a new {@code TrailingWhitespaceEditor} instance taking a string to use for separating lines.
054 *
055 * @param lineSeparator String to use for separating lines.
056 */
057 public TrailingWhitespaceEditor( final String lineSeparator )
058 {
059 this( null, lineSeparator );
060 }
061
062 /**
063 * Creates a new {@code TrailingWhitespaceEditor} instance taking an editor to chain.
064 *
065 * @param editor The editor to chain.
066 */
067 public TrailingWhitespaceEditor( final LineEditor editor )
068 {
069 this( editor, null );
070 }
071
072 /**
073 * Creates a new {@code TrailingWhitespaceEditor} instance taking an editor to chain and a string to use for separating lines.
074 *
075 * @param editor The editor to chain.
076 * @param lineSeparator String to use for separating lines.
077 */
078 public TrailingWhitespaceEditor( final LineEditor editor, final String lineSeparator )
079 {
080 super( editor, lineSeparator );
081 }
082
083 /**
084 * {@inheritDoc}
085 * <p>This method returns {@code line} with any trailing whitespace characters removed.</p>
086 *
087 * @see Character#isWhitespace(char)
088 */
089 @Override
090 protected String editLine( final String line )
091 {
092 String replacement = line;
093
094 if ( line != null )
095 {
096 StringBuilder whitespace = null;
097 boolean sawWhitespace = false;
098 final StringBuilder buf = new StringBuilder( line.length() );
099 final char[] chars = line.toCharArray();
100
101 for ( int i = 0; i < chars.length; i++ )
102 {
103 if ( Character.isWhitespace( chars[i] ) )
104 {
105 if ( whitespace == null )
106 {
107 whitespace = new StringBuilder();
108 }
109
110 whitespace.append( chars[i] );
111 sawWhitespace = true;
112 }
113 else
114 {
115 if ( sawWhitespace )
116 {
117 buf.append( whitespace );
118 sawWhitespace = false;
119 whitespace = null;
120 }
121 buf.append( chars[i] );
122 }
123 }
124
125 replacement = buf.toString();
126 }
127
128 return replacement;
129 }
130
131 }