KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > xmlutil > HtmlBodyRemovalHandler


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.xmlutil;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
23
24 import java.util.Enumeration JavaDoc;
25
26 /**
27  * ContentHandler that only passes on the content of the HTML body element.
28  */

29 public class HtmlBodyRemovalHandler implements ContentHandler JavaDoc {
30     private int nestingLevel = 0;
31     private boolean inHtml = false;
32     private boolean inBody = false;
33     private ContentHandler JavaDoc consumer;
34     private NamespaceSupport JavaDoc namespaceSupport = new NamespaceSupport JavaDoc();
35
36     public HtmlBodyRemovalHandler(ContentHandler JavaDoc consumer) {
37         this.consumer = consumer;
38     }
39
40     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
41         nestingLevel++;
42
43         namespaceSupport.pushContext();
44
45         if (inBody) {
46             // Given that there is no grouping element around the content anymore, we need to redeclare
47
// the namespace prefixes (if any) for each child element of body
48
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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
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 JavaDoc {
73         Enumeration JavaDoc prefixEnum = namespaceSupport.getPrefixes();
74         while (prefixEnum.hasMoreElements()) {
75             String JavaDoc prefix = (String JavaDoc)prefixEnum.nextElement();
76             if (!prefix.equals("xml"))
77                 consumer.startPrefixMapping(prefix, namespaceSupport.getURI(prefix));
78         }
79     }
80
81     private void undeclarePrefixes() throws SAXException JavaDoc {
82         Enumeration JavaDoc prefixEnum = namespaceSupport.getPrefixes();
83         while (prefixEnum.hasMoreElements()) {
84             String JavaDoc prefix = (String JavaDoc)prefixEnum.nextElement();
85             if (!prefix.equals("xml"))
86                 consumer.endPrefixMapping(prefix);
87         }
88     }
89
90     public void endDocument() throws SAXException JavaDoc {
91         // ignore
92
}
93
94     public void startDocument() throws SAXException JavaDoc {
95         // ignore
96
}
97
98     public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
99         if (inBody)
100             consumer.characters(ch, start, length);
101     }
102
103     public void ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
104         if (inBody)
105             consumer.ignorableWhitespace(ch, start, length);
106     }
107
108     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
109         if (inBody)
110             consumer.skippedEntity(name);
111     }
112
113     public void setDocumentLocator(Locator JavaDoc locator) {
114         if (inBody)
115             consumer.setDocumentLocator(locator);
116     }
117
118     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
119         if (inBody)
120             consumer.processingInstruction(target, data);
121     }
122
123     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
124         if (!inBody)
125             namespaceSupport.declarePrefix(prefix, uri);
126         else
127             consumer.startPrefixMapping(prefix, uri);
128     }
129
130     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
131         if (!inBody)
132             consumer.endPrefixMapping(prefix);
133     }
134 }
135
Popular Tags