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:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
private static void validateErrorSection(String fileName, String sectionName, Node subSection, Object instance) throws Exception { final Class<?> clss = instance.getClass(); final Set<Field> fields = CheckUtil.getCheckMessages(clss); final Set<String> list = new TreeSet<>(); for (Field field : fields) { // below is required for package/private classes if (!field.isAccessible()) { field.setAccessible(true);/*from w w w . j a va 2 s .c om*/ } list.add(field.get(null).toString()); } final StringBuilder expectedText = new StringBuilder(); for (String s : list) { expectedText.append(s); expectedText.append("\n"); } if (expectedText.length() > 0) { expectedText.append("All messages can be customized if the default message doesn't " + "suit you.\nPlease see the documentation to learn how to."); } if (subSection == null) { Assert.assertEquals(fileName + " section '" + sectionName + "' should have the expected error keys", "", expectedText.toString()); } else { Assert.assertEquals(fileName + " section '" + sectionName + "' should have the expected error keys", expectedText.toString().trim(), subSection.getTextContent().replaceAll("\n\\s+", "\n").trim()); for (Node node : XmlUtil.findChildElementsByTag(subSection, "a")) { final String url = node.getAttributes().getNamedItem("href").getTextContent(); final String linkText = node.getTextContent().trim(); final String expectedUrl; if ("see the documentation".equals(linkText)) { expectedUrl = "config.html#Custom_messages"; } else { expectedUrl = "https://github.com/search?q=" + "path%3Asrc%2Fmain%2Fresources%2F" + clss.getPackage().getName().replace(".", "%2F") + "+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22" + linkText + "%22"; } Assert.assertEquals( fileName + " section '" + sectionName + "' should have matching url for '" + linkText + "'", expectedUrl, url); } } }
From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
private static void validateCheckSection(ModuleFactory moduleFactory, String fileName, String sectionName, Node section) throws Exception { final Object instance; try {// ww w . java 2 s . c om instance = moduleFactory.createModule(sectionName); } catch (CheckstyleException ex) { throw new CheckstyleException(fileName + " couldn't find class: " + sectionName, ex); } int subSectionPos = 0; for (Node subSection : XmlUtil.getChildrenElements(section)) { final String subSectionName = subSection.getAttributes().getNamedItem("name").getNodeValue(); // can be in different orders, and completely optional if ("Notes".equals(subSectionName) || "Rule Description".equals(subSectionName)) { continue; } // optional sections that can be skipped if they have nothing to report if (subSectionPos == 1 && !"Properties".equals(subSectionName)) { validatePropertySection(fileName, sectionName, null, instance); subSectionPos++; } if (subSectionPos == 4 && !"Error Messages".equals(subSectionName)) { validateErrorSection(fileName, sectionName, null, instance); subSectionPos++; } Assert.assertEquals(fileName + " section '" + sectionName + "' should be in order", getSubSectionName(subSectionPos), subSectionName); switch (subSectionPos) { case 0: break; case 1: validatePropertySection(fileName, sectionName, subSection, instance); break; case 2: break; case 3: validateUsageExample(fileName, sectionName, subSection); break; case 4: validateErrorSection(fileName, sectionName, subSection, instance); break; case 5: validatePackageSection(fileName, sectionName, subSection, instance); break; case 6: validateParentSection(fileName, sectionName, subSection); break; default: break; } subSectionPos++; } }
From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java
/** * Gets the rdf pids.// w ww . ja va2 s. com * * @param pid * the pid * @param relation * the relation * @return the rdf pids */ public static ArrayList<String> getRdfPids(String pid, String relation) { ArrayList<String> pids = new ArrayList<String>(); try { String command = configuration.getFedoraHost() + "/get/" + pid + "/" + RELS_EXT_STREAM; InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(), configuration.getFedoraPassword(), true); Document contentDom = XMLUtils.parseDocument(is); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String xPathStr = "/RDF/Description/" + relation; XPathExpression expr = xpath.compile(xPathStr); NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node childnode = nodes.item(i); if (!childnode.getNodeName().contains("hasModel")) { pids.add(childnode.getNodeName() + " " + childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]); } } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return pids; }
From source file:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java
public static String getXmlDataSubstituteNode(Node node, String substituteWith, String substituteInWhat) { StringBuilder sb = new StringBuilder(); switch (node.getNodeType()) { case Node.COMMENT_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: case Node.PROCESSING_INSTRUCTION_NODE: break;/*from w w w . j a v a 2 s . c o m*/ case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: case Node.ELEMENT_NODE: { String nodeName = node.getNodeName(); if (!substituteInWhat.equals(nodeName)) { sb.append("<").append(nodeName); StringBuilder attrs = new StringBuilder(); StringBuilder children = new StringBuilder(); NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Node attr = nnm.item(i); attrs.append(" ").append(getXmlDataSubstituteNode(attr, substituteWith, substituteInWhat)); } } NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { attrs.append(" ") .append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } else { children.append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } } } sb.append(attrs.toString()); if (children.length() > 0) { sb.append(">").append(children.toString()).append("</").append(nodeName).append(">"); } else { sb.append("/>"); } } else { sb.append(substituteWith); } break; } case Node.ATTRIBUTE_NODE: { sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue())) .append("\""); break; } case Node.CDATA_SECTION_NODE: { sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>"); } case Node.TEXT_NODE: { sb.append(StringEscapeUtils.escapeXml(node.getNodeValue())); } } return sb.toString(); }
From source file:Main.java
public static Node findNode(Node root, String elementName, boolean deep, boolean elementsOnly) { //Check to see if root has any children if not return null if (!(root.hasChildNodes())) return null; //Root has children, so continue searching for them Node matchingNode = null;/*from w w w .ja v a 2s . c o m*/ String nodeName = null; Node child = null; NodeList childNodes = root.getChildNodes(); int noChildren = childNodes.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childNodes.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; if (deep) matchingNode = findNode(child, elementName, deep, elementsOnly); } else break; } if (!elementsOnly) { NamedNodeMap childAttrs = root.getAttributes(); noChildren = childAttrs.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childAttrs.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; } else break; } } return matchingNode; }
From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
private static void validateStyleModules(Set<Node> checks, Set<Node> configs, Set<String> styleChecks, String fileName, String ruleName) { final Iterator<Node> itrChecks = checks.iterator(); final Iterator<Node> itrConfigs = configs.iterator(); while (itrChecks.hasNext()) { final Node module = itrChecks.next(); final String moduleName = module.getTextContent().trim(); if (!module.getAttributes().getNamedItem("href").getTextContent().startsWith("config_")) { continue; }/*from w w w .ja va2 s.co m*/ Assert.assertTrue( fileName + " rule '" + ruleName + "' module '" + moduleName + "' shouldn't end with 'Check'", !moduleName.endsWith("Check")); styleChecks.remove(moduleName); for (String configName : new String[] { "config", "test" }) { Node config = null; try { config = itrConfigs.next(); } catch (NoSuchElementException ignore) { Assert.fail(fileName + " rule '" + ruleName + "' module '" + moduleName + "' is missing the config link: " + configName); } Assert.assertEquals(fileName + " rule '" + ruleName + "' module '" + moduleName + "' has mismatched config/test links", configName, config.getTextContent().trim()); final String configUrl = config.getAttributes().getNamedItem("href").getTextContent(); if ("config".equals(configName)) { final String expectedUrl = "https://github.com/search?q=" + "path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+" + "repo%3Acheckstyle%2Fcheckstyle+" + moduleName; Assert.assertEquals(fileName + " rule '" + ruleName + "' module '" + moduleName + "' should have matching " + configName + " url", expectedUrl, configUrl); } else if ("test".equals(configName)) { Assert.assertTrue( fileName + " rule '" + ruleName + "' module '" + moduleName + "' should have matching " + configName + " url", configUrl.startsWith("https://github.com/checkstyle/checkstyle/" + "blob/master/src/it/java/com/google/checkstyle/test/")); Assert.assertTrue(fileName + " rule '" + ruleName + "' module '" + moduleName + "' should have matching " + configName + " url", configUrl.endsWith("/" + moduleName + "Test.java")); Assert.assertTrue( fileName + " rule '" + ruleName + "' module '" + moduleName + "' should have a test that exists", new File(configUrl.substring(53).replace('/', File.separatorChar)).exists()); } } } Assert.assertFalse(fileName + " rule '" + ruleName + "' has too many configs", itrConfigs.hasNext()); }
From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java
private static void validateStyleAnchors(Set<Node> anchors, String fileName, String ruleName) { Assert.assertEquals(fileName + " rule '" + ruleName + "' must have two row anchors", 2, anchors.size()); final int space = ruleName.indexOf(' '); Assert.assertTrue(fileName + " rule '" + ruleName + "' must have have a space between the rule's number and the rule's name", space != -1); final String ruleNumber = ruleName.substring(0, space); int position = 1; for (Node anchor : anchors) { final String actualUrl; final String expectedUrl; if (position == 1) { actualUrl = anchor.getAttributes().getNamedItem("name").getTextContent(); expectedUrl = ruleNumber;/*w ww .j av a2s . c o m*/ } else { actualUrl = anchor.getAttributes().getNamedItem("href").getTextContent(); expectedUrl = "#" + ruleNumber; } Assert.assertEquals( fileName + " rule '" + ruleName + "' anchor " + position + " should have matching name/url", expectedUrl, actualUrl); position++; } }
From source file:Main.java
/** * _more_// w w w .j av a 2s. c om * * @param html _more_ * @param node _more_ */ public static void toHtml(StringBuffer html, Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>"); html.append(": "); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { String v = child.getNodeValue(); if (v == null) { continue; } if (v.trim().length() == 0) { continue; } html.append(v); html.append(" "); } } boolean didone = false; NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); String attrName = attr.getNodeName(); if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) { continue; } if (!didone) { html.append("<ul>"); didone = true; } html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue()); html.append("<br>\n"); } } int cnt = 0; for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { continue; } if (!didone) { html.append("<ul>"); didone = true; } if (cnt > 0) { html.append("<br>"); } toHtml(html, child); cnt++; } if (didone) { html.append("</ul>"); } break; } } }
From source file:com.github.xdcrafts.flower.spring.impl.xml.FeatureBeanDefinitionHandler.java
protected void doParse(Element element, BeanDefinitionBuilder bean) { final ManagedMap<Object, Object> extensions = new ManagedMap<>(); final NodeList bindingNodes = element.getElementsByTagNameNS("http://xdcrafts.github.com/schema/flower", "binding"); if (bindingNodes != null && bindingNodes.getLength() != 0) { for (int i = 0; i < bindingNodes.getLength(); i++) { final Node bindingNode = bindingNodes.item(i); final String extension = bindingNode.getAttributes().getNamedItem("extension").getNodeValue(); final String selector = bindingNode.getAttributes().getNamedItem("selector").getNodeValue(); extensions.put(new RuntimeBeanReference(extension), new RuntimeBeanReference(selector)); }// ww w . j a va 2s.co m } bean.addPropertyValue("extensions", extensions); }
From source file:Main.java
/** * @param node/*from w w w. j a v a2s . c o m*/ * @throws IOException */ public static void serializeNode(Node node) throws IOException { if (writer == null) writer = new BufferedWriter(new OutputStreamWriter(System.out)); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: Document doc = (Document) node; writer.write("<?xml version=\""); writer.write(doc.getXmlVersion()); writer.write("\" encoding=\"UTF-8\" standalone=\""); if (doc.getXmlStandalone()) writer.write("yes"); else writer.write("no"); writer.write("\"?>\n"); NodeList nodes = node.getChildNodes(); if (nodes != null) for (int i = 0; i < nodes.getLength(); i++) serializeNode(nodes.item(i)); break; case Node.ELEMENT_NODE: String name = node.getNodeName(); writer.write("<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); writer.write(" " + current.getNodeName() + "=\""); print(current.getNodeValue()); writer.write("\""); } writer.write(">"); NodeList children = node.getChildNodes(); if (children != null) { //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) // writer.write("\n"); for (int i = 0; i < children.getLength(); i++) serializeNode(children.item(i)); if ((children.item(0) != null) && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE)) writer.write(""); } writer.write("</" + name + ">"); break; case Node.TEXT_NODE: print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: writer.write("CDATA"); print(node.getNodeValue()); writer.write(""); break; case Node.COMMENT_NODE: writer.write("<!-- " + node.getNodeValue() + " -->\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n"); break; case Node.ENTITY_REFERENCE_NODE: writer.write("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; String publicId = docType.getPublicId(); String systemId = docType.getSystemId(); String internalSubset = docType.getInternalSubset(); writer.write("<!DOCTYPE " + docType.getName()); if (publicId != null) writer.write(" PUBLIC \"" + publicId + "\" "); else writer.write(" SYSTEM "); writer.write("\"" + systemId + "\""); if (internalSubset != null) writer.write(" [" + internalSubset + "]"); writer.write(">\n"); break; } writer.flush(); }