2 * Copyright 2001-2008 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file xmltooling/util/ParserPool.h
20 * A thread-safe pool of parsers that share characteristics.
23 #ifndef __xmltooling_pool_h__
24 #define __xmltooling_pool_h__
26 #include <xmltooling/unicode.h>
27 #include <xmltooling/util/Threads.h>
32 #include <xercesc/dom/DOM.hpp>
33 #include <xercesc/sax/InputSource.hpp>
34 #include <xercesc/util/BinInputStream.hpp>
35 #include <xercesc/util/SecurityManager.hpp>
37 #ifndef XMLTOOLING_NO_XMLSEC
\r
38 # include <xsec/framework/XSECDefs.hpp>
\r
41 #if defined (_MSC_VER)
42 #pragma warning( push )
43 #pragma warning( disable : 4250 4251 )
46 namespace xmltooling {
49 * A thread-safe pool of DOMBuilders that share characteristics.
51 class XMLTOOL_API ParserPool : xercesc::DOMErrorHandler,
52 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
53 public xercesc::DOMLSResourceResolver
55 public xercesc::DOMEntityResolver
58 MAKE_NONCOPYABLE(ParserPool);
61 * Constructs a new pool
63 * @param namespaceAware indicates whether parsers should be namespace-aware or not
64 * @param schemaAware indicates whether parsers should be schema-validating or not
66 ParserPool(bool namespaceAware=true, bool schemaAware=false);
70 * Creates a new document using a parser from this pool.
72 * @return new XML document
75 xercesc::DOMDocument* newDocument();
78 * Parses a document using a pooled parser with the proper settings
80 * @param domsrc An input source containing the content to be parsed
81 * @return The DOM document resulting from the parse
82 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
84 xercesc::DOMDocument* parse(
85 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
86 xercesc::DOMLSInput& domsrc
88 xercesc::DOMInputSource& domsrc
93 * Parses a document using a pooled parser with the proper settings
95 * @param is An input stream containing the content to be parsed
96 * @return The DOM document resulting from the parse
97 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
99 xercesc::DOMDocument* parse(std::istream& is);
102 * Load an OASIS catalog file to map schema namespace URIs to filenames.
104 * This does not provide real catalog support; only the <uri> element
105 * is supported to map from a namespace URI to a relative path or file:// URI.
107 * @param pathname path to a catalog file
108 * @return true iff the catalog was successfully processed
110 bool loadCatalog(const XMLCh* pathname);
113 * Load a schema explicitly from a local file.
115 * Note that "successful processing" does not imply that the schema is valid,
116 * only that a reference to it was successfully registered with the pool.
118 * @param nsURI XML namespace to load
119 * @param pathname path to schema file
120 * @return true iff the schema was successfully processed
122 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
125 * Supplies all external entities (primarily schemas) to the parser
127 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
128 xercesc::DOMLSInput* resolveResource(
129 const XMLCh *const resourceType,
130 const XMLCh *const namespaceUri,
131 const XMLCh *const publicId,
132 const XMLCh *const systemId,
133 const XMLCh *const baseURI
136 xercesc::DOMInputSource* resolveEntity(
137 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
142 * Handles parsing errors
144 bool handleError(const xercesc::DOMError& e);
147 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
148 xercesc::DOMLSParser* createBuilder();
149 xercesc::DOMLSParser* checkoutBuilder();
150 void checkinBuilder(xercesc::DOMLSParser* builder);
152 xercesc::DOMBuilder* createBuilder();
153 xercesc::DOMBuilder* checkoutBuilder();
154 void checkinBuilder(xercesc::DOMBuilder* builder);
158 xstring m_schemaLocations;
159 std::map<xstring,xstring> m_schemaLocMap;
161 std::string m_schemaLocations;
162 std::map<std::string,std::string> m_schemaLocMap;
164 bool m_namespaceAware,m_schemaAware;
165 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
166 std::stack<xercesc::DOMLSParser*> m_pool;
168 std::stack<xercesc::DOMBuilder*> m_pool;
171 xercesc::SecurityManager* m_security;
175 * A parser source that wraps a C++ input stream
177 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
179 MAKE_NONCOPYABLE(StreamInputSource);
182 * Constructs an input source around an input stream reference.
184 * @param is reference to an input stream
185 * @param systemId optional system identifier to attach to the stream
187 StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
189 virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
193 * A Xerces input stream that wraps a C++ input stream
195 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
199 * Constructs a Xerces input stream around a C++ input stream reference.
201 * @param is reference to an input stream
203 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
205 #ifdef XMLTOOLING_XERCESC_64BITSAFE
210 curPos() const { return m_pos; }
211 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
212 #ifdef XMLTOOLING_XERCESC_64BITSAFE
213 const XMLCh* getContentType() const { return NULL; }
226 #if defined (_MSC_VER)
227 #pragma warning( pop )
230 #endif /* __xmltooling_pool_h__ */