⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saxoutputter.java

📁 openlogic-jdom-1.1-all-src-1.zip 可以用于操作xml文件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**     * <p>     * This will invoke the callbacks for the content of an element.     * </p>     *     * @param node a <code>Content</code> node.     * @param namespaces <code>List</code> stack of Namespaces in scope.     */    private void elementContent(Content node, NamespaceStack namespaces)                      throws JDOMException {        // update locator        if (locator != null) {          locator.setNode(node);        }        if (node instanceof Element) {            element((Element) node, namespaces);        }        else if (node instanceof CDATA) {            cdata(((CDATA) node).getText());        }        else if (node instanceof Text) {            // contentHandler.characters()            characters(((Text) node).getText());        }        else if (node instanceof ProcessingInstruction) {            // contentHandler.processingInstruction()            processingInstruction((ProcessingInstruction) node);        }        else if (node instanceof Comment) {            // lexicalHandler.comment()            comment(((Comment) node).getText());        }        else if (node instanceof EntityRef) {            // contentHandler.skippedEntity()            entityRef((EntityRef) node);        }        else {            // Not a valid element child. This could happen with            // application-provided lists which may contain non            // JDOM objects.            handleError(new JDOMException("Invalid element content: " + node));        }    }    /**     * <p>     * This will be called for each chunk of CDATA section encountered.     * </p>     *     * @param cdataText all text in the CDATA section, including whitespace.     */    private void cdata(String cdataText) throws JDOMException {        try {            if (lexicalHandler != null) {                lexicalHandler.startCDATA();                characters(cdataText);                lexicalHandler.endCDATA();            }            else {                characters(cdataText);            }        }        catch (SAXException se) {            throw new JDOMException("Exception in CDATA", se);        }    }    /**     * <p>     * This will be called for each chunk of character data encountered.     * </p>     *     * @param elementText all text in an element, including whitespace.     */    private void characters(String elementText) throws JDOMException {        char[] c = elementText.toCharArray();        try {            contentHandler.characters(c, 0, c.length);        }        catch (SAXException se) {            throw new JDOMException("Exception in characters", se);        }    }    /**     * <p>     *  This will be called for each chunk of comment data encontered.     * </p>     *     * @param commentText all text in a comment, including whitespace.     */    private void comment(String commentText) throws JDOMException {        if (lexicalHandler != null) {            char[] c = commentText.toCharArray();            try {                lexicalHandler.comment(c, 0, c.length);            } catch (SAXException se) {                throw new JDOMException("Exception in comment", se);            }        }    }    /**     * <p>     * This will invoke the <code>ContentHandler.skippedEntity</code>     * callback when an entity reference is encountered.     * </p>     *     * @param entity <code>EntityRef</code>.     */    private void entityRef(EntityRef entity) throws JDOMException {        if (entity != null) {            try {                // No need to worry about appending a '%' character as                // we do not support parameter entities                contentHandler.skippedEntity(entity.getName());            }            catch (SAXException se) {                throw new JDOMException("Exception in entityRef", se);            }        }    }    /**     * <p>     * Appends a namespace declaration in the form of a xmlns attribute to     * an attribute list, crerating this latter if needed.     * </p>     *     * @param atts <code>AttributeImpl</code> where to add the attribute.     * @param ns <code>Namespace</code> the namespace to declare.     *     * @return <code>AttributeImpl</code> the updated attribute list.     */    private AttributesImpl addNsAttribute(AttributesImpl atts, Namespace ns) {        if (this.declareNamespaces) {            if (atts == null) {                atts = new AttributesImpl();            }            String prefix = ns.getPrefix();            if (prefix.equals("")) {                atts.addAttribute("",                        // namespace                                  "",                        // local name                                  "xmlns",                   // qualified name                                  "CDATA",                   // type                                  ns.getURI());              // value            }            else {                atts.addAttribute("",                        // namespace                                  "",                        // local name                                  "xmlns:" + ns.getPrefix(), // qualified name                                  "CDATA",                   // type                                  ns.getURI());              // value            }        }        return atts;    }    /**     * <p>     * Returns the SAX 2.0 attribute type string from the type of     * a JDOM Attribute.     * </p>     *     * @param type <code>int</code> the type of the JDOM attribute.     *     * @return <code>String</code> the SAX 2.0 attribute type string.     *     * @see org.jdom.Attribute#getAttributeType     * @see org.xml.sax.Attributes#getType     */    private static String getAttributeTypeName(int type) {        if ((type < 0) || (type >= attrTypeToNameMap.length)) {            type = Attribute.UNDECLARED_TYPE;        }        return attrTypeToNameMap[type];    }    /**     * <p>     * Notifies the registered {@link ErrorHandler SAX error handler}     * (if any) of an input processing error. The error handler can     * choose to absorb the error and let the processing continue.     * </p>     *     * @param exception <code>JDOMException</code> containing the     *                  error information; will be wrapped in a     *                  {@link SAXParseException} when reported to     *                  the SAX error handler.     *     * @throws JDOMException if no error handler has been registered     *                       or if the error handler fired a     *                       {@link SAXException}.     */    private void handleError(JDOMException exception) throws JDOMException {        if (errorHandler != null) {            try {                errorHandler.error(new SAXParseException(                                exception.getMessage(), null, exception));            }            catch (SAXException se) {               if (se.getException() instanceof JDOMException) {                   throw (JDOMException)(se.getException());               }               else {                   throw new JDOMException(se.getMessage(), se);               }            }        }        else {            throw exception;        }    }    /**     * <p>     * Creates a SAX XMLReader.     * </p>     *     * @return <code>XMLReader</code> a SAX2 parser.     *     * @throws Exception if no parser can be created.     */    protected XMLReader createParser() throws Exception {        XMLReader parser = null;        // Try using JAXP...        // Note we need JAXP 1.1, and if JAXP 1.0 is all that's        // available then the getXMLReader call fails and we skip        // to the hard coded default parser        try {            Class factoryClass =                    Class.forName("javax.xml.parsers.SAXParserFactory");            // factory = SAXParserFactory.newInstance();            Method newParserInstance =                    factoryClass.getMethod("newInstance", null);            Object factory = newParserInstance.invoke(null, null);            // jaxpParser = factory.newSAXParser();            Method newSAXParser = factoryClass.getMethod("newSAXParser", null);            Object jaxpParser   = newSAXParser.invoke(factory, null);            // parser = jaxpParser.getXMLReader();            Class parserClass = jaxpParser.getClass();            Method getXMLReader =                    parserClass.getMethod("getXMLReader", null);            parser = (XMLReader)getXMLReader.invoke(jaxpParser, null);        } catch (ClassNotFoundException e) {            //e.printStackTrace();        } catch (InvocationTargetException e) {            //e.printStackTrace();        } catch (NoSuchMethodException e) {            //e.printStackTrace();        } catch (IllegalAccessException e) {            //e.printStackTrace();        }        // Check to see if we got a parser yet, if not, try to use a        // hard coded default        if (parser == null) {            parser = XMLReaderFactory.createXMLReader(                                "org.apache.xerces.parsers.SAXParser");        }        return parser;    }    /**     * <p>     * This will create a SAX XMLReader capable of parsing a DTD and     * configure it so that the DTD parsing events are routed to the     * handlers registered onto this SAXOutputter.     * </p>     *     * @return <code>XMLReader</code> a SAX2 parser.     *     * @throws JDOMException if no parser can be created.     */    private XMLReader createDTDParser() throws JDOMException {        XMLReader parser = null;        // Get a parser instance        try        {            parser = createParser();        }        catch (Exception ex1) {           throw new JDOMException("Error in SAX parser allocation", ex1);        }        // Register handlers        if (this.getDTDHandler() != null) {            parser.setDTDHandler(this.getDTDHandler());        }        if (this.getEntityResolver() != null) {            parser.setEntityResolver(this.getEntityResolver());        }        if (this.getLexicalHandler() != null) {            try {                parser.setProperty(LEXICAL_HANDLER_SAX_PROPERTY,                                   this.getLexicalHandler());            }            catch (SAXException ex1) {                try {                    parser.setProperty(LEXICAL_HANDLER_ALT_PROPERTY,                                       this.getLexicalHandler());                } catch (SAXException ex2) {                    // Forget it!                }            }        }        if (this.getDeclHandler() != null) {            try {                parser.setProperty(DECL_HANDLER_SAX_PROPERTY,                                   this.getDeclHandler());            }            catch (SAXException ex1) {                try {                    parser.setProperty(DECL_HANDLER_ALT_PROPERTY,                                       this.getDeclHandler());                } catch (SAXException ex2) {                    // Forget it!                }            }        }        // Absorb errors as much as possible, per Laurent        parser.setErrorHandler(new DefaultHandler());        return parser;    }    /**     * Returns a JDOMLocator object referencing the node currently     * being processed by this outputter.  The returned object is a     * snapshot of the  location information and can thus safely be     * memorized for later use.     * <p>     * This method allows direct access to the location information     * maintained by SAXOutputter without requiring to implement     * <code>XMLFilter</code>. (In SAX, locators are only available     * though the <code>ContentHandler</code> interface).</p>     * <p>     * Note that location information is only available while     * SAXOutputter is outputting nodes. Hence this method should     * only be used by objects taking part in the output processing     * such as <code>ErrorHandler</code>s.     *     * @return a JDOMLocator object referencing the node currently     *         being processed or <code>null</code> if no output     *         operation is being performed.     */    public JDOMLocator getLocator() {        return (locator != null)? new JDOMLocator(locator): null;    }}

⌨️ 快捷键说明

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