Example usage for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

List of usage examples for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

Introduction

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

Prototype

String XMLNS_ATTRIBUTE_NS_URI

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

Click Source Link

Document

The official XML attribute used for specifying XML Namespace declarations, #XMLNS_ATTRIBUTE XMLConstants.XMLNS_ATTRIBUTE , Namespace name URI.

Usage

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*ww  w.  j  a va  2 s  .com*/
public void testAddAttributeFromQNameWithNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement retValue = element.addAttribute(new QName("urn:ns", "attr", "p"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS("urn:ns", "attr");
    assertEquals("urn:ns", attr.getNamespaceURI());
    assertEquals("attr", attr.getLocalName());
    assertEquals("p", attr.getPrefix());
    assertEquals("value", attr.getValue());
    Attr nsDecl = element.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p");
    assertNotNull(nsDecl);
    assertEquals("urn:ns", nsDecl.getValue());
}

From source file:de.pangaea.fixo3.xml.ProcessXmlFiles.java

private void run() throws FileNotFoundException, SAXException, IOException, ParserConfigurationException,
        XPathExpressionException {
    // src/test/resources/, src/main/resources/files
    File folder = new File("src/main/resources/files");
    File[] files = folder.listFiles(new FileFilter() {

        @Override/*  ww  w .ja v a 2  s  . c o  m*/
        public boolean accept(File pathname) {
            if (pathname.getName().endsWith(".xml"))
                return true;

            return false;
        }
    });

    JsonArrayBuilder json = Json.createArrayBuilder();

    Document doc;
    String deviceLabel, deviceComment, deviceImage;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    final Map<String, String> prefixToNS = new HashMap<>();
    prefixToNS.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    prefixToNS.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
    prefixToNS.put("sml", "http://www.opengis.net/sensorml/2.0");
    prefixToNS.put("gml", "http://www.opengis.net/gml/3.2");
    prefixToNS.put("gmd", "http://www.isotc211.org/2005/gmd");

    XPath x = XPathFactory.newInstance().newXPath();
    x.setNamespaceContext(new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            Objects.requireNonNull(prefix, "Namespace prefix cannot be null");
            final String uri = prefixToNS.get(prefix);
            if (uri == null) {
                throw new IllegalArgumentException("Undeclared namespace prefix: " + prefix);
            }
            return uri;
        }

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

        @Override
        public Iterator<?> getPrefixes(String namespaceURI) {
            throw new UnsupportedOperationException();
        }
    });

    XPathExpression exGmlName = x.compile("/sml:PhysicalSystem/gml:name");
    XPathExpression exGmlDescription = x.compile("/sml:PhysicalSystem/gml:description");
    XPathExpression exGmdUrl = x.compile(
            "/sml:PhysicalSystem/sml:documentation/sml:DocumentList/sml:document/gmd:CI_OnlineResource/gmd:linkage/gmd:URL");
    XPathExpression exTerm = x
            .compile("/sml:PhysicalSystem/sml:classification/sml:ClassifierList/sml:classifier/sml:Term");

    int m = 0;
    int n = 0;

    for (File file : files) {
        log.info(file.getName());
        doc = dbf.newDocumentBuilder().parse(new FileInputStream(file));

        deviceLabel = exGmlName.evaluate(doc).trim();
        deviceComment = exGmlDescription.evaluate(doc).trim();
        deviceImage = exGmdUrl.evaluate(doc).trim();
        NodeList terms = (NodeList) exTerm.evaluate(doc, XPathConstants.NODESET);

        JsonObjectBuilder jobDevice = Json.createObjectBuilder();

        jobDevice.add("name", toUri(deviceLabel));
        jobDevice.add("label", deviceLabel);
        jobDevice.add("comment", toAscii(deviceComment));
        jobDevice.add("image", deviceImage);

        JsonArrayBuilder jabSubClasses = Json.createArrayBuilder();

        for (int i = 0; i < terms.getLength(); i++) {
            Node term = terms.item(i);
            NodeList attributes = term.getChildNodes();

            String attributeLabel = null;
            String attributeValue = null;

            for (int j = 0; j < attributes.getLength(); j++) {
                Node attribute = attributes.item(j);
                String attributeName = attribute.getNodeName();

                if (attributeName.equals("sml:label")) {
                    attributeLabel = attribute.getTextContent();
                } else if (attributeName.equals("sml:value")) {
                    attributeValue = attribute.getTextContent();
                }
            }

            if (attributeLabel == null || attributeValue == null) {
                throw new RuntimeException("Attribute label or value cannot be null [attributeLabel = "
                        + attributeLabel + "; attributeValue = " + attributeValue + "]");
            }

            if (attributeLabel.equals("model")) {
                continue;
            }

            if (attributeLabel.equals("manufacturer")) {
                jobDevice.add("manufacturer", attributeValue);
                continue;
            }

            n++;

            Quantity quantity = getQuantity(attributeValue);

            if (quantity == null) {
                continue;
            }

            m++;

            JsonObjectBuilder jobSubClass = Json.createObjectBuilder();
            JsonObjectBuilder jobCapability = Json.createObjectBuilder();
            JsonObjectBuilder jobQuantity = Json.createObjectBuilder();

            String quantityLabel = getQuantityLabel(attributeLabel);
            String capabilityLabel = deviceLabel + " " + quantityLabel;

            jobCapability.add("label", capabilityLabel);

            jobQuantity.add("label", quantity.getLabel());

            if (quantity.getValue() != null) {
                jobQuantity.add("value", quantity.getValue());
            } else if (quantity.getMinValue() != null && quantity.getMaxValue() != null) {
                jobQuantity.add("minValue", quantity.getMinValue());
                jobQuantity.add("maxValue", quantity.getMaxValue());
            } else {
                throw new RuntimeException(
                        "Failed to determine quantity value [attributeValue = " + attributeValue + "]");
            }

            jobQuantity.add("unitCode", quantity.getUnitCode());
            jobQuantity.add("type", toUri(quantityLabel));

            jobCapability.add("quantity", jobQuantity);
            jobSubClass.add("capability", jobCapability);

            jabSubClasses.add(jobSubClass);
        }

        jobDevice.add("subClasses", jabSubClasses);

        json.add(jobDevice);
    }

    Map<String, Object> properties = new HashMap<>(1);
    properties.put(JsonGenerator.PRETTY_PRINTING, true);

    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(properties);
    JsonWriter jsonWriter = jsonWriterFactory.createWriter(new FileWriter(new File(jsonFileName)));
    jsonWriter.write(json.build());
    jsonWriter.close();

    System.out.println("Fraction of characteristics included: " + m + "/" + n);
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from ww w .  j a va 2  s. c  o m*/
public void testAddAttributeFromNameWithNamespace() throws Exception {
    SOAPEnvelope envelope = saajUtil.createSOAP11Envelope();
    SOAPBody body = envelope.addBody();
    SOAPElement element = body.addChildElement(new QName("urn:test", "test"));
    SOAPElement retValue = element.addAttribute(envelope.createName("attr", "p", "urn:ns"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS("urn:ns", "attr");
    assertEquals("urn:ns", attr.getNamespaceURI());
    assertEquals("attr", attr.getLocalName());
    assertEquals("p", attr.getPrefix());
    assertEquals("value", attr.getValue());
    Attr nsDecl = element.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p");
    assertNotNull(nsDecl);
    assertEquals("urn:ns", nsDecl.getValue());
}

From source file:InlineSchemaValidator.java

public String getNamespaceURI(String prefix) {
    if (prefix == null) {
        throw new IllegalArgumentException("Prefix cannot be null.");
    } else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
        return XMLConstants.XML_NS_URI;
    } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
        return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
    } else if (fPrefixToURIMappings != null) {
        String uri = (String) fPrefixToURIMappings.get(prefix);
        if (uri != null) {
            return uri;
        }/*from   w ww . jav  a2s  . co  m*/
    }
    return XMLConstants.NULL_NS_URI;
}

