001 /* 002 * Copyright (c) 2006 Henri Sivonen 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a 005 * copy of this software and associated documentation files (the "Software"), 006 * to deal in the Software without restriction, including without limitation 007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 008 * and/or sell copies of the Software, and to permit persons to whom the 009 * Software is furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 020 * DEALINGS IN THE SOFTWARE. 021 */ 022 023 package fi.iki.hsivonen.xml; 024 025 import java.util.Arrays; 026 027 import org.xml.sax.Attributes; 028 import org.xml.sax.SAXException; 029 030 import gnu.xml.pipeline.EventConsumer; 031 import gnu.xml.pipeline.EventFilter; 032 033 public class BlockPassFilter extends EventFilter { 034 /** 035 * The XHTML namespace URI 036 */ 037 private final static String XHTML_NS = "http://www.w3.org/1999/xhtml"; 038 039 private final static String[] BLOCK_ELEMENTS = { "address", "blockquote", 040 "div", "dl", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "p", 041 "table", "ul" }; 042 043 private int depth; 044 045 public BlockPassFilter(EventConsumer next) { 046 super(next); 047 setContentHandler(this); 048 } 049 050 private static boolean isBlock(String name) { 051 return Arrays.binarySearch(BLOCK_ELEMENTS, name) > -1; 052 } 053 054 /** 055 * @see gnu.xml.pipeline.EventFilter#characters(char[], int, int) 056 */ 057 public void characters(char[] arg0, int arg1, int arg2) throws SAXException { 058 if (depth > 0) { 059 super.characters(arg0, arg1, arg2); 060 } 061 } 062 063 /** 064 * @see gnu.xml.pipeline.EventFilter#endElement(java.lang.String, 065 * java.lang.String, java.lang.String) 066 */ 067 public void endElement(String arg0, String arg1, String arg2) 068 throws SAXException { 069 if (depth > 0) { 070 super.endElement(arg0, arg1, arg2); 071 } 072 depth--; 073 } 074 075 /** 076 * @see gnu.xml.pipeline.EventFilter#ignorableWhitespace(char[], int, int) 077 */ 078 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) 079 throws SAXException { 080 if (depth > 0) { 081 super.ignorableWhitespace(arg0, arg1, arg2); 082 } 083 } 084 085 /** 086 * @see gnu.xml.pipeline.EventFilter#processingInstruction(java.lang.String, 087 * java.lang.String) 088 */ 089 public void processingInstruction(String arg0, String arg1) 090 throws SAXException { 091 if (depth > 0) { 092 super.processingInstruction(arg0, arg1); 093 } 094 } 095 096 /** 097 * @see gnu.xml.pipeline.EventFilter#startDocument() 098 */ 099 public void startDocument() throws SAXException { 100 depth = -1; 101 super.startDocument(); 102 } 103 104 /** 105 * @see gnu.xml.pipeline.EventFilter#startElement(java.lang.String, 106 * java.lang.String, java.lang.String, org.xml.sax.Attributes) 107 */ 108 public void startElement(String namespaceURI, String localName, 109 String qName, Attributes atts) throws SAXException { 110 if (depth > 0) { 111 super.startElement(namespaceURI, localName, qName, atts); 112 depth++; 113 } else if (XHTML_NS.equals(namespaceURI) && isBlock(localName)) { 114 super.startElement(namespaceURI, localName, qName, atts); 115 depth = 1; 116 } 117 } 118 119 }