1 16 19 20 package org.apache.xml.dtm.ref; 21 22 import java.io.IOException ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.Method ; 25 26 import org.apache.xerces.parsers.SAXParser; 27 import org.apache.xml.res.XMLErrorResources; 28 import org.apache.xml.res.XMLMessages; 29 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.XMLReader ; 33 34 35 43 public class IncrementalSAXSource_Xerces 44 implements IncrementalSAXSource 45 { 46 Method fParseSomeSetup=null; Method fParseSome=null; Object fPullParserConfig=null; Method fConfigSetInput=null; Method fConfigParse=null; Method fSetInputSource=null; Constructor fConfigInputSourceCtor=null; Method fConfigSetByteStream=null; Method fConfigSetCharStream=null; Method fConfigSetEncoding=null; Method fReset=null; 64 SAXParser fIncrementalParser; 68 private boolean fParseInProgress=false; 69 70 74 80 public IncrementalSAXSource_Xerces() 81 throws NoSuchMethodException 82 { 83 try 84 { 85 97 Class xniConfigClass=ObjectFactory.findProviderClass( 99 "org.apache.xerces.xni.parser.XMLParserConfiguration", 100 ObjectFactory.findClassLoader(), true); 101 Class [] args1={xniConfigClass}; 102 Constructor ctor=SAXParser.class.getConstructor(args1); 103 104 Class xniStdConfigClass=ObjectFactory.findProviderClass( 108 "org.apache.xerces.parsers.StandardParserConfiguration", 109 ObjectFactory.findClassLoader(), true); 110 fPullParserConfig=xniStdConfigClass.newInstance(); 111 Object [] args2={fPullParserConfig}; 112 fIncrementalParser = (SAXParser)ctor.newInstance(args2); 113 114 Class fXniInputSourceClass=ObjectFactory.findProviderClass( 118 "org.apache.xerces.xni.parser.XMLInputSource", 119 ObjectFactory.findClassLoader(), true); 120 Class [] args3={fXniInputSourceClass}; 121 fConfigSetInput=xniStdConfigClass.getMethod("setInputSource",args3); 122 123 Class [] args4={String .class,String .class,String .class}; 124 fConfigInputSourceCtor=fXniInputSourceClass.getConstructor(args4); 125 Class [] args5={java.io.InputStream .class}; 126 fConfigSetByteStream=fXniInputSourceClass.getMethod("setByteStream",args5); 127 Class [] args6={java.io.Reader .class}; 128 fConfigSetCharStream=fXniInputSourceClass.getMethod("setCharacterStream",args6); 129 Class [] args7={String .class}; 130 fConfigSetEncoding=fXniInputSourceClass.getMethod("setEncoding",args7); 131 132 Class [] argsb={Boolean.TYPE}; 133 fConfigParse=xniStdConfigClass.getMethod("parse",argsb); 134 Class [] noargs=new Class [0]; 135 fReset=fIncrementalParser.getClass().getMethod("reset",noargs); 136 } 137 catch(Exception e) 138 { 139 IncrementalSAXSource_Xerces dummy=new IncrementalSAXSource_Xerces(new SAXParser()); 144 this.fParseSomeSetup=dummy.fParseSomeSetup; 145 this.fParseSome=dummy.fParseSome; 146 this.fIncrementalParser=dummy.fIncrementalParser; 147 } 148 } 149 150 161 public IncrementalSAXSource_Xerces(SAXParser parser) 162 throws NoSuchMethodException 163 { 164 fIncrementalParser=parser; 170 Class me=parser.getClass(); 171 Class [] parms={InputSource .class}; 172 fParseSomeSetup=me.getMethod("parseSomeSetup",parms); 173 parms=new Class [0]; 174 fParseSome=me.getMethod("parseSome",parms); 175 } 178 179 static public IncrementalSAXSource createIncrementalSAXSource() 183 { 184 try 185 { 186 return new IncrementalSAXSource_Xerces(); 187 } 188 catch(NoSuchMethodException e) 189 { 190 IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter(); 193 iss.setXMLReader(new SAXParser()); 194 return iss; 195 } 196 } 197 198 static public IncrementalSAXSource 199 createIncrementalSAXSource(SAXParser parser) { 200 try 201 { 202 return new IncrementalSAXSource_Xerces(parser); 203 } 204 catch(NoSuchMethodException e) 205 { 206 IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter(); 209 iss.setXMLReader(parser); 210 return iss; 211 } 212 } 213 214 218 public void setContentHandler(org.xml.sax.ContentHandler handler) 220 { 221 ((XMLReader )fIncrementalParser).setContentHandler(handler); 224 } 225 226 public void setLexicalHandler(org.xml.sax.ext.LexicalHandler handler) 228 { 229 try 231 { 232 ((XMLReader )fIncrementalParser).setProperty("http://xml.org/sax/properties/lexical-handler", 235 handler); 236 } 237 catch(org.xml.sax.SAXNotRecognizedException e) 238 { 239 } 241 catch(org.xml.sax.SAXNotSupportedException e) 242 { 243 } 245 } 246 247 public void setDTDHandler(org.xml.sax.DTDHandler handler) 249 { 250 ((XMLReader )fIncrementalParser).setDTDHandler(handler); 253 } 254 255 262 public void startParse(InputSource source) throws SAXException 263 { 264 if (fIncrementalParser==null) 265 throw new SAXException (XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_NEEDS_SAXPARSER, null)); if (fParseInProgress) 267 throw new SAXException (XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_WHILE_PARSING, null)); 269 boolean ok=false; 270 271 try 272 { 273 ok = parseSomeSetup(source); 274 } 275 catch(Exception ex) 276 { 277 throw new SAXException (ex); 278 } 279 280 if(!ok) 281 throw new SAXException (XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_INIT_PARSER, null)); } 283 284 285 297 public Object deliverMoreNodes (boolean parsemore) 298 { 299 if(!parsemore) 300 { 301 fParseInProgress=false; 302 return Boolean.FALSE; 303 } 304 305 Object arg; 306 try { 307 boolean keepgoing = parseSome(); 308 arg = keepgoing ? Boolean.TRUE : Boolean.FALSE; 309 } catch (SAXException ex) { 310 arg = ex; 311 } catch (IOException ex) { 312 arg = ex; 313 } catch (Exception ex) { 314 arg = new SAXException (ex); 315 } 316 return arg; 317 } 318 319 private boolean parseSomeSetup(InputSource source) 321 throws SAXException , IOException , IllegalAccessException , 322 java.lang.reflect.InvocationTargetException , 323 java.lang.InstantiationException 324 { 325 if(fConfigSetInput!=null) 326 { 327 Object [] parms1={source.getPublicId(),source.getSystemId(),null}; 330 Object xmlsource=fConfigInputSourceCtor.newInstance(parms1); 331 Object [] parmsa={source.getByteStream()}; 332 fConfigSetByteStream.invoke(xmlsource,parmsa); 333 parmsa[0]=source.getCharacterStream(); 334 fConfigSetCharStream.invoke(xmlsource,parmsa); 335 parmsa[0]=source.getEncoding(); 336 fConfigSetEncoding.invoke(xmlsource,parmsa); 337 338 Object [] noparms=new Object [0]; 343 fReset.invoke(fIncrementalParser,noparms); 344 345 parmsa[0]=xmlsource; 346 fConfigSetInput.invoke(fPullParserConfig,parmsa); 347 348 return parseSome(); 350 } 351 else 352 { 353 Object [] parm={source}; 354 Object ret=fParseSomeSetup.invoke(fIncrementalParser,parm); 355 return ((Boolean )ret).booleanValue(); 356 } 357 } 358 359 static final Object [] noparms=new Object [0]; static final Object [] parmsfalse={Boolean.FALSE}; 361 private boolean parseSome() 362 throws SAXException , IOException , IllegalAccessException , 363 java.lang.reflect.InvocationTargetException 364 { 365 if(fConfigSetInput!=null) 367 { 368 Object ret=(Boolean )(fConfigParse.invoke(fPullParserConfig,parmsfalse)); 369 return ((Boolean )ret).booleanValue(); 370 } 371 else 372 { 373 Object ret=fParseSome.invoke(fIncrementalParser,noparms); 374 return ((Boolean )ret).booleanValue(); 375 } 376 } 377 378 379 383 public static void main(String args[]) 384 { 385 System.out.println("Starting..."); 386 387 CoroutineManager co = new CoroutineManager(); 388 int appCoroutineID = co.co_joinCoroutineSet(-1); 389 if (appCoroutineID == -1) 390 { 391 System.out.println("ERROR: Couldn't allocate coroutine number.\n"); 392 return; 393 } 394 IncrementalSAXSource parser= 395 createIncrementalSAXSource(); 396 397 org.apache.xml.serialize.XMLSerializer trace; 399 trace=new org.apache.xml.serialize.XMLSerializer(System.out,null); 400 parser.setContentHandler(trace); 401 parser.setLexicalHandler(trace); 402 403 405 for(int arg=0;arg<args.length;++arg) 406 { 407 try 408 { 409 InputSource source = new InputSource (args[arg]); 410 Object result=null; 411 boolean more=true; 412 parser.startParse(source); 413 for(result = parser.deliverMoreNodes(more); 414 result==Boolean.TRUE; 415 result = parser.deliverMoreNodes(more)) 416 { 417 System.out.println("\nSome parsing successful, trying more.\n"); 418 419 if(arg+1<args.length && "!".equals(args[arg+1])) 421 { 422 ++arg; 423 more=false; 424 } 425 426 } 427 428 if (result instanceof Boolean && ((Boolean )result)==Boolean.FALSE) 429 { 430 System.out.println("\nParser ended (EOF or on request).\n"); 431 } 432 else if (result == null) { 433 System.out.println("\nUNEXPECTED: Parser says shut down prematurely.\n"); 434 } 435 else if (result instanceof Exception ) { 436 throw new org.apache.xml.utils.WrappedRuntimeException((Exception )result); 437 } 440 441 } 442 443 catch(SAXException e) 444 { 445 e.printStackTrace(); 446 } 447 } 448 449 } 450 451 452 } | Popular Tags |