Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. 
   * See the copyright.txt in the distribution for a full listing 
   * of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  * 
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 package com.arjuna.webservices11.util;
 
 import java.util.Map;
 import java.util.Set;
 
 
Helper class for Stream operations.

Author(s):
kevin
 
 public class StreamHelper
 {
    
Pseudo namespace used to unbind a prefix.
 
     private static final String UNBOUND_NAMESPACE = "http://www.arjuna.com/stax/parsing/unboundnamespace" ;

    
Write a start element with appropriate namespace declarations if necessary

Parameters:
out The output stream.
elementName The element name.
Returns:
The previous namespace if written, null otherwise.
Throws:
javax.xml.stream.XMLStreamException for errors during writing.
 
     public static String writeStartElement(final XMLStreamWriter outfinal QName elementName)
         throws XMLStreamException
     {
         final String namespaceURI = QNameHelper.getNormalisedValue(elementName.getNamespaceURI()) ;
         final String localName = elementName.getLocalPart() ;
 
         final NamespaceContext namespaceContext = out.getNamespaceContext() ;
         final boolean writeNamespace = (namespaceContext.getPrefix(namespaceURI) == null) ;
 
         if (writeNamespace)
         {
             final String prefix = QNameHelper.getNormalisedValue(elementName.getPrefix()) ;
             final String origNamespace = QNameHelper.getNormalisedValue(namespaceContext.getNamespaceURI(prefix)) ;
             if (prefix.length() == 0)
             {
                 out.setDefaultNamespace(namespaceURI) ;
                 out.writeStartElement(namespaceURIlocalName) ;
                 out.writeDefaultNamespace(namespaceURI) ;
             }
             else
             {
                 out.setPrefix(prefixnamespaceURI) ;
                 out.writeStartElement(namespaceURIlocalName) ;
                 out.writeNamespace(prefixnamespaceURI) ;
             }
             return origNamespace ;
         }
         else
         {
             out.writeStartElement(namespaceURIlocalName) ;
             return null ;
         }
     }

    
Write an end element removing the namespace binding if necessary

Parameters:
out The output stream.
prefix The element prefix.
namespaceURI The previous binding for the prefix.
Throws:
javax.xml.stream.XMLStreamException for errors during writing.
 
     public static void writeEndElement(final XMLStreamWriter outfinal String prefix,
         final String namespaceURI)
        throws XMLStreamException
    {
        out.writeEndElement() ;
        if (namespaceURI != null)
        {
            final String resetNamespace = (namespaceURI.length() == 0 ?  : namespaceURI) ;
            if (prefix.length() == 0)
            {
                out.setDefaultNamespace(resetNamespace) ;
            }
            else
            {
                out.setPrefix(prefixresetNamespace) ;
            }
        }
    }
    
Write the attributes to the stream.

Parameters:
out The output stream.
attributes The attributes.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeAttributes(final XMLStreamWriter outfinal Map attributes)
        throws XMLStreamException
    {
        final Set entrySet = attributes.entrySet() ;
        final Iterator entryIter = entrySet.iterator() ;
        while(entryIter.hasNext())
        {
            final Map.Entry entry = (Map.Entry)entryIter.next() ;
            final QName name = (QName)entry.getKey() ;
            final Object value = entry.getValue() ;
            writeAttribute(outnamevalue) ;
        }
    }

    
Write the attribute to the stream.

Parameters:
out The output stream.
attributeName The attribute name.
attributeValue The attribute value.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeAttribute(final XMLStreamWriter outfinal QName attributeName,
        final Object attributeValue)
        throws XMLStreamException
    {
        if (attributeValue instanceof QName)
        {
            writeAttribute(outattributeName, (QName)attributeValue) ;
        }
        else
        {
            writeAttribute(outattributeNameattributeValue.toString()) ;
        }
    }

    
Write the attribute to the stream.

Parameters:
out The output stream.
attributeName The attribute name.
attributeValue The attribute value as a QName.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeAttribute(final XMLStreamWriter outfinal QName attributeName,
        final QName attributeValue)
        throws XMLStreamException
    {
        final String namespaceURI = QNameHelper.getNormalisedValue(attributeValue.getNamespaceURI()) ;
        if (namespaceURI.length() == 0)
        {
            writeAttribute(outattributeNameattributeValue.getLocalPart()) ;
        }
        else
        {
            final NamespaceContext namespaceContext = out.getNamespaceContext() ;
            final String origPrefix = namespaceContext.getPrefix(namespaceURI) ;
            if (origPrefix == null)
            {
                final String prefix = QNameHelper.getNormalisedValue(attributeValue.getPrefix()) ;
                writeNamespace(outprefixnamespaceURI) ;
                writeAttribute(outattributeName, QNameHelper.toQualifiedName(attributeValue)) ;
            }
            else
            {
                // KEV must handle clashes with default namespace
                writeAttribute(outattributeName, QNameHelper.toQualifiedName(origPrefixattributeValue.getLocalPart())) ;
            }
        }
    }

    
Write the attribute to the stream.

Parameters:
out The output stream.
attributeName The attribute name.
attributeValue The attribute value.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeAttribute(final XMLStreamWriter outfinal QName attributeName,
        final String attributeValue)
        throws XMLStreamException
    {
        final String namespaceURI = QNameHelper.getNormalisedValue(attributeName.getNamespaceURI()) ;
        final String localName = attributeName.getLocalPart() ;
        if (namespaceURI.length() == 0)
        {
            out.writeAttribute(localNameattributeValue) ;
        }
        else
        {
            // KEV must handle clashes with default namespace
            final String prefix = QNameHelper.getNormalisedValue(attributeName.getPrefix()) ;
            writeNamespace(outprefixnamespaceURI) ;
            out.writeAttribute(namespaceURIlocalNameattributeValue) ;
        }
    }

    
Write the QName to the stream as text.

Parameters:
out The output stream.
qName The qualified name.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeQualifiedName(final XMLStreamWriter outfinal QName qName)
        throws XMLStreamException
    {
        final String namespaceURI = QNameHelper.getNormalisedValue(qName.getNamespaceURI()) ;
        if (namespaceURI.length() == 0)
        {
            out.writeCharacters(QNameHelper.toQualifiedName(qName)) ;
        }
        else
        {
            final NamespaceContext namespaceContext = out.getNamespaceContext() ;
            final String origPrefix = namespaceContext.getPrefix(namespaceURI) ;
            if (origPrefix == null)
            {
                final String prefix = QNameHelper.getNormalisedValue(qName.getPrefix()) ;
                writeNamespace(outprefixnamespaceURI) ;
                out.writeCharacters(QNameHelper.toQualifiedName(qName)) ;
            }
            else
            {
                // KEV must handle clashes with default namespace
                out.writeCharacters(QNameHelper.toQualifiedName(origPrefixqName.getLocalPart())) ;
            }
        }
    }

    
Write the namespace if necessary.

Parameters:
out The output stream.
prefix The namespace prefix.
namespaceURI The namespaceURI.
Throws:
javax.xml.stream.XMLStreamException Thrown for errors during writing.
    public static void writeNamespace(final XMLStreamWriter outfinal String prefixfinal String namespaceURI)
        throws XMLStreamException
    {
        final NamespaceContext namespaceContext = out.getNamespaceContext() ;
        final boolean writeNamespace = (namespaceContext.getPrefix(namespaceURI) == null) ;
        if (writeNamespace)
        {
            out.setPrefix(prefixnamespaceURI) ;
            out.writeNamespace(prefixnamespaceURI) ;
        }
    }

    
Make sure the stream is at the start of the next element.

Parameters:
streamReader The current stream reader.
Throws:
javax.xml.stream.XMLStreamException For parsing errors.
    public static void skipToNextStartElement(final XMLStreamReader streamReader)
        throws XMLStreamException
    {
        if (streamReader.hasNext())
        {
            streamReader.nextTag() ;
            if (streamReader.isEndElement())
            {
                throw new XMLStreamException(..get_webservices_util_StreamHelper_1(streamReader.getName())) ;
            }
        }
        else
        {
        }
    }

    
Make sure the stream is at the start of an element.

Parameters:
streamReader The current stream reader.
Throws:
javax.xml.stream.XMLStreamException For parsing errors.
    public static void skipToStartElement(final XMLStreamReader streamReader)
        throws XMLStreamException
    {
        while (!streamReader.isStartElement())
        {
            if (streamReader.hasNext())
            {
                streamReader.next() ;
            }
            else
            {
                throw new XMLStreamException(..get_webservices_util_StreamHelper_2()) ;
            }
        }
    }

    
Check the next start tag is as expected.

Parameters:
streamReader The stream reader.
expected The expected qualified name.
Throws:
javax.xml.stream.XMLStreamException For errors during parsing.
    public static void checkNextStartTag(final XMLStreamReader streamReaderfinal QName expected)
        throws XMLStreamException
    {
        skipToNextStartElement(streamReader) ;
        checkTag(streamReaderexpected) ;
    }

    
Compare the element tag with the expected qualified name.

Parameters:
streamReader The current stream reader.
expected The expected qualified name.
Throws:
javax.xml.stream.XMLStreamException For errors during parsing.
    public static void checkTag(final XMLStreamReader streamReaderfinal QName expected)
        throws XMLStreamException
    {
        final QName elementName = streamReader.getName() ;
        if (!expected.equals(elementName))
        {
            throw new XMLStreamException(..get_webservices_util_StreamHelper_3(elementName)) ;
        }
    }

    
Check to see if the parent element is finished.

Parameters:
streamReader The stream reader.
Returns:
true if it is finished, false otherwise.
Throws:
javax.xml.stream.XMLStreamException For errors during parsing.
    public static boolean checkParentFinished(final XMLStreamReader streamReader)
        throws XMLStreamException
    {
        return (streamReader.nextTag() == .) ;
    }
New to GrepCode? Check out our FAQ X