Example usage for javax.xml.stream XMLEventFactory setLocation

List of usage examples for javax.xml.stream XMLEventFactory setLocation

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory setLocation.

Prototype

public abstract void setLocation(Location location);

Source Link

Document

This method allows setting of the Location on each event that is created by this factory.

Usage

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.
 * /*from w w w  .j  a  v a  2  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

/**
 * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can
 * also merge namespaces/*  ww w . j a  va  2s  .  c om*/
 * 
 * @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());
}