source: icXML/icXML-devel/src/xercesc/parsers/DOMLSParserImpl.hpp @ 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: 26.1 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 * $Id: DOMLSParserImpl.hpp 830538 2009-10-28 13:41:11Z amassari $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_DOMBUILDERIMPL_HPP)
23#define XERCESC_INCLUDE_GUARD_DOMBUILDERIMPL_HPP
24
25
26#include <icxercesc/parsers/AbstractDOMParser.hpp>
27#include <xercesc/dom/DOMLSParser.hpp>
28#include <xercesc/dom/DOMLSInput.hpp>
29#include <xercesc/dom/DOMConfiguration.hpp>
30#include <xercesc/util/XercesDefs.hpp>
31#include <xercesc/util/RefVectorOf.hpp>
32#include <icxercesc/util/ValueHashTableOf.hpp>
33
34XERCES_CPP_NAMESPACE_BEGIN
35
36class XMLEntityResolver;
37class XMLResourceIdentifier;
38class DOMStringListImpl;
39class DOMLSResourceResolver;
40
41 /**
42  * Introduced in DOM Level 3
43  *
44  * DOMLSParserImpl provides an implementation of a DOMLSParser interface.
45  * A DOMLSParser instance is obtained from the DOMImplementationLS interface
46  * by invoking its createDOMLSParser method.
47  */
48class PARSERS_EXPORT DOMLSParserImpl : public AbstractDOMParser,
49                                       public DOMLSParser,
50                                       public DOMConfiguration
51{
52public :
53    // -----------------------------------------------------------------------
54    //  Constructors and Detructor
55    // -----------------------------------------------------------------------
56
57    /** @name Constructors and Destructor */
58    //@{
59    /** Construct a DOMLSParserImpl, with an optional validator
60      *
61      * Constructor with an instance of validator class to use for
62      * validation. If you don't provide a validator, a default one will
63      * be created for you in the scanner.
64      *
65      * @param valToAdopt Pointer to the validator instance to use. The
66      *                   parser is responsible for freeing the memory.
67      * @param manager    The memory manager to be used for memory allocations
68      * @param gramPool   Pointer to the grammar pool instance from
69      *                   external application.
70      *                   The parser does NOT own it.
71      *
72      */
73    DOMLSParserImpl
74    (
75          XMLValidator* const   valToAdopt = 0
76        , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
77        , XMLGrammarPool* const gramPool = 0
78    );
79
80    /**
81      * Destructor
82      */
83    virtual ~DOMLSParserImpl();
84
85    //@}
86
87    // -----------------------------------------------------------------------
88    //  Implementation of DOMLSParser interface
89    // -----------------------------------------------------------------------
90    // -----------------------------------------------------------------------
91    //  Getter methods
92    // -----------------------------------------------------------------------
93
94    /** @name Getter methods */
95    //@{
96
97    /**
98      * @see DOMLSParser#getDomConfig
99      */
100    virtual DOMConfiguration* getDomConfig();
101
102    /**
103      * @see DOMLSParser#getFilter
104      */
105    virtual const DOMLSParserFilter* getFilter() const;
106
107    /**
108      * @see DOMLSParser#getAsync
109      */
110    virtual bool getAsync() const;
111
112    /**
113      * @see DOMLSParser#getBusy
114      */
115    virtual bool getBusy() const;
116    //@}
117
118
119    // -----------------------------------------------------------------------
120    //  Setter methods
121    // -----------------------------------------------------------------------
122
123    /** @name Setter methods */
124    //@{
125    /**
126      * @see DOMLSParser#setFilter
127      */
128    virtual void setFilter(DOMLSParserFilter* const filter);
129
130    //@}
131
132    // -----------------------------------------------------------------------
133    //  Parsing methods
134    // -----------------------------------------------------------------------
135    /** @name Parsing methods */
136    //@{
137
138    // -----------------------------------------------------------------------
139    //  Parsing methods
140    // -----------------------------------------------------------------------
141    /**
142      * @see DOMLSParser#parse
143      */
144    virtual DOMDocument* parse(const DOMLSInput* source);
145
146    /**
147      * @see DOMLSParser#parseURI
148      */
149    virtual DOMDocument* parseURI(const XMLCh* const uri);
150
151    /**
152      * @see DOMLSParser#parseURI
153      */
154    virtual DOMDocument* parseURI(const char* const uri);
155
156    /**
157      * @see DOMLSParser#parseWithContext
158      */
159    virtual DOMNode* parseWithContext
160    (
161        const   DOMLSInput*     source
162        ,       DOMNode*        contextNode
163        , const ActionType      action
164    );
165
166    /**
167      * @see DOMLSParser#abort
168      */
169    virtual void abort();
170
171
172    // -----------------------------------------------------------------------
173    //  Non-standard Extension
174    // -----------------------------------------------------------------------
175    /** @name Non-standard Extension */
176    //@{
177
178    /**
179     * Called to indicate that this DOMLSParser is no longer in use
180     * and that the implementation may relinquish any resources associated with it.
181     *
182     */
183    virtual void              release();
184
185    /** Reset the documents vector pool and release all the associated memory
186      * back to the system.
187      *
188      * When parsing a document using a DOM parser, all memory allocated
189      * for a DOM tree is associated to the DOM document.
190      *
191      * If you do multiple parse using the same DOM parser instance, then
192      * multiple DOM documents will be generated and saved in a vector pool.
193      * All these documents (and thus all the allocated memory)
194      * won't be deleted until the parser instance is destroyed.
195      *
196      * If you don't need these DOM documents anymore and don't want to
197      * destroy the DOM parser instance at this moment, then you can call this method
198      * to reset the document vector pool and release all the allocated memory
199      * back to the system.
200      *
201      * It is an error to call this method if you are in the middle of a
202      * parse (e.g. in the mid of a progressive parse).
203      *
204      * @exception IOException An exception from the parser if this function
205      *            is called when a parse is in progress.
206      *
207      */
208    virtual void resetDocumentPool();
209
210    /**
211      * Preparse schema grammar (XML Schema, DTD, etc.) via an input source
212      * object.
213      *
214      * This method invokes the preparsing process on a schema grammar XML
215      * file specified by the DOMLSInput parameter. If the 'toCache' flag
216      * is enabled, the parser will cache the grammars for re-use. If a grammar
217      * key is found in the pool, no caching of any grammar will take place.
218      *
219      * @param source A const reference to the DOMLSInput object which
220      *               points to the schema grammar file to be preparsed.
221      * @param grammarType The grammar type (Schema or DTD).
222      * @param toCache If <code>true</code>, we cache the preparsed grammar,
223      *                otherwise, no chaching. Default is <code>false</code>.
224      * @return The preparsed schema grammar object (SchemaGrammar or
225      *         DTDGrammar). That grammar object is owned by the parser.
226      *
227      * @exception SAXException Any SAX exception, possibly
228      *            wrapping another exception.
229      * @exception XMLException An exception from the parser or client
230      *            handler code.
231      * @exception DOMException A DOM exception as per DOM spec.
232      *
233      * @see DOMLSInput#DOMLSInput
234      */
235    virtual Grammar* loadGrammar(const DOMLSInput* source,
236                             const Grammar::GrammarType grammarType,
237                             const bool toCache = false);
238
239    /**
240      * Preparse schema grammar (XML Schema, DTD, etc.) via a file path or URL
241      *
242      * This method invokes the preparsing process on a schema grammar XML
243      * file specified by the file path parameter. If the 'toCache' flag
244      * is enabled, the parser will cache the grammars for re-use. If a grammar
245      * key is found in the pool, no caching of any grammar will take place.
246      *
247      * @param systemId A const XMLCh pointer to the Unicode string which
248      *                 contains the path to the XML grammar file to be
249      *                 preparsed.
250      * @param grammarType The grammar type (Schema or DTD).
251      * @param toCache If <code>true</code>, we cache the preparsed grammar,
252      *                otherwise, no chaching. Default is <code>false</code>.
253      * @return The preparsed schema grammar object (SchemaGrammar or
254      *         DTDGrammar). That grammar object is owned by the parser.
255      *
256      * @exception SAXException Any SAX exception, possibly
257      *            wrapping another exception.
258      * @exception XMLException An exception from the parser or client
259      *            handler code.
260      * @exception DOMException A DOM exception as per DOM spec.
261      */
262    virtual Grammar* loadGrammar(const XMLCh* const systemId,
263                             const Grammar::GrammarType grammarType,
264                             const bool toCache = false);
265
266    /**
267      * Preparse schema grammar (XML Schema, DTD, etc.) via a file path or URL
268      *
269      * This method invokes the preparsing process on a schema grammar XML
270      * file specified by the file path parameter. If the 'toCache' flag
271      * is enabled, the parser will cache the grammars for re-use. If a grammar
272      * key is found in the pool, no caching of any grammar will take place.
273      *
274      * @param systemId A const char pointer to a native string which contains
275      *                 the path to the XML grammar file to be preparsed.
276      * @param grammarType The grammar type (Schema or DTD).
277      * @param toCache If <code>true</code>, we cache the preparsed grammar,
278      *                otherwise, no chaching. Default is <code>false</code>.
279      * @return The preparsed schema grammar object (SchemaGrammar or
280      *         DTDGrammar). That grammar object is owned by the parser.
281      *
282      * @exception SAXException Any SAX exception, possibly
283      *            wrapping another exception.
284      * @exception XMLException An exception from the parser or client
285      *            handler code.
286      * @exception DOMException A DOM exception as per DOM spec.
287      */
288    virtual Grammar* loadGrammar(const char* const systemId,
289                                 const Grammar::GrammarType grammarType,
290                                 const bool toCache = false);
291
292    /**
293     * Retrieve the grammar that is associated with the specified namespace key
294     *
295     * @param  nameSpaceKey Namespace key
296     * @return Grammar associated with the Namespace key.
297     */
298    virtual Grammar* getGrammar(const XMLCh* const nameSpaceKey) const;
299
300    /**
301     * Retrieve the grammar where the root element is declared.
302     *
303     * @return Grammar where root element declared
304     */
305    virtual Grammar* getRootGrammar() const;
306
307    /**
308     * Returns the string corresponding to a URI id from the URI string pool.
309     *
310     * @param uriId id of the string in the URI string pool.
311     * @return URI string corresponding to the URI id.
312     */
313    virtual const XMLCh* getURIText(unsigned int uriId) const;
314
315    /**
316      * Clear the cached grammar pool
317      */
318    virtual void resetCachedGrammarPool();
319
320    /**
321      * Returns the current src offset within the input source.
322      * To be used only while parsing is in progress.
323      *
324      * @return offset within the input source
325      */
326    virtual XMLFilePos getSrcOffset() const;
327
328    //@}
329
330    // -----------------------------------------------------------------------
331    //  Implementation of the DOMConfiguration interface.
332    // -----------------------------------------------------------------------
333    /** @name Implementation of the DOMConfiguration interface. */
334    //@{
335    /**
336     * Set the value of a parameter.
337     *
338     * @param name The name of the parameter to set.
339     * @param value The new value or null if the user wishes to unset the
340     * parameter. While the type of the value parameter is defined as
341     * <code>DOMUserData</code>, the object type must match the type defined
342     * by the definition of the parameter. For example, if the parameter is
343     * "error-handler", the value must be of type <code>DOMErrorHandler</code>
344     *
345     * @exception DOMException (NOT_SUPPORTED_ERR) Raised when the
346     * parameter name is recognized but the requested value cannot be set.
347     * @exception DOMException (NOT_FOUND_ERR) Raised when the
348     * parameter name is not recognized.
349     *
350     * @since DOM level 3
351     **/
352    virtual void setParameter(const XMLCh* name, const void* value);
353    virtual void setParameter(const XMLCh* name, bool value);
354
355    /**
356     * Return the value of a parameter if known.
357     *
358     * @param name The name of the parameter.
359     * @return The current object associated with the specified parameter or
360     * null if no object has been associated or if the parameter is not
361     * supported.
362     *
363     * @exception DOMException (NOT_FOUND_ERR) Raised when the i
364     * boolean parameter
365     * name is not recognized.
366     *
367     * @since DOM level 3
368     **/
369    virtual const void* getParameter(const XMLCh* name) const;
370
371    /**
372     * Check if setting a parameter to a specific value is supported.
373     *
374     * @param name The name of the parameter to check.
375     * @param value An object. if null, the returned value is true.
376     * @return true if the parameter could be successfully set to the specified
377     * value, or false if the parameter is not recognized or the requested value
378     * is not supported. This does not change the current value of the parameter
379     * itself.
380     *
381     * @since DOM level 3
382     **/
383    virtual bool canSetParameter(const XMLCh* name, const void* value) const;
384    virtual bool canSetParameter(const XMLCh* name, bool value) const;
385
386    /**
387     * The list of the parameters supported by this DOMConfiguration object and
388     * for which at least one value can be set by the application.
389     * Note that this list can also contain parameter names defined outside this specification.
390     *
391     * @return The list of parameters that can be used with setParameter/getParameter
392     * @since DOM level 3
393     **/
394    virtual const DOMStringList* getParameterNames() const;
395    //@}
396
397    // -----------------------------------------------------------------------
398    //  Implementation of the XMLErrorReporter interface.
399    // -----------------------------------------------------------------------
400
401    /** @name Implementation of the XMLErrorReporter interface. */
402    //@{
403
404    /** Handle errors reported from the parser
405      *
406      * This method is used to report back errors found while parsing the
407      * XML file. This method is also borrowed from the SAX specification.
408      * It calls the corresponding user installed Error Handler method:
409      * 'fatal', 'error', 'warning' depending on the severity of the error.
410      * This classification is defined by the XML specification.
411      *
412      * @param errCode An integer code for the error.
413      * @param msgDomain A const pointer to an Unicode string representing
414      *                  the message domain to use.
415      * @param errType An enumeration classifying the severity of the error.
416      * @param errorText A const pointer to an Unicode string representing
417      *                  the text of the error message.
418      * @param systemId  A const pointer to an Unicode string representing
419      *                  the system id of the XML file where this error
420      *                  was discovered.
421      * @param publicId  A const pointer to an Unicode string representing
422      *                  the public id of the XML file where this error
423      *                  was discovered.
424      * @param lineNum   The line number where the error occurred.
425      * @param colNum    The column number where the error occurred.
426      * @see DOMErrorHandler
427      */
428    virtual void error
429    (
430        const   unsigned int                errCode
431        , const XMLCh* const                msgDomain
432        , const XMLErrorReporter::ErrTypes  errType
433        , const XMLCh* const                errorText
434        , const XMLCh* const                systemId
435        , const XMLCh* const                publicId
436        , const XMLFileLoc                  lineNum
437        , const XMLFileLoc                  colNum
438    );
439
440    /** Reset any error data before a new parse
441     *
442      * This method allows the user installed Error Handler callback to
443      * 'reset' itself.
444      *
445      * <b><font color="#FF0000">This method is a no-op for this DOM
446      * implementation.</font></b>
447      */
448    virtual void resetErrors();
449    //@}
450
451
452    // -----------------------------------------------------------------------
453    //  Implementation of the XMLEntityHandler interface.
454    // -----------------------------------------------------------------------
455
456    /** @name Implementation of the XMLEntityHandler interface. */
457    //@{
458
459    /** Handle an end of input source event
460      *
461      * This method is used to indicate the end of parsing of an external
462      * entity file.
463      *
464      * <b><font color="#FF0000">This method is a no-op for this DOM
465      * implementation.</font></b>
466      *
467      * @param inputSource A const reference to the InputSource object
468      *                    which points to the XML file being parsed.
469      * @see InputSource
470      */
471    virtual void endInputSource(const InputSource& inputSource);
472
473    /** Expand a system id
474      *
475      * This method allows an installed XMLEntityHandler to further
476      * process any system id's of enternal entities encountered in
477      * the XML file being parsed, such as redirection etc.
478      *
479      * <b><font color="#FF0000">This method always returns 'false'
480      * for this DOM implementation.</font></b>
481      *
482      * @param systemId  A const pointer to an Unicode string representing
483      *                  the system id scanned by the parser.
484      * @param toFill    A pointer to a buffer in which the application
485      *                  processed system id is stored.
486      * @return 'true', if any processing is done, 'false' otherwise.
487      */
488    virtual bool expandSystemId
489    (
490        const   XMLCh* const    systemId
491        ,       XMLBuffer&      toFill
492    );
493
494    /** Reset any entity handler information
495      *
496      * This method allows the installed XMLEntityHandler to reset
497      * itself.
498      *
499      * <b><font color="#FF0000">This method is a no-op for this DOM
500      * implementation.</font></b>
501      */
502    virtual void resetEntities();
503
504    /** Resolve a public/system id
505      *
506      * This method allows a user installed entity handler to further
507      * process any pointers to external entities. The applications can
508      * implement 'redirection' via this callback.
509      *
510      * @param resourceIdentifier An object containing the type of
511      *        resource to be resolved and the associated data members
512      *        corresponding to this type.
513      * @return The value returned by the user installed resolveEntity
514      *         method or NULL otherwise to indicate no processing was done.
515      *         The returned InputSource is owned by the parser which is
516      *         responsible to clean up the memory.
517      * @see XMLEntityHandler
518      * @see XMLEntityResolver
519      */
520    virtual InputSource* resolveEntity
521    (
522        XMLResourceIdentifier* resourceIdentifier
523    );
524
525    /** Handle a 'start input source' event
526      *
527      * This method is used to indicate the start of parsing an external
528      * entity file.
529      *
530      * <b><font color="#FF0000">This method is a no-op for this DOM parse
531      * implementation.</font></b>
532      *
533      * @param inputSource A const reference to the InputSource object
534      *                    which points to the external entity
535      *                    being parsed.
536      */
537    virtual void startInputSource(const InputSource& inputSource);
538
539    //@}
540
541    // -----------------------------------------------------------------------
542    //  Implementation of the XMLDocumentHandler interface.
543    // -----------------------------------------------------------------------
544    virtual void docCharacters
545    (
546        const   XMLCh* const    chars
547        , const XMLSize_t       length
548        , const bool            cdataSection
549    );
550    virtual void docComment
551    (
552        const   XMLCh* const    comment
553    );
554    virtual void docPI
555    (
556        const   XMLCh* const    target
557        , const XMLCh* const    data
558    );
559    virtual void startEntityReference
560    (
561        const   XMLEntityDecl&  entDecl
562    );
563    virtual void endElement
564    (
565        const   XMLElementDecl& elemDecl
566        , const unsigned int    urlId
567        , const bool            isRoot
568        , const XMLCh* const    elemPrefix
569    );
570    virtual void startElement
571    (
572        const   XMLElementDecl&         elemDecl
573        , const unsigned int            urlId
574        , const XMLCh* const            elemPrefix
575        , const RefVectorOf<XMLAttr>&   attrList
576        , const XMLSize_t               attrCount
577        , const bool                    isEmpty
578        , const bool                    isRoot
579    );
580
581    // overriden callbacks to implement parseWithContext behavior
582    virtual void startDocument();
583    virtual void XMLDecl
584    (
585        const   XMLCh* const    versionStr
586        , const XMLCh* const    encodingStr
587        , const XMLCh* const    standaloneStr
588        , const XMLCh* const    actualEncStr
589    );
590
591
592private :
593    // -----------------------------------------------------------------------
594    //  Initialize/Cleanup methods
595    // -----------------------------------------------------------------------
596    void resetParse();
597
598    // -----------------------------------------------------------------------
599    //  Helper methods
600    // -----------------------------------------------------------------------
601    void applyFilter(DOMNode* node);
602
603    // -----------------------------------------------------------------------
604    //  Private data members
605    //
606    //  fEntityResolver
607    //      The installed DOM entity resolver, if any. Null if none.
608    //
609    //  fXMLEntityResolver
610    //      The installed Xerces entity resolver, if any. Null if none.
611    //
612    //  fErrorHandler
613    //      The installed DOM error handler, if any. Null if none.
614    //
615    //  fFilter
616    //      The installed application filter, if any. Null if none.
617    //
618    //  fCharsetOverridesXMLEncoding
619    //      Indicates if the "charset-overrides-xml-encoding" is set or not
620    //
621    //  fUserAdoptsDocument
622    //      The DOMDocument ownership has been transferred to application
623    //      If set to true, the parser does not own the document anymore
624    //      and thus will not release its memory.
625    //
626    //  fSupportedParameters
627    //      A list of the parameters that can be set, including the ones
628    //      specific of Xerces
629        //
630    //  fFilterAction
631    //      A map of elements rejected by the DOMLSParserFilter::startElement
632    //      callback, used to avoid invoking DOMLSParserFilter::acceptNode
633    //      on its children
634        //
635    //  fFilterDelayedTextNodes
636    //      As text nodes are filled incrementally, store them in a map
637    //      so that we ask DOMLSParserFilter::acceptNode only once, when it
638    //      is completely created
639        //
640    //  fWrapNodesInDocumentFragment
641    //  fWrapNodesContext
642    //  fWrapNodesAction
643    //      Variables used to keep the state for parseWithContext API
644    //
645    //-----------------------------------------------------------------------
646    DOMLSResourceResolver*      fEntityResolver;
647    XMLEntityResolver*          fXMLEntityResolver;
648    DOMErrorHandler*            fErrorHandler;
649    DOMLSParserFilter*          fFilter;
650    bool                        fCharsetOverridesXMLEncoding;
651    bool                        fUserAdoptsDocument;
652    DOMStringListImpl*          fSupportedParameters;
653    ValueHashTableOf<DOMLSParserFilter::FilterAction, PtrHasher>*   fFilterAction;
654    ValueHashTableOf<bool, PtrHasher>*                              fFilterDelayedTextNodes;
655    DOMDocumentFragment*        fWrapNodesInDocumentFragment;
656    DOMNode*                    fWrapNodesContext;
657    ActionType                  fWrapNodesAction;
658
659    // -----------------------------------------------------------------------
660    // Unimplemented constructors and operators
661    // -----------------------------------------------------------------------
662    DOMLSParserImpl(const DOMLSParserImpl &);
663    DOMLSParserImpl & operator = (const DOMLSParserImpl &);
664};
665
666
667
668// ---------------------------------------------------------------------------
669//  DOMLSParserImpl: Handlers for the XMLEntityHandler interface
670// ---------------------------------------------------------------------------
671inline void DOMLSParserImpl::endInputSource(const InputSource&)
672{
673    // The DOM entity resolver doesn't handle this
674}
675
676inline bool DOMLSParserImpl::expandSystemId(const XMLCh* const, XMLBuffer&)
677{
678    // The DOM entity resolver doesn't handle this
679    return false;
680}
681
682inline void DOMLSParserImpl::resetEntities()
683{
684    // Nothing to do on this one
685}
686
687inline void DOMLSParserImpl::startInputSource(const InputSource&)
688{
689    // The DOM entity resolver doesn't handle this
690}
691
692
693// ---------------------------------------------------------------------------
694//  DOMLSParserImpl: Getter methods
695// ---------------------------------------------------------------------------
696inline DOMConfiguration* DOMLSParserImpl::getDomConfig()
697{
698    return this;
699}
700
701inline bool DOMLSParserImpl::getAsync() const
702{
703    // We are a synchronous parser
704    return false;
705}
706
707inline const DOMLSParserFilter* DOMLSParserImpl::getFilter() const
708{
709    return fFilter;
710}
711
712
713XERCES_CPP_NAMESPACE_END
714
715#endif
Note: See TracBrowser for help on using the repository browser.