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: * Built-in {@link XmlAdapter} to handle xs:token and its derived types. ohair@286: * ohair@286: *

ohair@286: * This adapter removes leading and trailing whitespaces, then truncate any ohair@286: * sequnce of tab, CR, LF, and SP by a single whitespace character ' '. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: * @since JAXB 2.0 ohair@286: */ ohair@286: public class CollapsedStringAdapter extends XmlAdapter { ohair@286: /** ohair@286: * Removes leading and trailing whitespaces of the string ohair@286: * given as the parameter, then truncate any ohair@286: * sequnce of tab, CR, LF, and SP by a single whitespace character ' '. ohair@286: */ ohair@286: public String unmarshal(String text) { ohair@286: if(text==null) return null; // be defensive ohair@286: ohair@286: int len = text.length(); ohair@286: ohair@286: // most of the texts are already in the collapsed form. ohair@286: // so look for the first whitespace in the hope that we will ohair@286: // never see it. ohair@286: int s=0; ohair@286: while(s 0 && result.charAt(len - 1) == ' ') ohair@286: result.setLength(len - 1); ohair@286: // whitespaces are already collapsed, ohair@286: // so all we have to do is to remove the last one character ohair@286: // if it's a whitespace. ohair@286: ohair@286: return result.toString(); 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: /** returns true if the specified char is a white space character. */ ohair@286: protected static boolean isWhiteSpace(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 || ch == 0x20; ohair@286: } ohair@286: }