aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.unmarshaller; aoqi@0: aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link XmlVisitor} decorator that interns all string tokens. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class InterningXmlVisitor implements XmlVisitor { aoqi@0: private final XmlVisitor next; aoqi@0: aoqi@0: private final AttributesImpl attributes = new AttributesImpl(); aoqi@0: aoqi@0: public InterningXmlVisitor(XmlVisitor next) { aoqi@0: this.next = next; aoqi@0: } aoqi@0: aoqi@0: public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException { aoqi@0: next.startDocument(locator,nsContext); aoqi@0: } aoqi@0: aoqi@0: public void endDocument() throws SAXException { aoqi@0: next.endDocument(); aoqi@0: } aoqi@0: aoqi@0: public void startElement(TagName tagName ) throws SAXException { aoqi@0: attributes.setAttributes(tagName.atts); aoqi@0: tagName.atts = attributes; aoqi@0: tagName.uri = intern(tagName.uri); aoqi@0: tagName.local = intern(tagName.local); aoqi@0: next.startElement(tagName); aoqi@0: } aoqi@0: aoqi@0: public void endElement(TagName tagName ) throws SAXException { aoqi@0: tagName.uri = intern(tagName.uri); aoqi@0: tagName.local = intern(tagName.local); aoqi@0: next.endElement(tagName); aoqi@0: } aoqi@0: aoqi@0: public void startPrefixMapping( String prefix, String nsUri ) throws SAXException { aoqi@0: next.startPrefixMapping(intern(prefix),intern(nsUri)); aoqi@0: } aoqi@0: aoqi@0: public void endPrefixMapping( String prefix ) throws SAXException { aoqi@0: next.endPrefixMapping(intern(prefix)); aoqi@0: } aoqi@0: aoqi@0: public void text( CharSequence pcdata ) throws SAXException { aoqi@0: next.text(pcdata); aoqi@0: } aoqi@0: aoqi@0: public UnmarshallingContext getContext() { aoqi@0: return next.getContext(); aoqi@0: } aoqi@0: aoqi@0: public TextPredictor getPredictor() { aoqi@0: return next.getPredictor(); aoqi@0: } aoqi@0: aoqi@0: private static class AttributesImpl implements Attributes { aoqi@0: private Attributes core; aoqi@0: aoqi@0: void setAttributes(Attributes att) { aoqi@0: this.core = att; aoqi@0: } aoqi@0: aoqi@0: public int getIndex(String qName) { aoqi@0: return core.getIndex(qName); aoqi@0: } aoqi@0: aoqi@0: public int getIndex(String uri, String localName) { aoqi@0: return core.getIndex(uri, localName); aoqi@0: } aoqi@0: aoqi@0: public int getLength() { aoqi@0: return core.getLength(); aoqi@0: } aoqi@0: aoqi@0: public String getLocalName(int index) { aoqi@0: return intern(core.getLocalName(index)); aoqi@0: } aoqi@0: aoqi@0: public String getQName(int index) { aoqi@0: return intern(core.getQName(index)); aoqi@0: } aoqi@0: aoqi@0: public String getType(int index) { aoqi@0: return intern(core.getType(index)); aoqi@0: } aoqi@0: aoqi@0: public String getType(String qName) { aoqi@0: return intern(core.getType(qName)); aoqi@0: } aoqi@0: aoqi@0: public String getType(String uri, String localName) { aoqi@0: return intern(core.getType(uri, localName)); aoqi@0: } aoqi@0: aoqi@0: public String getURI(int index) { aoqi@0: return intern(core.getURI(index)); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // since values may vary a lot, aoqi@0: // we don't (probably shouldn't) intern values. aoqi@0: // aoqi@0: aoqi@0: public String getValue(int index) { aoqi@0: return core.getValue(index); aoqi@0: } aoqi@0: aoqi@0: public String getValue(String qName) { aoqi@0: return core.getValue(qName); aoqi@0: } aoqi@0: aoqi@0: public String getValue(String uri, String localName) { aoqi@0: return core.getValue(uri, localName); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static String intern(String s) { aoqi@0: if(s==null) return null; aoqi@0: else return s.intern(); aoqi@0: } aoqi@0: }