001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: TrailingWhitespaceEditor.java 5091 2016-04-04 15:40:17Z schulte $ 029 * 030 */ 031package org.jomc.util; 032 033/** 034 * {@code LineEditor} removing trailing whitespace. 035 * 036 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 037 * @version $JOMC: TrailingWhitespaceEditor.java 5091 2016-04-04 15:40:17Z schulte $ 038 * 039 * @see #edit(java.lang.String) 040 */ 041public final class TrailingWhitespaceEditor extends LineEditor 042{ 043 044 /** 045 * Creates a new {@code TrailingWhitespaceEditor} instance. 046 */ 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 074 * separating lines. 075 * 076 * @param editor The editor to chain. 077 * @param lineSeparator String to use for separating lines. 078 */ 079 public TrailingWhitespaceEditor( final LineEditor editor, final String lineSeparator ) 080 { 081 super( editor, lineSeparator ); 082 } 083 084 /** 085 * {@inheritDoc} 086 * <p> 087 * This method returns {@code line} with any trailing whitespace characters removed. 088 * </p> 089 * 090 * @see Character#isWhitespace(char) 091 */ 092 @Override 093 protected String editLine( final String line ) 094 { 095 String replacement = line; 096 097 if ( line != null ) 098 { 099 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}