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