1 package org.mr.core.util.xml.sax; 2 3 import org.xml.sax.Attributes ; 4 import org.xml.sax.ContentHandler ; 5 import org.xml.sax.SAXException ; 6 7 import java.io.Serializable ; 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 11 56 57 67 public class WellFormedContentHandler extends org.mr.core.util.xml.sax.FilterContentHandler{ 68 69 private LinkedList m_stack = new LinkedList (); 70 71 public WellFormedContentHandler(ContentHandler i_handler) { 72 super(i_handler); 73 } 74 75 public void endAllEvents() throws SAXException { 76 if (m_stack.isEmpty()) return; 77 78 StringBuffer buf = new StringBuffer ("Not all tags where closed within content handler: "); 79 for (Iterator 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 { 92 int pos = m_stack.indexOf(i_event); 93 switch (pos) { 94 case -1: 95 return; 97 case 0: 98 ((SaxEvent) m_stack.removeFirst()).fire(); 99 return; 100 default: 101 StringBuffer buf = new StringBuffer ("Missing end events for: "); 103 for (Iterator 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 return; 110 } 111 buf.append(event).append(" "); 112 } 113 } 114 } 115 116 public void endDocument() throws SAXException { 117 endAllEvents(); 118 } 119 120 public void startPrefixMapping(String i_prefix, String i_uri) throws SAXException { 121 super.startPrefixMapping(i_prefix, i_uri); 122 push(new EndPrefix(i_prefix)); 123 } 124 125 public void endPrefixMapping(String i_prefix) throws SAXException { 126 pop(new EndPrefix(i_prefix)); 127 } 128 129 public void startElement(String i_namespaceURI, String i_localName, String i_qName, Attributes i_atts) throws SAXException { 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 i_namespaceURI, String i_localName, String i_qName) throws SAXException { 135 pop(new EndElement(i_namespaceURI, i_localName, i_qName)); 136 } 137 138 public void startDTD(String s, String s1, String s2) throws SAXException { 139 super.startDTD(s, s1, s2); 140 push(new EndDTD()); 141 } 142 143 public void endDTD() throws SAXException { 144 pop(new EndDTD()); 145 } 146 147 public void startEntity(String s) throws SAXException { 148 super.startEntity(s); 149 push(new EndEntity(s)); 150 } 151 152 public void endEntity(String s) throws SAXException { 153 pop(new EndEntity(s)); 154 } 155 156 public void startCDATA() throws SAXException { 157 super.startCDATA(); 158 push(new EndCDATA()); 159 } 160 161 public void endCDATA() throws SAXException { 162 pop(new EndCDATA()); 163 } 164 165 168 private abstract class SaxEvent implements Serializable { 169 170 174 public abstract void fire() 175 throws SAXException ; 176 177 public int hashCode() { 178 return this.getClass().hashCode(); 179 } 180 181 public boolean equals(Object obj) { 182 return this.getClass().equals(obj.getClass()); 183 } 184 } 185 186 private abstract class EndEntityOrPrefix extends SaxEvent { 187 protected final String m_string; 188 189 public EndEntityOrPrefix(String i_string) { 190 m_string = i_string; 191 } 192 193 public boolean equals(Object 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 i_string) { 214 super(i_string); 215 } 216 217 220 public void fire() throws SAXException { 221 WellFormedContentHandler.super.endPrefixMapping(m_string); 222 } 223 } 224 225 private class EndElement extends SaxEvent { 226 private final String m_nsURI; 227 private final String m_local; 228 private final String m_qName; 229 230 public EndElement(String i_nsURI, String i_local, String i_qName) { 231 m_nsURI = i_nsURI; 232 m_local = i_local; 233 m_qName = i_qName; 234 } 235 236 240 public void fire() throws SAXException { 241 WellFormedContentHandler.super.endElement(m_nsURI, m_local, m_qName); 242 } 243 244 public boolean equals(Object 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 271 public void fire() throws SAXException { 272 WellFormedContentHandler.super.endDTD(); 273 } 274 } 275 276 private class EndEntity extends EndEntityOrPrefix { 277 public EndEntity(String i_string) { 278 super(i_string); 279 } 280 281 284 public void fire() throws SAXException { 285 WellFormedContentHandler.super.endEntity(m_string); 286 } 287 } 288 289 private class EndCDATA extends SaxEvent { 290 293 public void fire() throws SAXException { 294 WellFormedContentHandler.super.endCDATA(); 295 } 296 } 297 } 298 | Popular Tags |