KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > annotation > adapters > CollapsedStringAdapter


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.bind.annotation.adapters;
7
8
9
10 /**
11  * Built-in {@link XmlAdapter} to handle <tt>xs:token</tt> and its derived types.
12  *
13  * <p>
14  * This adapter removes leading and trailing whitespaces, then truncate any
15  * sequnce of tab, CR, LF, and SP by a single whitespace character ' '.
16  *
17  * @author Kohsuke Kawaguchi
18  * @since JAXB 2.0
19  */

20 public class CollapsedStringAdapter extends XmlAdapter<String JavaDoc,String JavaDoc> {
21     /**
22      * Removes leading and trailing whitespaces of the string
23      * given as the parameter, then truncate any
24      * sequnce of tab, CR, LF, and SP by a single whitespace character ' '.
25      */

26     public String JavaDoc unmarshal(String JavaDoc text) {
27         if(text==null) return null; // be defensive
28

29         int len = text.length();
30
31         // most of the texts are already in the collapsed form.
32
// so look for the first whitespace in the hope that we will
33
// never see it.
34
int s=0;
35         while(s<len) {
36             if(isWhiteSpace(text.charAt(s)))
37                 break;
38             s++;
39         }
40         if(s==len)
41             // the input happens to be already collapsed.
42
return text;
43
44         // we now know that the input contains spaces.
45
// let's sit down and do the collapsing normally.
46

47         StringBuffer JavaDoc result = new StringBuffer JavaDoc(len /*allocate enough size to avoid re-allocation*/ );
48
49         if(s!=0) {
50             for( int i=0; i<s; i++ )
51                 result.append(text.charAt(i));
52             result.append(' ');
53         }
54
55         boolean inStripMode = true;
56         for (int i = s+1; i < len; i++) {
57             char ch = text.charAt(i);
58             boolean b = isWhiteSpace(ch);
59             if (inStripMode && b)
60                 continue; // skip this character
61

62             inStripMode = b;
63             if (inStripMode)
64                 result.append(' ');
65             else
66                 result.append(ch);
67         }
68
69         // remove trailing whitespaces
70
len = result.length();
71         if (len > 0 && result.charAt(len - 1) == ' ')
72             result.setLength(len - 1);
73         // whitespaces are already collapsed,
74
// so all we have to do is to remove the last one character
75
// if it's a whitespace.
76

77         return result.toString();
78     }
79
80     /**
81      * No-op.
82      *
83      * Just return the same string given as the parameter.
84      */

85     public String JavaDoc marshal(String JavaDoc s) {
86         return s;
87     }
88
89
90     /** returns true if the specified char is a white space character. */
91     protected static boolean isWhiteSpace(char ch) {
92         // most of the characters are non-control characters.
93
// so check that first to quickly return false for most of the cases.
94
if( ch>0x20 ) return false;
95
96         // other than we have to do four comparisons.
97
return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
98     }
99 }
100
Popular Tags