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.XMLEventFactory;
042: import javax.xml.stream.XMLEventWriter;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.events.Attribute;
045: import javax.xml.stream.events.Characters;
046:
047: import com.sun.xml.bind.v2.runtime.XMLSerializer;
048:
049: import org.xml.sax.SAXException;
050:
051: /**
052: * {@link XmlOutput} that writes to StAX {@link XMLEventWriter}.
053: *
054: * @author Kohsuke Kawaguchi
055: */
056: public class XMLEventWriterOutput extends XmlOutputAbstractImpl {
057: private final XMLEventWriter out;
058: private final XMLEventFactory ef;
059:
060: /** One whitespace. */
061: private final Characters sp;
062:
063: public XMLEventWriterOutput(XMLEventWriter out) {
064: this .out = out;
065: ef = XMLEventFactory.newInstance();
066: sp = ef.createCharacters(" ");
067: }
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 IOException,
074: SAXException, XMLStreamException {
075: super .startDocument(serializer, fragment,
076: nsUriIndex2prefixIndex, nsContext);
077: if (!fragment)
078: out.add(ef.createStartDocument());
079: }
080:
081: public void endDocument(boolean fragment) throws IOException,
082: SAXException, XMLStreamException {
083: if (!fragment) {
084: out.add(ef.createEndDocument());
085: out.flush();
086: }
087: super .endDocument(fragment);
088: }
089:
090: public void beginStartTag(int prefix, String localName)
091: throws IOException, XMLStreamException {
092: out.add(ef.createStartElement(nsContext.getPrefix(prefix),
093: nsContext.getNamespaceURI(prefix), localName));
094:
095: NamespaceContextImpl.Element nse = nsContext.getCurrent();
096: if (nse.count() > 0) {
097: for (int i = nse.count() - 1; i >= 0; i--) {
098: String uri = nse.getNsUri(i);
099: if (uri.length() == 0 && nse.getBase() == 1)
100: continue; // no point in definint xmlns='' on the root
101: out.add(ef.createNamespace(nse.getPrefix(i), uri));
102: }
103: }
104: }
105:
106: public void attribute(int prefix, String localName, String value)
107: throws IOException, XMLStreamException {
108: Attribute att;
109: if (prefix == -1)
110: att = ef.createAttribute(localName, value);
111: else
112: att = ef
113: .createAttribute(nsContext.getPrefix(prefix),
114: nsContext.getNamespaceURI(prefix),
115: localName, value);
116:
117: out.add(att);
118: }
119:
120: public void endStartTag() throws IOException, SAXException {
121: // noop
122: }
123:
124: public void endTag(int prefix, String localName)
125: throws IOException, SAXException, XMLStreamException {
126: out.add(ef.createEndElement(nsContext.getPrefix(prefix),
127: nsContext.getNamespaceURI(prefix), localName));
128: }
129:
130: public void text(String value, boolean needsSeparatingWhitespace)
131: throws IOException, SAXException, XMLStreamException {
132: if (needsSeparatingWhitespace)
133: out.add(sp);
134: out.add(ef.createCharacters(value));
135: }
136:
137: public void text(Pcdata value, boolean needsSeparatingWhitespace)
138: throws IOException, SAXException, XMLStreamException {
139: text(value.toString(), needsSeparatingWhitespace);
140: }
141: }
|