KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MarkupDigester


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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
17 import org.apache.commons.digester.Digester;
18 import org.apache.commons.digester.Rule;
19
20 import java.util.List JavaDoc;
21 import javax.xml.parsers.SAXParser JavaDoc;
22 import org.xml.sax.XMLReader JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25
26 /**
27  * This is a subclass of digester which supports rules which implement
28  * the TextSegmentHandler interface, causing the "textSegment" method
29  * on each matching rule (of the appropriate type) to be invoked when
30  * an element contains a segment of text followed by a child element.
31  * <p>
32  * See the readme file included with this example for more information.
33  */

34  
35 public class MarkupDigester extends Digester {
36
37     /** See equivalent constructor in Digester class. */
38     public MarkupDigester() {
39     }
40
41     /** See equivalent constructor in Digester class. */
42     public MarkupDigester(SAXParser JavaDoc parser) {
43         super(parser);
44     }
45
46     /** See equivalent constructor in Digester class. */
47     public MarkupDigester(XMLReader JavaDoc reader) {
48         super(reader);
49     }
50
51     //===================================================================
52

53     /**
54      * The text found in the current element since the last child element.
55      */

56     protected StringBuffer JavaDoc currTextSegment = new StringBuffer JavaDoc();
57
58     /**
59      * Process notification of character data received from the body of
60      * an XML element.
61      *
62      * @param buffer The characters from the XML document
63      * @param start Starting offset into the buffer
64      * @param length Number of characters from the buffer
65      *
66      * @exception SAXException if a parsing error is to be reported
67      */

68     public void characters(char buffer[], int start, int length)
69             throws SAXException JavaDoc {
70
71         super.characters(buffer, start, length);
72         currTextSegment.append(buffer, start, length);
73     }
74
75     /**
76      * Process notification of the start of an XML element being reached.
77      *
78      * @param namespaceURI The Namespace URI, or the empty string if the element
79      * has no Namespace URI or if Namespace processing is not being performed.
80      * @param localName The local name (without prefix), or the empty
81      * string if Namespace processing is not being performed.
82      * @param qName The qualified name (with prefix), or the empty
83      * string if qualified names are not available.
84      * @param list The attributes attached to the element. If there are
85      * no attributes, it shall be an empty Attributes object.
86      * @exception SAXException if a parsing error is to be reported
87      */

88  
89     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
90                              String JavaDoc qName, Attributes JavaDoc list)
91             throws SAXException JavaDoc {
92
93         handleTextSegments();
94
95         // Unlike bodyText, which accumulates despite intervening child
96
// elements, currTextSegment gets cleared here. This means that
97
// we don't need to save it on a stack either.
98
currTextSegment.setLength(0);
99
100         super.startElement(namespaceURI, localName, qName, list);
101     }
102
103     /**
104      * Process notification of the end of an XML element being reached.
105      *
106      * @param namespaceURI - The Namespace URI, or the empty string if the
107      * element has no Namespace URI or if Namespace processing is not
108      * being performed.
109      * @param localName - The local name (without prefix), or the empty
110      * string if Namespace processing is not being performed.
111      * @param qName - The qualified XML 1.0 name (with prefix), or the
112      * empty string if qualified names are not available.
113      * @exception SAXException if a parsing error is to be reported
114      */

115     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
116                            String JavaDoc qName) throws SAXException JavaDoc {
117  
118         handleTextSegments();
119         currTextSegment.setLength(0);
120         super.endElement(namespaceURI, localName, qName);
121      }
122
123     /**
124      * Iterate over the list of rules most recently matched, and
125      * if any of them implement the TextSegmentHandler interface then
126      * invoke that rule's textSegment method passing the current
127      * segment of text from the xml element body.
128      */

129     private void handleTextSegments() throws SAXException JavaDoc {
130         if (currTextSegment.length() > 0) {
131             String JavaDoc segment = currTextSegment.toString();
132             List JavaDoc parentMatches = (List JavaDoc) matches.peek();
133             int len = parentMatches.size();
134             for(int i=0; i<len; ++i) {
135                 Rule r = (Rule) parentMatches.get(i);
136                 if (r instanceof TextSegmentHandler) {
137                     TextSegmentHandler h = (TextSegmentHandler) r;
138                     try {
139                         h.textSegment(segment);
140                     } catch(Exception JavaDoc e) {
141                         throw createSAXException(e);
142                     }
143                 }
144             }
145         }
146     }
147 }
148
Popular Tags