Example usage for javax.xml XMLConstants NULL_NS_URI

List of usage examples for javax.xml XMLConstants NULL_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants NULL_NS_URI.

Prototype

String NULL_NS_URI

To view the source code for javax.xml XMLConstants NULL_NS_URI.

Click Source Link

Document

Namespace URI to use to represent that there is no Namespace.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>";

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    NamespaceContext context = new NamespaceContext() {
        String PREFIX = "nfe";
        String URI = "http://www.java2s.com/nfe";

        @Override/*from  w w w  .  ja v  a  2s .c  om*/
        public String getNamespaceURI(String prefix) {
            return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI;
        }

        @Override
        public String getPrefix(String namespaceUri) {
            return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX;
        }

        @Override
        public Iterator getPrefixes(String namespaceUri) {
            return Collections.singletonList(this.getPrefix(namespaceUri)).iterator();
        }
    };
    xPath.setNamespaceContext(context);
    InputSource inputSource = new InputSource(new StringReader(source));
    String versao = xPath.evaluate("//nfe:p/@versao", inputSource);
    System.out.println(versao.toString());
}

From source file:Main.java

public static String getAttributeValue(StartElement element, String namespaceURI, String localPart) {
    QName name = new QName(namespaceURI == null ? XMLConstants.NULL_NS_URI : namespaceURI, localPart);
    Attribute attribute = element.getAttributeByName(name);
    return attribute == null ? null : attribute.getValue();
}

From source file:Main.java

/**
 * Return the qualified name, that is to say the prefix -if any- with the
 * local name./*from  w  ww  .j  a v  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

/**
 * Get the namespace URI of a given node.
 *
 * @param node The node to get the namespace URI of.
 * @return The namespace URI, or the null namespace URI if the node has none.
 *//*from ww  w.  ja  va 2  s.com*/
public static String getNamespaceUri(Node node) {
    String namespace = node.getNamespaceURI();
    return (namespace != null) ? namespace : XMLConstants.NULL_NS_URI;
}

From source file:Main.java

public static XPathExpression buildXPath(String path, Map<String, String> map) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    if (map != null)
        xpath.setNamespaceContext(new NamespaceContext() {

            public Iterator getPrefixes(String namespaceURI) {
                throw new UnsupportedOperationException();
            }//from  ww w.j ava 2  s. c om

            public String getPrefix(String namespaceURI) {
                throw new UnsupportedOperationException();
            }

            public String getNamespaceURI(String prefix) {
                Objects.requireNonNull(prefix);
                if (map.containsKey(prefix))
                    return map.get(prefix);
                return XMLConstants.NULL_NS_URI;
            }
        });

    try {
        return xpath.compile(path);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Adds attribute with <code>localName</code> to <code>attributes</code> if
 * value is not null. Follows the same rules as
 * {@link AttributesImpl#addAttribute(String, String, String, String, String)}
 * ./*from  w  w  w .  j  a va 2 s. c  o m*/
 * 
 * @param attributes
 *          to add to.
 * @param localName
 *          of attribute to add.
 * @param value
 *          to add to attribute.
 * @since 8.1
 */
static public void addAttribute(final AttributesImpl attributes, final String localName, final Object value) {
    if (null != value) {
        attributes.addAttribute(XMLConstants.NULL_NS_URI, localName, localName, "", value.toString());
    }
}

From source file:eu.esdihumboldt.hale.common.instance.orient.internal.ONamespaceMap.java

/**
 * Map the namespace of the given qualified name to a short identifier and
 * return the adapted name.//from  w  w w  . j  a  va  2 s. c  o  m
 * 
 * @param org the original qualified name
 * @return the adapted qualified name
 */
public static QName map(QName org) {
    if (XMLConstants.NULL_NS_URI.equals(org.getNamespaceURI())) {
        return org;
    }

    return new QName(IDS.getId(org.getNamespaceURI()), org.getLocalPart());
}

From source file:Main.java

/**
 * Start new element on <code>contentHandler</code> with given
 * <code>prefix</code>, <code>localName</code> and <code>attributes</code>.
 * /*  ww  w  .j a  v a 2  s.com*/
 * @param contentHandler
 *          to start element on.
 * @param prefix
 *          of element
 * @param localName
 *          of element
 * @param attributes
 *          of element
 * @throws SAXException
 *           if
 *           {@link ContentHandler#startElement(String, String, String, Attributes)}
 *           throws {@link SAXException}.
 * @since 8.1
 */
static public void startElement(final ContentHandler contentHandler, final String prefix,
        final String localName, final AttributesImpl attributes) throws SAXException {
    contentHandler.startElement(XMLConstants.NULL_NS_URI, localName, prefix + ":" + localName, attributes);
}

From source file:eu.esdihumboldt.hale.common.instance.orient.internal.ONamespaceMap.java

/**
 * Encode a {@link QName} for runtime use with OrientDB.
 * //from w  w w.  j  a va2 s .c o m
 * @param org the qualified name
 * @return the encoded name
 */
public static String encode(QName org) {
    String ns = org.getNamespaceURI();
    if (!XMLConstants.NULL_NS_URI.equals(ns)) {
        ns = IDS.getId(org.getNamespaceURI());
    }

    return ns + "_" + ONameUtil.encodeName(org.getLocalPart());
}

From source file:Main.java

/**
 * Adds attribute with <code>prefix</code> and <code>localName</code> to
 * <code>attributes</code> if value is not null. Follows the same rules as
 * {@link AttributesImpl#addAttribute(String, String, String, String, String)}
 * ./*from  w w w.  j a va 2s .co m*/
 * 
 * @param attributes
 *          to add to.
 * @param prefix
 *          of the attribute.
 * @param localName
 *          of attribute to add.
 * @param value
 *          to add to attribute.
 * @since 8.1
 */
static public void addAttribute(final AttributesImpl attributes, final String prefix, final String localName,
        final Object value) {
    if (null != value) {
        attributes.addAttribute(XMLConstants.NULL_NS_URI, localName, prefix + ":" + localName, "",
                value.toString());
    }
}