DTDValidator.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:20k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Id: DTDValidator.cpp,v 1.22 2001/11/13 13:25:28 tng Exp $
  58.  */
  59. // ---------------------------------------------------------------------------
  60. //  Includes
  61. // ---------------------------------------------------------------------------
  62. #include <util/Janitor.hpp>
  63. #include <util/XMLUniDefs.hpp>
  64. #include <util/XMLUni.hpp>
  65. #include <internal/ReaderMgr.hpp>
  66. #include <internal/XMLScanner.hpp>
  67. #include <validators/DTD/DTDValidator.hpp>
  68. // ---------------------------------------------------------------------------
  69. //  DTDValidator: Constructors and Destructor
  70. // ---------------------------------------------------------------------------
  71. DTDValidator::DTDValidator(XMLErrorReporter* const errReporter) :
  72.     XMLValidator(errReporter)
  73.     , fDTDGrammar(0)
  74. {
  75.     reset();
  76. }
  77. DTDValidator::~DTDValidator()
  78. {
  79. }
  80. // ---------------------------------------------------------------------------
  81. //  DTDValidator: Implementation of the XMLValidator interface
  82. // ---------------------------------------------------------------------------
  83. int DTDValidator::checkContent(XMLElementDecl* const elemDecl
  84.                               , QName** const        children
  85.                               , const unsigned int   childCount)
  86. {
  87.     //
  88.     //  Look up the element id in our element decl pool. This will get us
  89.     //  the element decl in our own way of looking at them.
  90.     //
  91.     if (!elemDecl)
  92.         ThrowXML(RuntimeException, XMLExcepts::Val_InvalidElemId);
  93.     //
  94.     //  Get the content spec type of this element. This will tell us what
  95.     //  to do to validate it.
  96.     //
  97.     const DTDElementDecl::ModelTypes modelType = ((DTDElementDecl*) elemDecl)->getModelType();
  98.     if (modelType == DTDElementDecl::Empty)
  99.     {
  100.         //
  101.         //  We can do this one here. It cannot have any children. If it does
  102.         //  we return 0 as the index of the first bad child.
  103.         //
  104.         if (childCount)
  105.             return 0;
  106.     }
  107.      else if (modelType == DTDElementDecl::Any)
  108.     {
  109.         // We pass no judgement on this one, anything goes
  110.     }
  111.      else if ((modelType == DTDElementDecl::Mixed_Simple)
  112.           ||  (modelType == DTDElementDecl::Children))
  113.     {
  114.         // Get the element's content model or fault it in
  115.         const XMLContentModel* elemCM = elemDecl->getContentModel();
  116.         // Ask it to validate and return its return
  117.         return elemCM->validateContent(children, childCount, getScanner()->getEmptyNamespaceId());
  118.     }
  119.      else
  120.     {
  121.         ThrowXML(RuntimeException, XMLExcepts::CM_UnknownCMType);
  122.     }
  123.     // Went ok, so return success
  124.     return -1;
  125. }
  126. void DTDValidator::faultInAttr(XMLAttr& toFill, const XMLAttDef& attDef) const
  127. {
  128.     //
  129.     //  At this level, we cannot set the URI id. So we just set it to zero
  130.     //  and leave it at that. The scanner, who called us, will look at the
  131.     //  prefix we stored (if any), resolve it, and store the URL id if any.
  132.     //
  133.     const XMLCh* fullName = attDef.getFullName();
  134.     const int colonInd = XMLString::indexOf(fullName, chColon);
  135.     if (colonInd == -1)
  136.     {
  137.         // There is no prefix, so we just do a simple and quick setting
  138.         toFill.set
  139.         (
  140.             0
  141.             , fullName
  142.             , XMLUni::fgZeroLenString
  143.             , attDef.getValue()
  144.             , attDef.getType()
  145.         );
  146.     }
  147.      else
  148.     {
  149.         //
  150.         //  There is a colon, so we have to split apart the name and prefix
  151.         //  part.
  152.         //
  153.         XMLCh* tmpNameBuf = XMLString::replicate(fullName);
  154.         ArrayJanitor<XMLCh> janNameBuf(tmpNameBuf);
  155.         // Put a null where the colon is, to split it into two strings
  156.         tmpNameBuf[colonInd] = chNull;
  157.         //
  158.         //  And now we can set the attribute object with the prefix and name
  159.         //  parts.
  160.         //
  161.         toFill.set
  162.         (
  163.             0
  164.             , &tmpNameBuf[colonInd+1]
  165.             , tmpNameBuf
  166.             , attDef.getValue()
  167.             , attDef.getType()
  168.         );
  169.     }
  170. }
  171. void DTDValidator::reset()
  172. {
  173. }
  174. bool DTDValidator::requiresNamespaces() const
  175. {
  176.     // Namespaces are not supported for DTDs
  177.     return false;
  178. }
  179. void
  180. DTDValidator::validateAttrValue(const   XMLAttDef*      attDef
  181.                                 , const XMLCh* const    attrValue)
  182. {
  183.     //
  184.     //  Get quick refs to lost of of the stuff in the passed objects in
  185.     //  order to simplify the code below, which will reference them very
  186.     //  often.
  187.     //
  188.     const XMLAttDef::AttTypes       type = attDef->getType();
  189.     const XMLAttDef::DefAttTypes    defType = attDef->getDefaultType();
  190.     const XMLCh* const              valueText = attDef->getValue();
  191.     const XMLCh* const              fullName = attDef->getFullName();
  192.     const XMLCh* const              enumList = attDef->getEnumeration();
  193.     //
  194.     //  If the default type is fixed, then make sure the passed value maps
  195.     //  to the fixed value.
  196.     //
  197.     if (defType == XMLAttDef::Fixed)
  198.     {
  199.         if (XMLString::compareString(attrValue, valueText))
  200.             emitError(XMLValid::NotSameAsFixedValue, fullName, attrValue, valueText);
  201.     }
  202.     //
  203.     //  If its a CDATA attribute, then we are done with any DTD level
  204.     //  validation else do the rest.
  205.     //
  206.     if (type == XMLAttDef::CData)
  207.         return;
  208.     // An empty string cannot be valid for any of the other types
  209.     if (!attrValue[0])
  210.     {
  211.         emitError(XMLValid::InvalidEmptyAttValue, fullName);
  212.         return;
  213.     }
  214.     // See whether we are doing multiple values or not
  215.     const bool multipleValues =
  216.     (
  217.         (type == XMLAttDef::IDRefs)
  218.         || (type == XMLAttDef::Entities)
  219.         || (type == XMLAttDef::NmTokens)
  220.         || (type == XMLAttDef::Notation)
  221.         || (type == XMLAttDef::Enumeration)
  222.     );
  223.     // And whether we must check for a first name char
  224.     const bool firstNameChar =
  225.     (
  226.         (type == XMLAttDef::ID)
  227.         || (type == XMLAttDef::IDRef)
  228.         || (type == XMLAttDef::IDRefs)
  229.         || (type == XMLAttDef::Entity)
  230.         || (type == XMLAttDef::Entities)
  231.         || (type == XMLAttDef::Notation)
  232.     );
  233.     // Whether it requires ref checking stuff
  234.     const bool isARefType
  235.     (
  236.         (type == XMLAttDef::ID)
  237.         || (type == XMLAttDef::IDRef)
  238.         || (type == XMLAttDef::IDRefs)
  239.     );
  240.     // Some trigger flags to avoid issuing redundant errors and whatnot
  241.     bool sawOneValue;
  242.     bool alreadyCapped = false;
  243.     //
  244.     //  Make a copy of the text that we can mangle and get a pointer we can
  245.     //  move through the value
  246.     //
  247.     // Use a stack-based buffer, when possible...
  248.     XMLCh   tempBuffer[100];
  249.     XMLCh* pszTmpVal = 0;
  250.     ArrayJanitor<XMLCh> janTmpVal(0);
  251.     if (XMLString::stringLen(attrValue) < sizeof(tempBuffer) / sizeof(tempBuffer[0]))
  252.     {
  253.         XMLString::copyString(tempBuffer, attrValue);
  254.         pszTmpVal = tempBuffer;
  255.     }
  256.     else
  257.     {
  258.         janTmpVal.reset(XMLString::replicate(attrValue));
  259.         pszTmpVal = janTmpVal.get();
  260.     }
  261.     XMLCh* valPtr = pszTmpVal;
  262.     while (true)
  263.     {
  264.         // Reset the trigger flags
  265.         sawOneValue = false;
  266.         //
  267.         //  Make sure the first character is a valid first name char, i.e.
  268.         //  if its a Name value. For NmToken values we don't treat the first
  269.         //  char any differently.
  270.         //
  271.         if (firstNameChar)
  272.         {
  273.             // If its not, emit and error but try to keep going
  274.             if (!XMLReader::isFirstNameChar(*valPtr))
  275.                 emitError(XMLValid::AttrValNotName, fullName);
  276.             valPtr++;
  277.         }
  278.         // Make sure all the remaining chars are valid name chars
  279.         while (*valPtr)
  280.         {
  281.             //
  282.             //  If we hit a whitespace, its either a break between two
  283.             //  or more values, or an error if we have a single value.
  284.             //
  285.             if (XMLReader::isWhitespace(*valPtr))
  286.             {
  287.                 if (!multipleValues)
  288.                 {
  289.                     emitError(XMLValid::NoMultipleValues, fullName);
  290.                     return;
  291.                 }
  292.                 break;
  293.             }
  294.             if (!XMLReader::isNameChar(*valPtr))
  295.             {
  296.                 emitError(XMLValid::AttrValNotName, fullName);
  297.                 return;
  298.             }
  299.             valPtr++;
  300.         }
  301.         //
  302.         //  Cap it off at the current non-name char. If already capped,
  303.         //  then remember this.
  304.         //
  305.         if (!(*valPtr))
  306.             alreadyCapped = true;
  307.         *valPtr = 0;
  308.         //
  309.         //  If this type of attribute requires that we track reference
  310.         //  stuff, then handle that.
  311.         //
  312.         if (isARefType)
  313.         {
  314.             if ((type == XMLAttDef::ID)
  315.             ||  (type == XMLAttDef::IDRef)
  316.             ||  (type == XMLAttDef::IDRefs))
  317.             {
  318.                 XMLRefInfo* find = getScanner()->getIDRefList()->get(pszTmpVal);
  319.                 if (find)
  320.                 {
  321.                     if (find->getDeclared() && (type == XMLAttDef::ID))
  322.                         emitError(XMLValid::ReusedIDValue, pszTmpVal);
  323.                 }
  324.                  else
  325.                 {
  326.                     find = new XMLRefInfo(pszTmpVal);
  327.                     getScanner()->getIDRefList()->put((void*)find->getRefName(), find);
  328.                 }
  329.                 //
  330.                 //  Mark it declared or used, which might be redundant in some cases
  331.                 //  but not worth checking
  332.                 //
  333.                 if (type == XMLAttDef::ID)
  334.                     find->setDeclared(true);
  335.                 else
  336.                     find->setUsed(true);
  337.             }
  338.         }
  339.          else if ((type == XMLAttDef::Entity) || (type == XMLAttDef::Entities))
  340.         {
  341.             //
  342.             //  If its refering to a entity, then look up the name in the
  343.             //  general entity pool. If not there, then its an error. If its
  344.             //  not an external unparsed entity, then its an error.
  345.             //
  346.             const XMLEntityDecl* decl = getScanner()->getEntityDecl(pszTmpVal);
  347.             if (decl)
  348.             {
  349.                 if (!decl->isUnparsed())
  350.                     emitError(XMLValid::BadEntityRefAttr, fullName);
  351.             }
  352.              else
  353.             {
  354.                 emitError
  355.                 (
  356.                     XMLValid::UnknownEntityRefAttr
  357.                     , fullName
  358.                     , pszTmpVal
  359.                 );
  360.             }
  361.         }
  362.          else if ((type == XMLAttDef::Notation) || (type == XMLAttDef::Enumeration))
  363.         {
  364.             //
  365.             //  Make sure that this value maps to one of the enumeration or
  366.             //  notation values in the enumList parameter. We don't have to
  367.             //  look it up in the notation pool (if a notation) because we
  368.             //  will look up the enumerated values themselves. If they are in
  369.             //  the notation pool (after the DTD is parsed), then obviously
  370.             //  this value will be legal since it matches one of them.
  371.             //
  372.             if (!XMLString::isInList(pszTmpVal, enumList))
  373.                 emitError(XMLValid::DoesNotMatchEnumList, fullName);
  374.         }
  375.         // If not doing multiple values, then we are done
  376.         if (!multipleValues)
  377.             break;
  378.         //
  379.         //  If we are at the end, then break out now, else move up to the
  380.         //  next char and update the base pointer.
  381.         //
  382.         if (alreadyCapped)
  383.             break;
  384.         valPtr++;
  385.         pszTmpVal = valPtr;
  386.     }
  387. }
  388. void DTDValidator::preContentValidation(bool reuseGrammar)
  389. {
  390.     //
  391.     //  Lets enumerate all of the elements in the element decl pool
  392.     //  and put out an error for any that did not get declared.
  393.     //  We also check all of the attributes as well.
  394.     //
  395.     NameIdPoolEnumerator<DTDElementDecl> elemEnum = fDTDGrammar->getElemEnumerator();
  396.     while (elemEnum.hasMoreElements())
  397.     {
  398.         const DTDElementDecl& curElem = elemEnum.nextElement();
  399.         const DTDElementDecl::CreateReasons reason = curElem.getCreateReason();
  400.         //
  401.         //  See if this element decl was ever marked as declared. If
  402.         //  not, then put out an error. In some cases its just
  403.         //  a warning, such as being referenced in a content model.
  404.         //
  405.         if (reason != XMLElementDecl::Declared)
  406.         {
  407.             if (reason == XMLElementDecl::AttList)
  408.             {
  409.                 getScanner()->emitError
  410.                 (
  411.                     XMLErrs::UndeclaredElemInAttList
  412.                     , curElem.getFullName()
  413.                 );
  414.             }
  415.              else if (reason == XMLElementDecl::AsRootElem)
  416.             {
  417.                 // It's ok that the root element is not declared in the DTD
  418.                 /*
  419.                 emitError
  420.                 (
  421.                     XMLValid::UndeclaredElemInDocType
  422.                     , curElem.getFullName()
  423.                 );*/
  424.             }
  425.              else if (reason == XMLElementDecl::InContentModel)
  426.             {
  427.                 getScanner()->emitError
  428.                 (
  429.                     XMLErrs::UndeclaredElemInCM
  430.                     , curElem.getFullName()
  431.                 );
  432.             }
  433.             else
  434.             {
  435.                 #if defined(XERCES_DEBUG)
  436.                   if(reuseGrammar && reason == XMLElementDecl::JustFaultIn){
  437.                   }
  438.                   else
  439.                       ThrowXML(RuntimeException, XMLExcepts::DTD_UnknownCreateReason);
  440.                 #endif
  441.             }
  442.         }
  443.         //
  444.         //  Check all of the attributes of the current element.
  445.         //  We check for:
  446.         //
  447.         //  1) Multiple ID attributes
  448.         //  2) That all of the default values of attributes are
  449.         //      valid for their type.
  450.         //  3) That for any notation types, that their lists
  451.         //      of possible values refer to declared notations.
  452.         //
  453.         XMLAttDefList& attDefList = curElem.getAttDefList();
  454.         bool seenId = false;
  455.         while (attDefList.hasMoreElements())
  456.         {
  457.             const XMLAttDef& curAttDef = attDefList.nextElement();
  458.             if (curAttDef.getType() == XMLAttDef::ID)
  459.             {
  460.                 if (seenId)
  461.                 {
  462.                     emitError
  463.                     (
  464.                         XMLValid::MultipleIdAttrs
  465.                         , curElem.getFullName()
  466.                     );
  467.                     break;
  468.                 }
  469.                 seenId = true;
  470.             }
  471.              else if (curAttDef.getType() == XMLAttDef::Notation)
  472.             {
  473.                 //
  474.                 //  We need to verify that all of its possible values
  475.                 //  (in the enum list) refer to valid notations.
  476.                 //
  477.                 XMLCh* list = XMLString::replicate(curAttDef.getEnumeration());
  478.                 ArrayJanitor<XMLCh> janList(list);
  479.                 //
  480.                 //  Search forward for a space or a null. If a null,
  481.                 //  we are done. If a space, cap it and look it up.
  482.                 //
  483.                 bool    breakFlag = false;
  484.                 XMLCh*  listPtr = list;
  485.                 XMLCh*  lastPtr = listPtr;
  486.                 while (true)
  487.                 {
  488.                     while (*listPtr && (*listPtr != chSpace))
  489.                         listPtr++;
  490.                     //
  491.                     //  If at the end, indicate we need to break after
  492.                     //  this one. Else, cap it off here.
  493.                     //
  494.                     if (!*listPtr)
  495.                         breakFlag = true;
  496.                     else
  497.                         *listPtr = chNull;
  498.                     if (!fDTDGrammar->getNotationDecl(lastPtr))
  499.                     {
  500.                         emitError
  501.                         (
  502.                             XMLValid::UnknownNotRefAttr
  503.                             , curAttDef.getFullName()
  504.                             , lastPtr
  505.                         );
  506.                     }
  507.                     // Break out if we hit the end last time
  508.                     if (breakFlag)
  509.                         break;
  510.                     // Else move upwards and try again
  511.                     listPtr++;
  512.                     lastPtr = listPtr;
  513.                 }
  514.             }
  515.             // If it has a default/fixed value, then validate it
  516.             if (curAttDef.getValue())
  517.             {
  518.                 validateAttrValue
  519.                 (
  520.                     &curAttDef
  521.                     , curAttDef.getValue()
  522.                 );
  523.             }
  524.         }
  525.     }
  526.     //
  527.     //  And enumerate all of the general entities. If any of them
  528.     //  reference a notation, then make sure the notation exists.
  529.     //
  530.     NameIdPoolEnumerator<DTDEntityDecl> entEnum = getScanner()->getEntityEnumerator();
  531.     while (entEnum.hasMoreElements())
  532.     {
  533.         const DTDEntityDecl& curEntity = entEnum.nextElement();
  534.         if (!curEntity.getNotationName())
  535.             continue;
  536.         // It has a notation name, so look it up
  537.         if (!fDTDGrammar->getNotationDecl(curEntity.getNotationName()))
  538.         {
  539.             emitError
  540.             (
  541.                 XMLValid::NotationNotDeclared
  542.                 , curEntity.getNotationName()
  543.             );
  544.         }
  545.     }
  546. }
  547. void DTDValidator::postParseValidation()
  548. {
  549.     //
  550.     //  At this time, there is nothing to do here. The scanner itself handles
  551.     //  ID/IDREF validation, since that is the same no matter what kind of
  552.     //  validator.
  553.     //
  554. }