List of usage examples for org.w3c.dom Node getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:jGPIO.DTO.java
/** * Tries to use lshw to detect the physical system in use. * /*from w w w . j a v a2s.com*/ * @return The filename of the GPIO Definitions file. */ static private String autoDetectSystemFile() { String definitions = System.getProperty("definitions.lookup"); if (definitions == null) { definitions = DEFAULT_DEFINITIONS; } File capabilitiesFile = new File(definitions); // If it doesn't exist, fall back to the default if (!capabilitiesFile.exists() && !definitions.equals(DEFAULT_DEFINITIONS)) { System.out.println("Could not find definitions lookup file at: " + definitions); System.out.println("Trying default definitions file at: " + definitions); capabilitiesFile = new File(DEFAULT_DEFINITIONS); } if (!capabilitiesFile.exists()) { System.out.println("Could not find definitions file at: " + definitions); return null; } DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = null; try { dBuilder = dbFactory.newDocumentBuilder(); } catch (ParserConfigurationException e1) { e1.printStackTrace(); } // Generate the lshw output if available Process lshw; try { lshw = Runtime.getRuntime().exec("lshw -c bus -disable dmi -xml"); lshw.waitFor(); } catch (Exception e1) { System.out.println("Couldn't execute lshw to identify board"); e1.printStackTrace(); return null; } Document lshwXML = null; try { lshwXML = dBuilder.parse(lshw.getInputStream()); } catch (IOException e1) { System.out.println("IO Exception running lshw"); e1.printStackTrace(); return null; } catch (SAXException e1) { System.out.println("Could not parse lshw output"); e1.printStackTrace(); return null; } XPath xp = XPathFactory.newInstance().newXPath(); NodeList capabilities; try { capabilities = (NodeList) xp.evaluate("/list/node[@id=\"core\"]/capabilities/capability", lshwXML, XPathConstants.NODESET); } catch (XPathExpressionException e1) { System.out.println("Couldn't run Caoability lookup"); e1.printStackTrace(); return null; } Document lookupDocument = null; try { lookupDocument = dBuilder.parse(capabilitiesFile); String lookupID = null; for (int i = 0; i < capabilities.getLength(); i++) { Node c = capabilities.item(i); lookupID = c.getAttributes().getNamedItem("id").getNodeValue(); System.out.println("Looking for: " + lookupID); NodeList nl = (NodeList) xp.evaluate("/lookup/capability[@id=\"" + lookupID + "\"]", lookupDocument, XPathConstants.NODESET); if (nl.getLength() == 1) { definitionFile = nl.item(0).getAttributes().getNamedItem("file").getNodeValue(); pinDefinitions = (JSONArray) new JSONParser().parse(new FileReader(definitionFile)); return definitionFile; } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.github.sevntu.checkstyle.internal.ChecksTest.java
private static void validateSonarProperties(Class<?> module, Set<Node> parameters) { final String moduleName = module.getName(); final Set<String> properties = getFinalProperties(module); for (Node parameter : parameters) { final NamedNodeMap attributes = parameter.getAttributes(); final Node paramKeyNode = attributes.getNamedItem("key"); Assert.assertNotNull(moduleName + " requires a key for unknown parameter in sonar", paramKeyNode); final String paramKey = paramKeyNode.getTextContent(); Assert.assertFalse(moduleName + " requires a valid key for unknown parameter in sonar", paramKey.isEmpty());//from www .j a v a2 s . c om Assert.assertTrue(moduleName + " has an unknown parameter in sonar: " + paramKey, properties.remove(paramKey)); } for (String property : properties) { Assert.fail(moduleName + " parameter not found in sonar: " + property); } }
From source file:com.jaeksoft.searchlib.analysis.ClassFactory.java
/** * /*from w w w. jav a2s .co m*/ * @param classFactory * @return * @throws SearchLibException * @throws ClassNotFoundException * @throws DOMException */ protected static ClassFactory create(Config config, String packageName, Node node, String attributeNodeName) throws SearchLibException, DOMException, ClassNotFoundException { if (node == null) return null; NamedNodeMap nnm = node.getAttributes(); if (nnm == null) return null; Node classNode = nnm.getNamedItem(ClassPropertyEnum.CLASS.getAttribute()); if (classNode == null) return null; ClassFactory newClassFactory = create(config, packageName, classNode.getNodeValue()); newClassFactory.addProperties(nnm); List<Node> attrNodes = DomUtils.getNodes(node, attributeNodeName, "attribute"); if (attrNodes != null) for (Node attrNode : attrNodes) newClassFactory.addProperty(DomUtils.getAttributeText(attrNode, "name"), attrNode.getTextContent()); return newClassFactory; }
From source file:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java
/** * Serializes DOM content as JSON.//w ww .j a va 2 s. c o m * * @param client OData client. * @param jgen JSON generator. * @param content content. * @param propType whether to output type information in the way needed for property values or not. * @throws IOException in case of write error. */ public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content, final boolean propType) throws IOException { for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) { final String childName = XMLUtils.getSimpleName(child); final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE); if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) { jgen.writeStringField( propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeObjectFieldStart(childName); GeospatialJSONHandler.serialize(client, jgen, (Element) child, typeAttr.getTextContent()); jgen.writeEndObject(); } else if (XMLUtils.hasOnlyTextChildNodes(child)) { if (child.hasChildNodes()) { final String out; if (typeAttr == null) { out = child.getChildNodes().item(0).getNodeValue(); } else { final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent()); final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type) .setText(child.getChildNodes().item(0).getNodeValue()).build(); out = value.toString(); jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString()); } jgen.writeStringField(childName, out); } else { if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) { if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) { jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeStringField(childName, StringUtils.EMPTY); } else { jgen.writeArrayFieldStart(childName); jgen.writeEndArray(); } } else { jgen.writeNullField(childName); } } } else { if (XMLUtils.hasElementsChildNode(child)) { jgen.writeArrayFieldStart(childName); for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) { if (XMLUtils.hasOnlyTextChildNodes(nephew)) { jgen.writeString(nephew.getChildNodes().item(0).getNodeValue()); } else { jgen.writeStartObject(); writeSubtree(client, jgen, nephew); jgen.writeEndObject(); } } jgen.writeEndArray(); } else { jgen.writeObjectFieldStart(childName); if (typeAttr != null) { jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent()); } writeSubtree(client, jgen, child); jgen.writeEndObject(); } } } }
From source file:com.github.sevntu.checkstyle.internal.ChecksTest.java
private static void validateEclipseCsMetaXmlFileRules(String pkg, Set<Class<?>> pkgModules, Set<Node> rules) throws Exception { for (Node rule : rules) { final NamedNodeMap attributes = rule.getAttributes(); final Node internalNameNode = attributes.getNamedItem("internal-name"); Assert.assertNotNull(pkg + " checkstyle-metadata.xml must contain an internal name", internalNameNode); final String internalName = internalNameNode.getTextContent(); final String classpath = "com.github.sevntu.checkstyle.checks." + pkg + "." + internalName; final Class<?> module = findModule(pkgModules, classpath); pkgModules.remove(module);/*from w ww . j a va 2 s.c o m*/ Assert.assertNotNull("Unknown class found in " + pkg + " checkstyle-metadata.xml: " + internalName, module); final Node nameAttribute = attributes.getNamedItem("name"); Assert.assertNotNull(pkg + " checkstyle-metadata.xml requires a name for " + internalName, nameAttribute); Assert.assertEquals(pkg + " checkstyle-metadata.xml requires a valid name for " + internalName, "%" + internalName + ".name", nameAttribute.getTextContent()); final Node parentAttribute = attributes.getNamedItem("parent"); Assert.assertNotNull(pkg + " checkstyle-metadata.xml requires a parent for " + internalName, parentAttribute); Assert.assertEquals(pkg + " checkstyle-metadata.xml requires a valid parent for " + internalName, "TreeWalker", parentAttribute.getTextContent()); final Set<Node> children = XmlUtil.getChildrenElements(rule); validateEclipseCsMetaXmlFileRule(pkg, module, children); } }
From source file:de.akra.idocit.wsdl.services.DocumentationParser.java
/** * Extracts from the docpart element the addressees with their individual * documentations and the attributes//from w ww . jav a2 s .c o m * {@link DocumentationParser#THEMATIC_ROLE_ATTRIBUTE_NAME}, * {@link DocumentationParser#THEMATIC_SCOPE_ATTRIBUTE_NAME} and * {@link DocumentationParser#SIGNATURE_ELEMENT_ATTRIBUTE_NAME} of the docpart and * returns a new {@link Documentation} with this information. If an attribute is not * available or has an unknown value it is simply ignored and not set. * * @param docpartElem * docpart element that should be parsed. * @return A new {@link Documentation} with the parsed information. */ private static Documentation parseDocPartElement(Node docpartElem) { Documentation doc = parseAddressees(docpartElem); // read attributes NamedNodeMap attributes = docpartElem.getAttributes(); Node attr; if ((attr = attributes.getNamedItem(THEMATIC_ROLE_ATTRIBUTE_NAME)) != null) { logger.log(Level.FINE, THEMATIC_ROLE_ATTRIBUTE_NAME + "=\"" + attr.getNodeValue() + "\""); ThematicRole role = DescribedItemUtils.findThematicRole(attr.getNodeValue()); doc.setThematicRole(role); } if ((attr = attributes.getNamedItem(DocumentationParser.SIGNATURE_ELEMENT_ATTRIBUTE_NAME)) != null) { logger.log(Level.FINE, DocumentationParser.SIGNATURE_ELEMENT_ATTRIBUTE_NAME + "=\"" + attr.getNodeValue() + "\""); doc.setSignatureElementIdentifier(attr.getNodeValue()); } if ((attr = attributes.getNamedItem(DocumentationParser.ERROR_DOCUMENTATION_ATTRIBUTE_NAME)) != null) { logger.log(Level.FINE, DocumentationParser.ERROR_DOCUMENTATION_ATTRIBUTE_NAME + "=\"" + attr.getNodeValue() + "\""); doc.setErrorCase(Boolean.parseBoolean(attr.getNodeValue())); } return doc; }
From source file:Utils.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }/*from ww w . ja va 2 s. c o m*/ if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); ; } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:org.opencastproject.loadtest.engage.Main.java
/** * Parse the episode xml to retrieve all of the episode ids. * //ww w. j a v a 2 s . co m * @param is * This is the input stream representation of the xml. * @return A linked list of ids for the episodes on the engage server as Strings. **/ public static LinkedList<String> parseEpisodeXml(InputStream is) { LinkedList<String> episodes = new LinkedList<String>(); try { // Parse document DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document document = docBuilder.parse(is); // Process results Node result = null; Node id = null; for (int i = 0; i < document.getElementsByTagName("result").getLength(); i++) { result = document.getElementsByTagName("result").item(i); id = result.getAttributes().getNamedItem("id"); episodes.add(id.getNodeValue()); } } catch (ParserConfigurationException pce) { logger.error( "The list of episodes could not be parsed from the xml received from the engage server because of:", pce); } catch (IOException ioe) { logger.error( "The list of episodes could not be parsed from the xml received from the engage server because of:", ioe); } catch (SAXException sae) { logger.error( "The list of episodes could not be parsed from the xml received from the engage server because of:", sae); } return episodes; }
From source file:Main.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }//from w w w.j a v a2s. c o m if (name.startsWith("#")) { return ""; } StringBuilder sb = new StringBuilder(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { // if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>"); } } else { sb.append('>'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append('\n').append(childToString); hasValidChildren = true; } } if (hasValidChildren) { sb.append('\n'); } // if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:de.akra.idocit.wsdl.services.DocumentationParser.java
/** * Returns the name of the thematic grid in the given documentation-element. * /* ww w . j a v a2 s .c om*/ * Please note: This method returns the name of the first thematic grid found in the * list of documentation's children. * * @param docElemWithThematicGrid * The <documentation>-element containing the thematic grid. * * @return The name of the first found thematic grid */ public static String readThematicGridName(Element docElemWithThematicGrid) { NodeList documentationChildren = docElemWithThematicGrid.getChildNodes(); int children = documentationChildren.getLength(); for (int i = 0; i < children; i++) { Node child = documentationChildren.item(i); if (child.getNodeName().equals(THEMATIC_GRID_TAG_NAME)) { NamedNodeMap attributes = child.getAttributes(); if (attributes != null) { Node nameAttribute = attributes.getNamedItem(THEMATIC_GRID_ATTRIBUTE_NAME); if (nameAttribute != null) { return nameAttribute.getNodeValue(); } else { // It seems that we have an incomplete // <thematicgrid>-element. This is why we don't continue // to search for a named one. return null; } } else { // It seems that we have an incomplete // <thematicgrid>-element. This is why we don't continue to // search for a named one. return null; } } } return null; }