| 1 | /* |
|---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
|---|
| 4 | * this work for additional information regarding copyright ownership. |
|---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|---|
| 6 | * (the "License"); you may not use this file except in compliance with |
|---|
| 7 | * the License. You may obtain a copy of the License at |
|---|
| 8 | * |
|---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 10 | * |
|---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 14 | * See the License for the specific language governing permissions and |
|---|
| 15 | * limitations under the License. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | /* |
|---|
| 19 | * $Id: XMLEntityResolver.hpp 568078 2007-08-21 11:43:25Z amassari $ |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #ifndef XMLENTITYRESOLVER_HPP |
|---|
| 23 | #define XMLENTITYRESOLVER_HPP |
|---|
| 24 | |
|---|
| 25 | #include <xercesc/util/XercesDefs.hpp> |
|---|
| 26 | #include <xercesc/util/XMemory.hpp> |
|---|
| 27 | #include <xercesc/util/XMLResourceIdentifier.hpp> |
|---|
| 28 | |
|---|
| 29 | XERCES_CPP_NAMESPACE_BEGIN |
|---|
| 30 | |
|---|
| 31 | class InputSource; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Revised interface for resolving entities. |
|---|
| 35 | * |
|---|
| 36 | * <p>If an application needs to implement customized handling |
|---|
| 37 | * for external entities, it can implement this interface and |
|---|
| 38 | * register an instance with the parser using the parser's |
|---|
| 39 | * setXMLEntityResolver method or it can use the basic SAX interface |
|---|
| 40 | * (EntityResolver). The difference between the two interfaces is |
|---|
| 41 | * the arguments to the resolveEntity() method. With the SAX |
|---|
| 42 | * EntityResolve the arguments are systemId and publicId. With this |
|---|
| 43 | * interface the argument is a XMLResourceIdentifier object. <i>Only |
|---|
| 44 | * one EntityResolver can be set using setEntityResolver() or |
|---|
| 45 | * setXMLEntityResolver, if both are set the last one set is |
|---|
| 46 | * used.</i></p> |
|---|
| 47 | * |
|---|
| 48 | * <p>The parser will then allow the application to intercept any |
|---|
| 49 | * external entities (including the external DTD subset and external |
|---|
| 50 | * parameter entities, if any) before including them.</p> |
|---|
| 51 | * |
|---|
| 52 | * <p>Many applications will not need to implement this interface, |
|---|
| 53 | * but it will be especially useful for applications that build |
|---|
| 54 | * XML documents from databases or other specialised input sources, |
|---|
| 55 | * or for applications that use URI types other than URLs.</p> |
|---|
| 56 | * |
|---|
| 57 | * <p>The following resolver would provide the application |
|---|
| 58 | * with a special character stream for the entity with the system |
|---|
| 59 | * identifier "http://www.myhost.com/today":</p> |
|---|
| 60 | * |
|---|
| 61 | *<code> |
|---|
| 62 | * #include <xercesc/util/XMLEntityResolver.hpp><br> |
|---|
| 63 | * #include <xercesc/sax/InputSource.hpp><br> |
|---|
| 64 | *<br> |
|---|
| 65 | * class MyResolver : public XMLEntityResolver {<br> |
|---|
| 66 | * public:<br> |
|---|
| 67 | * InputSource resolveEntity (XMLResourceIdentifier* xmlri);<br> |
|---|
| 68 | * ...<br> |
|---|
| 69 | * };<br> |
|---|
| 70 | *<br> |
|---|
| 71 | * MyResolver::resolveEntity(XMLResourceIdentifier* xmlri) {<br> |
|---|
| 72 | * switch(xmlri->getResourceIdentifierType()) {<br> |
|---|
| 73 | * case XMLResourceIdentifier::SystemId:<br> |
|---|
| 74 | * if (XMLString::compareString(xmlri->getSystemId(), "http://www.myhost.com/today")) {<br> |
|---|
| 75 | * MyReader* reader = new MyReader();<br> |
|---|
| 76 | * return new InputSource(reader);<br> |
|---|
| 77 | * } else {<br> |
|---|
| 78 | * return null;<br> |
|---|
| 79 | * }<br> |
|---|
| 80 | * break;<br> |
|---|
| 81 | * default:<br> |
|---|
| 82 | * return null;<br> |
|---|
| 83 | * }<br> |
|---|
| 84 | * }</code> |
|---|
| 85 | * |
|---|
| 86 | * <p>The application can also use this interface to redirect system |
|---|
| 87 | * identifiers to local URIs or to look up replacements in a catalog |
|---|
| 88 | * (possibly by using the public identifier).</p> |
|---|
| 89 | * |
|---|
| 90 | * <p>The HandlerBase class implements the default behaviour for |
|---|
| 91 | * this interface, which is simply always to return null (to request |
|---|
| 92 | * that the parser use the default system identifier).</p> |
|---|
| 93 | * |
|---|
| 94 | * @see XMLResourceIdentifier |
|---|
| 95 | * @see Parser#setXMLEntityResolver |
|---|
| 96 | * @see InputSource#InputSource |
|---|
| 97 | * @see HandlerBase#HandlerBase |
|---|
| 98 | */ |
|---|
| 99 | class XMLUTIL_EXPORT XMLEntityResolver |
|---|
| 100 | { |
|---|
| 101 | public: |
|---|
| 102 | /** @name Constructors and Destructor */ |
|---|
| 103 | //@{ |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** Destructor */ |
|---|
| 107 | virtual ~XMLEntityResolver() |
|---|
| 108 | { |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | //@} |
|---|
| 112 | |
|---|
| 113 | /** @name The XMLEntityResolver interface */ |
|---|
| 114 | //@{ |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Allow the application to resolve external entities. |
|---|
| 118 | * |
|---|
| 119 | * <p>The Parser will call this method before opening any external |
|---|
| 120 | * entity except the top-level document entity (including the |
|---|
| 121 | * external DTD subset, external entities referenced within the |
|---|
| 122 | * DTD, and external entities referenced within the document |
|---|
| 123 | * element): the application may request that the parser resolve |
|---|
| 124 | * the entity itself, that it use an alternative URI, or that it |
|---|
| 125 | * use an entirely different input source.</p> |
|---|
| 126 | * |
|---|
| 127 | * <p>Application writers can use this method to redirect external |
|---|
| 128 | * system identifiers to secure and/or local URIs, to look up |
|---|
| 129 | * public identifiers in a catalogue, or to read an entity from a |
|---|
| 130 | * database or other input source (including, for example, a dialog |
|---|
| 131 | * box).</p> |
|---|
| 132 | * |
|---|
| 133 | * <p>If the system identifier is a URL, the SAX parser must |
|---|
| 134 | * resolve it fully before reporting it to the application.</p> |
|---|
| 135 | * |
|---|
| 136 | * @param resourceIdentifier An object containing the type of |
|---|
| 137 | * resource to be resolved and the associated data members |
|---|
| 138 | * corresponding to this type. |
|---|
| 139 | * @return An InputSource object describing the new input source, |
|---|
| 140 | * or null to request that the parser open a regular |
|---|
| 141 | * URI connection to the system identifier. |
|---|
| 142 | * The returned InputSource is owned by the parser which is |
|---|
| 143 | * responsible to clean up the memory. |
|---|
| 144 | * @exception SAXException Any SAX exception, possibly |
|---|
| 145 | * wrapping another exception. |
|---|
| 146 | * @exception IOException An IO exception, |
|---|
| 147 | * possibly the result of creating a new InputStream |
|---|
| 148 | * or Reader for the InputSource. |
|---|
| 149 | * |
|---|
| 150 | * @see InputSource#InputSource |
|---|
| 151 | * @see XMLResourceIdentifier |
|---|
| 152 | */ |
|---|
| 153 | virtual InputSource* resolveEntity |
|---|
| 154 | ( |
|---|
| 155 | XMLResourceIdentifier* resourceIdentifier |
|---|
| 156 | ) = 0; |
|---|
| 157 | |
|---|
| 158 | //@} |
|---|
| 159 | protected: |
|---|
| 160 | /** Default Constructor */ |
|---|
| 161 | XMLEntityResolver() |
|---|
| 162 | { |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | private : |
|---|
| 166 | /* Unimplemented constructors and operators */ |
|---|
| 167 | |
|---|
| 168 | /* Copy constructor */ |
|---|
| 169 | XMLEntityResolver(const XMLEntityResolver&); |
|---|
| 170 | |
|---|
| 171 | /* Assignment operator */ |
|---|
| 172 | XMLEntityResolver& operator=(const XMLEntityResolver&); |
|---|
| 173 | |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | XERCES_CPP_NAMESPACE_END |
|---|
| 177 | |
|---|
| 178 | #endif |
|---|