public XhtmlNode getElementByIndex(int i) { int c = 0; for (XhtmlNode n : childNodes) if (n.getNodeType() == NodeType.Element) { if (c == i) return n; else c++; } return null; }
public boolean allChildrenAreText() { boolean res = true; for (XhtmlNode n : childNodes) res = res && n.getNodeType() == NodeType.Text; return res; }
public XhtmlNode getNextElement(XhtmlNode c) { boolean f = false; for (XhtmlNode n : childNodes) { if (n == c) f = true; else if (f && n.getNodeType() == NodeType.Element) return n; } return null; }
public XhtmlNode getFirstElement() { for (XhtmlNode n : childNodes) if (n.getNodeType() == NodeType.Element) return n; return null; }
public XhtmlNode getElement(String name) { for (XhtmlNode n : childNodes) if (n.getNodeType() == NodeType.Element && name.equals(n.getName())) return n; return null; }
public XhtmlNode getDocumentElement() { for (XhtmlNode n : getChildNodes()) { if (n.getNodeType() == NodeType.Element) return n; } return null; } }
public String allText() { if (childNodes == null || childNodes.isEmpty()) return getContent(); StringBuilder b = new StringBuilder(); for (XhtmlNode n : childNodes) if (n.getNodeType() == NodeType.Text) b.append(n.getContent()); else if (n.getNodeType() == NodeType.Element) b.append(n.allText()); return b.toString(); }
private void writeNode(String indent, XhtmlNode node, boolean noPrettyOverride) throws IOException { if (node.getNodeType() == NodeType.Comment) writeComment(indent, node, noPrettyOverride); else if (node.getNodeType() == NodeType.DocType) writeDocType(node); else if (node.getNodeType() == NodeType.Instruction) writeInstruction(node); else if (node.getNodeType() == NodeType.Element) writeElement(indent, node, noPrettyOverride); else if (node.getNodeType() == NodeType.Document) writeDocument(indent, node); else if (node.getNodeType() == NodeType.Text) writeText(node); else if (node.getNodeType() == null) throw new IOException("Null node type"); else throw new IOException("Unknown node type: "+node.getNodeType().toString()); }
public void compose(IXMLWriter xml, XhtmlNode node, boolean noPrettyOverride) throws IOException { if (node.getNodeType() == NodeType.Comment) xml.comment(node.getContent(), pretty && !noPrettyOverride); else if (node.getNodeType() == NodeType.Element) composeElement(xml, node, noPrettyOverride); else if (node.getNodeType() == NodeType.Text) xml.text(node.getContent()); else throw new Error("Unhandled node type: "+node.getNodeType().toString()); }
private void appendChild(Element e, XhtmlNode node) { if (node.getNodeType() == NodeType.Comment) e.appendChild(e.getOwnerDocument().createComment(node.getContent())); else if (node.getNodeType() == NodeType.DocType) throw new Error("not done yet"); else if (node.getNodeType() == NodeType.Instruction) e.appendChild(e.getOwnerDocument().createProcessingInstruction("", node.getContent())); else if (node.getNodeType() == NodeType.Text) e.appendChild(e.getOwnerDocument().createTextNode(node.getContent())); else if (node.getNodeType() == NodeType.Element) { Element child = e.getOwnerDocument().createElementNS(XHTML_NS, node.getName()); e.appendChild(child); for (XhtmlNode c : node.getChildNodes()) { appendChild(child, c); } } else throw new Error("Unknown node type: "+node.getNodeType().toString()); }
private boolean composePlainText(XhtmlNode x, StringBuilder b, boolean lastWS) { if (x.getNodeType() == NodeType.Text) { String s = x.getContent(); if (!lastWS & (s.startsWith(" ") || s.startsWith("\r") || s.startsWith("\n") || s.endsWith("\t"))) { } else if (x.getNodeType() == NodeType.Element) { if (x.getName().equals("li")) { b.append("* ");
private void checkInnerNS(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { String ns = node.getNsDecl(); rule(errors, IssueType.INVALID, e.line(), e.col(), path, ns == null || FormatUtilities.XHTML_NS.equals(ns), "Wrong namespace on the XHTML ('"+ns+"')"); checkInnerNS(errors, e, path, node.getChildNodes()); } } }
private void checkInnerNS(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { String ns = node.getNsDecl(); rule(errors, IssueType.INVALID, e.line(), e.col(), path, ns == null || FormatUtilities.XHTML_NS.equals(ns), "Wrong namespace on the XHTML ('"+ns+"')"); checkInnerNS(errors, e, path, node.getChildNodes()); } } }
private void checkInnerNS(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { String ns = node.getNsDecl(); rule(errors, IssueType.INVALID, e.line(), e.col(), path, ns == null || FormatUtilities.XHTML_NS.equals(ns), "Wrong namespace on the XHTML ('"+ns+"')"); checkInnerNS(errors, e, path, node.getChildNodes()); } } }
private void writeElement(String indent, XhtmlNode node, boolean noPrettyOverride) throws IOException { if (!pretty || noPrettyOverride) indent = ""; // html self closing tags: http://xahlee.info/js/html5_non-closing_tag.html if (node.getChildNodes().size() == 0 && (xml || Utilities.existsInList(node.getName(), "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr"))) dst.append(indent + "<" + node.getName() + attributes(node) + "/>" + (pretty && !noPrettyOverride ? "\r\n" : "")); else { boolean act = node.allChildrenAreText(); if (act || !pretty || noPrettyOverride) dst.append(indent + "<" + node.getName() + attributes(node)+">"); else dst.append(indent + "<" + node.getName() + attributes(node) + ">\r\n"); if (node.getName() == "head" && node.getElement("meta") == null) dst.append(indent + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" + (pretty && !noPrettyOverride ? "\r\n" : "")); for (XhtmlNode c : node.getChildNodes()) writeNode(indent + " ", c, noPrettyOverride || node.isNoPretty()); if (act) dst.append("</" + node.getName() + ">" + (pretty && !noPrettyOverride ? "\r\n" : "")); else if (node.getChildNodes().get(node.getChildNodes().size() - 1).getNodeType() == NodeType.Text) dst.append((pretty && !noPrettyOverride ? "\r\n"+ indent : "") + "</" + node.getName() + ">" + (pretty && !noPrettyOverride ? "\r\n" : "")); else dst.append(indent + "</" + node.getName() + ">" + (pretty && !noPrettyOverride ? "\r\n" : "")); } }
private void addNode(List<Piece> list, XhtmlNode c) { if (c.getNodeType() == NodeType.Text) list.add(new Piece(null, c.getContent(), null)); else if (c.getNodeType() == NodeType.Element) { if (c.getName().equals("a")) { list.add(new Piece(c.getAttribute("href"), c.allText(), c.getAttribute("title"))); throw new Error("Unhandled type "+c.getNodeType().toString());
private void checkInnerNames(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { rule(errors, IssueType.INVALID, e.line(), e.col(), path, Utilities.existsInList(node.getName(), "p", "br", "div", "h1", "h2", "h3", "h4", "h5", "h6", "a", "span", "b", "em", "i", "strong", "small", "big", "tt", "small", "dfn", "q", "var", "abbr", "acronym", "cite", "blockquote", "hr", "address", "bdo", "kbd", "q", "sub", "sup", "ul", "ol", "li", "dl", "dt", "dd", "pre", "table", "caption", "colgroup", "col", "thead", "tr", "tfoot", "tbody", "th", "td", "code", "samp", "img", "map", "area" ), "Illegal element name in the XHTML ('"+node.getName()+"')"); for (String an : node.getAttributes().keySet()) { boolean ok = an.startsWith("xmlns") || Utilities.existsInList(an, "title", "style", "class", "id", "lang", "xml:lang", "dir", "accesskey", "tabindex", // tables "span", "width", "align", "valign", "char", "charoff", "abbr", "axis", "headers", "scope", "rowspan", "colspan") || Utilities.existsInList(node.getName()+"."+an, "a.href", "a.name", "img.src", "img.border", "div.xmlns", "blockquote.cite", "q.cite", "a.charset", "a.type", "a.name", "a.href", "a.hreflang", "a.rel", "a.rev", "a.shape", "a.coords", "img.src", "img.alt", "img.longdesc", "img.height", "img.width", "img.usemap", "img.ismap", "map.name", "area.shape", "area.coords", "area.href", "area.nohref", "area.alt", "table.summary", "table.width", "table.border", "table.frame", "table.rules", "table.cellspacing", "table.cellpadding", "pre.space", "td.nowrap" ); if (!ok) rule(errors, IssueType.INVALID, e.line(), e.col(), path, false, "Illegal attribute name in the XHTML ('"+an+"' on '"+node.getName()+"')"); } checkInnerNames(errors, e, path, node.getChildNodes()); } } }
private void checkInnerNames(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { rule(errors, IssueType.INVALID, e.line(), e.col(), path, Utilities.existsInList(node.getName(), "p", "br", "div", "h1", "h2", "h3", "h4", "h5", "h6", "a", "span", "b", "em", "i", "strong", "small", "big", "tt", "small", "dfn", "q", "var", "abbr", "acronym", "cite", "blockquote", "hr", "address", "bdo", "kbd", "q", "sub", "sup", "ul", "ol", "li", "dl", "dt", "dd", "pre", "table", "caption", "colgroup", "col", "thead", "tr", "tfoot", "tbody", "th", "td", "code", "samp", "img", "map", "area" ), "Illegal element name in the XHTML ('"+node.getName()+"')"); for (String an : node.getAttributes().keySet()) { boolean ok = an.startsWith("xmlns") || Utilities.existsInList(an, "title", "style", "class", "id", "lang", "xml:lang", "dir", "accesskey", "tabindex", // tables "span", "width", "align", "valign", "char", "charoff", "abbr", "axis", "headers", "scope", "rowspan", "colspan") || Utilities.existsInList(node.getName()+"."+an, "a.href", "a.name", "img.src", "img.border", "div.xmlns", "blockquote.cite", "q.cite", "a.charset", "a.type", "a.name", "a.href", "a.hreflang", "a.rel", "a.rev", "a.shape", "a.coords", "img.src", "img.alt", "img.longdesc", "img.height", "img.width", "img.usemap", "img.ismap", "map.name", "area.shape", "area.coords", "area.href", "area.nohref", "area.alt", "table.summary", "table.width", "table.border", "table.frame", "table.rules", "table.cellspacing", "table.cellpadding", "pre.space" ); if (!ok) rule(errors, IssueType.INVALID, e.line(), e.col(), path, false, "Illegal attribute name in the XHTML ('"+an+"' on '"+node.getName()+"')"); } checkInnerNames(errors, e, path, node.getChildNodes()); } } }
private void checkInnerNames(List<ValidationMessage> errors, Element e, String path, List<XhtmlNode> list) { for (XhtmlNode node : list) { if (node.getNodeType() == NodeType.Element) { rule(errors, IssueType.INVALID, e.line(), e.col(), path, Utilities.existsInList(node.getName(), "p", "br", "div", "h1", "h2", "h3", "h4", "h5", "h6", "a", "span", "b", "em", "i", "strong", "small", "big", "tt", "small", "dfn", "q", "var", "abbr", "acronym", "cite", "blockquote", "hr", "address", "bdo", "kbd", "q", "sub", "sup", "ul", "ol", "li", "dl", "dt", "dd", "pre", "table", "caption", "colgroup", "col", "thead", "tr", "tfoot", "tbody", "th", "td", "code", "samp", "img", "map", "area" ), "Illegal element name in the XHTML ('"+node.getName()+"')"); for (String an : node.getAttributes().keySet()) { boolean ok = an.startsWith("xmlns") || Utilities.existsInList(an, "title", "style", "class", "id", "lang", "xml:lang", "dir", "accesskey", "tabindex", // tables "span", "width", "align", "valign", "char", "charoff", "abbr", "axis", "headers", "scope", "rowspan", "colspan") || Utilities.existsInList(node.getName()+"."+an, "a.href", "a.name", "img.src", "img.border", "div.xmlns", "blockquote.cite", "q.cite", "a.charset", "a.type", "a.name", "a.href", "a.hreflang", "a.rel", "a.rev", "a.shape", "a.coords", "img.src", "img.alt", "img.longdesc", "img.height", "img.width", "img.usemap", "img.ismap", "map.name", "area.shape", "area.coords", "area.href", "area.nohref", "area.alt", "table.summary", "table.width", "table.border", "table.frame", "table.rules", "table.cellspacing", "table.cellpadding", "pre.space" ); if (!ok) rule(errors, IssueType.INVALID, e.line(), e.col(), path, false, "Illegal attribute name in the XHTML ('"+an+"' on '"+node.getName()+"')"); } checkInnerNames(errors, e, path, node.getChildNodes()); } } }
protected void composeXhtml(String name, XhtmlNode html) throws IOException { if (!Utilities.noString(xhtmlMessage)) { xml.enter(XhtmlComposer.XHTML_NS, name); xml.comment(xhtmlMessage, false); xml.exit(XhtmlComposer.XHTML_NS, name); } else { XhtmlComposer comp = new XhtmlComposer(XhtmlComposer.XML, htmlPretty); // name is also found in the html and should the same // ? check that boolean oldPretty = xml.isPretty(); xml.setPretty(htmlPretty); if (html.getNodeType() != NodeType.Text && html.getNsDecl() == null) xml.namespace(XhtmlComposer.XHTML_NS, null); comp.compose(xml, html); xml.setPretty(oldPretty); } }