Search in sources :

Example 91 with Document

use of org.w3c.dom.Document in project checkstyle by checkstyle.

the class XdocsPagesTest method testAllStyleRules.

@Test
public void testAllStyleRules() throws Exception {
    for (Path path : XdocUtil.getXdocsStyleFilePaths(XdocUtil.getXdocsFilePaths())) {
        final String fileName = path.getFileName().toString();
        final String input = new String(Files.readAllBytes(path), UTF_8);
        final Document document = XmlUtil.getRawXml(fileName, input, input);
        final NodeList sources = document.getElementsByTagName("tr");
        Set<String> styleChecks = null;
        if (path.toFile().getName().contains("google")) {
            styleChecks = new HashSet<>(GOOGLE_MODULES);
        } else if (path.toFile().getName().contains("sun")) {
            styleChecks = new HashSet<>();
        }
        String lastRuleName = null;
        for (int position = 0; position < sources.getLength(); position++) {
            final Node row = sources.item(position);
            final List<Node> columns = new ArrayList<>(XmlUtil.findChildElementsByTag(row, "td"));
            if (columns.isEmpty()) {
                continue;
            }
            final String ruleName = columns.get(1).getTextContent().trim();
            if (lastRuleName != null) {
                Assert.assertTrue(fileName + " rule '" + ruleName + "' is out of order compared to '" + lastRuleName + "'", ruleName.toLowerCase(Locale.ENGLISH).compareTo(lastRuleName.toLowerCase(Locale.ENGLISH)) >= 0);
            }
            if (!"--".equals(ruleName)) {
                validateStyleAnchors(XmlUtil.findChildElementsByTag(columns.get(0), "a"), fileName, ruleName);
            }
            validateStyleModules(XmlUtil.findChildElementsByTag(columns.get(2), "a"), XmlUtil.findChildElementsByTag(columns.get(3), "a"), styleChecks, fileName, ruleName);
            lastRuleName = ruleName;
        }
        // these modules aren't documented, but are added to the config
        styleChecks.remove("TreeWalker");
        styleChecks.remove("Checker");
        Assert.assertTrue(fileName + " requires the following check(s) to appear: " + styleChecks, styleChecks.isEmpty());
    }
}
Also used : Path(java.nio.file.Path) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 92 with Document

use of org.w3c.dom.Document in project checkstyle by checkstyle.

the class XdocsPagesTest method testAllCheckSections.

@Test
public void testAllCheckSections() throws Exception {
    final ModuleFactory moduleFactory = TestUtils.getPackageObjectFactory();
    for (Path path : XdocUtil.getXdocsConfigFilePaths(XdocUtil.getXdocsFilePaths())) {
        final String fileName = path.getFileName().toString();
        if ("config_reporting.xml".equals(fileName)) {
            continue;
        }
        final String input = new String(Files.readAllBytes(path), UTF_8);
        final Document document = XmlUtil.getRawXml(fileName, input, input);
        final NodeList sources = document.getElementsByTagName("section");
        String lastSectioName = null;
        for (int position = 0; position < sources.getLength(); position++) {
            final Node section = sources.item(position);
            final String sectionName = section.getAttributes().getNamedItem("name").getNodeValue();
            if ("Content".equals(sectionName) || "Overview".equals(sectionName)) {
                Assert.assertNull(fileName + " section '" + sectionName + "' should be first", lastSectioName);
                continue;
            }
            Assert.assertTrue(fileName + " section '" + sectionName + "' shouldn't end with 'Check'", !sectionName.endsWith("Check"));
            if (lastSectioName != null) {
                Assert.assertTrue(fileName + " section '" + sectionName + "' is out of order compared to '" + lastSectioName + "'", sectionName.toLowerCase(Locale.ENGLISH).compareTo(lastSectioName.toLowerCase(Locale.ENGLISH)) >= 0);
            }
            validateCheckSection(moduleFactory, fileName, sectionName, section);
            lastSectioName = sectionName;
        }
    }
}
Also used : Path(java.nio.file.Path) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 93 with Document

use of org.w3c.dom.Document in project lucida by claritylab.

the class RuleBasedQuestionClassifier method loadRulesFile.

private void loadRulesFile(String fileName) {
    // PARSE XML RULES FILE 
    log.debug("Parsing xml rules file");
    Document rulesDocument;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        rulesDocument = db.parse(fileName);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse XML patterns file", e);
    }
    // EXTRACT RULE DATA.
    log.debug("Loading rules");
    NodeList ruleList = rulesDocument.getElementsByTagName("RULE");
    for (int i = 0; i < ruleList.getLength(); i++) {
        Node rule = ruleList.item(i);
        if (!rule.getNodeName().equals("RULE") || rule.getNodeType() != Node.ELEMENT_NODE)
            continue;
        rules.add(new Rule((Element) rule));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 94 with Document

use of org.w3c.dom.Document in project lucida by claritylab.

the class RuleElement method main.

/**
     * Tests RuleElement creation, compilation and matching.
     * 
     * @param args
     */
public static void main(String[] args) throws Exception {
    String test = "<RULE_ELEMENT feature_name=\"TEST_FEATURE123\">" + "<FEATURE_VALUE>value1</FEATURE_VALUE>" + "<FEATURE_VALUE>value2</FEATURE_VALUE>" + "<FEATURE_VALUE>value3</FEATURE_VALUE>" + "</RULE_ELEMENT>";
    Document ruleElementDocument;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        ruleElementDocument = db.parse(new InputSource(new StringReader(test)));
        RuleElement re = new RuleElement(ruleElementDocument.getDocumentElement());
        System.out.println("Test input: " + test);
        System.out.println(re.toString());
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse XML string", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 95 with Document

use of org.w3c.dom.Document in project weixin-java-tools by chanjarster.

the class WxCryptUtilTest method testNormal.

public void testNormal() throws ParserConfigurationException, SAXException, IOException {
    WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
    String encryptedXml = pc.encrypt(replyMsg);
    System.out.println(encryptedXml);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml)));
    Element root = document.getDocumentElement();
    String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent();
    String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent();
    String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent();
    String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent();
    String messageText = String.format(xmlFormat, cipherText);
    // 第三方收到企业号平台发送的消息
    String plainMessage = pc.decrypt(cipherText);
    System.out.println(plainMessage);
    assertEquals(plainMessage, replyMsg);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Aggregations

Document (org.w3c.dom.Document)2446 Element (org.w3c.dom.Element)990 DocumentBuilder (javax.xml.parsers.DocumentBuilder)719 NodeList (org.w3c.dom.NodeList)648 Node (org.w3c.dom.Node)545 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)528 IOException (java.io.IOException)425 SAXException (org.xml.sax.SAXException)301 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)299 InputSource (org.xml.sax.InputSource)250 Test (org.junit.Test)233 File (java.io.File)190 StringReader (java.io.StringReader)182 ArrayList (java.util.ArrayList)174 InputStream (java.io.InputStream)167 DOMSource (javax.xml.transform.dom.DOMSource)161 ByteArrayInputStream (java.io.ByteArrayInputStream)154 Attr (org.w3c.dom.Attr)134 DOMException (org.w3c.dom.DOMException)129 XPath (javax.xml.xpath.XPath)107