Example #1
0
EventGenerator::Ptr XQCommentConstructor::generateEvents(EventHandler *events, DynamicContext *context,
                                                    bool preserveNS, bool preserveType) const
{
  XMLBuffer value;
  getStringValue(m_value, value, context);

  // Check for two dashes in a row, or a dash at the end
  if(xslt_) {
    XMLBuffer buf(value.getLen());
    bool foundDash = false;
    const XMLCh *ptr = value.getRawBuffer();
    const XMLCh *end = ptr + value.getLen();
    while(ptr != end) {
      if(*ptr == chDash) {
        if(foundDash) {
          buf.append(' ');
        }
        foundDash = true;
      }
      else foundDash = false;

      buf.append(*ptr);
      ++ptr;
    }

    if(foundDash) {
      buf.append(' ');
    }

    value.set(buf.getRawBuffer());
  }
  else {
    bool foundDash = false;
    const XMLCh *ptr = value.getRawBuffer();
    const XMLCh *end = ptr + value.getLen();
    while(ptr != end) {
      if(*ptr == chDash) {
        if(foundDash) break;
        foundDash = true;
      }
      else foundDash = false;
      ++ptr;
    }
    if(foundDash)
      XQThrow(ASTException,X("DOM Constructor"),X("It is a dynamic error if the result of the content expression of "
                                                  "a computed comment constructor contains two adjacent hyphens or "
                                                  "ends with a hyphen. [err:XQDY0072]"));
  }

  events->commentEvent(value.getRawBuffer());
  return 0;
}
EventGenerator::Ptr XQNamespaceConstructor::generateEvents(EventHandler *events, DynamicContext *context,
                                                      bool preserveNS, bool preserveType) const
{
  const XMLCh *nodeName = m_name->createResult(context)->next(context)->asString(context);
  if(*nodeName && !XMLChar1_0::isValidNCName(nodeName, XMLString::stringLen(nodeName)))
    XQThrow(ASTException,X("XQNamespaceConstructor::generateEvents"),
            X("The name for the namespace node must be either a zero-length string or a valid xs:NCName [err:XTDE0920]"));

  if(XPath2Utils::equals(nodeName, XMLUni::fgXMLNSString))
    XQThrow(ASTException,X("XQNamespaceConstructor::generateEvents"),
            X("The name for the namespace node must not be \"xmlns\" [err:XTDE0920]"));

  XMLBuffer value;
  getStringValue(m_children, value, context);

  if(value.getLen() == 0)
    XQThrow(ASTException,X("XQNamespaceConstructor::generateEvents"),
            X("The value for the namespace node must not be empty [err:XTDE0930]"));

  if(XPath2Utils::equals(nodeName, XMLUni::fgXMLString) &&
     !XPath2Utils::equals(value.getRawBuffer(), XMLUni::fgXMLURIName))
    XQThrow(ASTException,X("XQNamespaceConstructor::generateEvents"),
            X("The name for the namespace node must not be \"xml\"  when the value is not \"http://www.w3.org/XML/1998/namespace\" [err:XTDE0925]"));

  if(XPath2Utils::equals(value.getRawBuffer(), XMLUni::fgXMLURIName) &&
     !XPath2Utils::equals(nodeName, XMLUni::fgXMLString))
    XQThrow(ASTException,X("XQNamespaceConstructor::generateEvents"),
            X("The value for the namespace node must not be \"http://www.w3.org/XML/1998/namespace\"  when the name is not \"xml\" [err:XTDE0925]"));

  events->namespaceEvent(nodeName, value.getRawBuffer());
  return 0;
}
//------------------------------------------------------------------------
//
//  processTestFile   Given the file name of an encoding test xml file,
//                    run it.
//
//------------------------------------------------------------------------
static bool  processTestFile(const XMLCh* fileName)
{
    //
    //  Send the input file through the parse, create a DOM document for it.
    //
    char cFileName[4000];
    XMLString::transcode(fileName, cFileName, 3999);
    DOMDocument* testDoc = parseFile(cFileName);
    if (testDoc == 0)
        return false;    // parse errors in the source xml.

    //
    //  Pull the "data" element out of the document.
    //
    XMLCh tempStr[4000];
    XMLString::transcode("data", tempStr, 3999);
    DOMNodeList* nl = testDoc->getElementsByTagName(tempStr);
    if (nl->getLength() != 1) {
        fprintf(stderr, "Test file \"%s\" must have exactly one \"data\" element.\n", cFileName);
        return false;
    };
    DOMNode* tmpNode = nl->item(0);
    DOMElement* data = (DOMElement*) tmpNode;


    //
    //  Build up a string containing the character data contents of the data element.
    //
    DOMNode* child;
    XMLBuffer elData;
    for (child=data->getFirstChild(); child != 0; child= child->getNextSibling())
    {
		if (child->getNodeType() == DOMNode::COMMENT_NODE)
			continue;
        if (! (child->getNodeType() == DOMNode::TEXT_NODE ||
               child->getNodeType() == DOMNode::CDATA_SECTION_NODE ||
               child->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE))
        {
               fprintf(stderr, "Test file \"%s\": data element contains unexpected children.",
                    cFileName);
               return false;
        }
        elData.append(((DOMCharacterData *)child)->getData());
    };

    //
    //  Pull the "udata" element out of the document
    //
    XMLString::transcode("udata", tempStr, 3999);
    nl = testDoc->getElementsByTagName(tempStr);
    if (nl->getLength() != 1) {
        fprintf(stderr, "Test file \"%s\" must have exactly one \"udata\" element.\n", cFileName);
        return false;
    };
    DOMNode* tmpNode1 = nl->item(0);
    DOMElement* udata = (DOMElement*) tmpNode1;

    //
    //  Build up a string containing the character data contents of the udata element.
    //  This will consist of a whole bunch hex numbers, still in string from
    //

    XMLBuffer rawUData;
    for (child=udata->getFirstChild(); child != 0; child= child->getNextSibling())
    {
        if (child->getNodeType() == DOMNode::COMMENT_NODE)
            continue;
        if (! (child->getNodeType() == DOMNode::TEXT_NODE ||
            child->getNodeType() == DOMNode::CDATA_SECTION_NODE ||
            child->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE))
        {
            fprintf(stderr, "Test file \"%s\": udata element contains unexpected children.",
                cFileName);
            return false;
        }
        rawUData.append(((DOMCharacterData *)child)->getData());
    };


    //
    // Convert the raw (hex numbers)  form of the udata to the corresponding string.
    //
    XMLBuffer uData;
    unsigned int rawIndex = 0;

    while (rawIndex < rawUData.getLen())
    {
        eatWhiteSpace(rawUData.getRawBuffer(), rawIndex);
        XMLCh c = convertHexValue(rawUData.getRawBuffer(), rawIndex);
        if (c > 0)
            uData.append(c);
        else
            if (rawIndex < rawUData.getLen())
            {
                fprintf(stderr, "Test file \"%s\": Bad hex number in udata element.  "
                    "Data character number %d\n", cFileName, uData.getLen());
                return false;
            }
    }


    //
    // Compare the two strings.
    //
    unsigned int i;
    for (i=0; i< elData.getLen(); i++)
    {
        XMLCh* elDataRaw = elData.getRawBuffer();
        XMLCh* uDataRaw = uData.getRawBuffer();
        if (i >= uData.getLen())
        {
            fprintf(stderr, "Test file \"%s\": udata element shorter than data at char number %d\n",
                cFileName, i);
            writeUData(elDataRaw);
            return false;
        }
        if (uDataRaw[i] != elDataRaw[i])
        {
            fprintf(stderr, "Test file \"%s\": comparison failure at character number %d\n",
                cFileName, i);
            writeUData(elDataRaw);
            return false;
        };
    }

    if (elData.getLen() != uData.getLen())
    {
        fprintf(stderr, "Test file \"%s\": udata element longer than data at char number %d\n",
            cFileName, i);
        writeUData(elData.getRawBuffer());
        return false;
    }

    return true;
}