1 16 package org.apache.cocoon.portal.util; 17 18 import java.io.IOException ; 19 import java.io.StringReader ; 20 import java.util.Iterator ; 21 import java.util.Properties ; 22 23 import org.apache.cocoon.xml.ContentHandlerWrapper; 24 import org.apache.excalibur.xml.sax.XMLConsumer; 25 import org.apache.xerces.parsers.AbstractSAXParser; 26 import org.cyberneko.html.HTMLConfiguration; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.ext.LexicalHandler ; 32 33 38 public class HtmlSaxParser extends AbstractSAXParser { 39 40 public HtmlSaxParser(Properties properties) { 41 super(getConfig(properties)); 42 } 43 44 protected static HTMLConfiguration getConfig(Properties properties) { 45 HTMLConfiguration config = new HTMLConfiguration(); 46 config.setProperty("http://cyberneko.org/html/properties/names/elems", "lower"); 47 if (properties != null) { 48 for (Iterator i = properties.keySet().iterator();i.hasNext();) { 49 String name = (String ) i.next(); 50 config.setProperty(name, properties.getProperty(name)); 51 } 52 } 53 return config; 54 } 55 56 59 public static void parseString(String content, ContentHandler ch) 60 throws SAXException { 61 final HtmlSaxParser parser = new HtmlSaxParser(null); 62 parser.setContentHandler(ch); 63 if ( ch instanceof LexicalHandler ) { 64 parser.setLexicalHandler((LexicalHandler )ch); 65 } 66 final InputSource is = new InputSource (new StringReader (content)); 67 try { 68 parser.parse(is); 69 } catch (IOException ioe) { 70 throw new SAXException ("Error during parsing of html markup.", ioe); 71 } 72 } 73 74 public static XMLConsumer getContentFilter(ContentHandler ch) { 75 return new ContentFilter(ch); 76 } 77 78 protected static final class ContentFilter extends ContentHandlerWrapper { 79 80 public ContentFilter(ContentHandler ch) { 81 this.setContentHandler(ch); 82 if ( ch instanceof LexicalHandler ) { 83 this.setLexicalHandler((LexicalHandler )ch); 84 } 85 } 86 87 90 public void endElement(String uri, String loc, String raw) throws SAXException { 91 if ( !loc.equals("html") && !loc.equals("body") ) { 92 super.endElement(uri, loc, raw); 93 } 94 } 95 96 99 public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { 100 if ( !loc.equals("html") && !loc.equals("body") ) { 101 super.startElement(uri, loc, raw, a); 102 } 103 } 104 } 105 106 } 107 | Popular Tags |