Search in sources :

Example 36 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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 37 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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 38 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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)

Example 39 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project weixin-java-tools by chanjarster.

the class WxCryptUtilTest method testValidateSignatureError.

public void testValidateSignatureError() throws ParserConfigurationException, SAXException, IOException {
    try {
        WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
        String afterEncrpt = pc.encrypt(replyMsg);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        StringReader sr = new StringReader(afterEncrpt);
        InputSource is = new InputSource(sr);
        Document document = db.parse(is);
        Element root = document.getDocumentElement();
        NodeList nodelist1 = root.getElementsByTagName("Encrypt");
        String encrypt = nodelist1.item(0).getTextContent();
        String fromXML = String.format(xmlFormat, encrypt);
        // 这里签名错误
        pc.decrypt("12345", timestamp, nonce, fromXML);
    } catch (RuntimeException e) {
        assertEquals(e.getMessage(), "加密消息签名校验失败");
        return;
    }
    fail("错误流程不抛出异常???");
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 40 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project lucida by claritylab.

the class TREC13To16Parser method loadTargets.

/**
	 * Loads the target objects from a file.
	 * 
	 * @param filename file that contains the targets
	 * @return targets or <code>null</code>, if the file could not be parsed
	 */
public static TRECTarget[] loadTargets(String filename) {
    try {
        // create factory object
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // create DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // parse file and build tree
        Document trecD = parser.parse(new File(filename));
        NodeList targetL = trecD.getElementsByTagName("target");
        TRECTarget[] targets = new TRECTarget[targetL.getLength()];
        for (int i = 0; i < targets.length; i++) {
            Element targetE = (Element) targetL.item(i);
            String targetId = targetE.getAttribute("id").trim();
            String targetDesc = targetE.getAttribute("text").trim();
            NodeList questionL = targetE.getElementsByTagName("q");
            TRECQuestion[] questions = new TRECQuestion[questionL.getLength()];
            for (int j = 0; j < questions.length; j++) {
                Element questionE = (Element) questionL.item(j);
                String questionId = questionE.getAttribute("id").trim();
                String type = questionE.getAttribute("type").trim();
                String questionString = questionE.getFirstChild().getNodeValue().trim();
                questions[j] = new TRECQuestion(questionId, type, questionString);
            }
            targets[i] = new TRECTarget(targetId, targetDesc, questions);
        }
        return targets;
    } catch (Exception e) {
        MsgPrinter.printErrorMsg("Failed to load or parse question file:");
        MsgPrinter.printErrorMsg(e.toString());
        return null;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44