1 /*
2  * Copyright (c) 1997, 2011, 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 com.sun.xml.internal.bind.v2.runtime.unmarshaller;
27 
28 import javax.xml.namespace.NamespaceContext;
29 
30 import org.xml.sax.Attributes;
31 import org.xml.sax.SAXException;
32 
33 /**
34  * {@link XmlVisitor} decorator that interns all string tokens.
35  *
36  * @author Kohsuke Kawaguchi
37  */
38 public final class InterningXmlVisitor implements XmlVisitor {
39     private final XmlVisitor next;
40 
41     private final AttributesImpl attributes = new AttributesImpl();
42 
InterningXmlVisitor(XmlVisitor next)43     public InterningXmlVisitor(XmlVisitor next) {
44         this.next = next;
45     }
46 
startDocument(LocatorEx locator, NamespaceContext nsContext)47     public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
48         next.startDocument(locator,nsContext);
49     }
50 
endDocument()51     public void endDocument() throws SAXException {
52         next.endDocument();
53     }
54 
startElement(TagName tagName )55     public void startElement(TagName tagName ) throws SAXException {
56         attributes.setAttributes(tagName.atts);
57         tagName.atts = attributes;
58         tagName.uri = intern(tagName.uri);
59         tagName.local = intern(tagName.local);
60         next.startElement(tagName);
61     }
62 
endElement(TagName tagName )63     public void endElement(TagName tagName ) throws SAXException {
64         tagName.uri = intern(tagName.uri);
65         tagName.local = intern(tagName.local);
66         next.endElement(tagName);
67     }
68 
startPrefixMapping( String prefix, String nsUri )69     public void startPrefixMapping( String prefix, String nsUri ) throws SAXException {
70         next.startPrefixMapping(intern(prefix),intern(nsUri));
71     }
72 
endPrefixMapping( String prefix )73     public void endPrefixMapping( String prefix ) throws SAXException {
74         next.endPrefixMapping(intern(prefix));
75     }
76 
text( CharSequence pcdata )77     public void text( CharSequence pcdata ) throws SAXException {
78         next.text(pcdata);
79     }
80 
getContext()81     public UnmarshallingContext getContext() {
82         return next.getContext();
83     }
84 
getPredictor()85     public TextPredictor getPredictor() {
86         return next.getPredictor();
87     }
88 
89     private static class AttributesImpl implements Attributes {
90         private Attributes core;
91 
setAttributes(Attributes att)92         void setAttributes(Attributes att) {
93             this.core = att;
94         }
95 
getIndex(String qName)96         public int getIndex(String qName) {
97             return core.getIndex(qName);
98         }
99 
getIndex(String uri, String localName)100         public int getIndex(String uri, String localName) {
101             return core.getIndex(uri, localName);
102         }
103 
getLength()104         public int getLength() {
105             return core.getLength();
106         }
107 
getLocalName(int index)108         public String getLocalName(int index) {
109             return intern(core.getLocalName(index));
110         }
111 
getQName(int index)112         public String getQName(int index) {
113             return intern(core.getQName(index));
114         }
115 
getType(int index)116         public String getType(int index) {
117             return intern(core.getType(index));
118         }
119 
getType(String qName)120         public String getType(String qName) {
121             return intern(core.getType(qName));
122         }
123 
getType(String uri, String localName)124         public String getType(String uri, String localName) {
125             return intern(core.getType(uri, localName));
126         }
127 
getURI(int index)128         public String getURI(int index) {
129             return intern(core.getURI(index));
130         }
131 
132         //
133         // since values may vary a lot,
134         // we don't (probably shouldn't) intern values.
135         //
136 
getValue(int index)137         public String getValue(int index) {
138             return core.getValue(index);
139         }
140 
getValue(String qName)141         public String getValue(String qName) {
142             return core.getValue(qName);
143         }
144 
getValue(String uri, String localName)145         public String getValue(String uri, String localName) {
146             return core.getValue(uri, localName);
147         }
148     }
149 
intern(String s)150     private static String intern(String s) {
151         if(s==null)     return null;
152         else            return s.intern();
153     }
154 }
155