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));
}
}
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);
}
}
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);
}
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("错误流程不抛出异常???");
}
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;
}
}
Aggregations