286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: IncrementalSAXSource_Xerces.java,v 1.2.4.1 2005/09/15 08:15:08 suresh_emailid Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xml.internal.dtm.ref;
286N/A
286N/Aimport java.io.IOException;
286N/Aimport java.lang.reflect.Constructor;
286N/Aimport java.lang.reflect.Method;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.parsers.SAXParser;
286N/Aimport com.sun.org.apache.xml.internal.res.XMLErrorResources;
286N/Aimport com.sun.org.apache.xml.internal.res.XMLMessages;
286N/Aimport com.sun.org.apache.xalan.internal.utils.ObjectFactory;
286N/A
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.SAXException;
286N/Aimport org.xml.sax.XMLReader;
286N/A
286N/A
286N/A/** <p>IncrementalSAXSource_Xerces takes advantage of the fact that Xerces1
286N/A * incremental mode is already a coroutine of sorts, and just wraps our
286N/A * IncrementalSAXSource API around it.</p>
286N/A *
286N/A * <p>Usage example: See main().</p>
286N/A *
286N/A * <p>Status: Passes simple main() unit-test. NEEDS JAVADOC.</p>
286N/A * */
286N/Apublic class IncrementalSAXSource_Xerces
286N/A implements IncrementalSAXSource
286N/A{
286N/A //
286N/A // Reflection. To allow this to compile with both Xerces1 and Xerces2, which
286N/A // require very different methods and objects, we need to avoid static
286N/A // references to those APIs. So until Xerces2 is pervasive and we're willing
286N/A // to make it a prerequisite, we will rely upon relection.
286N/A //
286N/A Method fParseSomeSetup=null; // Xerces1 method
286N/A Method fParseSome=null; // Xerces1 method
286N/A Object fPullParserConfig=null; // Xerces2 pull control object
286N/A Method fConfigSetInput=null; // Xerces2 method
286N/A Method fConfigParse=null; // Xerces2 method
286N/A Method fSetInputSource=null; // Xerces2 pull control method
286N/A Constructor fConfigInputSourceCtor=null; // Xerces2 initialization method
286N/A Method fConfigSetByteStream=null; // Xerces2 initialization method
286N/A Method fConfigSetCharStream=null; // Xerces2 initialization method
286N/A Method fConfigSetEncoding=null; // Xerces2 initialization method
286N/A Method fReset=null; // Both Xerces1 and Xerces2, but diff. signatures
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A SAXParser fIncrementalParser;
286N/A private boolean fParseInProgress=false;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Create a IncrementalSAXSource_Xerces, and create a SAXParser
286N/A * to go with it. Xerces2 incremental parsing is only supported if
286N/A * this constructor is used, due to limitations in the Xerces2 API (as of
286N/A * Beta 3). If you don't like that restriction, tell the Xerces folks that
286N/A * there should be a simpler way to request incremental SAX parsing.
286N/A * */
286N/A public IncrementalSAXSource_Xerces()
286N/A throws NoSuchMethodException
286N/A {
286N/A try
286N/A {
286N/A // Xerces-2 incremental parsing support (as of Beta 3)
286N/A // ContentHandlers still get set on fIncrementalParser (to get
286N/A // conversion from XNI events to SAX events), but
286N/A // _control_ for incremental parsing must be exercised via the config.
286N/A //
286N/A // At this time there's no way to read the existing config, only
286N/A // to assert a new one... and only when creating a brand-new parser.
286N/A //
286N/A // Reflection is used to allow us to continue to compile against
286N/A // Xerces1. If/when we can abandon the older versions of the parser,
286N/A // this will simplify significantly.
286N/A
286N/A // If we can't get the magic constructor, no need to look further.
286N/A Class xniConfigClass=ObjectFactory.findProviderClass(
286N/A "com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration",
286N/A true);
286N/A Class[] args1={xniConfigClass};
286N/A Constructor ctor=SAXParser.class.getConstructor(args1);
286N/A
286N/A // Build the parser configuration object. StandardParserConfiguration
286N/A // happens to implement XMLPullParserConfiguration, which is the API
286N/A // we're going to want to use.
286N/A Class xniStdConfigClass=ObjectFactory.findProviderClass(
286N/A "com.sun.org.apache.xerces.internal.parsers.StandardParserConfiguration",
286N/A true);
286N/A fPullParserConfig=xniStdConfigClass.newInstance();
286N/A Object[] args2={fPullParserConfig};
286N/A fIncrementalParser = (SAXParser)ctor.newInstance(args2);
286N/A
286N/A // Preload all the needed the configuration methods... I want to know they're
286N/A // all here before we commit to trying to use them, just in case the
286N/A // API changes again.
286N/A Class fXniInputSourceClass=ObjectFactory.findProviderClass(
286N/A "com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource",
286N/A true);
286N/A Class[] args3={fXniInputSourceClass};
286N/A fConfigSetInput=xniStdConfigClass.getMethod("setInputSource",args3);
286N/A
286N/A Class[] args4={String.class,String.class,String.class};
286N/A fConfigInputSourceCtor=fXniInputSourceClass.getConstructor(args4);
286N/A Class[] args5={java.io.InputStream.class};
286N/A fConfigSetByteStream=fXniInputSourceClass.getMethod("setByteStream",args5);
286N/A Class[] args6={java.io.Reader.class};
286N/A fConfigSetCharStream=fXniInputSourceClass.getMethod("setCharacterStream",args6);
286N/A Class[] args7={String.class};
286N/A fConfigSetEncoding=fXniInputSourceClass.getMethod("setEncoding",args7);
286N/A
286N/A Class[] argsb={Boolean.TYPE};
286N/A fConfigParse=xniStdConfigClass.getMethod("parse",argsb);
286N/A Class[] noargs=new Class[0];
286N/A fReset=fIncrementalParser.getClass().getMethod("reset",noargs);
286N/A }
286N/A catch(Exception e)
286N/A {
286N/A // Fallback if this fails (implemented in createIncrementalSAXSource) is
286N/A // to attempt Xerces-1 incremental setup. Can't do tail-call in
286N/A // constructor, so create new, copy Xerces-1 initialization,
286N/A // then throw it away... Ugh.
286N/A IncrementalSAXSource_Xerces dummy=new IncrementalSAXSource_Xerces(new SAXParser());
286N/A this.fParseSomeSetup=dummy.fParseSomeSetup;
286N/A this.fParseSome=dummy.fParseSome;
286N/A this.fIncrementalParser=dummy.fIncrementalParser;
286N/A }
286N/A }
286N/A
286N/A /** Create a IncrementalSAXSource_Xerces wrapped around
286N/A * an existing SAXParser. Currently this works only for recent
286N/A * releases of Xerces-1. Xerces-2 incremental is currently possible
286N/A * only if we are allowed to create the parser instance, due to
286N/A * limitations in the API exposed by Xerces-2 Beta 3; see the
286N/A * no-args constructor for that code.
286N/A *
286N/A * @exception if the SAXParser class doesn't support the Xerces
286N/A * incremental parse operations. In that case, caller should
286N/A * fall back upon the IncrementalSAXSource_Filter approach.
286N/A * */
286N/A public IncrementalSAXSource_Xerces(SAXParser parser)
286N/A throws NoSuchMethodException
286N/A {
286N/A // Reflection is used to allow us to compile against
286N/A // Xerces2. If/when we can abandon the older versions of the parser,
286N/A // this constructor will simply have to fail until/unless the
286N/A // Xerces2 incremental support is made available on previously
286N/A // constructed SAXParser instances.
286N/A fIncrementalParser=parser;
286N/A Class me=parser.getClass();
286N/A Class[] parms={InputSource.class};
286N/A fParseSomeSetup=me.getMethod("parseSomeSetup",parms);
286N/A parms=new Class[0];
286N/A fParseSome=me.getMethod("parseSome",parms);
286N/A // Fallback if this fails (implemented in createIncrementalSAXSource) is
286N/A // to use IncrementalSAXSource_Filter rather than Xerces-specific code.
286N/A }
286N/A
286N/A //
286N/A // Factories
286N/A //
286N/A static public IncrementalSAXSource createIncrementalSAXSource()
286N/A {
286N/A try
286N/A {
286N/A return new IncrementalSAXSource_Xerces();
286N/A }
286N/A catch(NoSuchMethodException e)
286N/A {
286N/A // Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded.
286N/A // Fall back on filtering solution.
286N/A IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter();
286N/A iss.setXMLReader(new SAXParser());
286N/A return iss;
286N/A }
286N/A }
286N/A
286N/A static public IncrementalSAXSource
286N/A createIncrementalSAXSource(SAXParser parser) {
286N/A try
286N/A {
286N/A return new IncrementalSAXSource_Xerces(parser);
286N/A }
286N/A catch(NoSuchMethodException e)
286N/A {
286N/A // Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded.
286N/A // Fall back on filtering solution.
286N/A IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter();
286N/A iss.setXMLReader(parser);
286N/A return iss;
286N/A }
286N/A }
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A // Register handler directly with the incremental parser
286N/A public void setContentHandler(org.xml.sax.ContentHandler handler)
286N/A {
286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
286N/A // %OPT% Cast at asignment?
286N/A ((XMLReader)fIncrementalParser).setContentHandler(handler);
286N/A }
286N/A
286N/A // Register handler directly with the incremental parser
286N/A public void setLexicalHandler(org.xml.sax.ext.LexicalHandler handler)
286N/A {
286N/A // Not supported by all SAX2 parsers but should work in Xerces:
286N/A try
286N/A {
286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
286N/A // %OPT% Cast at asignment?
286N/A ((XMLReader)fIncrementalParser).setProperty("http://xml.org/sax/properties/lexical-handler",
286N/A handler);
286N/A }
286N/A catch(org.xml.sax.SAXNotRecognizedException e)
286N/A {
286N/A // Nothing we can do about it
286N/A }
286N/A catch(org.xml.sax.SAXNotSupportedException e)
286N/A {
286N/A // Nothing we can do about it
286N/A }
286N/A }
286N/A
286N/A // Register handler directly with the incremental parser
286N/A public void setDTDHandler(org.xml.sax.DTDHandler handler)
286N/A {
286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
286N/A // %OPT% Cast at asignment?
286N/A ((XMLReader)fIncrementalParser).setDTDHandler(handler);
286N/A }
286N/A
286N/A //================================================================
286N/A /** startParse() is a simple API which tells the IncrementalSAXSource
286N/A * to begin reading a document.
286N/A *
286N/A * @throws SAXException is parse thread is already in progress
286N/A * or parsing can not be started.
286N/A * */
286N/A public void startParse(InputSource source) throws SAXException
286N/A {
286N/A if (fIncrementalParser==null)
286N/A throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_NEEDS_SAXPARSER, null)); //"startParse needs a non-null SAXParser.");
286N/A if (fParseInProgress)
286N/A throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_WHILE_PARSING, null)); //"startParse may not be called while parsing.");
286N/A
286N/A boolean ok=false;
286N/A
286N/A try
286N/A {
286N/A ok = parseSomeSetup(source);
286N/A }
286N/A catch(Exception ex)
286N/A {
286N/A throw new SAXException(ex);
286N/A }
286N/A
286N/A if(!ok)
286N/A throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_INIT_PARSER, null)); //"could not initialize parser with");
286N/A }
286N/A
286N/A
286N/A /** deliverMoreNodes() is a simple API which tells the coroutine
286N/A * parser that we need more nodes. This is intended to be called
286N/A * from one of our partner routines, and serves to encapsulate the
286N/A * details of how incremental parsing has been achieved.
286N/A *
286N/A * @param parsemore If true, tells the incremental parser to generate
286N/A * another chunk of output. If false, tells the parser that we're
286N/A * satisfied and it can terminate parsing of this document.
286N/A * @return Boolean.TRUE if the CoroutineParser believes more data may be available
286N/A * for further parsing. Boolean.FALSE if parsing ran to completion.
286N/A * Exception if the parser objected for some reason.
286N/A * */
286N/A public Object deliverMoreNodes (boolean parsemore)
286N/A {
286N/A if(!parsemore)
286N/A {
286N/A fParseInProgress=false;
286N/A return Boolean.FALSE;
286N/A }
286N/A
286N/A Object arg;
286N/A try {
286N/A boolean keepgoing = parseSome();
286N/A arg = keepgoing ? Boolean.TRUE : Boolean.FALSE;
286N/A } catch (SAXException ex) {
286N/A arg = ex;
286N/A } catch (IOException ex) {
286N/A arg = ex;
286N/A } catch (Exception ex) {
286N/A arg = new SAXException(ex);
286N/A }
286N/A return arg;
286N/A }
286N/A
286N/A // Private methods -- conveniences to hide the reflection details
286N/A private boolean parseSomeSetup(InputSource source)
286N/A throws SAXException, IOException, IllegalAccessException,
286N/A java.lang.reflect.InvocationTargetException,
286N/A java.lang.InstantiationException
286N/A {
286N/A if(fConfigSetInput!=null)
286N/A {
286N/A // Obtain input from SAX inputSource object, construct XNI version of
286N/A // that object. Logic adapted from Xerces2.
286N/A Object[] parms1={source.getPublicId(),source.getSystemId(),null};
286N/A Object xmlsource=fConfigInputSourceCtor.newInstance(parms1);
286N/A Object[] parmsa={source.getByteStream()};
286N/A fConfigSetByteStream.invoke(xmlsource,parmsa);
286N/A parmsa[0]=source.getCharacterStream();
286N/A fConfigSetCharStream.invoke(xmlsource,parmsa);
286N/A parmsa[0]=source.getEncoding();
286N/A fConfigSetEncoding.invoke(xmlsource,parmsa);
286N/A
286N/A // Bugzilla5272 patch suggested by Sandy Gao.
286N/A // Has to be reflection to run with Xerces2
286N/A // after compilation against Xerces1. or vice
286N/A // versa, due to return type mismatches.
286N/A Object[] noparms=new Object[0];
286N/A fReset.invoke(fIncrementalParser,noparms);
286N/A
286N/A parmsa[0]=xmlsource;
286N/A fConfigSetInput.invoke(fPullParserConfig,parmsa);
286N/A
286N/A // %REVIEW% Do first pull. Should we instead just return true?
286N/A return parseSome();
286N/A }
286N/A else
286N/A {
286N/A Object[] parm={source};
286N/A Object ret=fParseSomeSetup.invoke(fIncrementalParser,parm);
286N/A return ((Boolean)ret).booleanValue();
286N/A }
286N/A }
286N/A// Would null work???
286N/A private static final Object[] noparms=new Object[0];
286N/A private static final Object[] parmsfalse={Boolean.FALSE};
286N/A private boolean parseSome()
286N/A throws SAXException, IOException, IllegalAccessException,
286N/A java.lang.reflect.InvocationTargetException
286N/A {
286N/A // Take next parsing step, return false iff parsing complete:
286N/A if(fConfigSetInput!=null)
286N/A {
286N/A Object ret=(Boolean)(fConfigParse.invoke(fPullParserConfig,parmsfalse));
286N/A return ((Boolean)ret).booleanValue();
286N/A }
286N/A else
286N/A {
286N/A Object ret=fParseSome.invoke(fIncrementalParser,noparms);
286N/A return ((Boolean)ret).booleanValue();
286N/A }
286N/A }
286N/A
286N/A
286N/A //================================================================
286N/A /** Simple unit test. Attempt coroutine parsing of document indicated
286N/A * by first argument (as a URI), report progress.
286N/A */
286N/A public static void _main(String args[])
286N/A {
286N/A System.out.println("Starting...");
286N/A
286N/A CoroutineManager co = new CoroutineManager();
286N/A int appCoroutineID = co.co_joinCoroutineSet(-1);
286N/A if (appCoroutineID == -1)
286N/A {
286N/A System.out.println("ERROR: Couldn't allocate coroutine number.\n");
286N/A return;
286N/A }
286N/A IncrementalSAXSource parser=
286N/A createIncrementalSAXSource();
286N/A
286N/A // Use a serializer as our sample output
286N/A com.sun.org.apache.xml.internal.serialize.XMLSerializer trace;
286N/A trace=new com.sun.org.apache.xml.internal.serialize.XMLSerializer(System.out,null);
286N/A parser.setContentHandler(trace);
286N/A parser.setLexicalHandler(trace);
286N/A
286N/A // Tell coroutine to begin parsing, run while parsing is in progress
286N/A
286N/A for(int arg=0;arg<args.length;++arg)
286N/A {
286N/A try
286N/A {
286N/A InputSource source = new InputSource(args[arg]);
286N/A Object result=null;
286N/A boolean more=true;
286N/A parser.startParse(source);
286N/A for(result = parser.deliverMoreNodes(more);
286N/A result==Boolean.TRUE;
286N/A result = parser.deliverMoreNodes(more))
286N/A {
286N/A System.out.println("\nSome parsing successful, trying more.\n");
286N/A
286N/A // Special test: Terminate parsing early.
286N/A if(arg+1<args.length && "!".equals(args[arg+1]))
286N/A {
286N/A ++arg;
286N/A more=false;
286N/A }
286N/A
286N/A }
286N/A
286N/A if (result instanceof Boolean && ((Boolean)result)==Boolean.FALSE)
286N/A {
286N/A System.out.println("\nParser ended (EOF or on request).\n");
286N/A }
286N/A else if (result == null) {
286N/A System.out.println("\nUNEXPECTED: Parser says shut down prematurely.\n");
286N/A }
286N/A else if (result instanceof Exception) {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException((Exception)result);
286N/A // System.out.println("\nParser threw exception:");
286N/A // ((Exception)result).printStackTrace();
286N/A }
286N/A
286N/A }
286N/A
286N/A catch(SAXException e)
286N/A {
286N/A e.printStackTrace();
286N/A }
286N/A }
286N/A
286N/A }
286N/A
286N/A
286N/A} // class IncrementalSAXSource_Xerces