Example usage for javax.xml.namespace QName getPrefix

List of usage examples for javax.xml.namespace QName getPrefix

Introduction

In this page you can find the example usage for javax.xml.namespace QName getPrefix.

Prototype

public String getPrefix() 

Source Link

Document

Get the prefix of this QName.

The prefix assigned to a QName might NOT be valid in a different context.

Usage

From source file:Main.java

public static String getQName(QName qname) {
    if (qname.getPrefix() == null)
        throw new IllegalArgumentException("prefix is null in " + qname);
    return XMLConstants.DEFAULT_NS_PREFIX.equals(qname.getPrefix()) ? qname.getLocalPart()
            : qname.getPrefix() + ':' + qname.getLocalPart();
}

From source file:Main.java

public static String serializeQName(QName qname) {
    String prefix = qname.getPrefix();
    if (prefix.isEmpty()) {
        return qname.getLocalPart();
    }// ww w. j  a  va  2s.c o m
    return prefix + ":" + qname.getLocalPart();
}

From source file:Main.java

public final static String getEnclosingTagPair(QName qname) {
    return getEnclosingTagPair(qname.getPrefix(), qname.getLocalPart());
}

From source file:Main.java

/**
 * @param key Fully qualified element name.
 *///ww  w .j ava2s  .co m
public static Element createElement(QName key) {
    return createDocument().createElementNS(key.getNamespaceURI(), key.getPrefix() + ":" + key.getLocalPart());
}

From source file:Main.java

/**
 * @param doc//w w  w. ja  va 2  s.co m
 *            The Document that is the factory for the new Element.
 * @param qname
 *            The QName of the new Element.
 * @return An empty Element whose owner is the given Document.
 */
private static Element createEmptyElement(final Document doc, final QName qname) {
    final String prefix = qname.getPrefix();
    String name = qname.getLocalPart();

    //
    // NOTE: The DOM API requires that elements with qualified names 
    //       be created with prefix:localName as the tag name. We 
    //       CANNOT just use the localName and expect the API to fill 
    //       in a prefix, nor can we use setPrefix after creation. For 
    //       parsing to work correctly, the second argument to the 
    //       createElementNS method MUST be a qualified name.
    //
    if (prefix != null && prefix.length() > 0)
        name = prefix + ':' + name;

    final String uri = qname.getNamespaceURI();

    return doc.createElementNS(uri, name);
}

From source file:Main.java

private static void writeAsEncodedUnicode(EndElement element, Writer writer) throws XMLStreamException {
    try {/*from   w w  w . ja  va 2  s. c o  m*/
        // Write end tags.
        writer.write("</");
        QName name = element.getName();
        String prefix = name.getPrefix();
        if (prefix != null && prefix.length() > 0) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(name.getLocalPart());
        writer.write('>');
    } catch (IOException ioe) {
        throw new XMLStreamException(ioe);
    }
}

From source file:Main.java

/**
 * Constructs a new StartElement that merges the attributes and namespaces
 * found in the specified StartElement, with the provided attributes. The
 * returned StartElement will contain all the attributes and namespaces of
 * the original, plus those defined in the map.
 * //  www  . j a  va2 s. co  m
 * @param tag The original StartElement
 * @param attrs An iterator of Atributes to add to the element.
 * @return A new StartElement that contains all the original attributes and
 *         namespaces, plus the provided attributes.
 */
public static StartElement mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory) {

    // create Attribute map
    Map attributes = new HashMap();

    // iterate through start tag's attributes
    for (Iterator i = tag.getAttributes(); i.hasNext();) {

        Attribute attr = (Attribute) i.next();
        attributes.put(attr.getName(), attr);

    }

    // iterate through new attributes
    while (attrs.hasNext()) {

        Attribute attr = (Attribute) attrs.next();
        attributes.put(attr.getName(), attr);

    }

    factory.setLocation(tag.getLocation());

    QName tagName = tag.getName();
    return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(),
            attributes.values().iterator(), tag.getNamespaces(), tag.getNamespaceContext());

}

From source file:Main.java

private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty)
        throws XMLStreamException {
    try {//from  w ww. j  a va  2s .  c o m
        // Write start tag.
        writer.write('<');
        QName name = element.getName();

        String prefix = name.getPrefix();
        if (prefix != null && prefix.length() > 0) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(name.getLocalPart());

        // Write namespace declarations.
        Iterator nsIter = element.getNamespaces();
        while (nsIter.hasNext()) {
            Namespace ns = (Namespace) nsIter.next();
            writer.write(' ');
            ns.writeAsEncodedUnicode(writer);
        }

        // Write attributes
        Iterator attrIter = element.getAttributes();
        while (attrIter.hasNext()) {
            Attribute attr = (Attribute) attrIter.next();
            writer.write(' ');
            attr.writeAsEncodedUnicode(writer);
        }

        if (isEmpty)
            writer.write('/');
        writer.write('>');
    } catch (IOException ioe) {
        throw new XMLStreamException(ioe);
    }
}

From source file:Main.java

/**
 * Return the qualified name, that is to say the prefix -if any- with the
 * local name./*  www  .  jav a 2  s . c o m*/
 *
 * @param qName
 *            The QName.
 * @return A string that looks like "<tt>prefix:localName</tt>" or "
 *         <tt>NCName</tt>".
 */
public static String getQualifiedName(QName qName) {
    if (!XMLConstants.NULL_NS_URI.equals(qName.getNamespaceURI())
            && !XMLConstants.DEFAULT_NS_PREFIX.equals(qName.getPrefix())) {
        return qName.getPrefix() + ":" + qName.getLocalPart();
    } else {
        return qName.getLocalPart();
    }
}

From source file:Main.java

/**
 * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can
 * also merge namespaces// w ww. ja v  a2 s . co m
 * 
 * @param tag
 * @param attrs
 * @param nsps
 */
public static StartElement mergeAttributes(StartElement tag, Iterator<? extends Attribute> attrs,
        Iterator<? extends Namespace> nsps, XMLEventFactory factory) {
    // create Attribute map
    Map<QName, Attribute> attributes = new HashMap<QName, Attribute>();

    // iterate through start tag's attributes
    for (Iterator i = tag.getAttributes(); i.hasNext();) {
        Attribute attr = (Attribute) i.next();
        attributes.put(attr.getName(), attr);
    }
    if (attrs != null) {
        // iterate through new attributes
        while (attrs.hasNext()) {
            Attribute attr = attrs.next();
            attributes.put(attr.getName(), attr);
        }
    }

    Map<QName, Namespace> namespaces = new HashMap<QName, Namespace>();
    for (Iterator i = tag.getNamespaces(); i.hasNext();) {
        Namespace ns = (Namespace) i.next();
        namespaces.put(ns.getName(), ns);
    }
    if (nsps != null) {
        while (nsps.hasNext()) {
            Namespace ns = nsps.next();
            namespaces.put(ns.getName(), ns);
        }
    }

    factory.setLocation(tag.getLocation());

    QName tagName = tag.getName();
    return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(),
            attributes.values().iterator(), namespaces.values().iterator(), tag.getNamespaceContext());
}