2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * @file ParserPool.h
\r
23 #if !defined(__xmltooling_pool_h__)
\r
24 #define __xmltooling_pool_h__
\r
26 #include <xmltooling/unicode.h>
\r
31 #include <xercesc/dom/DOM.hpp>
\r
32 #include <xercesc/sax/InputSource.hpp>
\r
33 #include <xercesc/util/BinInputStream.hpp>
\r
35 using namespace xercesc;
\r
37 #if defined (_MSC_VER)
\r
38 #pragma warning( push )
\r
39 #pragma warning( disable : 4250 4251 )
\r
42 namespace xmltooling {
\r
45 * A thread-safe pool of DOMBuilders that share characteristics
\r
47 class XMLTOOL_API ParserPool : public DOMEntityResolver, DOMErrorHandler
\r
49 MAKE_NONCOPYABLE(ParserPool);
\r
52 * Constructs a new pool
\r
54 * @param namespaceAware indicates whether parsers should be namespace-aware or not
\r
55 * @param schemaAware indicates whether parsers should be schema-validating or not
\r
57 ParserPool(bool namespaceAware=true, bool schemaAware=false);
\r
61 * Creates a new document using a parser from this pool.
\r
63 * @return new XML document
\r
66 DOMDocument* newDocument();
\r
69 * Parses a document using a pooled parser with the proper settings
\r
71 * @param domsrc A DOM source containing the content to be parsed
\r
72 * @return The DOM document resulting from the parse
\r
73 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
\r
75 DOMDocument* parse(DOMInputSource& domsrc);
\r
78 * Parses a document using a pooled parser with the proper settings
\r
80 * @param is An input stream containing the content to be parsed
\r
81 * @return The DOM document resulting from the parse
\r
82 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
\r
84 DOMDocument* parse(std::istream& is);
\r
87 * Load an OASIS catalog file to map schema namespace URIs to filenames.
\r
89 * This does not provide real catalog support; only the <uri> element
\r
90 * is supported to map from a namespace URI to a relative path or file:// URI.
\r
92 * @param pathname path to a catalog file
\r
93 * @return true iff the catalog was successfully processed
\r
95 bool loadCatalog(const XMLCh* pathname);
\r
98 * Load a schema explicitly from a local file.
\r
100 * Note that "successful processing" does not imply that the schema is valid,
\r
101 * only that a reference to it was successfully registered with the pool.
\r
103 * @param nsURI XML namespace to load
\r
104 * @param pathname path to schema file
\r
105 * @return true iff the schema was successfully processed
\r
107 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
\r
110 * Supplies all external entities (primarily schemas) to the parser
\r
112 DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);
\r
115 * Handles parsing errors
\r
117 bool handleError(const DOMError& e);
\r
120 DOMBuilder* createBuilder();
\r
121 DOMBuilder* checkoutBuilder();
\r
122 void checkinBuilder(DOMBuilder* builder);
\r
124 #ifdef HAVE_GOOD_STL
\r
125 xstring m_schemaLocations;
\r
126 std::map<xstring,xstring> m_schemaLocMap;
\r
128 std::string m_schemaLocations;
\r
129 std::map<std::string,std::string> m_schemaLocMap;
\r
131 bool m_namespaceAware,m_schemaAware;
\r
132 std::stack<DOMBuilder*> m_pool;
\r
137 * A parser source that wraps a C++ input stream
\r
139 class XMLTOOL_API StreamInputSource : public InputSource
\r
141 MAKE_NONCOPYABLE(StreamInputSource);
\r
144 * Constructs an input source around an input stream reference.
\r
146 * @param is reference to an input stream
\r
147 * @param systemId optional system identifier to attach to the stream
\r
149 StreamInputSource(std::istream& is, const char* systemId=NULL) : InputSource(systemId), m_is(is) {}
\r
151 virtual BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
\r
156 std::istream& m_is;
\r
158 class XMLTOOL_API StreamBinInputStream : public BinInputStream
\r
161 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
\r
162 virtual unsigned int curPos() const { return m_pos; }
\r
163 virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
\r
165 std::istream& m_is;
\r
166 unsigned int m_pos;
\r
171 #if defined (_MSC_VER)
\r
172 #pragma warning( pop )
\r
175 #endif /* __xmltooling_pool_h__ */
\r