1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/annotation/adapters/CollapsedStringAdapter.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 + * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.xml.bind.annotation.adapters; 1.30 + 1.31 + 1.32 + 1.33 +/** 1.34 + * Built-in {@link XmlAdapter} to handle <tt>xs:token</tt> and its derived types. 1.35 + * 1.36 + * <p> 1.37 + * This adapter removes leading and trailing whitespaces, then truncate any 1.38 + * sequnce of tab, CR, LF, and SP by a single whitespace character ' '. 1.39 + * 1.40 + * @author Kohsuke Kawaguchi 1.41 + * @since JAXB 2.0 1.42 + */ 1.43 +public class CollapsedStringAdapter extends XmlAdapter<String,String> { 1.44 + /** 1.45 + * Removes leading and trailing whitespaces of the string 1.46 + * given as the parameter, then truncate any 1.47 + * sequnce of tab, CR, LF, and SP by a single whitespace character ' '. 1.48 + */ 1.49 + public String unmarshal(String text) { 1.50 + if(text==null) return null; // be defensive 1.51 + 1.52 + int len = text.length(); 1.53 + 1.54 + // most of the texts are already in the collapsed form. 1.55 + // so look for the first whitespace in the hope that we will 1.56 + // never see it. 1.57 + int s=0; 1.58 + while(s<len) { 1.59 + if(isWhiteSpace(text.charAt(s))) 1.60 + break; 1.61 + s++; 1.62 + } 1.63 + if(s==len) 1.64 + // the input happens to be already collapsed. 1.65 + return text; 1.66 + 1.67 + // we now know that the input contains spaces. 1.68 + // let's sit down and do the collapsing normally. 1.69 + 1.70 + StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/ ); 1.71 + 1.72 + if(s!=0) { 1.73 + for( int i=0; i<s; i++ ) 1.74 + result.append(text.charAt(i)); 1.75 + result.append(' '); 1.76 + } 1.77 + 1.78 + boolean inStripMode = true; 1.79 + for (int i = s+1; i < len; i++) { 1.80 + char ch = text.charAt(i); 1.81 + boolean b = isWhiteSpace(ch); 1.82 + if (inStripMode && b) 1.83 + continue; // skip this character 1.84 + 1.85 + inStripMode = b; 1.86 + if (inStripMode) 1.87 + result.append(' '); 1.88 + else 1.89 + result.append(ch); 1.90 + } 1.91 + 1.92 + // remove trailing whitespaces 1.93 + len = result.length(); 1.94 + if (len > 0 && result.charAt(len - 1) == ' ') 1.95 + result.setLength(len - 1); 1.96 + // whitespaces are already collapsed, 1.97 + // so all we have to do is to remove the last one character 1.98 + // if it's a whitespace. 1.99 + 1.100 + return result.toString(); 1.101 + } 1.102 + 1.103 + /** 1.104 + * No-op. 1.105 + * 1.106 + * Just return the same string given as the parameter. 1.107 + */ 1.108 + public String marshal(String s) { 1.109 + return s; 1.110 + } 1.111 + 1.112 + 1.113 + /** returns true if the specified char is a white space character. */ 1.114 + protected static boolean isWhiteSpace(char ch) { 1.115 + // most of the characters are non-control characters. 1.116 + // so check that first to quickly return false for most of the cases. 1.117 + if( ch>0x20 ) return false; 1.118 + 1.119 + // other than we have to do four comparisons. 1.120 + return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20; 1.121 + } 1.122 +}