All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.serializer.xml.PrettyXmlWriter.scala Maven / Gradle / Ivy

The newest version!
/*
 * Beangle, Agile Development Scaffold and Toolkits.
 *
 * Copyright © 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package org.beangle.serializer.xml

import java.io.Writer

import org.beangle.serializer.text.io.{AbstractWriter, StreamException}
import org.beangle.serializer.text.marshal.MarshallingContext

object PrettyXmlWriter {
  val NULL: Array[Char] = "�".toCharArray
  val AMP: Array[Char] = "&".toCharArray
  val LT: Array[Char] = "<".toCharArray
  val GT: Array[Char] = ">".toCharArray
  val CR: Array[Char] = "
".toCharArray
  val QUOT: Array[Char] = """.toCharArray
  val APOS: Array[Char] = "'".toCharArray
  val CLOSE: Array[Char] = "')
    }
    readyForNewLine = true
    if (depth == 0) writer.flush()
  }

  override def flush(): Unit = {
    writer.flush()
  }

  override def close(): Unit = {
    writer.close()
  }

  protected def writeAttributeValue(text: String): Unit = {
    writeText(text, true)
  }

  protected def writeText(text: String): Unit = {
    writeText(text, false)
  }

  private def writerChar(c: Char): Unit = {
    if (c <= '\u001f' || c > '\ud7ff' && c < '\ue000' || c >= '\ufffe')
      throw new StreamException("Invalid character 0x" + Integer.toHexString(c) + " in XML 1.0 stream")
    if (Character.isDefined(c) && !Character.isISOControl(c)) {
      this.writer.write(c)
    } else {
      this.writer.write("&#x")
      this.writer.write(Integer.toHexString(c))
      this.writer.write(';')
    }
  }

  private def writeText(text: String, isAttribute: Boolean): Unit = {
    val length = text.length()
    (0 until length) foreach { i =>
      val c = text.charAt(i)
      c match {
        case '&' => this.writer.write(AMP)
        case '<' => this.writer.write(LT)
        case '>' => this.writer.write(GT)
        case '"' => this.writer.write(QUOT)
        case '\'' => this.writer.write(APOS)
        case '\r' => this.writer.write(CR)
        case '\t' | '\n' => if (!isAttribute) this.writer.write(c) else writerChar(c)
        case _ => writerChar(c)
      }
    }
  }

  private def finishTag(depth: Int): Unit = {
    if (tagInProgress) {
      writer.write('>')
      tagInProgress = false
    }
    if (readyForNewLine) {
      indentNewLine(depth)
      readyForNewLine = false
    }
    tagIsEmpty = false
  }

  protected def indentNewLine(depth: Int): Unit = {
    writer.write(newLine)
    (0 until depth) foreach (_ => writer.write(lineIndenter))
  }

  override def start(context: MarshallingContext): Unit = {
    writer.write("\n")
  }

  override def end(context: MarshallingContext): Unit = {

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy