KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > xml > sax > WellFormedContentHandler


1 package org.mr.core.util.xml.sax;
2
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.ContentHandler JavaDoc;
5 import org.xml.sax.SAXException JavaDoc;
6
7 import java.io.Serializable JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10
11 /*
12  * Copyright 2002 by
13  * <a HREF="http://www.coridan.com">Coridan</a>
14  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
15  *
16  * The contents of this file are subject to the Mozilla Public License Version
17  * 1.1 (the "License"); you may not use this file except in compliance with the
18  * License. You may obtain a copy of the License at
19  * http://www.mozilla.org/MPL/
20  *
21  * Software distributed under the License is distributed on an "AS IS" basis,
22  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
23  * for the specific language governing rights and limitations under the
24  * License.
25  *
26  * The Original Code is "MantaRay" (TM).
27  *
28  * The Initial Developer of the Original Code is Coridan.
29  * Portions created by the Initial Developer are Copyright (C) 2006
30  * Coridan Inc. All Rights Reserved.
31  *
32  * Contributor(s): all the names of the contributors are added in the source
33  * code where applicable.
34  *
35  * Alternatively, the contents of this file may be used under the terms of the
36  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
37  * provisions of LGPL are applicable instead of those above. If you wish to
38  * allow use of your version of this file only under the terms of the LGPL
39  * License and not to allow others to use your version of this file under
40  * the MPL, indicate your decision by deleting the provisions above and
41  * replace them with the notice and other provisions required by the LGPL.
42  * If you do not delete the provisions above, a recipient may use your version
43  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
44  
45  *
46  * This library is free software; you can redistribute it and/or modify it
47  * under the terms of the MPL as stated above or under the terms of the GNU
48  * Lesser General Public License as published by the Free Software Foundation;
49  * either version 2.1 of the License, or any later version.
50  *
51  * This library is distributed in the hope that it will be useful, but WITHOUT
52  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
54  * License for more details.
55  */

56
57  /** User: Moti Tal
58  * Date: Apr 10, 2005
59  * Time: 3:40:35 PM
60   *
61  * This is a self correcting FilterContentHandler that tries to make sure
62  * that the propagated events correspond to well-formed XML.
63  *
64  * The handler makes sure XML nesting rules are obeyed.
65  *
66  */

67 public class WellFormedContentHandler extends org.mr.core.util.xml.sax.FilterContentHandler{
68
69     private LinkedList JavaDoc m_stack = new LinkedList JavaDoc();
70
71     public WellFormedContentHandler(ContentHandler JavaDoc i_handler) {
72         super(i_handler);
73     }
74
75     public void endAllEvents() throws SAXException JavaDoc {
76         if (m_stack.isEmpty()) return;
77
78         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Not all tags where closed within content handler: ");
79         for (Iterator JavaDoc it = m_stack.iterator(); it.hasNext();) {
80             SaxEvent event = (SaxEvent) it.next();
81             event.fire();
82             it.remove();
83             buf.append(event).append(" ");
84         }
85     }
86
87     private void push(SaxEvent i_event) {
88         m_stack.addFirst(i_event);
89     }
90
91     private void pop(SaxEvent i_event) throws SAXException JavaDoc {
92         int pos = m_stack.indexOf(i_event);
93         switch (pos) {
94         case -1:
95             // The event is not in the stack at all, ignore it.
96
return;
97         case 0:
98             ((SaxEvent) m_stack.removeFirst()).fire();
99             return;
100         default:
101             // The event is further down in the stack
102
StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Missing end events for: ");
103             for (Iterator JavaDoc it = m_stack.iterator(); it.hasNext();) {
104                 SaxEvent event = (SaxEvent) it.next();
105                 event.fire();
106                 it.remove();
107                 if (event.equals(i_event)) {
108                     // Found matching one
109
return;
110                 }
111                 buf.append(event).append(" ");
112             }
113         }
114     }
115
116     public void endDocument() throws SAXException JavaDoc {
117         endAllEvents();
118     }
119
120     public void startPrefixMapping(String JavaDoc i_prefix, String JavaDoc i_uri) throws SAXException JavaDoc {
121         super.startPrefixMapping(i_prefix, i_uri);
122         push(new EndPrefix(i_prefix));
123     }
124
125     public void endPrefixMapping(String JavaDoc i_prefix) throws SAXException JavaDoc {
126         pop(new EndPrefix(i_prefix));
127     }
128
129     public void startElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName, Attributes JavaDoc i_atts) throws SAXException JavaDoc {
130         super.startElement(i_namespaceURI, i_localName, i_qName, i_atts);
131         push(new EndElement(i_namespaceURI, i_localName, i_qName));
132     }
133
134     public void endElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName) throws SAXException JavaDoc {
135         pop(new EndElement(i_namespaceURI, i_localName, i_qName));
136     }
137
138     public void startDTD(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) throws SAXException JavaDoc {
139         super.startDTD(s, s1, s2);
140         push(new EndDTD());
141     }
142
143     public void endDTD() throws SAXException JavaDoc {
144         pop(new EndDTD());
145     }
146
147     public void startEntity(String JavaDoc s) throws SAXException JavaDoc {
148         super.startEntity(s);
149         push(new EndEntity(s));
150     }
151
152     public void endEntity(String JavaDoc s) throws SAXException JavaDoc {
153         pop(new EndEntity(s));
154     }
155
156     public void startCDATA() throws SAXException JavaDoc {
157         super.startCDATA();
158         push(new EndCDATA());
159     }
160
161     public void endCDATA() throws SAXException JavaDoc {
162         pop(new EndCDATA());
163     }
164
165     /**
166      * A Sax Event
167      */

168     private abstract class SaxEvent implements Serializable JavaDoc {
169
170         /**
171          * The function that writes the event to the handler.
172          *
173          */

174         public abstract void fire()
175                 throws SAXException JavaDoc;
176
177         public int hashCode() {
178             return this.getClass().hashCode();
179         }
180
181         public boolean equals(Object JavaDoc obj) {
182             return this.getClass().equals(obj.getClass());
183         }
184     }
185
186     private abstract class EndEntityOrPrefix extends SaxEvent {
187         protected final String JavaDoc m_string;
188
189         public EndEntityOrPrefix(String JavaDoc i_string) {
190             m_string = i_string;
191         }
192
193         public boolean equals(Object JavaDoc o) {
194             if (this == o) return true;
195             if (!(o instanceof EndEntityOrPrefix)) return false;
196             if (!super.equals(o)) return false;
197
198             final EndEntityOrPrefix endEntity = (EndEntityOrPrefix) o;
199
200             if (!m_string.equals(endEntity.m_string)) return false;
201
202             return true;
203         }
204
205         public int hashCode() {
206             int result = super.hashCode();
207             result = 29 * result + m_string.hashCode();
208             return result;
209         }
210
211     }
212     private class EndPrefix extends EndEntityOrPrefix {
213         public EndPrefix(String JavaDoc i_string) {
214             super(i_string);
215         }
216
217         /**
218          * The function that writes the event to the handler.
219          */

220         public void fire() throws SAXException JavaDoc {
221             WellFormedContentHandler.super.endPrefixMapping(m_string);
222         }
223     }
224
225     private class EndElement extends SaxEvent {
226         private final String JavaDoc m_nsURI;
227         private final String JavaDoc m_local;
228         private final String JavaDoc m_qName;
229
230         public EndElement(String JavaDoc i_nsURI, String JavaDoc i_local, String JavaDoc i_qName) {
231             m_nsURI = i_nsURI;
232             m_local = i_local;
233             m_qName = i_qName;
234         }
235
236         /**
237          * The function that writes the event to the handler.
238          *
239          */

240         public void fire() throws SAXException JavaDoc {
241             WellFormedContentHandler.super.endElement(m_nsURI, m_local, m_qName);
242         }
243
244         public boolean equals(Object JavaDoc o) {
245             if (this == o) return true;
246             if (!(o instanceof EndElement)) return false;
247             if (!super.equals(o)) return false;
248
249             final EndElement endElement = (EndElement) o;
250
251             if (!m_local.equals(endElement.m_local)) return false;
252             if (!m_nsURI.equals(endElement.m_nsURI)) return false;
253             if (!m_qName.equals(endElement.m_qName)) return false;
254
255             return true;
256         }
257
258         public int hashCode() {
259             int result = super.hashCode();
260             result = 29 * result + m_nsURI.hashCode();
261             result = 29 * result + m_local.hashCode();
262             result = 29 * result + m_qName.hashCode();
263             return result;
264         }
265     }
266
267     private class EndDTD extends SaxEvent {
268         /**
269          * The function that writes the event to the handler.
270          */

271         public void fire() throws SAXException JavaDoc {
272             WellFormedContentHandler.super.endDTD();
273         }
274     }
275
276     private class EndEntity extends EndEntityOrPrefix {
277         public EndEntity(String JavaDoc i_string) {
278             super(i_string);
279         }
280
281         /**
282          * The function that writes the event to the handler.
283          */

284         public void fire() throws SAXException JavaDoc {
285             WellFormedContentHandler.super.endEntity(m_string);
286         }
287     }
288
289     private class EndCDATA extends SaxEvent {
290         /**
291          * The function that writes the event to the handler.
292          */

293         public void fire() throws SAXException JavaDoc {
294             WellFormedContentHandler.super.endCDATA();
295         }
296     }
297 }
298
Popular Tags