[111] | 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: DTDGrammar.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #if !defined(DTDGRAMMAR_HPP) |
---|
| 25 | #define DTDGRAMMAR_HPP |
---|
| 26 | |
---|
| 27 | #include <xercesc/util/RefHashTableOf.hpp> |
---|
| 28 | #include <xercesc/util/NameIdPool.hpp> |
---|
| 29 | #include <xercesc/util/StringPool.hpp> |
---|
| 30 | #include <xercesc/validators/common/Grammar.hpp> |
---|
| 31 | #include <xercesc/validators/DTD/DTDElementDecl.hpp> |
---|
| 32 | #include <xercesc/validators/DTD/DTDEntityDecl.hpp> |
---|
| 33 | #include <xercesc/framework/XMLDTDDescription.hpp> |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 37 | |
---|
| 38 | // |
---|
| 39 | // This class stores the DTD information |
---|
| 40 | // NOTE: DTDs are not namespace aware, so we just use regular NameIdPool |
---|
| 41 | // data structures to store element and attribute decls. They are all set |
---|
| 42 | // to be in the global namespace and the full QName is used as the base name |
---|
| 43 | // of the decl. This means that all the URI parameters below are expected |
---|
| 44 | // to be null pointers (and anything else will cause an exception.) |
---|
| 45 | // |
---|
| 46 | |
---|
| 47 | class VALIDATORS_EXPORT DTDGrammar : public Grammar |
---|
| 48 | { |
---|
| 49 | public: |
---|
| 50 | // ----------------------------------------------------------------------- |
---|
| 51 | // Constructors and Destructor |
---|
| 52 | // ----------------------------------------------------------------------- |
---|
| 53 | DTDGrammar(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 54 | virtual ~DTDGrammar(); |
---|
| 55 | |
---|
| 56 | // ----------------------------------------------------------------------- |
---|
| 57 | // Implementation of Virtual Interface |
---|
| 58 | // ----------------------------------------------------------------------- |
---|
| 59 | virtual Grammar::GrammarType getGrammarType() const; |
---|
| 60 | virtual const XMLCh* getTargetNamespace() const; |
---|
| 61 | |
---|
| 62 | // this method should only be used while the grammar is being |
---|
| 63 | // constructed, not while it is being used |
---|
| 64 | // in a validation episode! |
---|
| 65 | virtual XMLElementDecl* findOrAddElemDecl |
---|
| 66 | ( |
---|
| 67 | const unsigned int uriId |
---|
| 68 | , const XMLCh* const baseName |
---|
| 69 | , const XMLCh* const prefixName |
---|
| 70 | , const XMLCh* const qName |
---|
| 71 | , unsigned int scope |
---|
| 72 | , bool& wasAdded |
---|
| 73 | ) ; |
---|
| 74 | |
---|
| 75 | virtual unsigned int getElemId |
---|
| 76 | ( |
---|
| 77 | const unsigned int uriId |
---|
| 78 | , const XMLCh* const baseName |
---|
| 79 | , const XMLCh* const qName |
---|
| 80 | , unsigned int scope |
---|
| 81 | ) const ; |
---|
| 82 | |
---|
| 83 | virtual const XMLElementDecl* getElemDecl |
---|
| 84 | ( |
---|
| 85 | const unsigned int uriId |
---|
| 86 | , const XMLCh* const baseName |
---|
| 87 | , const XMLCh* const qName |
---|
| 88 | , unsigned int scope |
---|
| 89 | ) const ; |
---|
| 90 | |
---|
| 91 | virtual XMLElementDecl* getElemDecl |
---|
| 92 | ( |
---|
| 93 | const unsigned int uriId |
---|
| 94 | , const XMLCh* const baseName |
---|
| 95 | , const XMLCh* const qName |
---|
| 96 | , unsigned int scope |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | virtual const XMLElementDecl* getElemDecl |
---|
| 100 | ( |
---|
| 101 | const unsigned int elemId |
---|
| 102 | ) const; |
---|
| 103 | |
---|
| 104 | virtual XMLElementDecl* getElemDecl |
---|
| 105 | ( |
---|
| 106 | const unsigned int elemId |
---|
| 107 | ); |
---|
| 108 | |
---|
| 109 | virtual const XMLNotationDecl* getNotationDecl |
---|
| 110 | ( |
---|
| 111 | const XMLCh* const notName |
---|
| 112 | ) const; |
---|
| 113 | |
---|
| 114 | virtual XMLNotationDecl* getNotationDecl |
---|
| 115 | ( |
---|
| 116 | const XMLCh* const notName |
---|
| 117 | ); |
---|
| 118 | |
---|
| 119 | virtual bool getValidated() const; |
---|
| 120 | |
---|
| 121 | virtual XMLElementDecl* putElemDecl |
---|
| 122 | ( |
---|
| 123 | const unsigned int uriId |
---|
| 124 | , const XMLCh* const baseName |
---|
| 125 | , const XMLCh* const prefixName |
---|
| 126 | , const XMLCh* const qName |
---|
| 127 | , unsigned int scope |
---|
| 128 | , const bool notDeclared = false |
---|
| 129 | ); |
---|
| 130 | |
---|
| 131 | virtual unsigned int putElemDecl |
---|
| 132 | ( |
---|
| 133 | XMLElementDecl* const elemDecl |
---|
| 134 | , const bool notDeclared = false |
---|
| 135 | ) ; |
---|
| 136 | |
---|
| 137 | virtual unsigned int putNotationDecl |
---|
| 138 | ( |
---|
| 139 | XMLNotationDecl* const notationDecl |
---|
| 140 | ) const; |
---|
| 141 | |
---|
| 142 | virtual void setValidated(const bool newState); |
---|
| 143 | |
---|
| 144 | virtual void reset(); |
---|
| 145 | |
---|
| 146 | // ----------------------------------------------------------------------- |
---|
| 147 | // Getter methods |
---|
| 148 | // ----------------------------------------------------------------------- |
---|
| 149 | |
---|
| 150 | // deprecated. returns the ID of the root element; not |
---|
| 151 | // useable in multithreaded environments! |
---|
| 152 | unsigned int getRootElemId() const; |
---|
| 153 | const DTDEntityDecl* getEntityDecl(const XMLCh* const entName) const; |
---|
| 154 | DTDEntityDecl* getEntityDecl(const XMLCh* const entName); |
---|
| 155 | NameIdPool<DTDEntityDecl>* getEntityDeclPool(); |
---|
| 156 | const NameIdPool<DTDEntityDecl>* getEntityDeclPool() const; |
---|
| 157 | NameIdPoolEnumerator<DTDElementDecl> getElemEnumerator() const; |
---|
| 158 | NameIdPoolEnumerator<DTDEntityDecl> getEntityEnumerator() const; |
---|
| 159 | NameIdPoolEnumerator<XMLNotationDecl> getNotationEnumerator() const; |
---|
| 160 | |
---|
| 161 | // ----------------------------------------------------------------------- |
---|
| 162 | // Setter methods |
---|
| 163 | // ----------------------------------------------------------------------- |
---|
| 164 | |
---|
| 165 | // deprecated. Not usable in multithreaded environments |
---|
| 166 | void setRootElemId(unsigned int rootElemId); |
---|
| 167 | |
---|
| 168 | virtual void setGrammarDescription( XMLGrammarDescription*); |
---|
| 169 | virtual XMLGrammarDescription* getGrammarDescription() const; |
---|
| 170 | |
---|
| 171 | // ----------------------------------------------------------------------- |
---|
| 172 | // Content management methods |
---|
| 173 | // ----------------------------------------------------------------------- |
---|
| 174 | unsigned int putEntityDecl(DTDEntityDecl* const entityDecl) const; |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | // ----------------------------------------------------------------------- |
---|
| 178 | // Notification that lazy data has been deleted |
---|
| 179 | // ----------------------------------------------------------------------- |
---|
| 180 | static void reinitDfltEntities(); |
---|
| 181 | |
---|
| 182 | /*** |
---|
| 183 | * Support for Serialization/De-serialization |
---|
| 184 | ***/ |
---|
| 185 | DECL_XSERIALIZABLE(DTDGrammar) |
---|
| 186 | |
---|
| 187 | private: |
---|
| 188 | // ----------------------------------------------------------------------- |
---|
| 189 | // Private helper methods |
---|
| 190 | // ----------------------------------------------------------------------- |
---|
| 191 | void resetEntityDeclPool(); |
---|
| 192 | |
---|
| 193 | // ----------------------------------------------------------------------- |
---|
| 194 | // Unimplemented constructors and operators |
---|
| 195 | // ----------------------------------------------------------------------- |
---|
| 196 | DTDGrammar(const DTDGrammar &); |
---|
| 197 | DTDGrammar& operator = (const DTDGrammar&); |
---|
| 198 | |
---|
| 199 | // ----------------------------------------------------------------------- |
---|
| 200 | // Private data members |
---|
| 201 | // |
---|
| 202 | // fElemDeclPool |
---|
| 203 | // This is the element decl pool. It contains all of the elements |
---|
| 204 | // declared in the DTD (and their associated attributes.) |
---|
| 205 | // |
---|
| 206 | // fElemNonDeclPool |
---|
| 207 | // This is the element decl pool that is is populated as new elements |
---|
| 208 | // are seen in the XML document (not declared in the DTD), and they |
---|
| 209 | // are given default characteristics. |
---|
| 210 | // |
---|
| 211 | // fEntityDeclPool |
---|
| 212 | // This is a pool of EntityDecl objects, which contains all of the |
---|
| 213 | // general entities that are declared in the DTD subsets, plus the |
---|
| 214 | // default entities (such as > < ...) defined by the XML Standard. |
---|
| 215 | // |
---|
| 216 | // fNotationDeclPool |
---|
| 217 | // This is a pool of NotationDecl objects, which contains all of the |
---|
| 218 | // notations declared in the DTD subsets. |
---|
| 219 | // |
---|
| 220 | // fRootElemId |
---|
| 221 | // The id of the root element that we found in the DOCTYPE statement. |
---|
| 222 | // Its initialized to ContentModel::fgInvalidElemId, so that its |
---|
| 223 | // invalid unless we have a DOCTYPE. |
---|
| 224 | // |
---|
| 225 | // fValidated |
---|
| 226 | // Indicates if the content of the Grammar has been pre-validated |
---|
| 227 | // or not. When using a cached grammar, no need for pre content |
---|
| 228 | // validation. |
---|
| 229 | // |
---|
| 230 | // fGramDesc: adopted |
---|
| 231 | // |
---|
| 232 | // ----------------------------------------------------------------------- |
---|
| 233 | static NameIdPool<DTDEntityDecl>* fDefaultEntities; |
---|
| 234 | MemoryManager* fMemoryManager; |
---|
| 235 | NameIdPool<DTDElementDecl>* fElemDeclPool; |
---|
| 236 | NameIdPool<DTDElementDecl>* fElemNonDeclPool; |
---|
| 237 | NameIdPool<DTDEntityDecl>* fEntityDeclPool; |
---|
| 238 | NameIdPool<XMLNotationDecl>* fNotationDeclPool; |
---|
| 239 | XMLDTDDescription* fGramDesc; |
---|
| 240 | |
---|
| 241 | unsigned int fRootElemId; |
---|
| 242 | bool fValidated; |
---|
| 243 | |
---|
| 244 | friend class XMLInitializer; |
---|
| 245 | }; |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | // --------------------------------------------------------------------------- |
---|
| 249 | // DTDGrammar: Getter methods |
---|
| 250 | // --------------------------------------------------------------------------- |
---|
| 251 | inline unsigned int DTDGrammar::getRootElemId() const |
---|
| 252 | { |
---|
| 253 | return fRootElemId; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | // --------------------------------------------------------------------------- |
---|
| 257 | // DTDGrammar: Getter methods |
---|
| 258 | // --------------------------------------------------------------------------- |
---|
| 259 | inline NameIdPoolEnumerator<DTDElementDecl> |
---|
| 260 | DTDGrammar::getElemEnumerator() const |
---|
| 261 | { |
---|
| 262 | return NameIdPoolEnumerator<DTDElementDecl>(fElemDeclPool, fMemoryManager); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | inline NameIdPoolEnumerator<DTDEntityDecl> |
---|
| 266 | DTDGrammar::getEntityEnumerator() const |
---|
| 267 | { |
---|
| 268 | return NameIdPoolEnumerator<DTDEntityDecl>(fEntityDeclPool, fMemoryManager); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | inline NameIdPoolEnumerator<XMLNotationDecl> |
---|
| 272 | DTDGrammar::getNotationEnumerator() const |
---|
| 273 | { |
---|
| 274 | return NameIdPoolEnumerator<XMLNotationDecl>(fNotationDeclPool, fMemoryManager); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | inline const DTDEntityDecl* |
---|
| 278 | DTDGrammar::getEntityDecl(const XMLCh* const entName) const |
---|
| 279 | { |
---|
| 280 | DTDEntityDecl* decl = fDefaultEntities->getByKey(entName); |
---|
| 281 | |
---|
| 282 | if (!decl) |
---|
| 283 | return fEntityDeclPool->getByKey(entName); |
---|
| 284 | |
---|
| 285 | return decl; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | inline DTDEntityDecl* DTDGrammar::getEntityDecl(const XMLCh* const entName) |
---|
| 289 | { |
---|
| 290 | DTDEntityDecl* decl = fDefaultEntities->getByKey(entName); |
---|
| 291 | |
---|
| 292 | if (!decl) |
---|
| 293 | return fEntityDeclPool->getByKey(entName); |
---|
| 294 | |
---|
| 295 | return decl; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | inline NameIdPool<DTDEntityDecl>* DTDGrammar::getEntityDeclPool() |
---|
| 300 | { |
---|
| 301 | return fEntityDeclPool; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | inline const NameIdPool<DTDEntityDecl>* DTDGrammar::getEntityDeclPool() const |
---|
| 305 | { |
---|
| 306 | return fEntityDeclPool; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | // ----------------------------------------------------------------------- |
---|
| 310 | // Setter methods |
---|
| 311 | // ----------------------------------------------------------------------- |
---|
| 312 | inline void DTDGrammar::setRootElemId(unsigned int rootElemId) { |
---|
| 313 | fRootElemId = rootElemId; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | inline unsigned int DTDGrammar::putEntityDecl(DTDEntityDecl* const entityDecl) const |
---|
| 317 | { |
---|
| 318 | return fEntityDeclPool->put(entityDecl); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | |
---|
| 322 | // --------------------------------------------------------------------------- |
---|
| 323 | // DTDGrammar: Virtual methods |
---|
| 324 | // --------------------------------------------------------------------------- |
---|
| 325 | inline Grammar::GrammarType DTDGrammar::getGrammarType() const { |
---|
| 326 | return Grammar::DTDGrammarType; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | inline const XMLCh* DTDGrammar::getTargetNamespace() const { |
---|
| 330 | return XMLUni::fgZeroLenString; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | // Element Decl |
---|
| 334 | inline unsigned int DTDGrammar::getElemId (const unsigned int |
---|
| 335 | , const XMLCh* const |
---|
| 336 | , const XMLCh* const qName |
---|
| 337 | , unsigned int) const |
---|
| 338 | { |
---|
| 339 | // |
---|
| 340 | // In this case, we don't return zero to mean 'not found', so we have to |
---|
| 341 | // map it to the official not found value if we don't find it. |
---|
| 342 | // |
---|
| 343 | const DTDElementDecl* decl = fElemDeclPool->getByKey(qName); |
---|
| 344 | if (!decl) |
---|
| 345 | return XMLElementDecl::fgInvalidElemId; |
---|
| 346 | return decl->getId(); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | inline const XMLElementDecl* DTDGrammar::getElemDecl( const unsigned int |
---|
| 350 | , const XMLCh* const |
---|
| 351 | , const XMLCh* const qName |
---|
| 352 | , unsigned int) const |
---|
| 353 | { |
---|
| 354 | const XMLElementDecl* elemDecl = fElemDeclPool->getByKey(qName); |
---|
| 355 | |
---|
| 356 | if (!elemDecl && fElemNonDeclPool) |
---|
| 357 | elemDecl = fElemNonDeclPool->getByKey(qName); |
---|
| 358 | |
---|
| 359 | return elemDecl; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | inline XMLElementDecl* DTDGrammar::getElemDecl (const unsigned int |
---|
| 363 | , const XMLCh* const |
---|
| 364 | , const XMLCh* const qName |
---|
| 365 | , unsigned int) |
---|
| 366 | { |
---|
| 367 | XMLElementDecl* elemDecl = fElemDeclPool->getByKey(qName); |
---|
| 368 | |
---|
| 369 | if (!elemDecl && fElemNonDeclPool) |
---|
| 370 | elemDecl = fElemNonDeclPool->getByKey(qName); |
---|
| 371 | |
---|
| 372 | return elemDecl; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | inline const XMLElementDecl* DTDGrammar::getElemDecl(const unsigned int elemId) const |
---|
| 376 | { |
---|
| 377 | // Look up this element decl by id |
---|
| 378 | return fElemDeclPool->getById(elemId); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | inline XMLElementDecl* DTDGrammar::getElemDecl(const unsigned int elemId) |
---|
| 382 | { |
---|
| 383 | // Look up this element decl by id |
---|
| 384 | return fElemDeclPool->getById(elemId); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | inline unsigned int |
---|
| 388 | DTDGrammar::putElemDecl(XMLElementDecl* const elemDecl, |
---|
| 389 | const bool notDeclared) |
---|
| 390 | { |
---|
| 391 | if (notDeclared) |
---|
| 392 | { |
---|
| 393 | if(!fElemNonDeclPool) |
---|
| 394 | fElemNonDeclPool = new (fMemoryManager) NameIdPool<DTDElementDecl>(29, 128, fMemoryManager); |
---|
| 395 | return fElemNonDeclPool->put((DTDElementDecl*) elemDecl); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | return fElemDeclPool->put((DTDElementDecl*) elemDecl); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | // Notation Decl |
---|
| 402 | inline const XMLNotationDecl* DTDGrammar::getNotationDecl(const XMLCh* const notName) const |
---|
| 403 | { |
---|
| 404 | return fNotationDeclPool->getByKey(notName); |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | inline XMLNotationDecl* DTDGrammar::getNotationDecl(const XMLCh* const notName) |
---|
| 408 | { |
---|
| 409 | return fNotationDeclPool->getByKey(notName); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | inline unsigned int DTDGrammar::putNotationDecl(XMLNotationDecl* const notationDecl) const |
---|
| 413 | { |
---|
| 414 | return fNotationDeclPool->put(notationDecl); |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | inline bool DTDGrammar::getValidated() const |
---|
| 418 | { |
---|
| 419 | return fValidated; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | inline void DTDGrammar::setValidated(const bool newState) |
---|
| 423 | { |
---|
| 424 | fValidated = newState; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | XERCES_CPP_NAMESPACE_END |
---|
| 428 | |
---|
| 429 | #endif |
---|