001 // SAX entity resolver. 002 // http://www.saxproject.org 003 // No warranty; no copyright -- use this as you will. 004 // $Id: EntityResolver.java,v 1.1 2004/12/23 22:38:42 mark Exp $ 005 006 package org.xml.sax; 007 008 import java.io.IOException; 009 010 011 /** 012 * Basic interface for resolving entities. 013 * 014 * <blockquote> 015 * <em>This module, both source code and documentation, is in the 016 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> 017 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> 018 * for further information. 019 * </blockquote> 020 * 021 * <p>If a SAX application needs to implement customized handling 022 * for external entities, it must implement this interface and 023 * register an instance with the SAX driver using the 024 * {@link org.xml.sax.XMLReader#setEntityResolver setEntityResolver} 025 * method.</p> 026 * 027 * <p>The XML reader will then allow the application to intercept any 028 * external entities (including the external DTD subset and external 029 * parameter entities, if any) before including them.</p> 030 * 031 * <p>Many SAX applications will not need to implement this interface, 032 * but it will be especially useful for applications that build 033 * XML documents from databases or other specialised input sources, 034 * or for applications that use URI types other than URLs.</p> 035 * 036 * <p>The following resolver would provide the application 037 * with a special character stream for the entity with the system 038 * identifier "http://www.myhost.com/today":</p> 039 * 040 * <pre> 041 * import org.xml.sax.EntityResolver; 042 * import org.xml.sax.InputSource; 043 * 044 * public class MyResolver implements EntityResolver { 045 * public InputSource resolveEntity (String publicId, String systemId) 046 * { 047 * if (systemId.equals("http://www.myhost.com/today")) { 048 * // return a special input source 049 * MyReader reader = new MyReader(); 050 * return new InputSource(reader); 051 * } else { 052 * // use the default behaviour 053 * return null; 054 * } 055 * } 056 * } 057 * </pre> 058 * 059 * <p>The application can also use this interface to redirect system 060 * identifiers to local URIs or to look up replacements in a catalog 061 * (possibly by using the public identifier).</p> 062 * 063 * @since SAX 1.0 064 * @author David Megginson 065 * @version 2.0.1 (sax2r2) 066 * @see org.xml.sax.XMLReader#setEntityResolver 067 * @see org.xml.sax.InputSource 068 */ 069 public interface EntityResolver { 070 071 072 /** 073 * Allow the application to resolve external entities. 074 * 075 * <p>The parser will call this method before opening any external 076 * entity except the top-level document entity. Such entities include 077 * the external DTD subset and external parameter entities referenced 078 * within the DTD (in either case, only if the parser reads external 079 * parameter entities), and external general entities referenced 080 * within the document element (if the parser reads external general 081 * entities). The application may request that the parser locate 082 * the entity itself, that it use an alternative URI, or that it 083 * use data provided by the application (as a character or byte 084 * input stream).</p> 085 * 086 * <p>Application writers can use this method to redirect external 087 * system identifiers to secure and/or local URIs, to look up 088 * public identifiers in a catalogue, or to read an entity from a 089 * database or other input source (including, for example, a dialog 090 * box). Neither XML nor SAX specifies a preferred policy for using 091 * public or system IDs to resolve resources. However, SAX specifies 092 * how to interpret any InputSource returned by this method, and that 093 * if none is returned, then the system ID will be dereferenced as 094 * a URL. </p> 095 * 096 * <p>If the system identifier is a URL, the SAX parser must 097 * resolve it fully before reporting it to the application.</p> 098 * 099 * @param publicId The public identifier of the external entity 100 * being referenced, or null if none was supplied. 101 * @param systemId The system identifier of the external entity 102 * being referenced. 103 * @return An InputSource object describing the new input source, 104 * or null to request that the parser open a regular 105 * URI connection to the system identifier. 106 * @exception org.xml.sax.SAXException Any SAX exception, possibly 107 * wrapping another exception. 108 * @exception java.io.IOException A Java-specific IO exception, 109 * possibly the result of creating a new InputStream 110 * or Reader for the InputSource. 111 * @see org.xml.sax.InputSource 112 */ 113 public abstract InputSource resolveEntity (String publicId, 114 String systemId) 115 throws SAXException, IOException; 116 117 } 118 119 // end of EntityResolver.java