1 17 package org.apache.ws.jaxme.xs.parser.impl; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.UndeclaredThrowableException ; 22 23 import org.apache.ws.jaxme.xs.parser.AttributeSetter; 24 import org.apache.ws.jaxme.xs.parser.ChildSetter; 25 import org.apache.ws.jaxme.xs.parser.XSContext; 26 import org.apache.ws.jaxme.xs.parser.XsSAXParser; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.Locator ; 30 import org.xml.sax.SAXException ; 31 32 33 36 public abstract class AbstractXsSAXParser implements ContentHandler , XsSAXParser { 37 private final static Class [] ZERO_CLASSES = new Class [0]; 38 private final static Object [] ZERO_OBJECTS = new Object [0]; 39 40 private Object bean; 41 private int level; 42 private String qName, namespaceURI, localName; 43 private ContentHandler childHandler; 44 45 protected abstract XSContext getData(); 46 47 public AbstractXsSAXParser(Object pBean) { 48 bean = pBean; 49 } 50 51 public Object getBean() { 52 return bean; 53 } 54 55 public void setQName(String pQName) { 56 qName = pQName; 57 } 58 59 public void setNamespaceURI(String pNamespaceURI) { 60 namespaceURI = pNamespaceURI; 61 } 62 63 public String getNamespaceURI() { 64 return namespaceURI; 65 } 66 67 public void setLocalName(String pLocalName) { 68 localName = pLocalName; 69 } 70 71 public String getLocalName() { 72 return localName; 73 } 74 75 public String getQName() { 76 return qName; 77 } 78 79 public ContentHandler getChildHandler() { 80 return childHandler; 81 } 82 83 public void startPrefixMapping(String pPrefix, String pURI) 84 throws SAXException { 85 if (childHandler == null) { 86 getData().getNamespaceSupport().declarePrefix(pPrefix, pURI); 87 } else { 88 childHandler.startPrefixMapping(pPrefix, pURI); 89 } 90 } 91 92 public void endPrefixMapping(String pPrefix) throws SAXException { 93 if (childHandler != null) { 94 childHandler.endPrefixMapping(pPrefix); 95 } 96 } 97 98 public void startDocument() throws SAXException { 99 getData().getNamespaceSupport().pushContext(); 100 } 101 102 public void endDocument() throws SAXException { 103 getData().getNamespaceSupport().popContext(); 104 } 105 106 public void characters(char[] pBuffer, int pOffset, int pLen) throws SAXException { 107 if (childHandler == null) { 108 try { 109 getData().getTextSetter().addText(new String (pBuffer, pOffset, pLen)); 110 } catch (SAXException e) { 111 throw e; 112 } catch (RuntimeException e) { 113 Exception ex = e; 114 for (;;) { 115 UndeclaredThrowableException te = null; 116 Throwable t; 117 if (ex instanceof UndeclaredThrowableException ) { 118 te = ((UndeclaredThrowableException ) ex); 119 t = te.getUndeclaredThrowable(); 120 } else if (ex instanceof InvocationTargetException ) { 121 t = ((InvocationTargetException ) ex).getTargetException(); 122 } else { 123 break; 124 } 125 if (t instanceof Exception ) { 126 ex = (Exception ) t; 127 } else { 128 if (te == null) { 129 te = new UndeclaredThrowableException (t); 130 } 131 t.printStackTrace(); 132 throw te; 133 } 134 } 135 throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(), 136 getData().getLocator(), ex); 137 } 138 } else { 139 childHandler.characters(pBuffer, pOffset, pLen); 140 } 141 } 142 143 public void ignorableWhitespace(char[] pBuffer, int pOffset, int pLen) 144 throws SAXException { 145 if (childHandler == null) { 146 characters(pBuffer, pOffset, pLen); 147 } else { 148 childHandler.ignorableWhitespace(pBuffer, pOffset, pLen); 149 } 150 } 151 152 public void skippedEntity(String pEntity) throws SAXException { 153 if (childHandler == null) { 154 throw new LocSAXException("Unable to skip entities: " + pEntity, 155 getData().getLocator()); 156 } else { 157 skippedEntity(pEntity); 158 } 159 } 160 161 public void processingInstruction(String pTarget, String pData) 162 throws SAXException { 163 if (childHandler != null) { 164 childHandler.processingInstruction(pTarget, pData); 165 } 166 } 167 168 public void startElement(String pNamespaceURI, String pLocalName, String pQName, Attributes pAttr) 169 throws SAXException { 170 switch (++level) { 171 case 1: 172 setQName(pQName); 173 setNamespaceURI(pNamespaceURI); 174 setLocalName(pLocalName); 175 if (pAttr != null) { 176 for (int i = 0; i < pAttr.getLength(); i++) { 177 try { 178 AttributeSetter attrSetter = getData().getAttributeSetter(); 179 attrSetter.setAttribute(pAttr.getQName(i), pAttr.getURI(i), 180 pAttr.getLocalName(i), pAttr.getValue(i)); 181 } catch (SAXException e) { 182 throw e; 183 } catch (RuntimeException e) { 184 Exception ex = e; 185 for (;;) { 186 UndeclaredThrowableException te = null; 187 Throwable t; 188 if (ex instanceof UndeclaredThrowableException ) { 189 te = ((UndeclaredThrowableException ) ex); 190 t = te.getUndeclaredThrowable(); 191 } else if (ex instanceof InvocationTargetException ) { 192 t = ((InvocationTargetException ) ex).getTargetException(); 193 } else { 194 break; 195 } 196 if (t instanceof Exception ) { 197 ex = (Exception ) t; 198 } else { 199 if (te == null) { 200 te = new UndeclaredThrowableException (t); 201 } 202 t.printStackTrace(); 203 throw te; 204 } 205 } 206 throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(), 207 getData().getLocator(), ex); 208 } 209 } 210 } 211 break; 212 case 2: 213 try { 214 ChildSetter childSetter = getData().getChildSetter(); 215 childHandler = childSetter.getChildHandler(pQName, pNamespaceURI, pLocalName); 216 } catch (SAXException e) { 217 throw e; 218 } catch (RuntimeException e) { 219 Exception ex = e; 220 for (;;) { 221 UndeclaredThrowableException te = null; 222 Throwable t; 223 if (ex instanceof UndeclaredThrowableException ) { 224 te = ((UndeclaredThrowableException ) ex); 225 t = te.getUndeclaredThrowable(); 226 } else if (ex instanceof InvocationTargetException ) { 227 t = ((InvocationTargetException ) ex).getTargetException(); 228 } else { 229 break; 230 } 231 if (t instanceof Exception ) { 232 ex = (Exception ) t; 233 } else { 234 if (te == null) { 235 te = new UndeclaredThrowableException (t); 236 } 237 t.printStackTrace(); 238 throw te; 239 } 240 } 241 throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(), 242 getData().getLocator(), ex); 243 } 244 getData().setCurrentContentHandler(childHandler); 245 childHandler.startDocument(); 246 childHandler.startElement(pNamespaceURI, pLocalName, pQName, pAttr); 247 break; 248 default: 249 childHandler.startElement(pNamespaceURI, pLocalName, pQName, pAttr); 250 break; 251 } 252 } 253 254 public void endElement(String pNamespaceURI, String pLocalName, String pQName) 255 throws SAXException { 256 switch (level--) { 257 case 1: 258 Object o = getBean(); 259 if (o != null) { 260 Method m = null; 261 try { 262 m = o.getClass().getMethod("validate", ZERO_CLASSES); 263 } catch (NoSuchMethodException e) { 264 } 265 if (m != null) { 266 try { 267 m.invoke(o, ZERO_OBJECTS); 268 } catch (RuntimeException e) { 269 throw new LocSAXException(e.getClass().getName() + ": " + e.getMessage(), 270 getData().getLocator(), e); 271 } catch (InvocationTargetException e) { 272 Throwable t = e.getTargetException(); 273 if (t instanceof SAXException ) { 274 throw (SAXException ) t; 275 } else if (t instanceof RuntimeException ) { 276 throw new LocSAXException(t.getClass().getName() + ": " + t.getMessage(), 277 getData().getLocator(), 278 (RuntimeException ) t); 279 } else if (t instanceof Exception ) { 280 throw new LocSAXException("Failed to invoke method validate() " + 281 " of class " + m.getDeclaringClass() + 282 " with argument " + o.getClass().getName() + ": " + 283 t.getClass().getName() + ", " + t.getMessage(), 284 getData().getLocator(), 285 (Exception ) t); 286 } else { 287 throw new LocSAXException("Failed to invoke method validate() " + 288 " of class " + m.getDeclaringClass() + 289 " with argument " + o.getClass().getName() + ": " + 290 t.getClass().getName() + ", " + t.getMessage(), 291 getData().getLocator(), e); 292 } 293 } catch (IllegalAccessException e) { 294 throw new LocSAXException("Failed to invoke method validate() " + 295 " of class " + m.getDeclaringClass() + 296 " with argument " + o.getClass().getName() + ": IllegalAccessException, " + 297 e.getMessage(), 298 getData().getLocator(), e); 299 300 } 301 } 302 } 303 break; 304 case 2: 305 childHandler.endElement(pNamespaceURI, pLocalName, pQName); 306 childHandler.endDocument(); 307 getData().setCurrentContentHandler(this); 308 childHandler = null; 309 break; 310 default: 311 childHandler.endElement(pNamespaceURI, pLocalName, pQName); 312 break; 313 } 314 } 315 316 public void setDocumentLocator(Locator pLocator) { 317 getData().setLocator(pLocator); 318 if (childHandler != null) { 319 childHandler.setDocumentLocator(pLocator); 320 } 321 } 322 } 323 | Popular Tags |