EntityResolver
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
Basic interface for resolving entities.
This module, both source code and documentation, is in the
Public Domain, and comes with NO WARRANTY.
See http://www.saxproject.org
for further information.
If a SAX application needs to implement customized handling
for external entities, it must implement this interface and
register an instance with the SAX driver using the
setEntityResolver
method.
The XML reader will then allow the application to intercept any
external entities (including the external DTD subset and external
parameter entities, if any) before including them.
Many SAX applications will not need to implement this interface,
but it will be especially useful for applications that build
XML documents from databases or other specialised input sources,
or for applications that use URI types other than URLs.
The following resolver would provide the application
with a special character stream for the entity with the system
identifier "http://www.myhost.com/today":
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
public class MyResolver implements EntityResolver {
public InputSource resolveEntity (String publicId, String systemId)
{
if (systemId.equals("http://www.myhost.com/today")) {
// return a special input source
MyReader reader = new MyReader();
return new InputSource(reader);
} else {
// use the default behaviour
return null;
}
}
}
The application can also use this interface to redirect system
identifiers to local URIs or to look up replacements in a catalog
(possibly by using the public identifier).
Public Methods
Allow the application to resolve external entities.
The parser will call this method before opening any external
entity except the top-level document entity. Such entities include
the external DTD subset and external parameter entities referenced
within the DTD (in either case, only if the parser reads external
parameter entities), and external general entities referenced
within the document element (if the parser reads external general
entities). The application may request that the parser locate
the entity itself, that it use an alternative URI, or that it
use data provided by the application (as a character or byte
input stream).
Application writers can use this method to redirect external
system identifiers to secure and/or local URIs, to look up
public identifiers in a catalogue, or to read an entity from a
database or other input source (including, for example, a dialog
box). Neither XML nor SAX specifies a preferred policy for using
public or system IDs to resolve resources. However, SAX specifies
how to interpret any InputSource returned by this method, and that
if none is returned, then the system ID will be dereferenced as
a URL.
If the system identifier is a URL, the SAX parser must
resolve it fully before reporting it to the application.
Parameters
publicId |
The public identifier of the external entity
being referenced, or null if none was supplied. |
systemId |
The system identifier of the external entity
being referenced. |
Returns
- An InputSource object describing the new input source,
or null to request that the parser open a regular
URI connection to the system identifier.
Throws
SAXException |
Any SAX exception, possibly
wrapping another exception. |
IOException |
A Java-specific IO exception,
possibly the result of creating a new InputStream
or Reader for the InputSource. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eEntityResolver\u003c/code\u003e interface allows applications to customize the handling of external entities in SAX parsing.\u003c/p\u003e\n"],["\u003cp\u003eImplement this interface and register it with the SAX driver to intercept and manage external entities before they are included.\u003c/p\u003e\n"],["\u003cp\u003eThis is particularly useful for applications that build XML documents from diverse sources or utilize non-URL URI types.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eresolveEntity\u003c/code\u003e method provides the mechanism to redirect system identifiers or use custom input sources for entities.\u003c/p\u003e\n"],["\u003cp\u003eReturning \u003ccode\u003enull\u003c/code\u003e from \u003ccode\u003eresolveEntity\u003c/code\u003e indicates that the default system identifier resolution should be used.\u003c/p\u003e\n"]]],["The `EntityResolver` interface allows SAX applications to customize handling of external entities. Applications implementing this interface can intercept external entities, including the DTD subset, before inclusion. The key action is using the `resolveEntity` method to specify how external entities are resolved. This method takes public and system identifiers as input and returns an `InputSource`, or null to default to the system identifier. The application can use this to redirect or provide custom input sources.\n"],null,["# EntityResolver\n\npublic interface **EntityResolver** \n\n|---|---|---|\n| Known Indirect Subclasses [DefaultHandler](../../../../reference/org/xml/sax/helpers/DefaultHandler.html), [DefaultHandler2](../../../../reference/org/xml/sax/ext/DefaultHandler2.html), [EntityResolver2](../../../../reference/org/xml/sax/ext/EntityResolver2.html), [HandlerBase](../../../../reference/org/xml/sax/HandlerBase.html), [XMLFilterImpl](../../../../reference/org/xml/sax/helpers/XMLFilterImpl.html) |---------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [DefaultHandler](../../../../reference/org/xml/sax/helpers/DefaultHandler.html) | Default base class for SAX2 event handlers. | | [DefaultHandler2](../../../../reference/org/xml/sax/ext/DefaultHandler2.html) | This class extends the SAX2 base handler class to support the SAX2 [LexicalHandler](../../../../reference/org/xml/sax/ext/LexicalHandler.html), [DeclHandler](../../../../reference/org/xml/sax/ext/DeclHandler.html), and [EntityResolver2](../../../../reference/org/xml/sax/ext/EntityResolver2.html) extensions. | | [EntityResolver2](../../../../reference/org/xml/sax/ext/EntityResolver2.html) | Extended interface for mapping external entity references to input sources, or providing a missing external subset. | | [HandlerBase](../../../../reference/org/xml/sax/HandlerBase.html) | *This class is deprecated. This class works with the deprecated [DocumentHandler](../../../../reference/org/xml/sax/DocumentHandler.html) interface. It has been replaced by the SAX2 [DefaultHandler](../../../../reference/org/xml/sax/helpers/DefaultHandler.html) class.* | | [XMLFilterImpl](../../../../reference/org/xml/sax/helpers/XMLFilterImpl.html) | Base class for deriving an XML filter. | |||\n\nBasic interface for resolving entities.\n\n\u003e *This module, both source code and documentation, is in the\n\u003e Public Domain, and comes with **NO WARRANTY**.* See \u003chttp://www.saxproject.org\u003e for further information.\n\nIf a SAX application needs to implement customized handling\nfor external entities, it must implement this interface and\nregister an instance with the SAX driver using the\n[setEntityResolver](../../../../reference/org/xml/sax/XMLReader.html#setEntityResolver(org.xml.sax.EntityResolver))\nmethod.\n\nThe XML reader will then allow the application to intercept any\nexternal entities (including the external DTD subset and external\nparameter entities, if any) before including them.\n\nMany SAX applications will not need to implement this interface,\nbut it will be especially useful for applications that build\nXML documents from databases or other specialised input sources,\nor for applications that use URI types other than URLs.\n\nThe following resolver would provide the application\nwith a special character stream for the entity with the system\nidentifier \"http://www.myhost.com/today\": \n\n```\n import org.xml.sax.EntityResolver;\n import org.xml.sax.InputSource;\n\n public class MyResolver implements EntityResolver {\n public InputSource resolveEntity (String publicId, String systemId)\n {\n if (systemId.equals(\"http://www.myhost.com/today\")) {\n // return a special input source\n MyReader reader = new MyReader();\n return new InputSource(reader);\n } else {\n // use the default behaviour\n return null;\n }\n }\n }\n \n```\n\nThe application can also use this interface to redirect system\nidentifiers to local URIs or to look up replacements in a catalog\n(possibly by using the public identifier).\n\n\u003cbr /\u003e\n\n##### See Also\n\n- [XMLReader.setEntityResolver(EntityResolver)](../../../../reference/org/xml/sax/XMLReader.html#setEntityResolver(org.xml.sax.EntityResolver))\n- [InputSource](../../../../reference/org/xml/sax/InputSource.html) \n\n### Public Method Summary\n\n|----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [InputSource](../../../../reference/org/xml/sax/InputSource.html) | [resolveEntity](../../../../reference/org/xml/sax/EntityResolver.html#resolveEntity(java.lang.String,%20java.lang.String))([String](../../../../reference/java/lang/String.html) publicId, [String](../../../../reference/java/lang/String.html) systemId) Allow the application to resolve external entities. |\n\nPublic Methods\n--------------\n\n#### public abstract [InputSource](../../../../reference/org/xml/sax/InputSource.html)\n**resolveEntity**\n([String](../../../../reference/java/lang/String.html) publicId, [String](../../../../reference/java/lang/String.html) systemId)\n\nAllow the application to resolve external entities.\n\nThe parser will call this method before opening any external\nentity except the top-level document entity. Such entities include\nthe external DTD subset and external parameter entities referenced\nwithin the DTD (in either case, only if the parser reads external\nparameter entities), and external general entities referenced\nwithin the document element (if the parser reads external general\nentities). The application may request that the parser locate\nthe entity itself, that it use an alternative URI, or that it\nuse data provided by the application (as a character or byte\ninput stream).\n\nApplication writers can use this method to redirect external\nsystem identifiers to secure and/or local URIs, to look up\npublic identifiers in a catalogue, or to read an entity from a\ndatabase or other input source (including, for example, a dialog\nbox). Neither XML nor SAX specifies a preferred policy for using\npublic or system IDs to resolve resources. However, SAX specifies\nhow to interpret any InputSource returned by this method, and that\nif none is returned, then the system ID will be dereferenced as\na URL.\n\nIf the system identifier is a URL, the SAX parser must\nresolve it fully before reporting it to the application.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| publicId | The public identifier of the external entity being referenced, or null if none was supplied. |\n| systemId | The system identifier of the external entity being referenced. |\n|----------|----------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- An InputSource object describing the new input source, or null to request that the parser open a regular URI connection to the system identifier. \n\n##### Throws\n\n| [SAXException](../../../../reference/org/xml/sax/SAXException.html) | Any SAX exception, possibly wrapping another exception. |\n| [IOException](../../../../reference/java/io/IOException.html) | A Java-specific IO exception, possibly the result of creating a new InputStream or Reader for the InputSource. |\n|---------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n\n##### See Also\n\n- [InputSource](../../../../reference/org/xml/sax/InputSource.html)"]]