source: icXML/icXML-devel/src/icxercesc/framework/XMLValidator.hpp @ 2720

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

Initial check-in of icXML 0.8 source files

File size: 14.7 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: XMLValidator.hpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_XMLVALIDATOR_HPP)
23#define XERCESC_INCLUDE_GUARD_XMLVALIDATOR_HPP
24
25#include <xercesc/framework/XMLAttr.hpp>
26#include <xercesc/framework/XMLValidityCodes.hpp>
27#include <icxmlc/XMLChildElement.hpp>
28#include <icxmlc/XMLConfig.hpp>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32class ReaderMgr;
33class XMLBufferMgr;
34class XMLElementDecl;
35class XMLScanner;
36class Grammar;
37
38
39/**
40 *  This abstract class provides the interface for all validators. This is
41 *  the simple amount of API that all validators must honor, in order for
42 *  the scanner to use them to do validation. All validators will actually
43 *  contain much more functionality than is accessible via this common API,
44 *  but that functionality requires that you know what type of validator you
45 *  are dealing with.
46 *
47 *  Basically, at this level, the primary concern is to be able to query
48 *  core information about elements and attributes. Adding decls to the
49 *  validator requires that you go through the derived interface because they
50 *  all have their own decl types. At this level, we can return information
51 *  via the base decl classes, from which each validator derives its own
52 *  decl classes.
53 */
54class XMLPARSER_EXPORT XMLValidator : public XMemory
55{
56public:
57        // -----------------------------------------------------------------------
58        //  Constructors are hidden, just the virtual destructor is exposed
59        // -----------------------------------------------------------------------
60
61        /** @name Destructor */
62        //@{
63
64        /**
65         *  The derived class should clean up its allocated data, then this class
66         *  will do the same for data allocated at this level.
67         */
68        virtual ~XMLValidator()
69        {
70        }
71        //@}
72
73
74        // -----------------------------------------------------------------------
75        //  The virtual validator interface
76        // -----------------------------------------------------------------------
77
78        /** @name Virtual validator interface */
79        //@{
80
81        #ifdef STORE_CHILDREN_INFORMATION_IN_PARSER
82        /**
83          * The derived class should look up its declaration of the passed element
84          * from its element pool. It should then use the content model description
85          * contained in that element declaration to validate that the passed list
86          * of child elements are valid for that content model. The count can be
87          * zero, indicating no child elements.
88          *
89          * Note that whitespace and text content are not validated here. Those are
90          * handled by the scanner. So only element ids are provided here.
91          *
92          * @param  elemDecl    The element whose content is to be checked.
93          *
94          * @param  children    An array of element QName which represent the elements
95          *                     found within the parent element, i.e. the content
96          *                     to be validated.
97          *
98          * @param  childCount  The number of elements in the childIds array. It can
99          *                     be zero if the element had none.
100          *
101          * @param  indexOfFailingChild  On return, it will contain the index of the
102          *                            children failing validation, if the retun value
103          *                            is false
104          *
105          */
106        virtual bool checkContent
107        (
108                XMLElementDecl* const           elemDecl
109                , XMLElementDecl ** const       children
110                , XMLSize_t                                     childCount
111                , XMLSize_t*                            indexOfFailingChild
112        ) = 0;
113        #endif
114
115        /**
116          * The derived class should look up its declaration of the passed element
117          * from its element pool. It should then use the content model description
118          * contained in that element declaration to validate that the passed list
119          * of child elements are valid for that content model. The count can be
120          * zero, indicating no child elements.
121          *
122          * Note that whitespace and text content are not validated here. Those are
123          * handled by the scanner. So only element ids are provided here.
124          *
125          * @param  elemDecl    The element whose content is to be checked.
126          *
127          * @param  children    An array of element QName which represent the elements
128          *                     found within the parent element, i.e. the content
129          *                     to be validated.
130          *
131          * @param  childCount  The number of elements in the childIds array. It can
132          *                     be zero if the element had none.
133          *
134          * @param  indexOfFailingChild  On return, it will contain the index of the
135          *                            children failing validation, if the retun value
136          *                            is false
137          *
138          */
139        virtual bool checkContent
140        (
141                XMLElementDecl* const   elemDecl
142                , QName** const         children
143                , XMLSize_t             childCount
144                , XMLSize_t*            indexOfFailingChild
145        ) = 0;
146
147        /**
148          * The derived class should fault in the passed XMLAttr value. It should
149          * use the passeed attribute definition (which is passed via the base
150          * type so it must often be downcast to the appropriate type for the
151          * derived validator class), to fill in the passed attribute. This is done
152          * as a performance enhancement since the derived class has more direct
153          * access to the information.
154          */
155        virtual void faultInAttr
156        (
157                                XMLAttr&    toFill
158                , const XMLAttDef&  attDef
159        )   const = 0;
160
161        /**
162          * This method is called by the scanner after a Grammar is scanned.
163          */
164        virtual void preContentValidation(bool reuseGrammar,
165                                                                          bool validateDefAttr = false) = 0;
166
167        /**
168          * This method is called by the scanner after the parse has completed. It
169          * gives the validator a chance to check certain things that can only be
170          * checked after the whole document has been parsed, such as referential
171          * integrity of ID/IDREF pairs and so forth. The validator should just
172          * issue errors for any problems it finds.
173          */
174        virtual void postParseValidation() = 0;
175
176        /**
177          * This method is called by the scanner before a new document is about
178          * to start. It gives the validator a change to reset itself in preparation
179          * for another validation pass.
180          */
181        virtual void reset() = 0;
182
183        /**
184          * The derived class should return a boolean that indicates whether it
185          * requires namespace processing or not. Some do and some allow it to be
186          * optional. This flag is used to control whether the client code's
187          * requests to disable namespace processing can be honored or not.
188          */
189        virtual bool requiresNamespaces() const = 0;
190
191        /**
192          * The derived class should apply any rules to the passed attribute value
193          * that are above and beyond those defined by XML 1.0. The scanner itself
194          * will impose XML 1.0 rules, based on the type of the attribute. This
195          * will generally be used to check things such as range checks and other
196          * datatype related validation.
197          *
198          * If the value breaks any rules as defined by the derived class, it
199          * should just issue errors as usual.
200          */
201        virtual void validateAttrValue
202        (
203                const   XMLAttDef*                  attDef
204                , const XMLCh* const                attrValue
205                , bool                              preValidation = false
206                , const XMLElementDecl*             elemDecl = 0
207        ) = 0;
208
209        /**
210          * The derived class should apply any rules to the passed element decl
211          * that are above and beyond those defined by XML 1.0.
212          *
213          * If the value breaks any rules as defined by the derived class, it
214          * should just issue errors as usual.
215          */
216        virtual void validateElement
217        (
218                const   XMLElementDecl*             elemDef
219        ) = 0;
220
221        /**
222          * Retrieve the Grammar used
223          */
224        virtual Grammar* getGrammar() const =0;
225
226        /**
227          * Set the Grammar
228          */
229        virtual void setGrammar(Grammar* aGrammar) =0;
230
231
232        //@}
233
234        // -----------------------------------------------------------------------
235        //  Virtual DTD handler interface.
236        // -----------------------------------------------------------------------
237
238        /** @name Virtual DTD handler interface */
239        //@{
240
241        /**
242          * This method allows the scanner to ask the validator if it handles
243          * DTDs or not.
244          */
245        virtual bool handlesDTD() const = 0;
246
247        // -----------------------------------------------------------------------
248        //  Virtual Schema handler interface.
249        // -----------------------------------------------------------------------
250
251        /** @name Virtual Schema handler interface */
252
253        /**
254          * This method allows the scanner to ask the validator if it handles
255          * Schema or not.
256          */
257        virtual bool handlesSchema() const = 0;
258
259        //@}
260
261        // -----------------------------------------------------------------------
262        //  Setter methods
263        //
264        //  setScannerInfo() is called by the scanner to tell the validator
265        //  about the stuff it needs to have access to.
266        // -----------------------------------------------------------------------
267
268        /** @name Setter methods */
269        //@{
270
271        /**
272          * @param  owningScanner   This is a pointer to the scanner to which the
273          *                         validator belongs. The validator will often
274          *                         need to query state data from the scanner.
275          *
276          * @param  readerMgr       This is a pointer to the reader manager that is
277          *                         being used by the scanner.
278          *
279          * @param  bufMgr          This is the buffer manager of the scanner. This
280          *                         is provided as a convenience so that the validator
281          *                         doesn't have to create its own buffer manager
282          *                         during the parse process.
283          */
284        void setScannerInfo
285        (
286                XMLScanner* const           owningScanner
287                , ReaderMgr* const          readerMgr
288                , XMLBufferMgr* const       bufMgr
289        );
290
291        /**
292          * This method is called to set an error reporter on the validator via
293          * which it will report any errors it sees during parsing or validation.
294          * This is generally called by the owning scanner.
295          *
296          * @param  errorReporter   A pointer to the error reporter to use. This
297          *                         is not adopted, just referenced so the caller
298          *                         remains responsible for its cleanup, if any.
299          */
300        void setErrorReporter
301        (
302                XMLErrorReporter* const errorReporter
303        );
304
305        //@}
306
307
308        // -----------------------------------------------------------------------
309        //  Error emitter methods
310        // -----------------------------------------------------------------------
311
312        /** @name Error emittor methods */
313        //@{
314
315        /**
316         *  This call is a convenience by which validators can emit errors. Most
317         *  of the grunt work of loading the text, getting the current source
318         *  location, ect... is handled here.
319         *
320         *  If the loaded text has replacement parameters, then text strings can be
321         *  passed. These will be used to replace the tokens {0}, {1}, {2}, and {3}
322         *  in the order passed. So text1 will replace {0}, text2 will replace {1},
323         *  and so forth.
324         *
325         *  textX   Up to four replacement parameters. They can be provided
326         *          as either XMLCh strings, or local code page strings which
327         *          will be transcoded internally.
328         *
329         *  @param toEmit   The error code to emit. it must be one of the defined
330         *                  validator error codes.
331         *
332         */
333        void emitError(const XMLValid::Codes toEmit);
334        void emitError
335        (
336                const   XMLValid::Codes toEmit
337                , const XMLCh* const    text1
338                , const XMLCh* const    text2 = 0
339                , const XMLCh* const    text3 = 0
340                , const XMLCh* const    text4 = 0
341        );
342        void emitError
343        (
344                const   XMLValid::Codes toEmit
345                , const char* const     text1
346                , const char* const     text2 = 0
347                , const char* const     text3 = 0
348                , const char* const     text4 = 0
349        );
350        void emitError
351        (
352                const   XMLValid::Codes toEmit
353                , const XMLExcepts::Codes   originalErrorCode
354                , const XMLCh* const        text1 = 0
355                , const XMLCh* const        text2 = 0
356                , const XMLCh* const        text3 = 0
357                , const XMLCh* const        text4 = 0
358
359        );
360
361        //@}
362
363protected :
364        // -----------------------------------------------------------------------
365        //  Hidden constructors
366        // -----------------------------------------------------------------------
367        XMLValidator
368        (
369                XMLErrorReporter* const errReporter = 0
370        );
371
372
373        // -----------------------------------------------------------------------
374        //  Protected getters
375        // -----------------------------------------------------------------------
376        const XMLBufferMgr* getBufMgr() const;
377        XMLBufferMgr* getBufMgr();
378        const ReaderMgr* getReaderMgr() const;
379        ReaderMgr* getReaderMgr();
380        const XMLScanner* getScanner() const;
381        XMLScanner* getScanner();
382
383
384private :
385        // -----------------------------------------------------------------------
386        //  Unimplemented Constructors and Operators
387        // -----------------------------------------------------------------------
388        XMLValidator(const XMLValidator&);
389        XMLValidator& operator=(const XMLValidator&);
390
391
392        // -----------------------------------------------------------------------
393        //  Private data members
394        //
395        //  fErrorReporter
396        //      The error reporter we are to use, if any.
397        //
398        // -----------------------------------------------------------------------
399        XMLBufferMgr*       fBufMgr;
400        XMLErrorReporter*   fErrorReporter;
401        ReaderMgr*          fReaderMgr;
402        XMLScanner*         fScanner;
403};
404
405
406// -----------------------------------------------------------------------
407//  Setter methods
408// -----------------------------------------------------------------------
409inline void
410XMLValidator::setScannerInfo(XMLScanner* const      owningScanner
411                                                        , ReaderMgr* const      readerMgr
412                                                        , XMLBufferMgr* const   bufMgr)
413{
414        // We don't own any of these, we just reference them
415        fScanner = owningScanner;
416        fReaderMgr = readerMgr;
417        fBufMgr = bufMgr;
418}
419
420inline void
421XMLValidator::setErrorReporter(XMLErrorReporter* const errorReporter)
422{
423        fErrorReporter = errorReporter;
424}
425
426
427// ---------------------------------------------------------------------------
428//  XMLValidator: Protected getter
429// ---------------------------------------------------------------------------
430inline const XMLBufferMgr* XMLValidator::getBufMgr() const
431{
432        return fBufMgr;
433}
434
435inline XMLBufferMgr* XMLValidator::getBufMgr()
436{
437        return fBufMgr;
438}
439
440inline const ReaderMgr* XMLValidator::getReaderMgr() const
441{
442        return fReaderMgr;
443}
444
445inline ReaderMgr* XMLValidator::getReaderMgr()
446{
447        return fReaderMgr;
448}
449
450inline const XMLScanner* XMLValidator::getScanner() const
451{
452        return fScanner;
453}
454
455inline XMLScanner* XMLValidator::getScanner()
456{
457        return fScanner;
458}
459
460XERCES_CPP_NAMESPACE_END
461
462#endif
Note: See TracBrowser for help on using the repository browser.