| 1 16 package org.outerj.daisy.xmlutil; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.helpers.NamespaceSupport ; 23 24 import java.util.Enumeration ; 25 26 29 public class HtmlBodyRemovalHandler implements ContentHandler { 30 private int nestingLevel = 0; 31 private boolean inHtml = false; 32 private boolean inBody = false; 33 private ContentHandler consumer; 34 private NamespaceSupport namespaceSupport = new NamespaceSupport (); 35 36 public HtmlBodyRemovalHandler(ContentHandler consumer) { 37 this.consumer = consumer; 38 } 39 40 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 41 nestingLevel++; 42 43 namespaceSupport.pushContext(); 44 45 if (inBody) { 46 if (nestingLevel == 3) 49 declarePrefixes(); 50 51 consumer.startElement(namespaceURI, localName, qName, atts); 52 } else if (nestingLevel == 1 && localName.equals("html") && namespaceURI.equals("")) { 53 inHtml = true; 54 } else if (inHtml && nestingLevel == 2 && localName.equals("body") && namespaceURI.equals("")) { 55 inBody = true; 56 } 57 } 58 59 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 60 namespaceSupport.popContext(); 61 if (inBody && nestingLevel == 3) 62 undeclarePrefixes(); 63 64 if (inBody && nestingLevel == 2 && localName.equals("body") && namespaceURI.equals("")) { 65 inBody = false; 66 } else if (inBody) { 67 consumer.endElement(namespaceURI, localName, qName); 68 } 69 nestingLevel--; 70 } 71 72 private void declarePrefixes() throws SAXException { 73 Enumeration prefixEnum = namespaceSupport.getPrefixes(); 74 while (prefixEnum.hasMoreElements()) { 75 String prefix = (String )prefixEnum.nextElement(); 76 if (!prefix.equals("xml")) 77 consumer.startPrefixMapping(prefix, namespaceSupport.getURI(prefix)); 78 } 79 } 80 81 private void undeclarePrefixes() throws SAXException { 82 Enumeration prefixEnum = namespaceSupport.getPrefixes(); 83 while (prefixEnum.hasMoreElements()) { 84 String prefix = (String )prefixEnum.nextElement(); 85 if (!prefix.equals("xml")) 86 consumer.endPrefixMapping(prefix); 87 } 88 } 89 90 public void endDocument() throws SAXException { 91 } 93 94 public void startDocument() throws SAXException { 95 } 97 98 public void characters(char ch[], int start, int length) throws SAXException { 99 if (inBody) 100 consumer.characters(ch, start, length); 101 } 102 103 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 104 if (inBody) 105 consumer.ignorableWhitespace(ch, start, length); 106 } 107 108 public void skippedEntity(String name) throws SAXException { 109 if (inBody) 110 consumer.skippedEntity(name); 111 } 112 113 public void setDocumentLocator(Locator locator) { 114 if (inBody) 115 consumer.setDocumentLocator(locator); 116 } 117 118 public void processingInstruction(String target, String data) throws SAXException { 119 if (inBody) 120 consumer.processingInstruction(target, data); 121 } 122 123 public void startPrefixMapping(String prefix, String uri) throws SAXException { 124 if (!inBody) 125 namespaceSupport.declarePrefix(prefix, uri); 126 else 127 consumer.startPrefixMapping(prefix, uri); 128 } 129 130 public void endPrefixMapping(String prefix) throws SAXException { 131 if (!inBody) 132 consumer.endPrefixMapping(prefix); 133 } 134 } 135 | Popular Tags |