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.
23 #if !defined(__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>
36 #if defined (_MSC_VER)
37 #pragma warning( push )
38 #pragma warning( disable : 4250 4251 )
41 namespace xmltooling {
44 * A thread-safe pool of DOMBuilders that share characteristics
46 class XMLTOOL_API ParserPool : public xercesc::DOMEntityResolver, xercesc::DOMErrorHandler
48 MAKE_NONCOPYABLE(ParserPool);
51 * Constructs a new pool
53 * @param namespaceAware indicates whether parsers should be namespace-aware or not
54 * @param schemaAware indicates whether parsers should be schema-validating or not
56 ParserPool(bool namespaceAware=true, bool schemaAware=false);
60 * Creates a new document using a parser from this pool.
62 * @return new XML document
65 xercesc::DOMDocument* newDocument();
68 * Parses a document using a pooled parser with the proper settings
70 * @param domsrc A DOM source containing the content to be parsed
71 * @return The DOM document resulting from the parse
72 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
74 xercesc::DOMDocument* parse(xercesc::DOMInputSource& domsrc);
77 * Parses a document using a pooled parser with the proper settings
79 * @param is An input stream containing the content to be parsed
80 * @return The DOM document resulting from the parse
81 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
83 xercesc::DOMDocument* parse(std::istream& is);
86 * Load an OASIS catalog file to map schema namespace URIs to filenames.
88 * This does not provide real catalog support; only the <uri> element
89 * is supported to map from a namespace URI to a relative path or file:// URI.
91 * @param pathname path to a catalog file
92 * @return true iff the catalog was successfully processed
94 bool loadCatalog(const XMLCh* pathname);
97 * Load a schema explicitly from a local file.
99 * Note that "successful processing" does not imply that the schema is valid,
100 * only that a reference to it was successfully registered with the pool.
102 * @param nsURI XML namespace to load
103 * @param pathname path to schema file
104 * @return true iff the schema was successfully processed
106 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
109 * Supplies all external entities (primarily schemas) to the parser
111 xercesc::DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);
114 * Handles parsing errors
116 bool handleError(const xercesc::DOMError& e);
119 xercesc::DOMBuilder* createBuilder();
120 xercesc::DOMBuilder* checkoutBuilder();
121 void checkinBuilder(xercesc::DOMBuilder* builder);
124 xstring m_schemaLocations;
125 std::map<xstring,xstring> m_schemaLocMap;
127 std::string m_schemaLocations;
128 std::map<std::string,std::string> m_schemaLocMap;
130 bool m_namespaceAware,m_schemaAware;
131 std::stack<xercesc::DOMBuilder*> m_pool;
136 * A parser source that wraps a C++ input stream
138 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
140 MAKE_NONCOPYABLE(StreamInputSource);
143 * Constructs an input source around an input stream reference.
145 * @param is reference to an input stream
146 * @param systemId optional system identifier to attach to the stream
148 StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
150 virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
154 * A Xerces input stream that wraps a C++ input stream
156 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
160 * Constructs a Xerces input stream around a C++ input stream reference.
162 * @param is reference to an input stream
164 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
166 virtual unsigned int curPos() const { return m_pos; }
167 virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
179 #if defined (_MSC_VER)
180 #pragma warning( pop )
183 #endif /* __xmltooling_pool_h__ */