domparserimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,089 行 · 第 1/3 页

JAVA
1,089
字号
            throw new DOMException (DOMException.NOT_FOUND_ERR, msg);        }        return null;    }    public boolean canSetParameter (String name, Object value) {        if(value instanceof Boolean){            boolean state = ((Boolean)value).booleanValue ();            if ( name.equalsIgnoreCase (Constants.DOM_SUPPORTED_MEDIATYPES_ONLY)            || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)            || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION )            || name.equalsIgnoreCase (Constants.DOM_CANONICAL_FORM) ) {                // true is not supported                return (state) ? false : true;            }            else if (name.equalsIgnoreCase (Constants.DOM_NAMESPACE_DECLARATIONS)            || name.equalsIgnoreCase (Constants.DOM_WELLFORMED)            || name.equalsIgnoreCase (Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {                // false is not supported                return (state) ? true : false;            }            else if (name.equalsIgnoreCase (Constants.DOM_CDATA_SECTIONS)            || name.equalsIgnoreCase (Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING)            || name.equalsIgnoreCase (Constants.DOM_COMMENTS)            || name.equalsIgnoreCase (Constants.DOM_DATATYPE_NORMALIZATION)            || name.equalsIgnoreCase (Constants.DOM_DISALLOW_DOCTYPE)            || name.equalsIgnoreCase (Constants.DOM_ENTITIES)            || name.equalsIgnoreCase (Constants.DOM_INFOSET)            || name.equalsIgnoreCase (Constants.DOM_NAMESPACES)            || name.equalsIgnoreCase (Constants.DOM_VALIDATE)            || name.equalsIgnoreCase (Constants.DOM_VALIDATE_IF_SCHEMA)            || name.equalsIgnoreCase (Constants.DOM_ELEMENT_CONTENT_WHITESPACE)            || name.equalsIgnoreCase (Constants.DOM_XMLDECL)) {                return true;            }            // Recognize Xerces features.            try {                fConfiguration.getFeature(name.toLowerCase(Locale.ENGLISH));                return true;            }            catch (XMLConfigurationException e) {                return false;            }        }        else { // check properties            if (name.equalsIgnoreCase (Constants.DOM_ERROR_HANDLER)) {                if (value instanceof DOMErrorHandler || value == null) {                    return true;                }                return false;            }            else if (name.equalsIgnoreCase (Constants.DOM_RESOURCE_RESOLVER)) {                if (value instanceof LSResourceResolver || value == null) {                    return true;                }                return false;            }            else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_TYPE)) {                if ((value instanceof String                && (value.equals (Constants.NS_XMLSCHEMA)                || value.equals (Constants.NS_DTD))) || value == null) {                    return true;                }                return false;            }            else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_LOCATION)) {                if (value instanceof String || value == null)                    return true;                return false;            }            else if (name.equalsIgnoreCase (DOCUMENT_CLASS_NAME)){                return true;            }            return false;        }    }    /**     *  DOM Level 3 CR - Experimental.     *     *  The list of the parameters supported by this     * <code>DOMConfiguration</code> object and for which at least one value     * can be set by the application. Note that this list can also contain     * parameter names defined outside this specification.     */    public DOMStringList getParameterNames () {        if (fRecognizedParameters == null){            Vector parameters = new Vector();                        // REVISIT: add Xerces recognized properties/features            parameters.add(Constants.DOM_NAMESPACES);            parameters.add(Constants.DOM_CDATA_SECTIONS);            parameters.add(Constants.DOM_CANONICAL_FORM);            parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS);            parameters.add(Constants.DOM_SPLIT_CDATA);                        parameters.add(Constants.DOM_ENTITIES);            parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA);            parameters.add(Constants.DOM_VALIDATE);            parameters.add(Constants.DOM_DATATYPE_NORMALIZATION);                        parameters.add(Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING);            parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION);            parameters.add(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY);            parameters.add(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS);                        parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);            parameters.add(Constants.DOM_WELLFORMED);            parameters.add(Constants.DOM_INFOSET);            parameters.add(Constants.DOM_DISALLOW_DOCTYPE);            parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE);            parameters.add(Constants.DOM_COMMENTS);                        parameters.add(Constants.DOM_ERROR_HANDLER);            parameters.add(Constants.DOM_RESOURCE_RESOLVER);            parameters.add(Constants.DOM_SCHEMA_LOCATION);            parameters.add(Constants.DOM_SCHEMA_TYPE);                        fRecognizedParameters = new DOMStringListImpl(parameters);                    }                return fRecognizedParameters;    }    /**     * Parse an XML document from a location identified by an URI reference.     * If the URI contains a fragment identifier (see section 4.1 in ), the     * behavior is not defined by this specification.     *     */    public Document parseURI (String uri) throws LSException {        //If DOMParser insstance is already busy parsing another document when this        // method is called, then raise INVALID_STATE_ERR according to DOM L3 LS spec        if ( fBusy ) {            String msg = DOMMessageFormatter.formatMessage (            DOMMessageFormatter.DOM_DOMAIN,            "INVALID_STATE_ERR",null);            throw new DOMException ( DOMException.INVALID_STATE_ERR,msg);        }        XMLInputSource source = new XMLInputSource (null, uri, null);        try {			//since we are depending on application to handle			//multithreading issues this should be ok.			if(!fBusy)currentThread = Thread.currentThread();			fBusy = true;            parse (source);            fBusy = false;			if(abortNow && currentThread.isInterrupted())			{				//reset interrupt state				abortNow = false;				currentThread.interrupted();			}        } catch (Exception e){            fBusy = false;            // Consume this exception if the user            // issued an interrupt or an abort.			if(abortNow && currentThread.isInterrupted())			{				//reset interrupt state				currentThread.interrupted();			}						if(abortNow){				abortNow = false;				return null;			}            if (e != abort) {                if (fErrorHandler != null) {                    DOMErrorImpl error = new DOMErrorImpl ();                    error.fException = e;                    error.fMessage = e.getMessage ();                    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;                    fErrorHandler.getErrorHandler ().handleError (error);                }                if (DEBUG) {                    e.printStackTrace ();                }                throw new LSException(LSException.PARSE_ERR, e.getMessage());            }        }        return getDocument ();    }    /**     * Parse an XML document from a resource identified by an     * <code>LSInput</code>.     *     */    public Document parse (LSInput is) throws LSException {        // need to wrap the LSInput with an XMLInputSource        XMLInputSource xmlInputSource = dom2xmlInputSource (is);        if ( fBusy ) {            String msg = DOMMessageFormatter.formatMessage (            DOMMessageFormatter.DOM_DOMAIN,            "INVALID_STATE_ERR",null);            throw new DOMException ( DOMException.INVALID_STATE_ERR,msg);        }        try {			//since we are depending on application to handle			//multithreading issues this should be ok.			if(!fBusy)currentThread = Thread.currentThread();            fBusy = true;            parse(xmlInputSource);            fBusy = false;			if(abortNow && currentThread.isInterrupted())			{				//reset interrupt state 				abortNow = false;				currentThread.interrupted();			}        } catch (Exception e) {            fBusy = false;            // Consume this exception if the user            // issued an interrupt or an abort.			if(abortNow && currentThread.isInterrupted())			{				//reset interrupt state 				currentThread.interrupted();			}			if(abortNow){				abortNow = false;				return null;			}            if (e != abort) {                if (fErrorHandler != null) {                   DOMErrorImpl error = new DOMErrorImpl ();                   error.fException = e;                   error.fMessage = e.getMessage ();                   error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;                   fErrorHandler.getErrorHandler().handleError (error);                }                if (DEBUG) {                   e.printStackTrace ();                }                throw new LSException(LSException.PARSE_ERR, e.getMessage());            }        }        return getDocument ();    }    /**     *  Parse an XML document or fragment from a resource identified by an     * <code>LSInput</code> and insert the content into an existing     * document at the position epcified with the <code>contextNode</code>     * and <code>action</code> arguments. When parsing the input stream the     * context node is used for resolving unbound namespace prefixes.     *     * @param is  The <code>LSInput</code> from which the source     *   document is to be read.     * @param cnode  The <code>Node</code> that is used as the context for     *   the data that is being parsed.     * @param action This parameter describes which action should be taken     *   between the new set of node being inserted and the existing     *   children of the context node. The set of possible actions is     *   defined above.     * @exception DOMException     *   HIERARCHY_REQUEST_ERR: Thrown if this action results in an invalid     *   hierarchy (i.e. a Document with more than one document element).     */    public Node parseWithContext (LSInput is, Node cnode,    short action) throws DOMException, LSException {        // REVISIT: need to implement.        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Not supported");    }    /**     * NON-DOM: convert LSInput to XNIInputSource     *     * @param is     * @return     */    XMLInputSource dom2xmlInputSource(LSInput is) {        // need to wrap the LSInput with an XMLInputSource        XMLInputSource xis = null;        // check whether there is a Reader        // according to DOM, we need to treat such reader as "UTF-16".        if (is.getCharacterStream () != null) {            xis = new XMLInputSource (is.getPublicId (), is.getSystemId (),            is.getBaseURI (), is.getCharacterStream (),            "UTF-16");        }        // check whether there is an InputStream        else if (is.getByteStream () != null) {            xis = new XMLInputSource (is.getPublicId (), is.getSystemId (),            is.getBaseURI (), is.getByteStream (),            is.getEncoding ());        }        // if there is a string data, use a StringReader        // according to DOM, we need to treat such data as "UTF-16".        else if (is.getStringData () != null && is.getStringData().length() > 0) {            xis = new XMLInputSource (is.getPublicId (), is.getSystemId (),            is.getBaseURI (), new StringReader (is.getStringData ()),            "UTF-16");        }        // otherwise, just use the public/system/base Ids        else if ((is.getSystemId() != null && is.getSystemId().length() > 0) ||             (is.getPublicId() != null && is.getPublicId().length() > 0)) {            xis = new XMLInputSource (is.getPublicId (), is.getSystemId (),            is.getBaseURI ());        }        else {             // all inputs are null            if (fErrorHandler != null) {                DOMErrorImpl error = new DOMErrorImpl();                error.fType = "no-input-specified";                error.fMessage = "no-input-specified";                error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;                fErrorHandler.getErrorHandler().handleError(error);            }            throw new LSException(LSException.PARSE_ERR, "no-input-specified");        }        return xis;    }	/**     * @see org.w3c.dom.ls.LSParser#getAsync()     */    public boolean getAsync () {        return false;    }    /**     * @see org.w3c.dom.ls.LSParser#getBusy()     */    public boolean getBusy () {        return fBusy;    }    /**     * @see org.w3c.dom.ls.DOMParser#abort()     */    public void abort () {        // If parse operation is in progress then reset it        if ( fBusy ) {            fBusy = false;			abortNow = true;			try{				//Revisit : Should we also close all opened readers ?				//Work towards other classes supporting abort instead of 				//calling reset.				reset();				//interrupt the thread doing the parse operation.				//come out of all block operations.				// current thread could be blocked on read operation.				if(currentThread != null )currentThread.interrupt();			}catch(Exception ex){			}        }        return; // If not busy then this is noop    }} // class DOMParserImpl

⌨️ 快捷键说明

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