source: icXML/icXML-devel/src/xercesc/parsers/XercesDOMParser.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 10.3 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19*  This file contains code to build the DOM tree. It registers a document
20*  handler with the scanner. In these handler methods, appropriate DOM nodes
21*  are created and added to the DOM tree.
22*
23* $Id: XercesDOMParser.cpp 882548 2009-11-20 13:44:14Z borisk $
24*
25*/
26
27
28
29// ---------------------------------------------------------------------------
30//  Includes
31// ---------------------------------------------------------------------------
32#include <xercesc/sax/EntityResolver.hpp>
33#include <xercesc/sax/ErrorHandler.hpp>
34#include <xercesc/sax/SAXParseException.hpp>
35#include <xercesc/util/IOException.hpp>
36#include <icxercesc/internal/XMLScanner.hpp>
37#include <xercesc/parsers/XercesDOMParser.hpp>
38#include <icxercesc/validators/common/GrammarResolver.hpp>
39#include <icxercesc/framework/XMLGrammarPool.hpp>
40#include <xercesc/framework/XMLSchemaDescription.hpp>
41#include <xercesc/util/Janitor.hpp>
42#include <xercesc/util/OutOfMemoryException.hpp>
43#include <xercesc/util/XMLEntityResolver.hpp>
44
45XERCES_CPP_NAMESPACE_BEGIN
46
47
48
49// ---------------------------------------------------------------------------
50//  XercesDOMParser: Constructors and Destructor
51// ---------------------------------------------------------------------------
52XercesDOMParser::XercesDOMParser( XMLValidator* const   valToAdopt
53                                , MemoryManager* const  manager
54                                , XMLGrammarPool* const gramPool):
55
56AbstractDOMParser(valToAdopt, manager, gramPool)
57, fEntityResolver(0)
58, fXMLEntityResolver(0)
59, fErrorHandler(0)
60{
61}
62
63
64XercesDOMParser::~XercesDOMParser()
65{
66}
67
68
69// ---------------------------------------------------------------------------
70//  XercesDOMParser: Getter methods
71// ---------------------------------------------------------------------------
72bool XercesDOMParser::isCachingGrammarFromParse() const
73{
74    return getScanner()->isCachingGrammarFromParse();
75}
76
77bool XercesDOMParser::isUsingCachedGrammarInParse() const
78{
79    return getScanner()->isUsingCachedGrammarInParse();
80}
81
82Grammar* XercesDOMParser::getGrammar(const XMLCh* const nameSpaceKey)
83{
84    return getGrammarResolver()->getGrammar(nameSpaceKey);
85}
86
87Grammar* XercesDOMParser::getRootGrammar()
88{
89    return getScanner()->getRootGrammar();
90}
91
92const XMLCh* XercesDOMParser::getURIText(unsigned int uriId) const
93{
94    return getScanner()->getURIText(uriId);
95}
96
97XMLFilePos XercesDOMParser::getSrcOffset() const
98{
99    return getScanner()->getSrcOffset();
100}
101
102bool XercesDOMParser::getIgnoreCachedDTD() const
103{
104    return getScanner()->getIgnoreCachedDTD();
105}
106
107// ---------------------------------------------------------------------------
108//  XercesDOMParser: Setter methods
109// ---------------------------------------------------------------------------
110void XercesDOMParser::setErrorHandler(ErrorHandler* const handler)
111{
112    fErrorHandler = handler;
113    XMLScanner* scanner = getScanner();
114    if (fErrorHandler) {
115        scanner->setErrorReporter(this);
116        scanner->setErrorHandler(fErrorHandler);
117    }
118    else {
119        scanner->setErrorReporter(0);
120        scanner->setErrorHandler(0);
121    }
122}
123
124void XercesDOMParser::setEntityResolver(EntityResolver* const handler)
125{
126    fEntityResolver = handler;
127    if (fEntityResolver) {
128        getScanner()->setEntityHandler(this);
129        fXMLEntityResolver = 0;
130    }
131    else {
132        getScanner()->setEntityHandler(0);
133    }
134}
135
136void XercesDOMParser::setXMLEntityResolver(XMLEntityResolver* const handler)
137{
138    fXMLEntityResolver = handler;
139    if (fXMLEntityResolver) {
140        getScanner()->setEntityHandler(this);
141        fEntityResolver = 0;
142    }
143    else {
144        getScanner()->setEntityHandler(0);
145    }
146}
147
148void XercesDOMParser::cacheGrammarFromParse(const bool newState)
149{
150    getScanner()->cacheGrammarFromParse(newState);
151
152    if (newState)
153        getScanner()->useCachedGrammarInParse(newState);
154}
155
156void XercesDOMParser::useCachedGrammarInParse(const bool newState)
157{
158    if (newState || !getScanner()->isCachingGrammarFromParse())
159        getScanner()->useCachedGrammarInParse(newState);
160}
161
162void XercesDOMParser::setIgnoreCachedDTD(const bool newValue)
163{
164    getScanner()->setIgnoredCachedDTD(newValue);
165}
166
167// ---------------------------------------------------------------------------
168//  XercesDOMParser: Utilities
169// ---------------------------------------------------------------------------
170void XercesDOMParser::resetDocumentPool()
171{
172    resetPool();
173}
174
175
176// ---------------------------------------------------------------------------
177//  XercesDOMParser: Implementation of the XMLErrorReporter interface
178// ---------------------------------------------------------------------------
179void XercesDOMParser::error( const   unsigned int
180                             , const XMLCh* const
181                             , const XMLErrorReporter::ErrTypes  errType
182                             , const XMLCh* const                errorText
183                             , const XMLCh* const                systemId
184                             , const XMLCh* const                publicId
185                             , const XMLFileLoc                  lineNum
186                             , const XMLFileLoc                  colNum)
187{
188    SAXParseException toThrow = SAXParseException
189        (
190        errorText
191        , publicId
192        , systemId
193        , lineNum
194        , colNum
195        , getMemoryManager()
196        );
197
198    //
199    //  If there is an error handler registered, call it, otherwise ignore
200    //  all but the fatal errors.
201    //
202    if (!fErrorHandler)
203    {
204        if (errType == XMLErrorReporter::ErrType_Fatal)
205            throw toThrow;
206        return;
207    }
208
209    if (errType == XMLErrorReporter::ErrType_Warning)
210        fErrorHandler->warning(toThrow);
211    else if (errType >= XMLErrorReporter::ErrType_Fatal)
212        fErrorHandler->fatalError(toThrow);
213    else
214        fErrorHandler->error(toThrow);
215}
216
217void XercesDOMParser::resetErrors()
218{
219}
220
221
222// ---------------------------------------------------------------------------
223//  XercesDOMParser: Implementation of XMLEntityHandler interface
224// ---------------------------------------------------------------------------
225InputSource*
226XercesDOMParser::resolveEntity(XMLResourceIdentifier* resourceIdentifier)
227{
228    //
229    //  Just map it to the SAX entity resolver. If there is not one installed,
230    //  return a null pointer to cause the default resolution.
231    //
232    if (fEntityResolver)
233        return fEntityResolver->resolveEntity(resourceIdentifier->getPublicId(),
234                                                resourceIdentifier->getSystemId());
235    if (fXMLEntityResolver)
236        return fXMLEntityResolver->resolveEntity(resourceIdentifier);
237
238    return 0;
239}
240
241typedef JanitorMemFunCall<XercesDOMParser>  ResetParseType;
242
243// ---------------------------------------------------------------------------
244//  XercesDOMParser: Grammar preparsing methods
245// ---------------------------------------------------------------------------
246Grammar* XercesDOMParser::loadGrammar(const char* const systemId,
247                                      const Grammar::GrammarType grammarType,
248                                      const bool toCache)
249{
250    // Avoid multiple entrance
251    if (getParseInProgress())
252        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
253
254    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
255
256    Grammar* grammar = 0;
257
258    try
259    {
260        setParseInProgress(true);
261        if (grammarType == Grammar::DTDGrammarType)
262            getScanner()->setDocTypeHandler(0);
263        grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
264    }
265    catch(const OutOfMemoryException&)
266    {
267        resetParse.release();
268
269        throw;
270    }
271
272    return grammar;
273}
274
275Grammar* XercesDOMParser::loadGrammar(const XMLCh* const systemId,
276                                      const Grammar::GrammarType grammarType,
277                                      const bool toCache)
278{
279    // Avoid multiple entrance
280    if (getParseInProgress())
281        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
282
283    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
284
285    Grammar* grammar = 0;
286
287    try
288    {
289        setParseInProgress(true);
290        if (grammarType == Grammar::DTDGrammarType)
291            getScanner()->setDocTypeHandler(0);
292        grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
293    }
294    catch(const OutOfMemoryException&)
295    {
296        resetParse.release();
297
298        throw;
299    }
300
301    return grammar;
302}
303
304Grammar* XercesDOMParser::loadGrammar(const InputSource& source,
305                                      const Grammar::GrammarType grammarType,
306                                      const bool toCache)
307{
308    // Avoid multiple entrance
309    if (getParseInProgress())
310        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
311
312    ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
313
314    Grammar* grammar = 0;
315
316    try
317    {
318        setParseInProgress(true);
319        if (grammarType == Grammar::DTDGrammarType)
320            getScanner()->setDocTypeHandler(0);
321        grammar = getScanner()->loadGrammar(source, grammarType, toCache);
322    }
323    catch(const OutOfMemoryException&)
324    {
325        resetParse.release();
326
327        throw;
328    }
329
330    return grammar;
331}
332
333void XercesDOMParser::resetParse()
334{
335    if (getScanner()->getDocTypeHandler() == 0)
336    {
337        getScanner()->setDocTypeHandler(this);
338    }
339
340    setParseInProgress(false);
341}
342
343void XercesDOMParser::resetCachedGrammarPool()
344{
345    getGrammarResolver()->resetCachedGrammar();
346    getScanner()->resetCachedGrammar();
347}
348
349XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.