source: NonGTP/Xerces/xerces/include/xercesc/framework/XMLValidator.hpp @ 358

Revision 358, 17.9 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2004 The Apache Software Foundation.
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 /*
18  * $Log: XMLValidator.hpp,v $
19  * Revision 1.10  2004/09/08 13:55:59  peiyongz
20  * Apache License Version 2.0
21  *
22  * Revision 1.9  2003/05/15 18:26:07  knoaman
23  * Partial implementation of the configurable memory manager.
24  *
25  * Revision 1.8  2003/03/07 18:08:10  tng
26  * Return a reference instead of void for operator=
27  *
28  * Revision 1.7  2002/11/07 21:59:22  tng
29  * Pass elemDecl to XMLValidator::validateAttrValue so that we can include element name in error message.
30  *
31  * Revision 1.6  2002/11/04 15:00:21  tng
32  * C++ Namespace Support.
33  *
34  * Revision 1.5  2002/09/04 18:17:55  tng
35  * Do not set IDREF to used during prevalidation.
36  *
37  * Revision 1.4  2002/08/20 16:54:11  tng
38  * [Bug 6251] Info during compilation.
39  *
40  * Revision 1.3  2002/07/11 18:55:44  knoaman
41  * Add a flag to the preContentValidation method to indicate whether to validate
42  * default/fixed attributes or not.
43  *
44  * Revision 1.2  2002/02/20 18:17:01  tng
45  * [Bug 5977] Warnings on generating apiDocs.
46  *
47  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
48  * sane_include
49  *
50  * Revision 1.17  2001/11/30 22:18:18  peiyongz
51  * cleanUp function made member function
52  * cleanUp object moved to file scope
53  * double mutex lock removed
54  *
55  * Revision 1.16  2001/11/13 13:24:46  tng
56  * Deprecate function XMLValidator::checkRootElement.
57  *
58  * Revision 1.15  2001/10/24 23:46:52  peiyongz
59  * [Bug 4342] fix the leak.
60  *
61  * Revision 1.14  2001/06/05 16:51:17  knoaman
62  * Add 'const' to getGrammar - submitted by Peter A. Volchek.
63  *
64  * Revision 1.13  2001/05/11 13:25:33  tng
65  * Copyright update.
66  *
67  * Revision 1.12  2001/05/03 20:34:22  tng
68  * Schema: SchemaValidator update
69  *
70  * Revision 1.11  2001/04/19 18:16:53  tng
71  * Schema: SchemaValidator update, and use QName in Content Model
72  *
73  * Revision 1.10  2001/03/21 21:56:03  tng
74  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
75  *
76  * Revision 1.9  2001/02/26 19:21:33  tng
77  * Schema: add parameter prefix in findElem and findAttr.
78  *
79  * Revision 1.8  2000/12/14 18:49:58  tng
80  * Fix API document generation warning: "Warning: end of member group without matching begin"
81  *
82  * Revision 1.7  2000/08/09 22:09:09  jpolast
83  * added const XMLCh* getURIText()
84  * allows parsers to use const URIs instead of appending
85  * to a XMLBuffer.
86  *
87  * Revision 1.6  2000/03/02 19:54:25  roddey
88  * This checkin includes many changes done while waiting for the
89  * 1.1.0 code to be finished. I can't list them all here, but a list is
90  * available elsewhere.
91  *
92  * Revision 1.5  2000/02/24 20:00:24  abagchi
93  * Swat for removing Log from API docs
94  *
95  * Revision 1.4  2000/02/15 23:59:07  roddey
96  * More updated documentation of Framework classes.
97  *
98  * Revision 1.3  2000/02/15 01:21:31  roddey
99  * Some initial documentation improvements. More to come...
100  *
101  * Revision 1.2  2000/02/06 07:47:49  rahulj
102  * Year 2K copyright swat.
103  *
104  * Revision 1.1.1.1  1999/11/09 01:08:38  twl
105  * Initial checkin
106  *
107  * Revision 1.4  1999/11/08 20:44:41  rahul
108  * Swat for adding in Product name and CVS comment log variable.
109  *
110  */
111
112
113#if !defined(XMLVALIDATOR_HPP)
114#define XMLVALIDATOR_HPP
115
116#include <xercesc/framework/XMLAttr.hpp>
117#include <xercesc/framework/XMLValidityCodes.hpp>
118
119XERCES_CPP_NAMESPACE_BEGIN
120
121class ReaderMgr;
122class XMLBufferMgr;
123class XMLElementDecl;
124class XMLScanner;
125class Grammar;
126
127
128/**
129 *  This abstract class provides the interface for all validators. This is
130 *  the simple amount of API that all validators must honor, in order for
131 *  the scanner to use them to do validation. All validators will actually
132 *  contain much more functionality than is accessible via this common API,
133 *  but that functionality requires that you know what type of validator you
134 *  are dealing with.
135 *
136 *  Basically, at this level, the primary concern is to be able to query
137 *  core information about elements and attributes. Adding decls to the
138 *  validator requires that you go through the derived interface because they
139 *  all have their own decl types. At this level, we can return information
140 *  via the base decl classes, from which each validator derives its own
141 *  decl classes.
142 */
143class XMLPARSER_EXPORT XMLValidator : public XMemory
144{
145public:
146    // -----------------------------------------------------------------------
147    //  Constructors are hidden, just the virtual destructor is exposed
148    // -----------------------------------------------------------------------
149
150    /** @name Destructor */
151    //@{
152
153    /**
154     *  The derived class should clean up its allocated data, then this class
155     *  will do the same for data allocated at this level.
156     */
157    virtual ~XMLValidator()
158    {
159    }
160    //@}
161
162
163    // -----------------------------------------------------------------------
164    //  The virtual validator interface
165    // -----------------------------------------------------------------------
166
167    /** @name Virtual validator interface */
168    //@{
169
170    /**
171      * The derived class should look up its declaration of the passed element
172      * from its element pool. It should then use the content model description
173      * contained in that element declaration to validate that the passed list
174      * of child elements are valid for that content model. The count can be
175      * zero, indicating no child elements.
176      *
177      * Note that whitespace and text content are not validated here. Those are
178      * handled by the scanner. So only element ids are provided here.
179      *
180      * @param  elemDecl    The element whose content is to be checked.
181      *
182      * @param  children    An array of element QName which represent the elements
183      *                     found within the parent element, i.e. the content
184      *                     to be validated.
185      *
186      * @param  childCount  The number of elements in the childIds array. It can
187      *                     be zero if the element had none.
188      */
189    virtual int checkContent
190    (
191        XMLElementDecl* const   elemDecl
192        , QName** const         children
193        , const unsigned int    childCount
194    ) = 0;
195
196    /**
197      * The derived class should fault in the passed XMLAttr value. It should
198      * use the passeed attribute definition (which is passed via the base
199      * type so it must often be downcast to the appropriate type for the
200      * derived validator class), to fill in the passed attribute. This is done
201      * as a performance enhancement since the derived class has more direct
202      * access to the information.
203      */
204    virtual void faultInAttr
205    (
206                XMLAttr&    toFill
207        , const XMLAttDef&  attDef
208    )   const = 0;
209
210    /**
211      * This method is called by the scanner after a Grammar is scanned.
212      */
213    virtual void preContentValidation(bool reuseGrammar,
214                                      bool validateDefAttr = false) = 0;
215
216    /**
217      * This method is called by the scanner after the parse has completed. It
218      * gives the validator a chance to check certain things that can only be
219      * checked after the whole document has been parsed, such as referential
220      * integrity of ID/IDREF pairs and so forth. The validator should just
221      * issue errors for any problems it finds.
222      */
223    virtual void postParseValidation() = 0;
224
225    /**
226      * This method is called by the scanner before a new document is about
227      * to start. It gives the validator a change to reset itself in preperation
228      * for another validation pass.
229      */
230    virtual void reset() = 0;
231
232    /**
233      * The derived class should return a boolean that indicates whether it
234      * requires namespace processing or not. Some do and some allow it to be
235      * optional. This flag is used to control whether the client code's
236      * requests to disable namespace processing can be honored or not.
237      */
238    virtual bool requiresNamespaces() const = 0;
239
240    /**
241      * The derived class should apply any rules to the passed attribute value
242      * that are above and beyond those defined by XML 1.0. The scanner itself
243      * will impose XML 1.0 rules, based on the type of the attribute. This
244      * will generally be used to check things such as range checks and other
245      * datatype related validation.
246      *
247      * If the value breaks any rules as defined by the derived class, it
248      * should just issue errors as usual.
249      */
250    virtual void validateAttrValue
251    (
252        const   XMLAttDef*                  attDef
253        , const XMLCh* const                attrValue
254        , bool                              preValidation = false
255        , const XMLElementDecl*             elemDecl = 0
256    ) = 0;
257
258    /**
259      * The derived class should apply any rules to the passed element decl
260      * that are above and beyond those defined by XML 1.0.
261      *
262      * If the value breaks any rules as defined by the derived class, it
263      * should just issue errors as usual.
264      */
265    virtual void validateElement
266    (
267        const   XMLElementDecl*             elemDef
268    ) = 0;
269
270    /**
271      * Retrieve the Grammar used
272      */
273    virtual Grammar* getGrammar() const =0;
274
275    /**
276      * Set the Grammar
277      */
278    virtual void setGrammar(Grammar* aGrammar) =0;
279
280
281    //@}
282
283    // -----------------------------------------------------------------------
284    //  Virtual DTD handler interface.
285    // -----------------------------------------------------------------------
286
287    /** @name Virtual DTD handler interface */
288    //@{
289
290    /**
291      * This method allows the scanner to ask the validator if it handles
292      * DTDs or not.
293      */
294    virtual bool handlesDTD() const = 0;
295
296    // -----------------------------------------------------------------------
297    //  Virtual Schema handler interface.
298    // -----------------------------------------------------------------------
299
300    /** @name Virtual Schema handler interface */
301
302    /**
303      * This method allows the scanner to ask the validator if it handles
304      * Schema or not.
305      */
306    virtual bool handlesSchema() const = 0;
307
308    //@}
309
310    // -----------------------------------------------------------------------
311    //  Setter methods
312    //
313    //  setScannerInfo() is called by the scanner to tell the validator
314    //  about the stuff it needs to have access to.
315    // -----------------------------------------------------------------------
316
317    /** @name Setter methods */
318    //@{
319
320    /**
321      * @param  owningScanner   This is a pointer to the scanner to which the
322      *                         validator belongs. The validator will often
323      *                         need to query state data from the scanner.
324      *
325      * @param  readerMgr       This is a pointer to the reader manager that is
326      *                         being used by the scanner.
327      *
328      * @param  bufMgr          This is the buffer manager of the scanner. This
329      *                         is provided as a convenience so that the validator
330      *                         doesn't have to create its own buffer manager
331      *                         during the parse process.
332      */
333    void setScannerInfo
334    (
335        XMLScanner* const           owningScanner
336        , ReaderMgr* const          readerMgr
337        , XMLBufferMgr* const       bufMgr
338    );
339
340    /**
341      * This method is called to set an error reporter on the validator via
342      * which it will report any errors it sees during parsing or validation.
343      * This is generally called by the owning scanner.
344      *
345      * @param  errorReporter   A pointer to the error reporter to use. This
346      *                         is not adopted, just referenced so the caller
347      *                         remains responsible for its cleanup, if any.
348      */
349    void setErrorReporter
350    (
351        XMLErrorReporter* const errorReporter
352    );
353
354    //@}
355
356
357    // -----------------------------------------------------------------------
358    //  Error emitter methods
359    // -----------------------------------------------------------------------
360
361    /** @name Error emittor methods */
362    //@{
363
364    /**
365     *  This call is a convenience by which validators can emit errors. Most
366     *  of the grunt work of loading the text, getting the current source
367     *  location, ect... is handled here.
368     *
369     *  If the loaded text has replacement parameters, then text strings can be
370     *  passed. These will be used to replace the tokens {0}, {1}, {2}, and {3}
371     *  in the order passed. So text1 will replace {0}, text2 will replace {1},
372     *  and so forth.
373     *
374     *  textX   Up to four replacement parameters. They can be provided
375     *          as either XMLCh strings, or local code page strings which
376     *          will be transcoded internally.
377     *
378     *  @param toEmit   The error code to emit. it must be one of the defined
379     *                  validator error codes.
380     *
381     */
382    void emitError(const XMLValid::Codes toEmit);
383    void emitError
384    (
385        const   XMLValid::Codes toEmit
386        , const XMLCh* const    text1
387        , const XMLCh* const    text2 = 0
388        , const XMLCh* const    text3 = 0
389        , const XMLCh* const    text4 = 0
390    );
391    void emitError
392    (
393        const   XMLValid::Codes toEmit
394        , const char* const     text1
395        , const char* const     text2 = 0
396        , const char* const     text3 = 0
397        , const char* const     text4 = 0
398    );
399
400    //@}
401
402    // -----------------------------------------------------------------------
403    //  Deprecated XMLValidator interface
404    // -----------------------------------------------------------------------
405    /**
406      *
407      * DEPRECATED.
408      * For those validators that contrain the possible root elements of a
409      * document to only particular elements, they should use this call to
410      * validate that the passed root element id is a legal root element.
411      */
412    bool checkRootElement
413    (
414        const   unsigned int
415    ) { return true;};
416
417    // -----------------------------------------------------------------------
418    //  Notification that lazy data has been deleted
419    // -----------------------------------------------------------------------
420        static void reinitMsgMutex();
421
422        static void reinitMsgLoader();
423
424protected :
425    // -----------------------------------------------------------------------
426    //  Hidden constructors
427    // -----------------------------------------------------------------------
428    XMLValidator
429    (
430        XMLErrorReporter* const errReporter = 0
431    );
432
433
434    // -----------------------------------------------------------------------
435    //  Protected getters
436    // -----------------------------------------------------------------------
437    const XMLBufferMgr* getBufMgr() const;
438    XMLBufferMgr* getBufMgr();
439    const ReaderMgr* getReaderMgr() const;
440    ReaderMgr* getReaderMgr();
441    const XMLScanner* getScanner() const;
442    XMLScanner* getScanner();
443
444
445private :
446    // -----------------------------------------------------------------------
447    //  Unimplemented Constructors and Operators
448    // -----------------------------------------------------------------------
449    XMLValidator(const XMLValidator&);
450    XMLValidator& operator=(const XMLValidator&);
451
452
453    // -----------------------------------------------------------------------
454    //  Private data members
455    //
456    //  fErrorReporter
457    //      The error reporter we are to use, if any.
458    //
459    // -----------------------------------------------------------------------
460    XMLBufferMgr*       fBufMgr;
461    XMLErrorReporter*   fErrorReporter;
462    ReaderMgr*          fReaderMgr;
463    XMLScanner*         fScanner;
464};
465
466
467// -----------------------------------------------------------------------
468//  Setter methods
469// -----------------------------------------------------------------------
470inline void
471XMLValidator::setScannerInfo(XMLScanner* const      owningScanner
472                            , ReaderMgr* const      readerMgr
473                            , XMLBufferMgr* const   bufMgr)
474{
475    // We don't own any of these, we just reference them
476    fScanner = owningScanner;
477    fReaderMgr = readerMgr;
478    fBufMgr = bufMgr;
479}
480
481inline void
482XMLValidator::setErrorReporter(XMLErrorReporter* const errorReporter)
483{
484    fErrorReporter = errorReporter;
485}
486
487
488// ---------------------------------------------------------------------------
489//  XMLValidator: Protected getter
490// ---------------------------------------------------------------------------
491inline const XMLBufferMgr* XMLValidator::getBufMgr() const
492{
493    return fBufMgr;
494}
495
496inline XMLBufferMgr* XMLValidator::getBufMgr()
497{
498    return fBufMgr;
499}
500
501inline const ReaderMgr* XMLValidator::getReaderMgr() const
502{
503    return fReaderMgr;
504}
505
506inline ReaderMgr* XMLValidator::getReaderMgr()
507{
508    return fReaderMgr;
509}
510
511inline const XMLScanner* XMLValidator::getScanner() const
512{
513    return fScanner;
514}
515
516inline XMLScanner* XMLValidator::getScanner()
517{
518    return fScanner;
519}
520
521XERCES_CPP_NAMESPACE_END
522
523#endif
Note: See TracBrowser for help on using the repository browser.