OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package com.dom_distiller.client; |
| 6 |
| 7 import com.google.gwt.core.client.JsArray; |
| 8 import com.google.gwt.dom.client.Element; |
| 9 import com.google.gwt.dom.client.Node; |
| 10 import com.google.gwt.dom.client.NodeList; |
| 11 import com.google.gwt.dom.client.Text; |
| 12 |
| 13 import org.xml.sax.Attributes; |
| 14 import org.xml.sax.AttributesImpl; |
| 15 import org.xml.sax.ContentHandler; |
| 16 import org.xml.sax.SAXException; |
| 17 |
| 18 import java.util.ArrayList; |
| 19 import java.util.List; |
| 20 import java.util.logging.Logger; |
| 21 |
| 22 /** |
| 23 * Used to generate sax events from the DOM tree. |
| 24 */ |
| 25 public class DomToSaxVisitor implements DomWalker.Visitor { |
| 26 private static Logger logger = Logger.getLogger("DomToSaxParser"); |
| 27 private static final String sHtmlNamespace = "http://www.w3.org/1999/xhtml"; |
| 28 private final ContentHandler handler; |
| 29 private List<Node> textNodes; |
| 30 |
| 31 DomToSaxVisitor(ContentHandler h) { |
| 32 handler = h; |
| 33 textNodes = new ArrayList<Node>(); |
| 34 } |
| 35 |
| 36 /* |
| 37 * @Return A list of the text nodes (in order). |
| 38 */ |
| 39 public final List<Node> getTextNodes() { return textNodes; } |
| 40 |
| 41 @Override |
| 42 public boolean visit(Node n) { |
| 43 try { |
| 44 switch (n.getNodeType()) { |
| 45 case Node.TEXT_NODE: |
| 46 textNodes.add(n); |
| 47 String text = Text.as(n).getData(); |
| 48 handler.characters(text.toCharArray(), 0, text.length()); |
| 49 return false; |
| 50 case Node.ELEMENT_NODE: |
| 51 Element e = Element.as(n); |
| 52 Attributes attrs = getSaxAttributes(e); |
| 53 handler.startElement(sHtmlNamespace, e.getTagName(), e.getTa
gName(), attrs); |
| 54 return true; |
| 55 case Node.DOCUMENT_NODE: // Don't recurse into sub-documents. |
| 56 default: // This case is for comment nodes. |
| 57 return false; |
| 58 } |
| 59 } catch (SAXException e) { |
| 60 return false; |
| 61 } |
| 62 } |
| 63 |
| 64 @Override |
| 65 public void exit(Node n) { |
| 66 Element e = Element.as(n); |
| 67 try { |
| 68 handler.endElement(sHtmlNamespace, e.getTagName(), e.getTagName()); |
| 69 } catch (SAXException ex) { |
| 70 // Intentionally ignored. |
| 71 } |
| 72 } |
| 73 |
| 74 /** |
| 75 * @Return The element's attribute list converted to org.xml.sax.Attributes. |
| 76 */ |
| 77 public static Attributes getSaxAttributes(Element e) { |
| 78 AttributesImpl attrs = new AttributesImpl(); |
| 79 |
| 80 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); |
| 81 for (int i = 0; i < jsAttrs.length(); ++i) { |
| 82 final Node jsAttr = jsAttrs.get(i); |
| 83 attrs.addAttribute("", jsAttr.getNodeName(), jsAttr.getNodeName(), "
CDATA", |
| 84 jsAttr.getNodeValue()); |
| 85 } |
| 86 |
| 87 return attrs; |
| 88 } |
| 89 } |
OLD | NEW |