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());
}
}
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;
}
}
}
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));
}
}
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);
}
}
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);
}
Aggregations