001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime;
027:
028: import java.io.IOException;
029:
030: import javax.xml.stream.XMLStreamException;
031:
032: import com.sun.istack.internal.FinalArrayList;
033: import com.sun.istack.internal.SAXException2;
034:
035: import org.xml.sax.Attributes;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: /**
040: * Receives SAX2 events and send the equivalent events to
041: * {@link XMLSerializer}
042: *
043: * @author
044: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
045: */
046: final class ContentHandlerAdaptor extends DefaultHandler {
047:
048: /** Stores newly declared prefix-URI mapping. */
049: private final FinalArrayList prefixMap = new FinalArrayList();
050:
051: /** Events will be sent to this object. */
052: private final XMLSerializer serializer;
053:
054: private final StringBuffer text = new StringBuffer();
055:
056: ContentHandlerAdaptor(XMLSerializer _serializer) {
057: this .serializer = _serializer;
058: }
059:
060: public void startDocument() {
061: prefixMap.clear();
062: }
063:
064: public void startPrefixMapping(String prefix, String uri) {
065: prefixMap.add(prefix);
066: prefixMap.add(uri);
067: }
068:
069: public void startElement(String namespaceURI, String localName,
070: String qName, Attributes atts) throws SAXException {
071: try {
072: flushText();
073:
074: int len = atts.getLength();
075:
076: serializer.startElement(namespaceURI, localName,
077: getPrefix(qName), null);
078: // declare namespace events
079: for (int i = 0; i < len; i++) {
080: String qname = atts.getQName(i);
081: if (qname.startsWith("xmlns"))
082: continue;
083: String prefix = getPrefix(qname);
084:
085: serializer.getNamespaceContext().declareNamespace(
086: atts.getURI(i), prefix, true);
087: }
088: for (int i = 0; i < prefixMap.size(); i += 2) {
089: String prefix = (String) prefixMap.get(i);
090: serializer.getNamespaceContext().declareNamespace(
091: (String) prefixMap.get(i + 1), prefix,
092: prefix.length() != 0);
093: }
094:
095: serializer.endNamespaceDecls(null);
096: // fire attribute events
097: for (int i = 0; i < len; i++) {
098: // be defensive.
099: if (atts.getQName(i).startsWith("xmlns"))
100: continue;
101: serializer.attribute(atts.getURI(i), atts
102: .getLocalName(i), atts.getValue(i));
103: }
104: prefixMap.clear();
105: serializer.endAttributes();
106: } catch (IOException e) {
107: throw new SAXException2(e);
108: } catch (XMLStreamException e) {
109: throw new SAXException2(e);
110: }
111: }
112:
113: private String getPrefix(String qname) {
114: int idx = qname.indexOf(':');
115: String prefix = (idx == -1) ? qname : qname.substring(0, idx);
116: return prefix;
117: }
118:
119: public void endElement(String namespaceURI, String localName,
120: String qName) throws SAXException {
121: try {
122: flushText();
123: serializer.endElement();
124: } catch (IOException e) {
125: throw new SAXException2(e);
126: } catch (XMLStreamException e) {
127: throw new SAXException2(e);
128: }
129: }
130:
131: private void flushText() throws SAXException, IOException,
132: XMLStreamException {
133: if (text.length() != 0) {
134: serializer.text(text.toString(), null);
135: text.setLength(0);
136: }
137: }
138:
139: public void characters(char[] ch, int start, int length) {
140: text.append(ch, start, length);
141: }
142: }
|