Example usage for javax.xml.namespace QName getLocalPart

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

Introduction

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

Prototype

public String getLocalPart() 

Source Link

Document

Get the local part of this QName.

Usage

From source file:Main.java

public static String convertToXpath(String qname) {
    QName name = QName.valueOf(qname);
    if ("".equals(name.getNamespaceURI())) {
        return "//" + name.getLocalPart();
    } else {/* w  ww  .j a v a  2  s  .c o  m*/
        return "//*[local-name()='" + name.getLocalPart() + "' and namespace-uri()='" + name.getNamespaceURI()
                + "']";
    }
}

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  ww  .  java2 s  .  c  o 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

public static String getNoNamespacePath(Stack<QName> stack, NamespaceContext ctx) {
    StringBuilder ret = new StringBuilder();
    for (QName q : stack) {
        if (q == null)
            throw new IllegalStateException("q is null");
        ret.append("/");
        ret.append(q.getLocalPart());
    }/*w  ww  . ja  v a  2s .  c  o  m*/
    return ret.toString();
}

From source file:no.digipost.api.interceptors.WsSecurityInterceptor.java

private static String partFromQName(final QName qname) {
    return "{}{" + qname.getNamespaceURI() + "}" + qname.getLocalPart();
}

From source file:Main.java

public static boolean isEqual(QName qn1, QName qn2, boolean ignoreCase) {
    if (qn1 == null && qn2 == null) {
        return true;
    }/*  w  w  w  . j av  a  2s. c o  m*/

    if (qn1 != null && qn2 != null && isEqual(qn1.getLocalPart(), qn2.getLocalPart(), ignoreCase)
            && isEqual(qn1.getNamespaceURI(), qn2.getNamespaceURI(), ignoreCase)) {
        return true;
    }

    return false;

}

From source file:Main.java

/**
 * /*www  .  ja va2  s .  c om*/
 * @param is
 * @param os
 * @param elementNames
 * @throws XMLStreamException
 * @throws FactoryConfigurationError
 * @throws UnsupportedEncodingException
 */
public static void stripElements(final InputStream is, final OutputStream os,
        final Collection<String> elementNames)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
    final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os);

    String elementName = null;

    while (xmlEventReader.peek() != null) {
        final XMLEvent event = (XMLEvent) xmlEventReader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            final StartElement startElement = event.asStartElement();
            final QName name = startElement.getName();

            if (elementNames.contains(name.getLocalPart())) {
                elementName = name.getLocalPart();
            }

            if (elementName == null) {
                xmlEventWriter.add(event);
            }

            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            final EndElement endElement = event.asEndElement();
            final QName name = endElement.getName();

            if (elementName == null) {
                xmlEventWriter.add(event);
            } else if (elementName.equals(name.getLocalPart())) {
                elementName = null;
            }

            break;
        }
        default: {
            if (elementName == null) {
                xmlEventWriter.add(event);
            }
        }
        }
    }

    xmlEventWriter.flush();
}

From source file:Main.java

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

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

/**
 * Return the qualified name, that is to say the prefix -if any- with the
 * local name./*from w  w w . java  2s.  co 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:com.evolveum.midpoint.util.QNameUtil.java

public static String getLocalPart(QName name) {
    return name != null ? name.getLocalPart() : null;
}