Path: blob/master/src/java.xml/share/classes/org/xml/sax/EntityResolver.java
40948 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package org.xml.sax;2627import java.io.IOException;282930/**31* Basic interface for resolving entities.32*33* <p>If a SAX application needs to implement customized handling34* for external entities, it must implement this interface and35* register an instance with the SAX driver using the36* {@link org.xml.sax.XMLReader#setEntityResolver setEntityResolver}37* method.</p>38*39* <p>The XML reader will then allow the application to intercept any40* external entities (including the external DTD subset and external41* parameter entities, if any) before including them.</p>42*43* <p>Many SAX applications will not need to implement this interface,44* but it will be especially useful for applications that build45* XML documents from databases or other specialised input sources,46* or for applications that use URI types other than URLs.</p>47*48* <p>The following resolver would provide the application49* with a special character stream for the entity with the system50* identifier "http://www.myhost.com/today":</p>51*52* <pre>53* import org.xml.sax.EntityResolver;54* import org.xml.sax.InputSource;55*56* public class MyResolver implements EntityResolver {57* public InputSource resolveEntity (String publicId, String systemId)58* {59* if (systemId.equals("http://www.myhost.com/today")) {60* // return a special input source61* MyReader reader = new MyReader();62* return new InputSource(reader);63* } else {64* // use the default behaviour65* return null;66* }67* }68* }69* </pre>70*71* <p>The application can also use this interface to redirect system72* identifiers to local URIs or to look up replacements in a catalog73* (possibly by using the public identifier).</p>74*75* @since 1.4, SAX 1.076* @author David Megginson77* @see org.xml.sax.XMLReader#setEntityResolver78* @see org.xml.sax.InputSource79*/80public interface EntityResolver {818283/**84* Allow the application to resolve external entities.85*86* <p>The parser will call this method before opening any external87* entity except the top-level document entity. Such entities include88* the external DTD subset and external parameter entities referenced89* within the DTD (in either case, only if the parser reads external90* parameter entities), and external general entities referenced91* within the document element (if the parser reads external general92* entities). The application may request that the parser locate93* the entity itself, that it use an alternative URI, or that it94* use data provided by the application (as a character or byte95* input stream).</p>96*97* <p>Application writers can use this method to redirect external98* system identifiers to secure and/or local URIs, to look up99* public identifiers in a catalogue, or to read an entity from a100* database or other input source (including, for example, a dialog101* box). Neither XML nor SAX specifies a preferred policy for using102* public or system IDs to resolve resources. However, SAX specifies103* how to interpret any InputSource returned by this method, and that104* if none is returned, then the system ID will be dereferenced as105* a URL. </p>106*107* <p>If the system identifier is a URL, the SAX parser must108* resolve it fully before reporting it to the application.</p>109*110* @param publicId The public identifier of the external entity111* being referenced, or null if none was supplied.112* @param systemId The system identifier of the external entity113* being referenced.114* @return An InputSource object describing the new input source,115* or null to request that the parser open a regular116* URI connection to the system identifier.117* @throws org.xml.sax.SAXException Any SAX exception, possibly118* wrapping another exception.119* @throws java.io.IOException A Java-specific IO exception,120* possibly the result of creating a new InputStream121* or Reader for the InputSource.122* @see org.xml.sax.InputSource123*/124public abstract InputSource resolveEntity (String publicId,125String systemId)126throws SAXException, IOException;127128}129130// end of EntityResolver.java131132133