2 * Copyright 2001-2006 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 using namespace xercesc;
38 #if defined (_MSC_VER)
39 #pragma warning( push )
40 #pragma warning( disable : 4250 4251 )
43 namespace xmltooling {
46 * A thread-safe pool of DOMBuilders that share characteristics
48 class XMLTOOL_API ParserPool : public DOMEntityResolver, DOMErrorHandler
50 MAKE_NONCOPYABLE(ParserPool);
53 * Constructs a new pool
55 * @param namespaceAware indicates whether parsers should be namespace-aware or not
56 * @param schemaAware indicates whether parsers should be schema-validating or not
58 ParserPool(bool namespaceAware=true, bool schemaAware=false);
62 * Creates a new document using a parser from this pool.
64 * @return new XML document
67 DOMDocument* newDocument();
70 * Parses a document using a pooled parser with the proper settings
72 * @param domsrc A DOM source containing the content to be parsed
73 * @return The DOM document resulting from the parse
74 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
76 DOMDocument* parse(DOMInputSource& domsrc);
79 * Parses a document using a pooled parser with the proper settings
81 * @param is An input stream containing the content to be parsed
82 * @return The DOM document resulting from the parse
83 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
85 DOMDocument* parse(std::istream& is);
88 * Load an OASIS catalog file to map schema namespace URIs to filenames.
90 * This does not provide real catalog support; only the <uri> element
91 * is supported to map from a namespace URI to a relative path or file:// URI.
93 * @param pathname path to a catalog file
94 * @return true iff the catalog was successfully processed
96 bool loadCatalog(const XMLCh* pathname);
99 * Load a schema explicitly from a local file.
101 * Note that "successful processing" does not imply that the schema is valid,
102 * only that a reference to it was successfully registered with the pool.
104 * @param nsURI XML namespace to load
105 * @param pathname path to schema file
106 * @return true iff the schema was successfully processed
108 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
111 * Supplies all external entities (primarily schemas) to the parser
113 DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);
116 * Handles parsing errors
118 bool handleError(const DOMError& e);
121 DOMBuilder* createBuilder();
122 DOMBuilder* checkoutBuilder();
123 void checkinBuilder(DOMBuilder* builder);
126 xstring m_schemaLocations;
127 std::map<xstring,xstring> m_schemaLocMap;
129 std::string m_schemaLocations;
130 std::map<std::string,std::string> m_schemaLocMap;
132 bool m_namespaceAware,m_schemaAware;
133 std::stack<DOMBuilder*> m_pool;
138 * A parser source that wraps a C++ input stream
140 class XMLTOOL_API StreamInputSource : public 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) : InputSource(systemId), m_is(is) {}
152 virtual 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 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__ */