ohair@286: /* mkos@397: * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package javax.xml.bind; ohair@286: ohair@286: /** ohair@286: * Processes white space normalization. ohair@286: * ohair@286: * @since 1.0 ohair@286: */ ohair@286: abstract class WhiteSpaceProcessor { ohair@286: ohair@286: // benchmarking (see test/src/ReplaceTest.java in the CVS Attic) ohair@286: // showed that this code is slower than the current code. ohair@286: // ohair@286: // public static String replace(String text) { ohair@286: // final int len = text.length(); ohair@286: // StringBuffer result = new StringBuffer(len); ohair@286: // ohair@286: // for (int i = 0; i < len; i++) { ohair@286: // char ch = text.charAt(i); ohair@286: // if (isWhiteSpace(ch)) ohair@286: // result.append(' '); ohair@286: // else ohair@286: // result.append(ch); ohair@286: // } ohair@286: // ohair@286: // return result.toString(); ohair@286: // } ohair@286: ohair@286: public static String replace(String text) { ohair@286: return replace( (CharSequence)text ).toString(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * @since 2.0 ohair@286: */ ohair@286: public static CharSequence replace(CharSequence text) { ohair@286: int i=text.length()-1; ohair@286: ohair@286: // look for the first whitespace char. ohair@286: while( i>=0 && !isWhiteSpaceExceptSpace(text.charAt(i)) ) ohair@286: i--; ohair@286: ohair@286: if( i<0 ) ohair@286: // no such whitespace. replace(text)==text. ohair@286: return text; ohair@286: ohair@286: // we now know that we need to modify the text. ohair@286: // allocate a char array to do it. ohair@286: StringBuilder buf = new StringBuilder(text); ohair@286: ohair@286: buf.setCharAt(i--,' '); ohair@286: for( ; i>=0; i-- ) ohair@286: if( isWhiteSpaceExceptSpace(buf.charAt(i))) ohair@286: buf.setCharAt(i,' '); ohair@286: ohair@286: return new String(buf); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Equivalent of {@link String#trim()}. ohair@286: * @since 2.0 ohair@286: */ ohair@286: public static CharSequence trim(CharSequence text) { ohair@286: int len = text.length(); ohair@286: int start = 0; ohair@286: ohair@286: while( startstart && isWhiteSpace(text.charAt(end)) ) ohair@286: end--; ohair@286: ohair@286: if(start==0 && end==len-1) ohair@286: return text; // no change ohair@286: else ohair@286: return text.subSequence(start,end+1); ohair@286: } ohair@286: ohair@286: public static String collapse(String text) { ohair@286: return collapse( (CharSequence)text ).toString(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * This is usually the biggest processing bottleneck. ohair@286: * ohair@286: * @since 2.0 ohair@286: */ ohair@286: public static CharSequence collapse(CharSequence text) { ohair@286: int len = text.length(); ohair@286: ohair@286: // most of the texts are already in the collapsed form. ohair@286: // so look for the first whitespace in the hope that we will ohair@286: // never see it. ohair@286: int s=0; ohair@286: while(s 0 && result.charAt(len - 1) == ' ') ohair@286: result.setLength(len - 1); ohair@286: // whitespaces are already collapsed, ohair@286: // so all we have to do is to remove the last one character ohair@286: // if it's a whitespace. ohair@286: ohair@286: return result; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns true if the specified string is all whitespace. ohair@286: */ ohair@286: public static final boolean isWhiteSpace(CharSequence s) { ohair@286: for( int i=s.length()-1; i>=0; i-- ) ohair@286: if(!isWhiteSpace(s.charAt(i))) ohair@286: return false; ohair@286: return true; ohair@286: } ohair@286: ohair@286: /** returns true if the specified char is a white space character. */ ohair@286: public static final boolean isWhiteSpace(char ch) { ohair@286: // most of the characters are non-control characters. ohair@286: // so check that first to quickly return false for most of the cases. ohair@286: if( ch>0x20 ) return false; ohair@286: ohair@286: // other than we have to do four comparisons. ohair@286: return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns true if the specified char is a white space character ohair@286: * but not 0x20. ohair@286: */ ohair@286: protected static final boolean isWhiteSpaceExceptSpace(char ch) { ohair@286: // most of the characters are non-control characters. ohair@286: // so check that first to quickly return false for most of the cases. ohair@286: if( ch>=0x20 ) return false; ohair@286: ohair@286: // other than we have to do four comparisons. ohair@286: return ch == 0x9 || ch == 0xA || ch == 0xD; ohair@286: } ohair@286: }