Class RepairingNsStreamWriter

  • All Implemented Interfaces:
    OutputConfigFlags, javax.xml.stream.XMLStreamConstants, javax.xml.stream.XMLStreamWriter, org.codehaus.stax2.typed.TypedXMLStreamWriter, org.codehaus.stax2.validation.Validatable, org.codehaus.stax2.validation.ValidationContext, org.codehaus.stax2.XMLStreamWriter2

    public final class RepairingNsStreamWriter
    extends BaseNsStreamWriter
    Namespace-aware implementation of XMLStreamWriter, that does namespace repairing, ie resolves possible conflicts between prefixes (add new bindings as necessary), as well as automatically creates namespace declarations as necessary.
    • Field Detail

      • mAutomaticNsPrefix

        protected final java.lang.String mAutomaticNsPrefix
      • mAutoNsSeq

        protected int[] mAutoNsSeq
        Sequence number used for generating dynamic namespace prefixes. Array used as a wrapper to allow for easy sharing of the sequence number.
      • mSuggestedDefNs

        protected java.lang.String mSuggestedDefNs
      • mSuggestedPrefixes

        protected java.util.HashMap<java.lang.String,​java.lang.String> mSuggestedPrefixes
        Map that contains URI-to-prefix entries that point out suggested prefixes for URIs. These are populated by calls to BaseNsStreamWriter.setPrefix(java.lang.String, java.lang.String), and they are only used as hints for binding; if there are conflicts, repairing writer can just use some other prefix.
    • Constructor Detail

      • RepairingNsStreamWriter

        public RepairingNsStreamWriter​(XmlWriter xw,
                                       java.lang.String enc,
                                       WriterConfig cfg)
    • Method Detail

      • writeAttribute

        public void writeAttribute​(java.lang.String nsURI,
                                   java.lang.String localName,
                                   java.lang.String value)
                            throws javax.xml.stream.XMLStreamException
        Specified by:
        writeAttribute in interface javax.xml.stream.XMLStreamWriter
        Specified by:
        writeAttribute in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeAttribute

        public void writeAttribute​(java.lang.String prefix,
                                   java.lang.String nsURI,
                                   java.lang.String localName,
                                   java.lang.String value)
                            throws javax.xml.stream.XMLStreamException
        Specified by:
        writeAttribute in interface javax.xml.stream.XMLStreamWriter
        Specified by:
        writeAttribute in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeDefaultNamespace

        public void writeDefaultNamespace​(java.lang.String nsURI)
                                   throws javax.xml.stream.XMLStreamException
        Specified by:
        writeDefaultNamespace in interface javax.xml.stream.XMLStreamWriter
        Specified by:
        writeDefaultNamespace in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeNamespace

        public void writeNamespace​(java.lang.String prefix,
                                   java.lang.String nsURI)
                            throws javax.xml.stream.XMLStreamException
        Specified by:
        writeNamespace in interface javax.xml.stream.XMLStreamWriter
        Specified by:
        writeNamespace in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • setDefaultNamespace

        public void setDefaultNamespace​(java.lang.String uri)
                                 throws javax.xml.stream.XMLStreamException
        With repairing writer, this is only taken as a suggestion as to how the caller would prefer prefixes to be mapped.
        Specified by:
        setDefaultNamespace in interface javax.xml.stream.XMLStreamWriter
        Specified by:
        setDefaultNamespace in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • doSetPrefix

        public void doSetPrefix​(java.lang.String prefix,
                                java.lang.String uri)
                         throws javax.xml.stream.XMLStreamException
        Specified by:
        doSetPrefix in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeStartElement

        public void writeStartElement​(javax.xml.stream.events.StartElement elem)
                               throws javax.xml.stream.XMLStreamException
        Description copied from class: BaseStreamWriter
        Convenience method needed by XMLEventWriter implementation, to use when writing a start element, and possibly its attributes and namespace declarations.
        Specified by:
        writeStartElement in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeTypedAttribute

        protected void writeTypedAttribute​(java.lang.String prefix,
                                           java.lang.String nsURI,
                                           java.lang.String localName,
                                           org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)
                                    throws javax.xml.stream.XMLStreamException
        Description copied from class: TypedStreamWriter
        Method that will write attribute with value that is known not to require additional escaping.
        Overrides:
        writeTypedAttribute in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeStartOrEmpty

        protected void writeStartOrEmpty​(java.lang.String localName,
                                         java.lang.String nsURI)
                                  throws javax.xml.stream.XMLStreamException
        Specified by:
        writeStartOrEmpty in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeStartOrEmpty

        protected void writeStartOrEmpty​(java.lang.String suggPrefix,
                                         java.lang.String localName,
                                         java.lang.String nsURI)
                                  throws javax.xml.stream.XMLStreamException
        Specified by:
        writeStartOrEmpty in class BaseNsStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • copyStartElement

        public final void copyStartElement​(InputElementStack elemStack,
                                           AttributeCollector ac)
                                    throws java.io.IOException,
                                           javax.xml.stream.XMLStreamException
        Element copier method implementation suitable for use with namespace-aware writers in repairing mode. The trickiest thing is having to properly order calls to setPrefix, writeNamespace and writeStartElement; the order writers expect is bit different from the order in which element information is passed in.
        Specified by:
        copyStartElement in class BaseStreamWriter
        Throws:
        java.io.IOException
        javax.xml.stream.XMLStreamException
      • validateQNamePrefix

        public java.lang.String validateQNamePrefix​(javax.xml.namespace.QName name)
                                             throws javax.xml.stream.XMLStreamException
        Description copied from class: BaseStreamWriter
        Method called before writing a QName via Typed Access API. In namespace-repairing mode it should take appropriate actions to ensure that the given namespace URI is bound to a namespace and return whatever it maps to. In non-repairing work no additional work is to be done and methods
        Specified by:
        validateQNamePrefix in class BaseStreamWriter
        Returns:
        Prefix to use when writing out given QName as an element or attribute value
        Throws:
        javax.xml.stream.XMLStreamException
      • findElemPrefix

        protected final java.lang.String findElemPrefix​(java.lang.String nsURI,
                                                        SimpleOutputElement elem)
                                                 throws javax.xml.stream.XMLStreamException
        Method called to find an existing prefix for the given namespace, if any exists in the scope. If one is found, it's returned (including "" for the current default namespace); if not, null is returned.
        Parameters:
        nsURI - URI of namespace for which we need a prefix
        Throws:
        javax.xml.stream.XMLStreamException
      • findOrCreateAttrPrefix

        protected final java.lang.String findOrCreateAttrPrefix​(java.lang.String suggPrefix,
                                                                java.lang.String nsURI,
                                                                SimpleOutputElement elem)
                                                         throws javax.xml.stream.XMLStreamException
        Method called to somehow find a prefix for given namespace, to be used for a new start element; either use an existing one, or generate a new one. If a new mapping needs to be generated, it will also be automatically bound, and necessary namespace declaration output.
        Parameters:
        suggPrefix - Suggested prefix to bind, if any; may be null to indicate "no preference"
        nsURI - URI of namespace for which we need a prefix
        elem - Currently open start element, on which the attribute will be added.
        Throws:
        javax.xml.stream.XMLStreamException
      • validateElemPrefix

        private final java.lang.String validateElemPrefix​(java.lang.String prefix,
                                                          java.lang.String nsURI,
                                                          SimpleOutputElement elem)
                                                   throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException