override def document()

in daffodil-lib/src/main/scala/org/apache/daffodil/lib/xml/DaffodilConstructingLoader.scala [387:465]


  override def document(): Document = {
    doc = new Document()
    this.dtd = null
    var children: NodeSeq = null

    if ('<' == ch) {
      nextch()
      if ('?' == ch) {
        nextch()
        // It's probably an XML prolog, but
        // there are cases where there is no XML Prolog, but a starting
        // PI of <?xml-model href="...."?>
        // So we have to recognize as a general PI, then look and see if
        // it is a prolog.
        val name = xName
        xSpace()
        val (md, scp) = xAttributes(TopScope)
        if (scp != TopScope)
          reportSyntaxError("no xmlns definitions allowed.")
        xToken('?')
        xToken('>')
        if (name == "xml") {
          val info_prolog = parseXMLPrologAttributes(md)
          doc.version = info_prolog._1
          doc.encoding = info_prolog._2
          doc.standAlone = info_prolog._3
        } else {
          // not an xml prolog. It's some other PI
          // do nothing. We're just skipping those PIs
        }
        children = content(TopScope)
      } else {
        val ts = new NodeBuffer()
        content1(TopScope, ts) // the 1 suffix means "without the first < character"
        ts &+ content(TopScope)
        children = NodeSeq.fromSeq(ts)
      }
    } else {
      children = content(TopScope)
    }

    var isErr = false
    var elemCount = 0
    var theNode: Node = null
    children.foreach { c =>
      c match {
        case _: ProcInstr => // skip
        case _: Comment => // skip
        // $COVERAGE-OFF$ // constructing parser never creates these - probably due to a bug
        case _: EntityRef => {
          reportSyntaxError("no entity references allowed here")
          isErr = true
        }
        // $COVERAGE-ON$
        case s: SpecialNode => {
          val txt = s.toString.trim()
          if (txt.length > 0) {
            reportSyntaxError("non-empty text nodes not allowed: '" + txt + "'.")
            isErr = true
          }
        }
        case m: Elem =>
          elemCount += 1
          theNode = m
      }
    }
    if (1 != elemCount) {
      reportSyntaxError("document must contain exactly one element")
      isErr = true
    }

    if (!isErr) {
      doc.children = children
      doc.docElem = theNode
      doc
    } else {
      null
    }
  }