List of usage examples for org.w3c.dom NamedNodeMap getNamedItemNS
public Node getNamedItemNS(String namespaceURI, String localName) throws DOMException;
From source file:com.fujitsu.dc.test.jersey.AbstractCase.java
/** * ?????Node?????./* ww w . j a v a 2 s. co m*/ * @param node ???(Node) * @param name ???? * @param ns ????? * @return ??? */ private String getAttributeValueNS(final Node node, final String name, final String ns) { NamedNodeMap nnm = node.getAttributes(); Node attr = nnm.getNamedItemNS(ns, name); if (attr != null) { return attr.getNodeValue(); } else { return ""; } }
From source file:edu.cornell.mannlib.vitro.webapp.controller.harvester.FileHarvestController.java
/** * Parse an XML node for all subnodes with qualified name "rdf:type", and return each's "rdf:resource" value in a list. * @param descriptionNode the RDF description node * @return a list of rdf:types of the given description node *//*from w w w . j av a 2s . c o m*/ private ArrayList<String> getRdfTypes(Node descriptionNode) { ArrayList<String> rdfTypesList = new ArrayList<String>(); NodeList children = descriptionNode.getChildNodes(); int numChildren = children.getLength(); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); String namespace = child.getNamespaceURI(); String name = child.getLocalName(); String fullName = namespace + name; if (fullName.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) { NamedNodeMap attributes = child.getAttributes(); Node resourceAttribute = attributes.getNamedItemNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "resource"); if (resourceAttribute != null) { //String attributeNamespace = resourceAttribute.getNamespaceURI(); String value = resourceAttribute.getNodeValue(); //rdfTypesList.add(attributeNamespace + value); rdfTypesList.add(value); } } } return rdfTypesList; }
From source file:edu.cornell.mannlib.vitro.webapp.controller.harvester.FileHarvestController.java
/** * Parse an additions file (RDF/XML) to get the URIs of newly-harvested data, which will be sent to the browser and * displayed to the user as links./*from w ww .j a va2s. c om*/ * @param additionsFile the file containing the newly-added RDF/XML * @param newlyAddedUris a list in which to place the newly added URIs */ private void extractNewlyAddedUris(File additionsFile, List<String> newlyAddedUris, FileHarvestJob job) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); Document document = factory.newDocumentBuilder().parse(additionsFile); //Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(additionsFile); NodeList descriptionNodes = document .getElementsByTagNameNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "Description"); int numNodes = descriptionNodes.getLength(); for (int i = 0; i < numNodes; i++) { Node node = descriptionNodes.item(i); ArrayList<String> types = getRdfTypes(node); boolean match = false; String[] validRdfTypesForJob = job.getRdfTypesForLinks(); for (String rdfType : validRdfTypesForJob) { if (types.contains(rdfType)) match = true; break; } if (match) { NamedNodeMap attributes = node.getAttributes(); Node aboutAttribute = attributes.getNamedItemNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "about"); if (aboutAttribute != null) { String value = aboutAttribute.getNodeValue(); newlyAddedUris.add(value); } } } } catch (Exception e) { log.error(e, e); } }
From source file:com.marklogic.client.functionaltest.TestSparqlQueryManager.java
@Test public void testBaseUri() throws IOException, SAXException, ParserConfigurationException { System.out.println("In SPARQL Query Manager Test testBaseUri method"); SPARQLQueryManager sparqlQmgr = writeclient.newSPARQLQueryManager(); StringBuffer sparqlQuery = new StringBuffer().append(" PREFIX foaf: <http://xmlns.com/foaf/0.1/>"); sparqlQuery.append(newline);/*from www.j ava 2s .c o m*/ sparqlQuery.append("CONSTRUCT { <qatest1> <qatest2> <qatest3> }"); sparqlQuery.append(newline); sparqlQuery.append("WHERE { ?s ?p ?o . } LIMIT 1"); SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); // Base URI set. qdef.setBaseUri("http://qa.marklogic.com/functional/tests/one/"); // Verifying Git Issue 356 also, below with JacksonHandle. JacksonHandle jh = new JacksonHandle(); JsonNode jsonResults = sparqlQmgr.executeConstruct(qdef, jh).get(); String s = jsonResults.fieldNames().next(); String p = jsonResults.get(s).fieldNames().next(); JsonNode o = jsonResults.get(s).get(p); // Verify the mimetype of the handle. assertEquals("Method testBaseUri MIME type is incorrect", RDFMimeTypes.RDFJSON, jh.getMimetype()); assertEquals("Method testBaseUri in-memory subject base URI is incorrect", "http://qa.marklogic.com/functional/tests/one/qatest1", s); assertEquals("Method testBaseUri in-memory predicte base URI is incorrect", "http://qa.marklogic.com/functional/tests/one/qatest2", p); assertEquals("Method testBaseUri in-memory object base URI is incorrect", "http://qa.marklogic.com/functional/tests/one/qatest3", o.path(0).path("value").asText()); // Verify if defaulting to RDFJSON when XMLHandle is used. Git Issue 356. DOMHandle handle = new DOMHandle(); Document xmlDoc = sparqlQmgr.executeConstruct(qdef, handle).get(); Node description = xmlDoc.getFirstChild().getFirstChild(); NamedNodeMap attrs = description.getFirstChild().getAttributes(); String oXML = attrs.getNamedItemNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "resource") .getTextContent(); assertEquals("Method testBaseUri MIME type is incorrect", RDFMimeTypes.RDFXML, handle.getMimetype()); assertEquals("Method testBaseUri in-memory subject base URI is incorrect", "http://qa.marklogic.com/functional/tests/one/qatest1", description.getAttributes().item(0).getTextContent()); assertEquals("Method testBaseUri in-memory predicate base URI is incorrect", "qatest2", description.getFirstChild().getNodeName()); assertEquals("Method testBaseUri in-memory object base URI is incorrect", "http://qa.marklogic.com/functional/tests/one/qatest3", oXML); }
From source file:de.escidoc.core.test.EscidocTestBase.java
/** * Assert XML content is equal.<br/> * <p/>/*from w ww . j av a2 s . c o m*/ * This methods compares the attributes (if any exist) and either recursively compares the child elements (if any * exists) or the text content.<br/> * Therefore, mixed content is NOT supported by this method. * * @param messageIn * The message printed if assertion fails. * @param expected * The expected XML content. * @param toBeAsserted * The XML content to be compared with the expected content. * @throws Exception * If anything fails. */ public static void assertXmlEquals(final String messageIn, final Node expected, final Node toBeAsserted) throws Exception { // Assert both nodes are null or both nodes are not null if (expected == null) { assertNull(messageIn + "Unexpected node. ", toBeAsserted); return; } assertNotNull(messageIn + " Expected node. ", toBeAsserted); if (expected.equals(toBeAsserted)) { return; } String nodeName = getLocalName(expected); String message = messageIn; if (!message.contains("-- Asserting ")) { message = message + "-- Asserting " + nodeName + ". "; } else { message = message + "/" + nodeName; } // assert both nodes are nodes of the same node type // if thedocument container xslt directive than is the nodeName // "#document" is here compared assertEquals(message + " Type of nodes are different", expected.getNodeType(), toBeAsserted.getNodeType()); if (expected.getNodeType() == Node.TEXT_NODE) { assertEquals(message + " Text nodes are different. ", expected.getTextContent().trim(), toBeAsserted.getTextContent().trim()); } // assert attributes NamedNodeMap expectedAttributes = expected.getAttributes(); NamedNodeMap toBeAssertedAttributes = toBeAsserted.getAttributes(); if (expectedAttributes == null) { assertNull(message + " Unexpected attributes. [" + nodeName + "]", toBeAssertedAttributes); } else { assertNotNull(message + " Expected attributes. ", toBeAssertedAttributes); final int expectedNumberAttributes = expectedAttributes.getLength(); for (int i = 0; i < expectedNumberAttributes; i++) { Node expectedAttribute = expectedAttributes.item(i); String expectedAttributeNamespace = expectedAttribute.getNamespaceURI(); Node toBeAssertedAttribute = null; if (expectedAttributeNamespace != null) { final String localName = expectedAttribute.getLocalName(); toBeAssertedAttribute = toBeAssertedAttributes.getNamedItemNS(expectedAttributeNamespace, localName); assertNotNull(message + " Expected attribute " + expectedAttribute.getNodeName(), toBeAssertedAttribute); } else { // not namespace aware parsed. Attributes may have different // prefixes which are now part of their node name. // To compare expected and to be asserted attribute, it is // first it is tried to find the appropriate to be asserted // attribute by the node name. If this fails, xpath // selection is used after extracting the expected // attribute name final String expectedAttributeNodeName = expectedAttribute.getNodeName(); toBeAssertedAttribute = toBeAssertedAttributes.getNamedItem(expectedAttributeNodeName); if (toBeAssertedAttribute == null) { final String attributeName = getLocalName(expectedAttribute); final String attributeXpath = "@" + attributeName; toBeAssertedAttribute = selectSingleNode(toBeAsserted, attributeXpath); } assertNotNull(message + " Expected attribute " + expectedAttributeNodeName, toBeAssertedAttribute); } assertEquals(message + " Attribute value mismatch [" + expectedAttribute.getNodeName() + "] ", expectedAttribute.getTextContent(), toBeAssertedAttribute.getTextContent()); } } // As mixed content (text + child elements) is not supported, // either the child elements or the text content have to be asserted. // Therefore, it is first tried to assert the children. // After that it is checked if children have been found. If this is not // the case, the text content is compared. NodeList expectedChildren = expected.getChildNodes(); NodeList toBeAssertedChildren = toBeAsserted.getChildNodes(); int expectedNumberElementNodes = 0; int toBeAssertedNumberElementNodes = 0; List<Node> previouslyAssertedChildren = new ArrayList<Node>(); for (int i = 0; i < expectedChildren.getLength(); i++) { Node expectedChild = expectedChildren.item(i); if (expectedChild.getNodeType() == Node.ELEMENT_NODE) { expectedNumberElementNodes++; String expectedChildName = getLocalName(expectedChild); String expectedUri = expectedChild.getNamespaceURI(); boolean expectedElementAsserted = false; for (int j = 0; j < toBeAssertedChildren.getLength(); j++) { final Node toBeAssertedChild = toBeAssertedChildren.item(j); // prevent previously asserted children from being // asserted again if (previouslyAssertedChildren.contains(toBeAssertedChild)) { continue; } if (toBeAssertedChild.getNodeType() == Node.ELEMENT_NODE && expectedChildName.equals(getLocalName(toBeAssertedChild)) && (expectedUri == null || expectedUri.equals(toBeAssertedChild.getNamespaceURI()))) { expectedElementAsserted = true; toBeAssertedNumberElementNodes++; assertXmlEquals(message, expectedChild, toBeAssertedChild); // add asserted child to list of asserted children to // prevent it from being asserted again. previouslyAssertedChildren.add(toBeAssertedChild); break; } } if (!expectedElementAsserted) { fail(new StringBuffer(message).append(" Did not found expected corresponding element [") .append(nodeName).append(", ").append(expectedChildName).append(", ").append(i) .append("]").toString()); } } } // check if any element node in toBeAssertedChildren exists // that has not been asserted. In this case, this element node // is unexpected! for (int i = 0; i < toBeAssertedChildren.getLength(); i++) { Node toBeAssertedChild = toBeAssertedChildren.item(i); // prevent previously asserted children from being // asserted again if (previouslyAssertedChildren.contains(toBeAssertedChild)) { continue; } if (toBeAssertedChild.getNodeType() == Node.ELEMENT_NODE) { fail(new StringBuffer(message).append("Found unexpected element node [").append(nodeName) .append(", ").append(getLocalName(toBeAssertedChild)).append(", ").append(i).append("]") .toString()); } } // if no children have been found, text content must be compared if (expectedNumberElementNodes == 0 && toBeAssertedNumberElementNodes == 0) { String expectedContent = expected.getTextContent(); String toBeAssertedContent = toBeAsserted.getTextContent(); assertEquals(message, expectedContent, toBeAssertedContent); } }
From source file:org.chiba.xml.xforms.xpath.test.InstanceFactoryTest.java
private void assertNamespaceDeclarations(Node node) { assertNotNull(node);//from w w w.j ava 2 s . c o m assertEquals(Node.ELEMENT_NODE, node.getNodeType()); NamedNodeMap attributes = node.getAttributes(); int nsDeclarations = 0; for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); if (NamespaceCtx.XMLNS_NS.equals(attr.getNamespaceURI())) { nsDeclarations++; } } assertEquals(7, nsDeclarations); assertEquals("", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "xmlns").getNodeValue()); assertEquals("http://chiba.sourceforge.net/2003/08/xforms", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "chiba").getNodeValue()); assertEquals("http://www.w3.org/2001/xml-events", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "ev").getNodeValue()); assertEquals("http://my", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "my").getNodeValue()); assertEquals("http://www.w3.org/2002/xforms", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "xforms").getNodeValue()); assertEquals("http://www.w3.org/2001/XMLSchema", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "xsd").getNodeValue()); assertEquals("http://www.w3.org/2001/XMLSchema-instance", attributes.getNamedItemNS(NamespaceCtx.XMLNS_NS, "xsi").getNodeValue()); }
From source file:org.chiba.xml.xpath.impl.JXPathDOMFactoryTest.java
private void assertNamespaceDeclarations(Node node) { assertNotNull(node);//from ww w . ja v a 2 s. co m assertEquals(Node.ELEMENT_NODE, node.getNodeType()); NamedNodeMap attributes = node.getAttributes(); int nsDeclarations = 0; for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); if (NamespaceConstants.XMLNS_NS.equals(attr.getNamespaceURI())) { nsDeclarations++; } } assertEquals(7, nsDeclarations); assertEquals("", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "xmlns").getNodeValue()); assertEquals("http://chiba.sourceforge.net/xforms", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "chiba").getNodeValue()); assertEquals("http://www.w3.org/2001/xml-events", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "ev").getNodeValue()); assertEquals("http://my", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "my").getNodeValue()); assertEquals("http://www.w3.org/2002/xforms", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "xf").getNodeValue()); assertEquals("http://www.w3.org/2001/XMLSchema", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "xsd").getNodeValue()); assertEquals("http://www.w3.org/2001/XMLSchema-instance", attributes.getNamedItemNS(NamespaceConstants.XMLNS_NS, "xsi").getNodeValue()); }
From source file:org.eclipse.uomo.xml.test.XMLTestCase.java
private String compareAttributes(Element e1, Element e2, String p) { NamedNodeMap n1 = e1.getAttributes(); NamedNodeMap n2 = e2.getAttributes(); for (int i = 0; i < n1.getLength(); i++) { Node a1 = n1.item(0);/*from ww w. j a v a 2 s. co m*/ Node a2 = n2.getNamedItemNS(a1.getNamespaceURI(), a1.getLocalName()); if (a2 == null) return "Unable to find attribute " + a1.getNodeName() + " @ " + p; if (a1.getNodeValue() != null || a2.getNodeValue() != null) { if (a1.getNodeValue() == null || a2.getNodeValue() == null || !a1.getNodeValue().equals(a2.getNodeValue())) return "Attribute text differs @ " + p + "/@" + a1.getNodeName() + ": '" + a1.getNodeValue() + "' / '" + a2.getNodeValue() + "'"; } } for (int i = 0; i < n2.getLength(); i++) { Node a2 = n2.item(0); Node a1 = n1.getNamedItemNS(a2.getNamespaceURI(), a2.getLocalName()); if (a1 == null) return "Unable to find attribute " + a2.getNodeName() + " @ " + p; if (a1.getNodeValue() != null || a2.getNodeValue() != null) { if (a1.getNodeValue() == null || a2.getNodeValue() == null || !a1.getNodeValue().equals(a2.getNodeValue())) return "Attribute text differs @ " + p + "/@" + a1.getNodeName() + ": '" + a1.getNodeValue() + "' / '" + a2.getNodeValue() + "'"; } } return null; }
From source file:org.exist.dom.ElementImpl.java
/** * Returns a list of all attribute nodes in attrs that are already present * in the current element./* ww w . ja v a 2 s. c o m*/ * * @param attrs * @return The attributes list * @throws DOMException */ private NodeList findDupAttributes(NodeList attrs) throws DOMException { NodeListImpl dupList = null; final NamedNodeMap map = getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { final Node attr = attrs.item(i); //Workaround: Xerces sometimes returns null for getLocalName() !!!! String localName = attr.getLocalName(); if (localName == null) { localName = attr.getNodeName(); } final Node duplicate = map.getNamedItemNS(attr.getNamespaceURI(), localName); if (duplicate != null) { if (dupList == null) { dupList = new NodeListImpl(); } dupList.add(duplicate); } } return dupList; }
From source file:org.kalypsodeegree.xml.XMLTools.java
/** * Returns the value of the specified node attribute. // * FIXME: Due to apparent bugs in getNamedItemNS (name, * namespace), // * when used to find attribute nodes, the current implementation // * uses a workaround. * <p>//from w ww . ja va 2 s.co m * * @param name * (local) name of attribute * @param namespace * namespace of attribute * @param node * current element * @return the textual contents of the attribute * @throws XMLParsingException * if specified attribute is missing */ public static String getRequiredAttrValue(final String name, final String namespace, final Node node) throws XMLParsingException { String value = null; final NamedNodeMap atts = node.getAttributes(); if (atts != null) { final Attr attribute = (Attr) atts.getNamedItemNS(namespace, name); if (attribute != null) { value = attribute.getValue(); } } // for (int i = 0; i < atts.getLength (); i++) { // Node n = atts.item (i); // System.out.println ("Nodename: " + toLocalName (n.getNodeName ())); // System.out.println ("NamespaceURI: " + n.getNamespaceURI ()); // if (n.getNamespaceURI ().equals (namespace) && toLocalName // (n.getNodeName // ()).equals (name)) { // System.out.println ("Me is here!"); // value = ((Attr) n).getValue (); // break; // } // } if (value == null) { throw new XMLParsingException("Required attribute '" + namespace + ":" + name + "' of element '" + node.getNodeName() + "' is missing."); } return value; }