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:
037: package com.sun.xml.bind.v2.runtime.output;
038:
039: import java.io.IOException;
040:
041: import javax.xml.stream.XMLStreamException;
042:
043: import com.sun.xml.bind.util.AttributesImpl;
044: import com.sun.xml.bind.v2.runtime.XMLSerializer;
045:
046: import org.xml.sax.ContentHandler;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.helpers.LocatorImpl;
049:
050: /**
051: * {@link XmlOutput} implementation that writes to SAX {@link ContentHandler}.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: public class SAXOutput extends XmlOutputAbstractImpl {
056: protected final ContentHandler out;
057:
058: public SAXOutput(ContentHandler out) {
059: this .out = out;
060: out.setDocumentLocator(new LocatorImpl());
061: }
062:
063: private String elementNsUri, elementLocalName, elementQName;
064:
065: private char[] buf = new char[256];
066:
067: private final AttributesImpl atts = new AttributesImpl();
068:
069: // not called if we are generating fragments
070: @Override
071: public void startDocument(XMLSerializer serializer,
072: boolean fragment, int[] nsUriIndex2prefixIndex,
073: NamespaceContextImpl nsContext) throws SAXException,
074: IOException, XMLStreamException {
075: super .startDocument(serializer, fragment,
076: nsUriIndex2prefixIndex, nsContext);
077: if (!fragment)
078: out.startDocument();
079: }
080:
081: public void endDocument(boolean fragment) throws SAXException,
082: IOException, XMLStreamException {
083: if (!fragment)
084: out.endDocument();
085: super .endDocument(fragment);
086: }
087:
088: public void beginStartTag(int prefix, String localName) {
089: elementNsUri = nsContext.getNamespaceURI(prefix);
090: elementLocalName = localName;
091: elementQName = getQName(prefix, localName);
092: atts.clear();
093: }
094:
095: public void attribute(int prefix, String localName, String value) {
096: String qname;
097: String nsUri;
098: if (prefix == -1) {
099: nsUri = "";
100: qname = localName;
101: } else {
102: nsUri = nsContext.getNamespaceURI(prefix);
103: String p = nsContext.getPrefix(prefix);
104: if (p.length() == 0)
105: // this is more likely a bug in the application code (NamespacePrefixMapper implementation)
106: // this only happens when it tries to assign "" prefix to a non-"" URI,
107: // which is by itself violation of namespace rec. But let's just be safe.
108: // See http://forums.java.net/jive/thread.jspa?messageID=212598#212598
109: qname = localName;
110: else
111: qname = p + ':' + localName;
112: }
113: atts.addAttribute(nsUri, localName, qname, "CDATA", value);
114: }
115:
116: public void endStartTag() throws SAXException {
117: NamespaceContextImpl.Element ns = nsContext.getCurrent();
118: if (ns != null) {
119: int sz = ns.count();
120: for (int i = 0; i < sz; i++) {
121: String p = ns.getPrefix(i);
122: String uri = ns.getNsUri(i);
123: if (uri.length() == 0 && ns.getBase() == 1)
124: continue; // no point in defining xmlns='' on the root
125: out.startPrefixMapping(p, uri);
126: }
127: }
128: out.startElement(elementNsUri, elementLocalName, elementQName,
129: atts);
130: }
131:
132: public void endTag(int prefix, String localName)
133: throws SAXException {
134: out.endElement(nsContext.getNamespaceURI(prefix), localName,
135: getQName(prefix, localName));
136:
137: NamespaceContextImpl.Element ns = nsContext.getCurrent();
138: if (ns != null) {
139: int sz = ns.count();
140: for (int i = sz - 1; i >= 0; i--) {
141: String p = ns.getPrefix(i);
142: String uri = ns.getNsUri(i);
143: if (uri.length() == 0 && ns.getBase() == 1)
144: continue; // no point in definint xmlns='' on the root
145: out.endPrefixMapping(p);
146: }
147: }
148: }
149:
150: private String getQName(int prefix, String localName) {
151: String qname;
152: String p = nsContext.getPrefix(prefix);
153: if (p.length() == 0)
154: qname = localName;
155: else
156: qname = p + ':' + localName;
157: return qname;
158: }
159:
160: public void text(String value, boolean needsSP) throws IOException,
161: SAXException, XMLStreamException {
162: int vlen = value.length();
163: if (buf.length <= vlen) {
164: buf = new char[Math.max(buf.length * 2, vlen + 1)];
165: }
166: if (needsSP) {
167: value.getChars(0, vlen, buf, 1);
168: buf[0] = ' ';
169: } else {
170: value.getChars(0, vlen, buf, 0);
171: }
172: out.characters(buf, 0, vlen + (needsSP ? 1 : 0));
173: }
174:
175: public void text(Pcdata value, boolean needsSP) throws IOException,
176: SAXException, XMLStreamException {
177: int vlen = value.length();
178: if (buf.length <= vlen) {
179: buf = new char[Math.max(buf.length * 2, vlen + 1)];
180: }
181: if (needsSP) {
182: value.writeTo(buf, 1);
183: buf[0] = ' ';
184: } else {
185: value.writeTo(buf, 0);
186: }
187: out.characters(buf, 0, vlen + (needsSP ? 1 : 0));
188: }
189: }
|