From source file:InlineSchemaValidator.java

public String getPrefix(String namespaceURI) {
    if (namespaceURI == null) {
        throw new IllegalArgumentException("Namespace URI cannot be null.");
    } else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XML_NS_PREFIX;
    } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XMLNS_ATTRIBUTE;
    } else if (fURIToPrefixMappings != null) {
        HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
        if (prefixes != null && prefixes.size() > 0) {
            return (String) prefixes.iterator().next();
        }/*ww  w.  ja  va2s .c o m*/
    }
    return null;
}

From source file:ch.entwine.weblounge.bridge.oaipmh.util.XmlGen.java

private Element appendNs(Element e, List<Namespace> namespaces) {
    for (Namespace n : namespaces) {
        e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                XMLConstants.XMLNS_ATTRIBUTE + ":" + n.getPrefix(), n.getNamespace());
    }/*  w w  w . j  a  va2  s.co m*/
    return e;
}

From source file:InlineSchemaValidator.java

public Iterator getPrefixes(String namespaceURI) {
    if (namespaceURI == null) {
        throw new IllegalArgumentException("Namespace URI cannot be null.");
    } else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
        return new Iterator() {
            boolean more = true;

            public boolean hasNext() {
                return more;
            }/*from  ww  w .j av  a2 s.c o  m*/

            public Object next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                more = false;
                return XMLConstants.XML_NS_PREFIX;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
        return new Iterator() {
            boolean more = true;

            public boolean hasNext() {
                return more;
            }

            public Object next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                more = false;
                return XMLConstants.XMLNS_ATTRIBUTE;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    } else if (fURIToPrefixMappings != null) {
        HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
        if (prefixes != null && prefixes.size() > 0) {
            return prefixes.iterator();
        }
    }
    return Collections.EMPTY_LIST.iterator();
}

From source file:de.fau.cs.osr.hddiff.perfsuite.RunFcDiff.java

private boolean compareAttributes(Collection<Wom3Attribute> a, Collection<Wom3Attribute> b) {
    final int size = a.size();
    Wom3Attribute[] aa = a.toArray(new Wom3Attribute[size]);
    Wom3Attribute[] ba = b.toArray(new Wom3Attribute[b.size()]);
    Comparator<Wom3Attribute> cmp = new Comparator<Wom3Attribute>() {
        @Override/*from ww w.  j a v a  2  s . c  o  m*/
        public int compare(Wom3Attribute o1, Wom3Attribute o2) {
            return o1.getName().compareTo(o2.getName());
        }
    };
    Arrays.sort(aa, cmp);
    Arrays.sort(ba, cmp);
    for (int i = 0, j = 0; i < aa.length && j < ba.length;) {
        Wom3Attribute attrA = aa[i];
        Wom3Attribute attrB = ba[i];
        if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attrA.getNamespaceURI())
                && XMLConstants.XMLNS_ATTRIBUTE.equals(attrA.getName())) {
            ++i;
            continue;
        }
        if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attrB.getNamespaceURI())
                && XMLConstants.XMLNS_ATTRIBUTE.equals(attrB.getName())) {
            ++j;
            continue;
        }
        if (!cmpStr(attrA.getName(), attrB.getName())) {
            return false;
        }
        if (!cmpStr(attrA.getValue(), attrB.getValue())) {
            return false;
        }
        ++i;
        ++j;
    }
    return true;
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from   w w w .  java2 s . c  o  m*/
public void testAddChildElementWithUndeclaredNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement child = element.addChildElement("test", "p", "urn:ns");
    assertEquals("urn:ns", child.getNamespaceURI());
    assertEquals("p", child.getPrefix());
    assertEquals("test", child.getLocalName());
    Attr nsDecl = child.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p");
    assertNotNull(nsDecl);
    assertEquals("urn:ns", nsDecl.getValue());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from ww  w. java2 s .c  o  m*/
public void testAddChildElementWithDeclaredNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:p", "urn:ns");
    SOAPElement child = element.addChildElement("test", "p", "urn:ns");
    assertEquals("urn:ns", child.getNamespaceURI());
    assertEquals("p", child.getPrefix());
    assertEquals("test", child.getLocalName());
    assertEquals(0, child.getAttributes().getLength());
}