src/share/jaxws_classes/javax/xml/bind/annotation/adapters/CollapsedStringAdapter.java

changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.xml.bind.annotation.adapters;
27
28
29
30 /**
31 * Built-in {@link XmlAdapter} to handle <tt>xs:token</tt> and its derived types.
32 *
33 * <p>
34 * This adapter removes leading and trailing whitespaces, then truncate any
35 * sequnce of tab, CR, LF, and SP by a single whitespace character ' '.
36 *
37 * @author Kohsuke Kawaguchi
38 * @since JAXB 2.0
39 */
40 public class CollapsedStringAdapter extends XmlAdapter<String,String> {
41 /**
42 * Removes leading and trailing whitespaces of the string
43 * given as the parameter, then truncate any
44 * sequnce of tab, CR, LF, and SP by a single whitespace character ' '.
45 */
46 public String unmarshal(String text) {
47 if(text==null) return null; // be defensive
48
49 int len = text.length();
50
51 // most of the texts are already in the collapsed form.
52 // so look for the first whitespace in the hope that we will
53 // never see it.
54 int s=0;
55 while(s<len) {
56 if(isWhiteSpace(text.charAt(s)))
57 break;
58 s++;
59 }
60 if(s==len)
61 // the input happens to be already collapsed.
62 return text;
63
64 // we now know that the input contains spaces.
65 // let's sit down and do the collapsing normally.
66
67 StringBuffer result = new StringBuffer(len /*allocate enough size to avoid re-allocation*/ );
68
69 if(s!=0) {
70 for( int i=0; i<s; i++ )
71 result.append(text.charAt(i));
72 result.append(' ');
73 }
74
75 boolean inStripMode = true;
76 for (int i = s+1; i < len; i++) {
77 char ch = text.charAt(i);
78 boolean b = isWhiteSpace(ch);
79 if (inStripMode && b)
80 continue; // skip this character
81
82 inStripMode = b;
83 if (inStripMode)
84 result.append(' ');
85 else
86 result.append(ch);
87 }
88
89 // remove trailing whitespaces
90 len = result.length();
91 if (len > 0 && result.charAt(len - 1) == ' ')
92 result.setLength(len - 1);
93 // whitespaces are already collapsed,
94 // so all we have to do is to remove the last one character
95 // if it's a whitespace.
96
97 return result.toString();
98 }
99
100 /**
101 * No-op.
102 *
103 * Just return the same string given as the parameter.
104 */
105 public String marshal(String s) {
106 return s;
107 }
108
109
110 /** returns true if the specified char is a white space character. */
111 protected static boolean isWhiteSpace(char ch) {
112 // most of the characters are non-control characters.
113 // so check that first to quickly return false for most of the cases.
114 if( ch>0x20 ) return false;
115
116 // other than we have to do four comparisons.
117 return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
118 }
119 }

mercurial