Ejemplo n.º 1
0
void XMLTreeBuilder::processStartTag(const AtomicXMLToken& token)
{
    exitText();

    bool isFirstElement = !m_sawFirstElement;
    m_sawFirstElement = true;

    NodeStackItem top = m_currentNodeStack.last();

    processNamespaces(token, top);

    QualifiedName qName(token.prefix(), token.name(), top.namespaceForPrefix(token.prefix(), top.namespaceURI()));
    RefPtr<Element> newElement = m_document->createElement(qName, true);

    processAttributes(token, top, newElement);

    newElement->beginParsingChildren();
    m_currentNodeStack.last().node()->parserAddChild(newElement.get());

    top.setNode(newElement);
    pushCurrentNode(top);

    if (!newElement->attached())
        newElement->attach();

    if (isFirstElement && m_document->frame())
        m_document->frame()->loader()->dispatchDocumentElementAvailable();

    if (token.selfClosing())
        closeElement(newElement);
}
Ejemplo n.º 2
0
void SchemaValidatorImpl::processElement(
    store::PUL* pul,
    TypeManager* typeManager,
    EventSchemaValidator& schemaValidator,
    store::Item_t element,
    const QueryLoc& loc)
{
  ZORBA_ASSERT(element->isNode());
  ZORBA_ASSERT(element->getNodeKind() == store::StoreConsts::elementNode);

  store::Item_t nodeName = element->getNodeName();
  zstring baseUri;
  element->getBaseURI(baseUri);

  //cout << " vup    - processElement: " << nodeName->getLocalName()->c_str()
  //    << " @ " << nodeName->getNamespace()->c_str() << "\n"; cout.flush();

  schemaValidator.startElem(nodeName);

  // namespace declarations must go first
  processNamespaces( schemaValidator, element);

  // since the type of an element is determined only after the validator
  // receives all of it's attributes, and an attribute node needs it's
  // parent when created we need to go through the attributes twice: once
  // for validation and once for creation
  validateAttributes(schemaValidator, element->getAttributes());

  store::Item_t typeQName = schemaValidator.getTypeQName();
  store::Item_t substitutedElemQName = schemaValidator.getSubstitutedElemQName();

  //cout << " vup      - elemType old: "
  //    << element->getType()->getLocalName()->c_str() << " @ "
  //    << element->getType()->getNamespace()->c_str() << "\n"; cout.flush();
  //cout << " vup      - elemType new: " << typeQName->getLocalName()->c_str()
  //    << " @ " << typeQName->getNamespace()->c_str() << "\n"; cout.flush();

  bool isInSubstitutionElement = false;
  if (substitutedElemQName)
  {
    isInSubstitutionElement = true;
    //    cout << " vup        - substitutes: " << substitutedElemQName->g
    //  etLocalName()->c_str() << " @ " << substitutedElemQName->getNamespace()->
    // c_str() << "\n"; cout.flush();
  }

  bool isNewType = false;
  xqtref_t newType;
  store::Item_t elm;

  if ( !typeQName->equals(element->getType()) )
  {
    isNewType = true;
    newType = typeManager->create_named_type(typeQName, SequenceType::QUANT_ONE, loc);

    elm = element;
  }

  store::NsBindings bindings;
  element->getNamespaceBindings(bindings);
  namespace_context nsCtx = namespace_context(theSctx, bindings);

  processAttributes(pul, nsCtx, typeManager, schemaValidator, element, 
                    element->getAttributes(), loc);

  std::vector<store::Item_t> typedValues;
  int noOfChildren = processChildren(pul,
                                     nsCtx,
                                     typeManager, 
                                     schemaValidator,
                                     element->getChildren(),
                                     typedValues,
                                     loc);

  if ( isNewType )
  {
    bool tHasValue      = Validator::typeHasValue(newType);
    bool tHasTypedValue = Validator::typeHasTypedValue(typeManager, newType, loc);
    bool tHasEmptyValue = Validator::typeHasEmptyValue(newType);

    if ( noOfChildren==0 )
    {
      // workaround for elem of type xsd:string with no text child
      if ( newType->is_builtin() && 
          newType->getQName()->equals(GENV_TYPESYSTEM.STRING_TYPE_ONE->getQName()) )
      {
        /*store::Item_t result;
         zstring emptyStr = "";
         GENV_ITEMFACTORY->createString( result, emptyStr);
         typedValues.push_back(result);*/
        tHasEmptyValue = true;
        tHasTypedValue = false;
        tHasValue = false;
      }
      else if ( newType->type_kind()==XQType::USER_DEFINED_KIND )
      {
        const UserDefinedXQType udXQType = static_cast<const UserDefinedXQType&>(*newType);
        if ( udXQType.isSubTypeOf(typeManager, *GENV_TYPESYSTEM.STRING_TYPE_ONE) )
        {
          tHasEmptyValue = true;
          tHasTypedValue = false;
          tHasValue = false;
        }
      }
    }

    //cout << " vup        - addSetElementType: " << elm->getNodeName()->
    //  getLocalName()->str() << "   " << newType->get_qname()->getLocalName() 
    //  << " @ " << newType->get_qname()->getNamespace() << "\n"; cout.flush();
    //cout << " vup             - " << ( tHasTypedValue ? "hasTypedValue" : "" )
    //  << " values.size: " << typedValues.size() << (typedValues.size()>0 ? 
    // " [0]=" + typedValues[0]->getStringValue()->str() : "" ) << ( tHasValue ?
    // " hasValue" : "" ) << ( tHasEmptyValue ? " hasEmptyValue" : "" ) << "\n";
    //  cout.flush();

    if ( typedValues.size()==1 )
      pul->addSetElementType(&loc,
                             elm,
                             typeQName,
                             typedValues[0],
                             tHasValue,
                             tHasEmptyValue,
                             tHasTypedValue,
                             isInSubstitutionElement);
    else
      pul->addSetElementType(&loc,
                             elm,
                             typeQName,
                             (std::vector<store::Item_t>&)typedValues,
                             tHasValue,
                             tHasEmptyValue,
                             tHasTypedValue,
                             isInSubstitutionElement);
  }

  schemaValidator.endElem(nodeName);
}