2 * Copyright 2001-2007 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 DOMBuilders 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 #if defined (_MSC_VER)
38 #pragma warning( push )
39 #pragma warning( disable : 4250 4251 )
42 namespace xmltooling {
45 * A thread-safe pool of DOMBuilders that share characteristics.
47 class XMLTOOL_API ParserPool : public xercesc::DOMEntityResolver, xercesc::DOMErrorHandler
49 MAKE_NONCOPYABLE(ParserPool);
52 * Constructs a new pool
54 * @param namespaceAware indicates whether parsers should be namespace-aware or not
55 * @param schemaAware indicates whether parsers should be schema-validating or not
57 ParserPool(bool namespaceAware=true, bool schemaAware=false);
61 * Creates a new document using a parser from this pool.
63 * @return new XML document
66 xercesc::DOMDocument* newDocument();
69 * Parses a document using a pooled parser with the proper settings
71 * @param domsrc A DOM source containing the content to be parsed
72 * @return The DOM document resulting from the parse
73 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
75 xercesc::DOMDocument* parse(xercesc::DOMInputSource& domsrc);
78 * Parses a document using a pooled parser with the proper settings
80 * @param is An input stream 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(std::istream& is);
87 * Load an OASIS catalog file to map schema namespace URIs to filenames.
89 * This does not provide real catalog support; only the <uri> element
90 * is supported to map from a namespace URI to a relative path or file:// URI.
92 * @param pathname path to a catalog file
93 * @return true iff the catalog was successfully processed
95 bool loadCatalog(const XMLCh* pathname);
98 * Load a schema explicitly from a local file.
100 * Note that "successful processing" does not imply that the schema is valid,
101 * only that a reference to it was successfully registered with the pool.
103 * @param nsURI XML namespace to load
104 * @param pathname path to schema file
105 * @return true iff the schema was successfully processed
107 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
110 * Supplies all external entities (primarily schemas) to the parser
112 xercesc::DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);
115 * Handles parsing errors
117 bool handleError(const xercesc::DOMError& e);
120 xercesc::DOMBuilder* createBuilder();
121 xercesc::DOMBuilder* checkoutBuilder();
122 void checkinBuilder(xercesc::DOMBuilder* builder);
125 xstring m_schemaLocations;
126 std::map<xstring,xstring> m_schemaLocMap;
128 std::string m_schemaLocations;
129 std::map<std::string,std::string> m_schemaLocMap;
131 bool m_namespaceAware,m_schemaAware;
132 std::stack<xercesc::DOMBuilder*> m_pool;
134 xercesc::SecurityManager* m_security;
138 * A parser source that wraps a C++ input stream
140 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
142 MAKE_NONCOPYABLE(StreamInputSource);
145 * Constructs an input source around an input stream reference.
147 * @param is reference to an input stream
148 * @param systemId optional system identifier to attach to the stream
150 StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
152 virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
156 * A Xerces input stream that wraps a C++ input stream
158 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
162 * Constructs a Xerces input stream around a C++ input stream reference.
164 * @param is reference to an input stream
166 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
168 virtual unsigned int curPos() const { return m_pos; }
169 virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
181 #if defined (_MSC_VER)
182 #pragma warning( pop )
185 #endif /* __xmltooling_pool_h__ */