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.output;
027:
028: import java.io.IOException;
029: import java.io.OutputStream;
030:
031: import javax.xml.stream.XMLStreamException;
032:
033: import com.sun.xml.internal.bind.v2.runtime.Name;
034: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
035:
036: import org.xml.sax.SAXException;
037:
038: /**
039: * Abstract implementation of {@link XmlOutput}
040: *
041: * Implements the optimal methods, where defer to
042: * the non-optimal methods.
043: *
044: * @author Kohsuke Kawaguchi
045: */
046: public abstract class XmlOutputAbstractImpl implements XmlOutput {
047: //
048: //
049: // Contracts
050: //
051: //
052: /**
053: * Called at the very beginning.
054: *
055: * @param serializer
056: * the {@link XMLSerializer} that coordinates this whole marshalling episode.
057: * @param fragment
058: * true if we are marshalling a fragment.
059: */
060: public void startDocument(XMLSerializer serializer,
061: boolean fragment, int[] nsUriIndex2prefixIndex,
062: NamespaceContextImpl nsContext) throws IOException,
063: SAXException, XMLStreamException {
064: this .nsUriIndex2prefixIndex = nsUriIndex2prefixIndex;
065: this .nsContext = nsContext;
066: this .serializer = serializer;
067: }
068:
069: /**
070: * Called at the very end.
071: *
072: * @param fragment
073: * false if we are writing the whole document.
074: */
075: public void endDocument(boolean fragment) throws IOException,
076: SAXException, XMLStreamException {
077: serializer = null;
078: }
079:
080: /**
081: * Writes a start tag.
082: *
083: * <p>
084: * At this point {@link #nsContext} holds namespace declarations needed for this
085: * new element.
086: *
087: * <p>
088: * This method is used for writing tags that are indexed.
089: */
090: public void beginStartTag(Name name) throws IOException,
091: XMLStreamException {
092: beginStartTag(nsUriIndex2prefixIndex[name.nsUriIndex],
093: name.localName);
094: }
095:
096: public abstract void beginStartTag(int prefix, String localName)
097: throws IOException, XMLStreamException;
098:
099: public void attribute(Name name, String value) throws IOException,
100: XMLStreamException {
101: short idx = name.nsUriIndex;
102: if (idx == -1)
103: attribute(-1, name.localName, value);
104: else
105: attribute(nsUriIndex2prefixIndex[idx], name.localName,
106: value);
107: }
108:
109: /**
110: * @param prefix
111: * -1 if this attribute does not have a prefix
112: * (this handling differs from that of elements.)
113: */
114: public abstract void attribute(int prefix, String localName,
115: String value) throws IOException, XMLStreamException;
116:
117: public abstract void endStartTag() throws IOException, SAXException;
118:
119: public void endTag(Name name) throws IOException, SAXException,
120: XMLStreamException {
121: endTag(nsUriIndex2prefixIndex[name.nsUriIndex], name.localName);
122: }
123:
124: public abstract void endTag(int prefix, String localName)
125: throws IOException, SAXException, XMLStreamException;
126:
127: /**
128: * Flush the output.
129: *
130: * @see OutputStream#flush()
131: */
132: public void flush() throws IOException, XMLStreamException {
133: }
134:
135: //
136: //
137: // Utilities for implementations
138: //
139: //
140: /**
141: * The conversion table from the namespace URI index to prefix index.
142: *
143: * This array is shared with {@link XMLSerializer} and
144: * is updated by it automatically.
145: *
146: * This allows {@link Name#nsUriIndex} to be converted to prefix index
147: * (for {@link NamespaceContextImpl}) quickly.
148: */
149: protected int[] nsUriIndex2prefixIndex;
150:
151: /**
152: * Set by the marshaller before the start tag is written for the root element.
153: */
154: protected NamespaceContextImpl nsContext;
155:
156: protected XMLSerializer serializer;
157: }
|