325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.runtime.output;
325N/A
325N/Aimport java.io.IOException;
325N/A
325N/Aimport javax.xml.bind.JAXBContext;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/A
325N/Aimport com.sun.xml.internal.bind.v2.runtime.Name;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.NameList;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
325N/A
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/A/**
325N/A * Well-formed XML writer.
325N/A *
325N/A * <p>
325N/A * Implementations of this interface is used to connect {@link XMLSerializer}
325N/A * to the actual target. This allows {@link XMLSerializer} to be API agnostic.
325N/A *
325N/A *
325N/A * <h2>Notes</h2>
325N/A * <p>
325N/A * {@link JAXBContext} assigns indices to URIs and local names
325N/A * that are statically known by using {@link NameList}.
325N/A * {@link XmlOutput} implementation can use these indices to improve
325N/A * the performance. For example, those namespace URI indices can be
325N/A * turned into prefixes quickly.
325N/A *
325N/A * <p>
325N/A * {@link XmlOutput} still allows arbitrary namepsace URIs / local names
325N/A * to be written.
325N/A *
325N/A * <p>
325N/A * The {@link NamespaceContextImpl} object, which is shared between {@link XmlOutput} and
325N/A * {@link XMLSerializer}, keeps track of the in-scope namespace bindings. By the time
325N/A * the {@link #beginStartTag} method is called, all the namespace bindings for the new
325N/A * element is already declared. Similarly, after the {@link #endTag} method is called,
325N/A * in-scope bindings will be removed. This book keeping is all done outside {@link XmlOutput}.
325N/A *
325N/A * <p>
325N/A * {@link XmlOutput} and {@link XMLSerializer} uses indices to
325N/A * reference prefixes/URIs to be written. {@link NamespaceContextImpl} can
325N/A * convert prefix indices to URIs and the string representations of prefixes.
325N/A * Binding from indices to URIs and prefixes do not change while indices
325N/A * are "in scope", so {@link XmlOutput} is again expected to take advantage of
325N/A * this to improve the perofmrnace.
325N/A *
325N/A * <p>
325N/A * prefix index 0 is reserved for "xml", and this binding is assumed to be always there.
325N/A * {@link NamespaceContextImpl} can handle this index correctly, but this binding will never
325N/A * be reported to {@link XmlOutput} through {@link #beginStartTag}.
325N/A *
325N/A * <p>
325N/A * One pecurilar behavior of a {@link NamespaceContextImpl} object is that it tries
325N/A * to define redundant xmlns="" on the root element. Implementations of {@link XmlOutput}
325N/A * is encouraged to check for this and avoid generating redundant namespace declarations.
325N/A *
325N/A *
325N/A *
325N/A * <h2>Call Sequence</h2>
325N/A * <p>
325N/A * {@link XMLSerializer} calls the writer methods in the following order:
325N/A *
325N/A * <pre>
325N/A * CALLSEQUENCE := {@link #startDocument startDocument} ELEMENT {@link #endDocument endDocument}
325N/A * | ELEMENT // for fragment
325N/A *
325N/A * ELEMENT := {@link #beginStartTag beginStartTag} {@link #attribute attribute}* {@link #endStartTag endStartTag} CONTENTS {@link #endTag endTag}
325N/A *
325N/A * CONTENTS := (ELEMENT | {@link #text text})*
325N/A * </pre>
325N/A *
325N/A * TODO: for FI, consider making attribute values from Strings to CharSequences.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic interface XmlOutput {
325N/A//
325N/A//
325N/A// Contracts
325N/A//
325N/A//
325N/A /**
325N/A * Called at the very beginning.
325N/A *
325N/A * @param serializer
325N/A * the {@link XMLSerializer} that coordinates this whole marshalling episode.
325N/A * @param fragment
325N/A * true if we are marshalling a fragment.
325N/A */
325N/A public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException;
325N/A
325N/A /**
325N/A * Called at the very end. This is the last method to be invoked.
325N/A *
325N/A * @param fragment
325N/A * false if we are writing the whole document.
325N/A */
325N/A public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException;
325N/A
325N/A /**
325N/A * Writes a start tag.
325N/A *
325N/A * <p>
325N/A * At this point {@link NamespaceContextImpl} holds namespace declarations needed for this
325N/A * new element.
325N/A *
325N/A * <p>
325N/A * This method is used for writing tags that are indexed.
325N/A */
325N/A public void beginStartTag(Name name) throws IOException, XMLStreamException;
325N/A
325N/A public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException;
325N/A
325N/A public void attribute( Name name, String value ) throws IOException, XMLStreamException;
325N/A
325N/A /**
325N/A * @param prefix
325N/A * -1 if this attribute does not have a prefix
325N/A * (this handling differs from that of elements.)
325N/A */
325N/A public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException;
325N/A
325N/A public void endStartTag() throws IOException, SAXException;
325N/A
325N/A public void endTag(Name name) throws IOException, SAXException, XMLStreamException;
325N/A
325N/A public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException;
325N/A
325N/A /**
325N/A * Writes XML text with character escaping, if necessary.
325N/A *
325N/A * @param value
325N/A * this string can contain characters that might need escaping
325N/A * (such as '&amp;' or '>')
325N/A * @param needsSeparatingWhitespace
325N/A */
325N/A public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException;
325N/A
325N/A /**
325N/A * Writes XML text with character escaping, if necessary.
325N/A *
325N/A * @param value
325N/A * this string can contain characters that might need escaping
325N/A * (such as '&amp;' or '>')
325N/A * @param needsSeparatingWhitespace
325N/A */
325N/A public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException;
325N/A}