Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(648)

Unified Diff: src/com/dom_distiller/client/DomToSaxVisitor.java

Issue 275493007: filter out invisible elements (Closed) Base URL: https://code.google.com/p/dom-distiller/@master
Patch Set: addressed comments Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/com/dom_distiller/client/DomToSaxParser.java ('k') | src/com/dom_distiller/client/DomUtil.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/com/dom_distiller/client/DomToSaxVisitor.java
diff --git a/src/com/dom_distiller/client/DomToSaxVisitor.java b/src/com/dom_distiller/client/DomToSaxVisitor.java
new file mode 100644
index 0000000000000000000000000000000000000000..ac77a15c24d6168d73db8e7bda0bf1ee0183edd5
--- /dev/null
+++ b/src/com/dom_distiller/client/DomToSaxVisitor.java
@@ -0,0 +1,89 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package com.dom_distiller.client;
+
+import com.google.gwt.core.client.JsArray;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
+import com.google.gwt.dom.client.Text;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.AttributesImpl;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+
+/**
+ * Used to generate sax events from the DOM tree.
+ */
+public class DomToSaxVisitor implements DomWalker.Visitor {
+ private static Logger logger = Logger.getLogger("DomToSaxParser");
+ private static final String sHtmlNamespace = "http://www.w3.org/1999/xhtml";
+ private final ContentHandler handler;
+ private List<Node> textNodes;
+
+ DomToSaxVisitor(ContentHandler h) {
+ handler = h;
+ textNodes = new ArrayList<Node>();
+ }
+
+ /*
+ * @Return A list of the text nodes (in order).
+ */
+ public final List<Node> getTextNodes() { return textNodes; }
+
+ @Override
+ public boolean visit(Node n) {
+ try {
+ switch (n.getNodeType()) {
+ case Node.TEXT_NODE:
+ textNodes.add(n);
+ String text = Text.as(n).getData();
+ handler.characters(text.toCharArray(), 0, text.length());
+ return false;
+ case Node.ELEMENT_NODE:
+ Element e = Element.as(n);
+ Attributes attrs = getSaxAttributes(e);
+ handler.startElement(sHtmlNamespace, e.getTagName(), e.getTagName(), attrs);
+ return true;
+ case Node.DOCUMENT_NODE: // Don't recurse into sub-documents.
+ default: // This case is for comment nodes.
+ return false;
+ }
+ } catch (SAXException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public void exit(Node n) {
+ Element e = Element.as(n);
+ try {
+ handler.endElement(sHtmlNamespace, e.getTagName(), e.getTagName());
+ } catch (SAXException ex) {
+ // Intentionally ignored.
+ }
+ }
+
+ /**
+ * @Return The element's attribute list converted to org.xml.sax.Attributes.
+ */
+ public static Attributes getSaxAttributes(Element e) {
+ AttributesImpl attrs = new AttributesImpl();
+
+ JsArray<Node> jsAttrs = DomUtil.getAttributes(e);
+ for (int i = 0; i < jsAttrs.length(); ++i) {
+ final Node jsAttr = jsAttrs.get(i);
+ attrs.addAttribute("", jsAttr.getNodeName(), jsAttr.getNodeName(), "CDATA",
+ jsAttr.getNodeValue());
+ }
+
+ return attrs;
+ }
+}
« no previous file with comments | « src/com/dom_distiller/client/DomToSaxParser.java ('k') | src/com/dom_distiller/client/DomUtil.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698