src/share/jaxws_classes/javax/xml/bind/WhiteSpaceProcessor.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind;
ohair@286 27
ohair@286 28 /**
ohair@286 29 * Processes white space normalization.
ohair@286 30 *
ohair@286 31 * @since 1.0
ohair@286 32 */
ohair@286 33 abstract class WhiteSpaceProcessor {
ohair@286 34
ohair@286 35 // benchmarking (see test/src/ReplaceTest.java in the CVS Attic)
ohair@286 36 // showed that this code is slower than the current code.
ohair@286 37 //
ohair@286 38 // public static String replace(String text) {
ohair@286 39 // final int len = text.length();
ohair@286 40 // StringBuffer result = new StringBuffer(len);
ohair@286 41 //
ohair@286 42 // for (int i = 0; i < len; i++) {
ohair@286 43 // char ch = text.charAt(i);
ohair@286 44 // if (isWhiteSpace(ch))
ohair@286 45 // result.append(' ');
ohair@286 46 // else
ohair@286 47 // result.append(ch);
ohair@286 48 // }
ohair@286 49 //
ohair@286 50 // return result.toString();
ohair@286 51 // }
ohair@286 52
ohair@286 53 public static String replace(String text) {
ohair@286 54 return replace( (CharSequence)text ).toString();
ohair@286 55 }
ohair@286 56
ohair@286 57 /**
ohair@286 58 * @since 2.0
ohair@286 59 */
ohair@286 60 public static CharSequence replace(CharSequence text) {
ohair@286 61 int i=text.length()-1;
ohair@286 62
ohair@286 63 // look for the first whitespace char.
ohair@286 64 while( i>=0 && !isWhiteSpaceExceptSpace(text.charAt(i)) )
ohair@286 65 i--;
ohair@286 66
ohair@286 67 if( i<0 )
ohair@286 68 // no such whitespace. replace(text)==text.
ohair@286 69 return text;
ohair@286 70
ohair@286 71 // we now know that we need to modify the text.
ohair@286 72 // allocate a char array to do it.
ohair@286 73 StringBuilder buf = new StringBuilder(text);
ohair@286 74
ohair@286 75 buf.setCharAt(i--,' ');
ohair@286 76 for( ; i>=0; i-- )
ohair@286 77 if( isWhiteSpaceExceptSpace(buf.charAt(i)))
ohair@286 78 buf.setCharAt(i,' ');
ohair@286 79
ohair@286 80 return new String(buf);
ohair@286 81 }
ohair@286 82
ohair@286 83 /**
ohair@286 84 * Equivalent of {@link String#trim()}.
ohair@286 85 * @since 2.0
ohair@286 86 */
ohair@286 87 public static CharSequence trim(CharSequence text) {
ohair@286 88 int len = text.length();
ohair@286 89 int start = 0;
ohair@286 90
ohair@286 91 while( start<len && isWhiteSpace(text.charAt(start)) )
ohair@286 92 start++;
ohair@286 93
ohair@286 94 int end = len-1;
ohair@286 95
ohair@286 96 while( end>start && isWhiteSpace(text.charAt(end)) )
ohair@286 97 end--;
ohair@286 98
ohair@286 99 if(start==0 && end==len-1)
ohair@286 100 return text; // no change
ohair@286 101 else
ohair@286 102 return text.subSequence(start,end+1);
ohair@286 103 }
ohair@286 104
ohair@286 105 public static String collapse(String text) {
ohair@286 106 return collapse( (CharSequence)text ).toString();
ohair@286 107 }
ohair@286 108
ohair@286 109 /**
ohair@286 110 * This is usually the biggest processing bottleneck.
ohair@286 111 *
ohair@286 112 * @since 2.0
ohair@286 113 */
ohair@286 114 public static CharSequence collapse(CharSequence text) {
ohair@286 115 int len = text.length();
ohair@286 116
ohair@286 117 // most of the texts are already in the collapsed form.
ohair@286 118 // so look for the first whitespace in the hope that we will
ohair@286 119 // never see it.
ohair@286 120 int s=0;
ohair@286 121 while(s<len) {
ohair@286 122 if(isWhiteSpace(text.charAt(s)))
ohair@286 123 break;
ohair@286 124 s++;
ohair@286 125 }
ohair@286 126 if(s==len)
ohair@286 127 // the input happens to be already collapsed.
ohair@286 128 return text;
ohair@286 129
ohair@286 130 // we now know that the input contains spaces.
ohair@286 131 // let's sit down and do the collapsing normally.
ohair@286 132
ohair@286 133 StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/ );
ohair@286 134
ohair@286 135 if(s!=0) {
ohair@286 136 for( int i=0; i<s; i++ )
ohair@286 137 result.append(text.charAt(i));
ohair@286 138 result.append(' ');
ohair@286 139 }
ohair@286 140
ohair@286 141 boolean inStripMode = true;
ohair@286 142 for (int i = s+1; i < len; i++) {
ohair@286 143 char ch = text.charAt(i);
ohair@286 144 boolean b = isWhiteSpace(ch);
ohair@286 145 if (inStripMode && b)
ohair@286 146 continue; // skip this character
ohair@286 147
ohair@286 148 inStripMode = b;
ohair@286 149 if (inStripMode)
ohair@286 150 result.append(' ');
ohair@286 151 else
ohair@286 152 result.append(ch);
ohair@286 153 }
ohair@286 154
ohair@286 155 // remove trailing whitespaces
ohair@286 156 len = result.length();
ohair@286 157 if (len > 0 && result.charAt(len - 1) == ' ')
ohair@286 158 result.setLength(len - 1);
ohair@286 159 // whitespaces are already collapsed,
ohair@286 160 // so all we have to do is to remove the last one character
ohair@286 161 // if it's a whitespace.
ohair@286 162
ohair@286 163 return result;
ohair@286 164 }
ohair@286 165
ohair@286 166 /**
ohair@286 167 * Returns true if the specified string is all whitespace.
ohair@286 168 */
ohair@286 169 public static final boolean isWhiteSpace(CharSequence s) {
ohair@286 170 for( int i=s.length()-1; i>=0; i-- )
ohair@286 171 if(!isWhiteSpace(s.charAt(i)))
ohair@286 172 return false;
ohair@286 173 return true;
ohair@286 174 }
ohair@286 175
ohair@286 176 /** returns true if the specified char is a white space character. */
ohair@286 177 public static final boolean isWhiteSpace(char ch) {
ohair@286 178 // most of the characters are non-control characters.
ohair@286 179 // so check that first to quickly return false for most of the cases.
ohair@286 180 if( ch>0x20 ) return false;
ohair@286 181
ohair@286 182 // other than we have to do four comparisons.
ohair@286 183 return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
ohair@286 184 }
ohair@286 185
ohair@286 186 /**
ohair@286 187 * Returns true if the specified char is a white space character
ohair@286 188 * but not 0x20.
ohair@286 189 */
ohair@286 190 protected static final boolean isWhiteSpaceExceptSpace(char ch) {
ohair@286 191 // most of the characters are non-control characters.
ohair@286 192 // so check that first to quickly return false for most of the cases.
ohair@286 193 if( ch>=0x20 ) return false;
ohair@286 194
ohair@286 195 // other than we have to do four comparisons.
ohair@286 196 return ch == 0x9 || ch == 0xA || ch == 0xD;
ohair@286 197 }
ohair@286 198 }

mercurial