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