Most visited

Recently visited

Added in API level 1

EntityResolver2

public interface EntityResolver2
implements EntityResolver

org.xml.sax.ext.EntityResolver2
Known Indirect Subclasses


扩展接口,用于将外部实体引用映射到输入源或提供缺少的外部子集。 XMLReader.setEntityResolver()方法用于为解析器提供此接口的实现。 当解析器使用此接口中的方法时,将使用EntityResolver2.resolveEntity()方法(在此接口中) 代替较旧的(SAX 1.0) EntityResolver.resolveEntity()方法。

This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY.

如果SAX应用程序需要此接口为外部实体定义的自定义处理,则必须确保它使用XMLReader并将http://xml.org/sax/features/use-entity-resolver2功能标志设置为true (其中是功能识别时的默认值)。 如果该标志无法识别,或者其值为false,或者解析器没有实现该接口,那么将只使用EntityResolver方法。

这支持修改实体分辨率的三类应用程序。 旧式应用程序不会知道这个接口; 他们将提供一个EntityResolver。 过渡模式提供了一个EntityResolver2,并且由于多态性而自动获得支持它的任何系统(解析器或其他工具)中其方法的好处。 旧式过渡模式应用程序都可以与任何SAX2解析器一起使用。 除了支持此特定功能的SAX2解析器外, 新样式的应用程序将无法运行。 他们会坚持认为功能标志的值为“true”,如果调用原始的SAX 1.0样式实体解析方法,它们提供的EntityResolver2实现可能会引发异常。

也可以看看:

Summary

Public methods

abstract InputSource getExternalSubset(String name, String baseURI)

允许应用程序为未明确定义文档的文档提供外部子集。

abstract InputSource resolveEntity(String name, String publicId, String baseURI, String systemId)

允许应用程序将对外部实体的引用映射到输入源,或者告诉解析器它应该使用传统的URI解析。

Inherited methods

From interface org.xml.sax.EntityResolver

Public methods

getExternalSubset

Added in API level 1
InputSource getExternalSubset (String name, 
                String baseURI)

允许应用程序为未明确定义文档的文档提供外部子集。 具有省略外部子集的DOCTYPE声明的文档因此可以增加可用于验证,实体处理和属性处理(标准化,默认和包括ID的报告类型)的声明。 通过startDTD()方法报告此增强,就好像文档文本最初包含外部子集一样; 此回调在任何内部子集数据或错误报告之前进行。

此方法也可以用于没有DOCTYPE声明的文档。 遇到根元素但未见DOCTYPE声明时,将调用此方法。 如果它为外部子集返回一个值,则将该根元素声明为根元素,从而在文档的序言末尾处拼接DOCTYPE声明的效果,否则该文档不会有效。 在这种情况下,解析器回调的顺序在逻辑上类似于以下内容:

 ... comments and PIs from the prolog (as usual)
 startDTD ("rootName", source.getPublicId (), source.getSystemId ());
 startEntity ("[dtd]");
 ... declarations, comments, and PIs from the external subset
 endEntity ("[dtd]");
 endDTD ();
 ... then the rest of the document (as usual)
 startElement (..., "rootName", ...);
 

请注意,InputSource不会得到进一步的解决方案。 该方法的实现可能希望调用resolveEntity()以获得诸如使用DTD实体的本地缓存之类的益处。 此外,此方法将永远不会被不包含外部参数实体的(非验证)处理器使用。

此方法的用途包括在与XML处理器互操作时促进数据验证,这些处理器始终要求对外部实体进行不良网络访问,或者出于其他原因采用“无DTD”策略。 非验证动机包括强制文档包含DTD,以便始终如一地处理属性。 例如,XPath处理器需要知道哪些属性具有类型“ID”,才能处理广泛使用的引用类型。

警告:返回外部子集会修改输入文档。 通过为一般实体提供定义,它可以使格式错误的文档看起来形成良好。

Parameters
name String: Identifies the document root element. This name comes from a DOCTYPE declaration (where available) or from the actual root element.
baseURI String: The document's base URI, serving as an additional hint for selecting the external subset. This is always an absolute URI, unless it is null because the XMLReader was given an InputSource without one.
Returns
InputSource An InputSource object describing the new external subset to be used by the parser, or null to indicate that no external subset is provided.
Throws
SAXException Any SAX exception, possibly wrapping another exception.
IOException Probably indicating a failure to create a new InputStream or Reader, or an illegal URL.

resolveEntity

Added in API level 1
InputSource resolveEntity (String name, 
                String publicId, 
                String baseURI, 
                String systemId)

允许应用程序将对外部实体的引用映射到输入源,或者告诉解析器它应该使用传统的URI解析。 此方法仅适用于已正确声明的外部实体。 这种方法比EntityResolver接口提供了更多的灵活性,支持更复杂的目录方案的实现,如OASIS XML Catalogs规范定义的目录方案。

配置为使用此解析器方法的解析器将调用它来确定由于XML文本中的引用而包含的任何外部实体使用的输入源。 这不包括文档实体和由getExternalSubset()返回的任何外部实体。 当(非验证)处理器通过使用特征标志被配置为不包括一类实体(参数或一般)时,不会为这些实体调用此方法。

请注意,此处使用的实体命名方案与 LexicalHandlerContentHandler.skippedEntity()方法中使用的实体命名方案相同。

Parameters
name String: Identifies the external entity being resolved. Either "[dtd]" for the external subset, or a name starting with "%" to indicate a parameter entity, or else the name of a general entity. This is never null when invoked by a SAX2 parser.
publicId String: The public identifier of the external entity being referenced (normalized as required by the XML specification), or null if none was supplied.
baseURI String: The URI with respect to which relative systemIDs are interpreted. This is always an absolute URI, unless it is null (likely because the XMLReader was given an InputSource without one). This URI is defined by the XML specification to be the one associated with the "<" starting the relevant declaration.
systemId String: The system identifier of the external entity being referenced; either a relative or absolute URI. This is never null when invoked by a SAX2 parser; only declared entities, and any external subset, are resolved by such parsers.
Returns
InputSource An InputSource object describing the new input source to be used by the parser. Returning null directs the parser to resolve the system ID against the base URI and open a connection to resulting URI.
Throws
SAXException Any SAX exception, possibly wrapping another exception.
IOException Probably indicating a failure to create a new InputStream or Reader, or an illegal URL.

Hooray!