Java XML Element Check isSetPrefixBeforeStartElement(XMLStreamWriter writer)

Here you can find the source of isSetPrefixBeforeStartElement(XMLStreamWriter writer)

Description

is Set Prefix Before Start Element

License

Apache License

Declaration

public static boolean isSetPrefixBeforeStartElement(XMLStreamWriter writer) 

Method Source Code

//package com.java2s;
/*//from  ww  w. jav a2 s.  c  o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import javax.xml.namespace.NamespaceContext;

import javax.xml.stream.XMLStreamWriter;

public class Main {
    private static final String IS_SET_PREFIX_BEFORE_PROPERTY = "javax.xml.stream.XMLStreamWriter.isSetPrefixBeforeStartElement";
    /**
     * Unfortunately there is disagreement in the user community about the semantics of setPrefix on
     * the XMLStreamWriter.  An example will explain the difference: writer.startElement("a")
     * writer.setPrefix("pre", "urn://sample") writer.startElement("b")
     * <p/>
     * Some user communities (woodstox) believe that the setPrefix is associate with the scope for
     * "a" and thus remains in scope until the end of a.  The basis for this believe is
     * XMLStreamWriter javadoc (which some would argue is incomplete).
     * <p/>
     * Some user communities believe that the setPrefix is associated with the "b" element. These
     * communities reference an example in the specification and historical usage of SAX.
     * <p/>
     * This method will return true if the setPrefix is associated with the next writeStartElement.
     *
     * @param writer
     * @return true if setPrefix should be generated before startElement
     */
    private static boolean cache_isSetPrefixBeforeStartElement;
    private static XMLStreamWriter cache_isSetPrefixBeforeStartElement_writer = null;
    private static String semifore = "isSetPrefixBeforeStartElement";

    public static boolean isSetPrefixBeforeStartElement(XMLStreamWriter writer) {
        // Try the cached value
        if (cache_isSetPrefixBeforeStartElement_writer == writer) {
            synchronized (semifore) {
                if (cache_isSetPrefixBeforeStartElement_writer == writer) {
                    return cache_isSetPrefixBeforeStartElement;
                }
            }
        }

        // There is no cached value for this writer, so try getting
        // the property from the writer.
        boolean ret = false;
        try {
            Boolean value = (Boolean) writer.getProperty(IS_SET_PREFIX_BEFORE_PROPERTY);
            // this will always be false if the property is defined
            if (value != null) {
                ret = value.booleanValue();
                // Cache the answer
                synchronized (semifore) {
                    cache_isSetPrefixBeforeStartElement_writer = writer;
                    cache_isSetPrefixBeforeStartElement = ret;
                }
                return ret;
            }
        } catch (IllegalArgumentException e) {
            // Some parsers throw an exception for unknown properties.
        } catch (NullPointerException e) {
            // Whoops.
        }
        if (!ret) {
            // Fallback: Toggle based on sun or woodstox implementation.
            NamespaceContext nc = writer.getNamespaceContext();
            ret = (nc == null || (nc.getClass().getName().indexOf("wstx") == -1
                    && nc.getClass().getName().indexOf("weblogic") == -1
                    && nc.getClass().getName().indexOf("sun") == -1));
        }

        // Cache the answer
        synchronized (semifore) {
            cache_isSetPrefixBeforeStartElement_writer = writer;
            cache_isSetPrefixBeforeStartElement = ret;
        }
        return ret;
    }
}

Related

  1. isLocalName(Element element, String name)
  2. isMuleNamespace(Element element)
  3. isNil(Element aElement)
  4. isNil(Element element)
  5. isSetPrefixBeforeStartElement( XMLStreamWriter writer)
  6. isStartElement(final XMLStreamReader reader)
  7. isStartElement(XMLStreamReader xmlRdr, String tagName)
  8. isTableNodes(Element e)
  9. isTextOnly(final Element element)