001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.bind.v2.runtime;
037:
038: import java.io.IOException;
039:
040: import javax.xml.stream.XMLStreamException;
041:
042: import com.sun.istack.FinalArrayList;
043: import com.sun.istack.SAXException2;
044:
045: import org.xml.sax.Attributes;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.helpers.DefaultHandler;
048:
049: /**
050: * Receives SAX2 events and send the equivalent events to
051: * {@link XMLSerializer}
052: *
053: * @author
054: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
055: */
056: final class ContentHandlerAdaptor extends DefaultHandler {
057:
058: /** Stores newly declared prefix-URI mapping. */
059: private final FinalArrayList<String> prefixMap = new FinalArrayList<String>();
060:
061: /** Events will be sent to this object. */
062: private final XMLSerializer serializer;
063:
064: private final StringBuffer text = new StringBuffer();
065:
066: ContentHandlerAdaptor(XMLSerializer _serializer) {
067: this .serializer = _serializer;
068: }
069:
070: public void startDocument() {
071: prefixMap.clear();
072: }
073:
074: public void startPrefixMapping(String prefix, String uri) {
075: prefixMap.add(prefix);
076: prefixMap.add(uri);
077: }
078:
079: private boolean containsPrefixMapping(String prefix, String uri) {
080: for (int i = 0; i < prefixMap.size(); i += 2) {
081: if (prefixMap.get(i).equals(prefix)
082: && prefixMap.get(i + 1).equals(uri))
083: return true;
084: }
085: return false;
086: }
087:
088: public void startElement(String namespaceURI, String localName,
089: String qName, Attributes atts) throws SAXException {
090: try {
091: flushText();
092:
093: int len = atts.getLength();
094:
095: String p = getPrefix(qName);
096:
097: // is this prefix going to be declared on this element?
098: if (containsPrefixMapping(p, namespaceURI))
099: serializer.startElementForce(namespaceURI, localName,
100: p, null);
101: else
102: serializer.startElement(namespaceURI, localName, p,
103: null);
104:
105: // declare namespace events
106: for (int i = 0; i < prefixMap.size(); i += 2) {
107: // forcibly set this binding, instead of using declareNsUri.
108: // this guarantees that namespaces used in DOM will show up
109: // as-is in the marshalled output (instead of reassigned to something else,
110: // which may happen if you'd use declareNsUri.)
111: serializer.getNamespaceContext().force(
112: prefixMap.get(i + 1), prefixMap.get(i));
113: }
114: // make sure namespaces needed by attributes are bound
115: for (int i = 0; i < len; i++) {
116: String qname = atts.getQName(i);
117: if (qname.startsWith("xmlns"))
118: continue;
119: String prefix = getPrefix(qname);
120:
121: serializer.getNamespaceContext().declareNamespace(
122: atts.getURI(i), prefix, true);
123: }
124:
125: serializer.endNamespaceDecls(null);
126: // fire attribute events
127: for (int i = 0; i < len; i++) {
128: // be defensive.
129: if (atts.getQName(i).startsWith("xmlns"))
130: continue;
131: serializer.attribute(atts.getURI(i), atts
132: .getLocalName(i), atts.getValue(i));
133: }
134: prefixMap.clear();
135: serializer.endAttributes();
136: } catch (IOException e) {
137: throw new SAXException2(e);
138: } catch (XMLStreamException e) {
139: throw new SAXException2(e);
140: }
141: }
142:
143: private String getPrefix(String qname) {
144: int idx = qname.indexOf(':');
145: String prefix = (idx == -1) ? qname : qname.substring(0, idx);
146: return prefix;
147: }
148:
149: public void endElement(String namespaceURI, String localName,
150: String qName) throws SAXException {
151: try {
152: flushText();
153: serializer.endElement();
154: } catch (IOException e) {
155: throw new SAXException2(e);
156: } catch (XMLStreamException e) {
157: throw new SAXException2(e);
158: }
159: }
160:
161: private void flushText() throws SAXException, IOException,
162: XMLStreamException {
163: if (text.length() != 0) {
164: serializer.text(text.toString(), null);
165: text.setLength(0);
166: }
167: }
168:
169: public void characters(char[] ch, int start, int length) {
170: text.append(ch, start, length);
171: }
172: }
|