@Test void testNamespaces() throws IOException { File file = new File(testDataRoot, "namespace.xml"); LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); lagartoDOMBuilder.enableXmlMode(); lagartoDOMBuilder.getConfig().setCalculatePosition(true); Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file)); assertTrue(doc.check()); Element cfgTestElement = (Element) doc.getChild(1); assertEquals("cfg:test", cfgTestElement.getNodeName()); Element cfgNode = (Element) cfgTestElement.getChild(0); assertEquals("cfg:node", cfgNode.getNodeName()); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode(); Jerry jerry = jerryParser.parse(FileUtil.readString(file)); final StringBuilder result = new StringBuilder(); jerry.$("cfg\\:test").each(($this, index) -> { result.append($this.$("cfg\\:node").text()); return true; }); assertEquals("This is a text", result.toString()); }
jerryParser = Jerry.jerry().enableHtmlMode(); jerryParser.getDOMBuilder().setCaseSensitive(false); jerryParser.getDOMBuilder().setParseSpecialTagsAsCdata(true); jerryParser.getDOMBuilder().setSelfCloseVoidTags(false); jerryParser.getDOMBuilder().setConditionalCommentExpression(null); jerryParser.getDOMBuilder().setEnableConditionalComments(false); jerryParser.getDOMBuilder().setImpliedEndTags(false); jerryParser.getDOMBuilder().setIgnoreComments(true); ipAddress = EntityUtils.toString(response.getEntity()); try { Jerry html = jerryParser.parse(ipAddress); Jerry ipNode = html.$("#greenip"); if(ipNode.length() == 1) {
/** * Updates the copyright dates in the given file. */ @Override public void handleFile(File htmlFile) { if(jerryParser == null){ jerryParser = Jerry.jerry().enableHtmlMode(); jerryParser.getDOMBuilder().setCaseSensitive(false); jerryParser.getDOMBuilder().setParseSpecialTagsAsCdata(true); jerryParser.getDOMBuilder().setSelfCloseVoidTags(false); jerryParser.getDOMBuilder().setConditionalCommentExpression(null); jerryParser.getDOMBuilder().setEnableConditionalComments(false); jerryParser.getDOMBuilder().setImpliedEndTags(false); } log.trace("Handling file {}", htmlFile); try { String fileContents = FileUtils.readFileToString(htmlFile); Jerry html = jerryParser.parse(fileContents); Jerry selector = html.$("[data-cadmium='copyright']"); log.debug("Found {} copyright tags.", selector.length()); if(selector.length() > 0) { selector.text(year.toString()); log.trace("Writing updated file {}",htmlFile); FileUtils.writeStringToFile(htmlFile, html.html(), false); } } catch(Throwable t) { log.warn("Failed to update file "+htmlFile, t); } }
/** * Parses input content and creates new <code>Jerry</code>. */ public static Jerry jerry(String content) { return jerry().parse(content); }
/** * Just creates new {@link JerryParser Jerry runner} to separate * parser creation and creation of new Jerry instances. */ public static JerryParser jerry() { return new JerryParser(); }
/** * Parses input sequence and creates new <code>Jerry</code>. */ public static Jerry jerry(char[] content) { return jerry().parse(content); }
/** * Creates new {@link JerryParser Jerry runner} with * provided implementation of {@link DOMBuilder}. */ public static JerryParser jerry(DOMBuilder domBuilder) { return new JerryParser(domBuilder); }
/** * Creates new {@link jodd.jerry.Jerry.JerryParser Jerry runner} with * provided implementation of {@link jodd.lagarto.dom.DOMBuilder}. */ public static JerryParser jerry(final DOMBuilder domBuilder) { return new JerryParser(domBuilder); }
/** * Parses input content and creates new <code>Jerry</code>. */ public static Jerry jerry(final String content) { return jerry().parse(content); }
/** * Just creates new {@link jodd.jerry.Jerry.JerryParser Jerry runner} to separate * parser creation and creation of new Jerry instances. */ public static JerryParser jerry() { return new JerryParser(); }
private static void getDeploymentInfo(WarInfo info, CadmiumWar warHelper) throws Exception { if(warHelper.fileExists("WEB-INF/jboss-web.xml")) { String jbossWeb = warHelper.fileToString("WEB-INF/jboss-web.xml"); Jerry jbossWebJerry = Jerry.jerry().enableXmlMode().parse(jbossWeb); info.setDomain(jbossWebJerry.$("jboss-web > virtual-host").text()); info.setContext(jbossWebJerry.$("jboss-web > context-root").text()); } }
/** * Parses input sequence and creates new <code>Jerry</code>. */ public static Jerry jerry(final char[] content) { return jerry().parse(content); }
((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode(); Jerry doc = jerryParser.parse(str); Document document = (Document) doc.get(0); Element divNode = (Element) document.getChild(0); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).getConfig().setCaseSensitive(true); doc = jerryParser.parse(str); document = (Document) doc.get(0); divNode = (Element) document.getChild(0);
@Test void testStuck() throws IOException { File file = new File(testDataRoot, "stuck.html.gz"); InputStream in = new GZIPInputStream(new FileInputStream(file)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamUtil.copy(in, out); in.close(); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); // LagartoDOMBuilder lagartoDOMBuilder = (LagartoDOMBuilder) jerryParser.getDOMBuilder(); // lagartoDOMBuilder.setParsingErrorLogLevelName("ERROR"); Jerry doc = jerryParser.parse(out.toString("UTF-8")); // parse try { doc.$("a").each(($this, index) -> { assertEquals("Go to Database Directory", $this.html().trim()); return false; }); } catch (StackOverflowError stackOverflowError) { fail("stack overflow!"); } } }
@Test void testConditionalTags3() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig() .setIgnoreComments(true) .setEnableConditionalComments(true) .setCondCommentIEVersion(10); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]><!--> <body> <!--<![endif]--> \n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><bodyclass=\"ie\"></body></html>", html); } }
@Test void testKelkoo() throws Exception { File file = new File(testDataRoot, "kelkoo.html"); Jerry jerry; try { jerry = Jerry.jerry().parse(FileUtil.readString(file)); } catch (Exception ex) { fail(ex.toString()); throw ex; } Element script = (Element) jerry.$("script").get(0); assertEquals("script", script.getNodeName()); assertEquals(6, script.getAttributesCount()); assertEquals("src", script.getAttribute(0).getName()); assertEquals("data-config", script.getAttribute(1).getName()); assertEquals("ext\\u00e9rieur|barbecue,", script.getAttribute(2).getName()); assertEquals("planchaaccessoires\":\"http:\\", script.getAttribute(3).getName()); assertEquals("www.kelkoo.fr\"}'", script.getAttribute(4).getName()); assertEquals("data-adsense-append", script.getAttribute(5).getName()); }
@Test void testConditionalTags2() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig() .setIgnoreComments(true) .setEnableConditionalComments(true) .setCondCommentIEVersion(8); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]>xxx--><body><!--<![endif]-->\n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><bodyclass=\"ieie8ie_lte_9ie_lte_8\"></body></html>", html); }
@Test void testConditionalTags() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig().setIgnoreComments(true); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]>xxx--><body><!--<![endif]-->\n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><body></body></html>", html); }
@Test void testPortletUtils() throws IOException { File file = new File(testDataRoot, "PortletUtils.html"); String content = FileUtil.readString(file); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); //jerryParser.getDOMBuilder().setCalculatePosition(true); Jerry doc = jerryParser.parse(content); // parse doc.$("a").each(($this, index) -> { assertEquals("<a name=\"navbar_top\"><!-- --></a>", $this.get()[0].getHtml()); return false; }); }
@Test void testAbstractFormController() throws IOException { File file = new File(testDataRoot, "AbstractFormController.html"); String content = FileUtil.readString(file); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); //jerryParser.getDOMBuilder().setCalculatePosition(true); Jerry doc = jerryParser.parse(content); // parse doc.$("a").each(($this, index) -> { assertEquals("<a name=\"navbar_top\"><!-- --></a>", $this.get()[0].getHtml()); return false; }); }