1 /* 2 * Copyright (c) 2004, 2013, 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 * {@link XmlAdapter} to handle {@code xs:normalizedString}. 32 * 33 * <p> 34 * Replaces any tab, CR, and LF by a whitespace character ' ', 35 * as specified in <a href="http://www.w3.org/TR/xmlschema-2/#rf-whiteSpace">the whitespace facet 'replace'</a> 36 * 37 * @author Kohsuke Kawaguchi, Martin Grebac 38 * @since 1.6, JAXB 2.0 39 */ 40 public final class NormalizedStringAdapter extends XmlAdapter<String,String> { 41 /** 42 * Replace any tab, CR, and LF by a whitespace character ' ', 43 * as specified in <a href="http://www.w3.org/TR/xmlschema-2/#rf-whiteSpace">the whitespace facet 'replace'</a> 44 */ 45 public String unmarshal(String text) { 46 if(text==null) return null; // be defensive 47 48 int i=text.length()-1; 49 50 // look for the first whitespace char. 51 while( i>=0 && !isWhiteSpaceExceptSpace(text.charAt(i)) ) 52 i--; 53 54 if( i<0 ) 55 // no such whitespace. replace(text)==text. 56 return text; 57 58 // we now know that we need to modify the text. 59 // allocate a char array to do it. 60 char[] buf = text.toCharArray(); 61 62 buf[i--] = ' '; 63 for( ; i>=0; i-- ) 64 if( isWhiteSpaceExceptSpace(buf[i])) 65 buf[i] = ' '; 66 67 return new String(buf); 68 } 69 70 /** 71 * No-op. 72 * 73 * Just return the same string given as the parameter. 74 */ 75 public String marshal(String s) { 76 return s; 77 } 78 79 80 /** 81 * Returns true if the specified char is a white space character 82 * but not 0x20. 83 */ 84 protected static boolean isWhiteSpaceExceptSpace(char ch) { 85 // most of the characters are non-control characters. 86 // so check that first to quickly return false for most of the cases. 87 if( ch>=0x20 ) return false; 88 89 // other than we have to do four comparisons. 90 return ch == 0x9 || ch == 0xA || ch == 0xD; 91 } 92 }