001 /*
002 * Copyright (c) 2009 The JOMC Project
003 * Copyright (c) 2005 Christian Schulte <cs@jomc.org>
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 509 2009-09-21 13:54:49Z schulte2005 $
031 *
032 */
033 package org.jomc.util;
034
035 /**
036 * {@code LineEditor} removing trailing whitespace.
037 *
038 * @author <a href="mailto:cs@jomc.org">Christian Schulte</a>
039 * @version $Id: TrailingWhitespaceEditor.java 509 2009-09-21 13:54:49Z 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 super();
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 super( 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 super( editor );
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 @Override
084 protected String editLine( final String line )
085 {
086 String replacement = line;
087
088 if ( line != null )
089 {
090 StringBuilder whitespace = null;
091 boolean sawWhitespace = false;
092 final StringBuilder buf = new StringBuilder( line.length() );
093 final char[] chars = line.toCharArray();
094
095 for ( int i = 0; i < chars.length; i++ )
096 {
097 if ( Character.isWhitespace( chars[i] ) )
098 {
099 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 }