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