rootxmlreadhandler.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 547 行 · 第 1/2 页

JAVA
547
字号
        if (manualDefinition == null) {            manualDefinition = genericFactory.getManualMappingDefinition(classToRead);        }        if (manualDefinition != null) {            // Log.debug ("Locating handler for " + manualDefinition.getBaseClass());            return loadHandlerClass(manualDefinition.getReadHandler());        }        // check whether a multiplexer is defined ...        // find multiplexer for this class...        MultiplexMappingDefinition mplex =            getFactoryLoader().getMultiplexDefinition(classToRead);        if (mplex == null) {            mplex = this.classToHandlerMapping.getMultiplexDefinition(classToRead);        }        if (mplex != null) {            final String attributeValue = atts.getValue(mplex.getAttributeName());            if (attributeValue == null) {                throw new XmlReaderException(                    "Multiplexer type attribute is not defined: " + mplex.getAttributeName()                     + " for " + classToRead                );            }            final MultiplexMappingEntry entry =                mplex.getEntryForType(attributeValue);            if (entry == null) {                throw new XmlReaderException(                    "Invalid type attribute value: " + mplex.getAttributeName() + " = "                     + attributeValue                );            }            final Class c = loadClass(entry.getTargetClass());            if (!c.equals(mplex.getBaseClass())) {                return findHandlerForClass(c, atts, history);            }        }        // check for generic classes ...        // and finally try the generic handler matches ...        if (this.classToHandlerMapping.isGenericHandler(classToRead)) {            return new GenericReadHandler                (this.classToHandlerMapping.getFactoryForClass(classToRead));        }        if (getFactoryLoader().isGenericHandler(classToRead)) {            return new GenericReadHandler                (getFactoryLoader().getFactoryForClass(classToRead));        }        return null;    }    /**     * Sets the root SAX handler.     *     * @param handler  the SAX handler.     */    protected void setRootHandler(final XmlReadHandler handler) {        this.rootHandler = handler;        this.rootHandlerInitialized = false;    }    /**     * Returns the root SAX handler.     *     * @return the root SAX handler.     */    protected XmlReadHandler getRootHandler() {        return this.rootHandler;    }    /**     * Start a new handler stack and delegate to another handler.     *     * @param handler  the handler.     * @param tagName  the tag name.     * @param attrs  the attributes.     *      * @throws XmlReaderException if there is a problem with the reader.     * @throws SAXException if there is a problem with the parser.     */    public void recurse(final XmlReadHandler handler, final String tagName, final Attributes attrs)        throws XmlReaderException, SAXException {                this.outerScopes.push(this.currentHandlers);        this.currentHandlers = new Stack();        this.currentHandlers.push(handler);        handler.startElement(tagName, attrs);        }    /**     * Delegate to another handler.     *      * @param handler  the new handler.     * @param tagName  the tag name.     * @param attrs  the attributes.     *      * @throws XmlReaderException if there is a problem with the reader.     * @throws SAXException if there is a problem with the parser.     */    public void delegate(final XmlReadHandler handler, final String tagName, final Attributes attrs)        throws XmlReaderException, SAXException {        this.currentHandlers.push(handler);        handler.init(this, tagName);        handler.startElement(tagName, attrs);    }    /**     * Hand control back to the previous handler.     *      * @param tagName  the tagname.     *      * @throws SAXException if there is a problem with the parser.     * @throws XmlReaderException if there is a problem with the reader.     */    public void unwind(final String tagName) throws SAXException, XmlReaderException {      // remove current handler from stack ..        this.currentHandlers.pop();        if (this.currentHandlers.isEmpty() && !this.outerScopes.isEmpty()) {            // if empty, but "recurse" had been called, then restore the old handler stack ..            // but do not end the recursed element ..            this.currentHandlers = (Stack) this.outerScopes.pop();        }        else if (!this.currentHandlers.isEmpty()) {            // if there are some handlers open, close them too (these handlers must be delegates)..            getCurrentHandler().endElement(tagName);        }    }    /**     * Returns the current handler.     *      * @return The current handler.     */    protected XmlReadHandler getCurrentHandler() {        return (XmlReadHandler) this.currentHandlers.peek();    }    /**     * Starts processing a document.     *      * @throws SAXException not in this implementation.     */    public void startDocument() throws SAXException {        this.outerScopes = new Stack();        this.currentHandlers = new Stack();        this.currentHandlers.push(this.rootHandler);    }    /**     * Starts processing an element.     *      * @param uri  the URI.     * @param localName  the local name.     * @param qName  the qName.     * @param attributes  the attributes.     *      * @throws SAXException if there is a parsing problem.     */    public void startElement(final String uri, final String localName,                             final String qName, final Attributes attributes)        throws SAXException {        if (rootHandlerInitialized == false) {            rootHandler.init(this, qName);            rootHandlerInitialized = true;        }        try {            getCurrentHandler().startElement(qName, attributes);        }        catch (XmlReaderException xre) {            throw new ParseException(xre, getLocator());        }    }    /**     * Process character data.     *      * @param ch  the character buffer.     * @param start  the start index.     * @param length  the length of the character data.     *      * @throws SAXException if there is a parsing error.     */    public void characters(final char[] ch, final int start, final int length) throws SAXException {        try {            getCurrentHandler().characters(ch, start, length);        }        catch (SAXException se) {            throw se;        }        catch (Exception e) {            throw new ParseException(e, getLocator());        }    }    /**     * Finish processing an element.     *      * @param uri  the URI.     * @param localName  the local name.     * @param qName  the qName.     *      * @throws SAXException if there is a parsing error.     */    public void endElement(final String uri, final String localName, final String qName)        throws SAXException {        try {            getCurrentHandler().endElement(qName);        }        catch (XmlReaderException xre) {            throw new ParseException(xre, getLocator());        }    }    /**     * Loads the given class, and ignores all exceptions which may occur     * during the loading. If the class was invalid, null is returned instead.     *     * @param className the name of the class to be loaded.     * @return the class or null.     * @throws XmlReaderException if there is a reader error.     */    protected XmlReadHandler loadHandlerClass(final String className)        throws XmlReaderException {        try {            final Class c = loadClass(className);            return (XmlReadHandler) c.newInstance();        }        catch (Exception e) {            // ignore buggy classes for now ..            throw new XmlReaderException("LoadHanderClass: Unable to instantiate " + className, e);        }    }    /**     * Loads the given class, and ignores all exceptions which may occur     * during the loading. If the class was invalid, null is returned instead.     *     * @param className the name of the class to be loaded.     * @return the class or null.     * @throws XmlReaderException if there is a reader error.     */    protected Class loadClass(final String className)        throws XmlReaderException {        if (className == null) {            throw new XmlReaderException("LoadHanderClass: Class name not defined");        }        try {            final Class c = ObjectUtilities.getClassLoader(getClass()).loadClass(className);            return c;        }        catch (Exception e) {            // ignore buggy classes for now ..            throw new XmlReaderException("LoadHanderClass: Unable to load " + className, e);        }    }    public Object getResult () throws SAXException    {        if (this.rootHandler != null) {          try          {            return this.rootHandler.getObject();          }          catch (XmlReaderException e)          {            throw new ElementDefinitionException(e);          }        }        return null;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?