override def runtimeDependencies = Vector()

in daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/unparsers/runtime1/DelimitedUnparsers.scala [43:144]


  override def runtimeDependencies = Vector()

  val fieldDFA = CreateFieldDFA()
  val textUnparser = new TextDelimitedUnparser(context)

  protected def theString(state: UState) =
    state.currentInfosetNode.asSimple.dataValueAsString

  def unparse(state: UState): Unit = {

    val schemeOpt = if (escapeScheme.isDefined) One(escapeScheme.get.evaluate(state)) else Nope

    try {
      val valueString = theString(state)

      val escapedValue: String =
        if (schemeOpt.isDefined) {
          state.withUnparserDataInputStream { dis =>
            val terminatingMarkup = state.allTerminatingMarkup

            dis.reset(valueString, state)

            val scheme = schemeOpt.get

            val (result, _) = {
              if (scheme.isInstanceOf[EscapeSchemeCharUnparserHelper]) {
                val theScheme = scheme.asInstanceOf[EscapeSchemeCharUnparserHelper]
                val hasEscCharAsDelimiter =
                  terminatingMarkup.exists(d => d.lookingFor(0) =#= theScheme.ec)
                val thingsToEscape = (terminatingMarkup ++ scheme.lookingFor).toArray

                textUnparser.escapeCharacter(
                  dis,
                  fieldDFA,
                  thingsToEscape,
                  hasEscCharAsDelimiter,
                  theScheme.ec,
                  theScheme.eec,
                  state
                )
              } else {
                val theScheme = scheme.asInstanceOf[EscapeSchemeBlockUnparserHelper]

                def hasInscopeTerminatingDelimiters(): Boolean = {
                  // Need to do this so we can 'break' the loop early
                  //
                  for (d <- terminatingMarkup) {
                    if (valueString.contains(d.lookingFor)) return true
                  }
                  false
                }

                val generateEscapeBlock =
                  (theScheme.generateEscapeBlock == GenerateEscape.Always) ||
                    valueString.startsWith(
                      theScheme.blockStart
                    ) || hasInscopeTerminatingDelimiters()

                val thingsToEscape = theScheme.lookingFor // blockEnd and extraEscapedCharacters

                textUnparser.escape(
                  dis,
                  fieldDFA,
                  thingsToEscape,
                  theScheme.blockEndDFA,
                  theScheme.eec,
                  theScheme.blockStart,
                  theScheme.blockEnd,
                  generateEscapeBlock,
                  state
                )
              }
            }

            result
          }
        } else valueString // No EscapeScheme

      val outStream = state.getDataOutputStream
      val nCharsWritten = outStream.putString(escapedValue, state)
      if (nCharsWritten != escapedValue.length)
        UE(
          state,
          "%s - Too many bits in field: IndexOutOfBounds. Insufficient space to write %s characters.",
          nom,
          escapedValue.length
        )
      Logger.log.debug(s"Ended at bit position ${outStream.relBitPos0b}")
    } catch {
      // Characters in infoset element cannot be encoded without error.
      //
      // This won't actually be thrown until encodingErrorPolicy='error' is
      // implemented.
      //
      case m: MalformedInputException => {
        UE(state, "%s - MalformedInputException: \n%s", nom, m.getMessage())
      }
      case u: UnmappableCharacterException => {
        UE(state, "%s - UnmappableCharacterException: \n%s", nom, u.getMessage())
      }
    }
  }