1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime.output;
  27 
  28 import java.io.IOException;
  29 import java.io.OutputStream;
  30 
  31 import javax.xml.stream.XMLStreamException;
  32 
  33 import com.sun.xml.internal.bind.v2.runtime.Name;
  34 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
  35 
  36 import org.xml.sax.SAXException;
  37 
  38 /**
  39  * Abstract implementation of {@link XmlOutput}
  40  *
  41  * Implements the optimal methods, where defer to
  42  * the non-optimal methods.
  43  *
  44  * @author Kohsuke Kawaguchi
  45  */
  46 public abstract class XmlOutputAbstractImpl implements XmlOutput {
  47 //
  48 //
  49 // Contracts
  50 //
  51 //
  52     /**
  53      * Called at the very beginning.
  54      *
  55      * @param serializer
  56      *      the {@link XMLSerializer} that coordinates this whole marshalling episode.
  57      * @param fragment
  58      *      true if we are marshalling a fragment.
  59      */
  60     public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
  61         this.nsUriIndex2prefixIndex = nsUriIndex2prefixIndex;
  62         this.nsContext = nsContext;
  63         this.serializer = serializer;
  64     }
  65 
  66     /**
  67      * Called at the very end.
  68      *
  69      * @param fragment
  70      *      false if we are writing the whole document.
  71      */
  72     public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
  73         serializer = null;
  74     }
  75 
  76     /**
  77      * Writes a start tag.
  78      *
  79      * <p>
  80      * At this point {@link #nsContext} holds namespace declarations needed for this
  81      * new element.
  82      *
  83      * <p>
  84      * This method is used for writing tags that are indexed.
  85      */
  86     public void beginStartTag(Name name) throws IOException, XMLStreamException {
  87         beginStartTag( nsUriIndex2prefixIndex[name.nsUriIndex], name.localName );
  88     }
  89 
  90     public abstract void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException;
  91 
  92     public void attribute( Name name, String value ) throws IOException, XMLStreamException {
  93         short idx = name.nsUriIndex;
  94         if(idx==-1)
  95             attribute(-1,name.localName, value);
  96         else
  97             attribute( nsUriIndex2prefixIndex[idx], name.localName, value );
  98     }
  99     /**
 100      * @param prefix
 101      *      -1 if this attribute does not have a prefix
 102      *      (this handling differs from that of elements.)
 103      */
 104     public abstract void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException;
 105 
 106     public abstract void endStartTag() throws IOException, SAXException;
 107 
 108     public void endTag(Name name) throws IOException, SAXException, XMLStreamException {
 109         endTag( nsUriIndex2prefixIndex[name.nsUriIndex], name.localName);
 110     }
 111     public abstract void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException;
 112 
 113 
 114 
 115 
 116 //
 117 //
 118 // Utilities for implementations
 119 //
 120 //
 121     /**
 122      * The conversion table from the namespace URI index to prefix index.
 123      *
 124      * This array is shared with {@link XMLSerializer} and
 125      * is updated by it automatically.
 126      *
 127      * This allows {@link Name#nsUriIndex} to be converted to prefix index
 128      * (for {@link NamespaceContextImpl}) quickly.
 129      */
 130     protected int[] nsUriIndex2prefixIndex;
 131 
 132     /**
 133      * Set by the marshaller before the start tag is written for the root element.
 134      */
 135     protected NamespaceContextImpl nsContext;
 136 
 137     protected XMLSerializer serializer;
 138 }