ohair@286: /* mkos@397: * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package javax.xml.bind.annotation.adapters; ohair@286: ohair@286: ohair@286: ohair@286: /** ohair@286: * {@link XmlAdapter} to handle xs:normalizedString. ohair@286: * ohair@286: *

ohair@286: * Replaces any tab, CR, and LF by a whitespace character ' ', ohair@286: * as specified in the whitespace facet 'replace' ohair@286: * ohair@286: * @author Kohsuke Kawaguchi, Martin Grebac ohair@286: * @since JAXB 2.0 ohair@286: */ ohair@286: public final class NormalizedStringAdapter extends XmlAdapter { ohair@286: /** ohair@286: * Replace any tab, CR, and LF by a whitespace character ' ', ohair@286: * as specified in the whitespace facet 'replace' ohair@286: */ ohair@286: public String unmarshal(String text) { ohair@286: if(text==null) return null; // be defensive ohair@286: ohair@286: int i=text.length()-1; ohair@286: ohair@286: // look for the first whitespace char. ohair@286: while( i>=0 && !isWhiteSpaceExceptSpace(text.charAt(i)) ) ohair@286: i--; ohair@286: ohair@286: if( i<0 ) ohair@286: // no such whitespace. replace(text)==text. ohair@286: return text; ohair@286: ohair@286: // we now know that we need to modify the text. ohair@286: // allocate a char array to do it. ohair@286: char[] buf = text.toCharArray(); ohair@286: ohair@286: buf[i--] = ' '; ohair@286: for( ; i>=0; i-- ) ohair@286: if( isWhiteSpaceExceptSpace(buf[i])) ohair@286: buf[i] = ' '; ohair@286: ohair@286: return new String(buf); ohair@286: } ohair@286: ohair@286: /** ohair@286: * No-op. ohair@286: * ohair@286: * Just return the same string given as the parameter. ohair@286: */ ohair@286: public String marshal(String s) { ohair@286: return s; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Returns true if the specified char is a white space character ohair@286: * but not 0x20. ohair@286: */ ohair@286: protected static boolean isWhiteSpaceExceptSpace(char ch) { ohair@286: // most of the characters are non-control characters. ohair@286: // so check that first to quickly return false for most of the cases. ohair@286: if( ch>=0x20 ) return false; ohair@286: ohair@286: // other than we have to do four comparisons. ohair@286: return ch == 0x9 || ch == 0xA || ch == 0xD; ohair@286: } ohair@286: }