1 5 6 package javax.xml.bind.annotation.adapters; 7 8 9 10 20 public class CollapsedStringAdapter extends XmlAdapter<String ,String > { 21 26 public String unmarshal(String text) { 27 if(text==null) return null; 29 int len = text.length(); 30 31 int s=0; 35 while(s<len) { 36 if(isWhiteSpace(text.charAt(s))) 37 break; 38 s++; 39 } 40 if(s==len) 41 return text; 43 44 47 StringBuffer result = new StringBuffer (len ); 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; 62 inStripMode = b; 63 if (inStripMode) 64 result.append(' '); 65 else 66 result.append(ch); 67 } 68 69 len = result.length(); 71 if (len > 0 && result.charAt(len - 1) == ' ') 72 result.setLength(len - 1); 73 77 return result.toString(); 78 } 79 80 85 public String marshal(String s) { 86 return s; 87 } 88 89 90 91 protected static boolean isWhiteSpace(char ch) { 92 if( ch>0x20 ) return false; 95 96 return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20; 98 } 99 } 100 | Popular Tags |