alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (bug2364.scala)

This example Scala source code file (bug2364.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, atom, bytearrayinputstream, comment, elem, entityref, io, nobindingfactoryadapter, nobindingfactoryadapter, procinstr, saxdocumentparser, saxdocumentserializer, saxdocumentserializer, test, topscope

The Scala bug2364.scala source code

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import com.sun.xml.internal.fastinfoset._
import com.sun.xml.internal.fastinfoset.sax._
import scala.xml.parsing.NoBindingFactoryAdapter
import scala.xml._

// Note - this is in pending because com.sun.xml.etc is not standard,
// and I don't have time to extract a smaller test.

object Test {
	def main(args: Array[String]) {
		val node = <test/>
		val bytes = new ByteArrayOutputStream
		val serializer = new SAXDocumentSerializer()

		serializer.setOutputStream(bytes)
		serializer.startDocument()
		serialize(node, serializer)
		serializer.endDocument()
		println(parse(new ByteArrayInputStream(bytes.toByteArray)))
	}
	def serialize(node: Node, serializer: SAXDocumentSerializer) {
		node match {
		case _ : ProcInstr | _ : Comment | _ : EntityRef =>
		case x : Atom[_] =>
			val chars = x.text.toCharArray
			serializer.characters(chars, 0, chars.length)
		case _ : Elem =>
			serializer.startElement("", node.label.toLowerCase, node.label.toLowerCase, attributes(node.attributes))
			for (m <- node.child) serialize(m, serializer)
			serializer.endElement("", node.label.toLowerCase, node.label.toLowerCase)
		}
	}
	def parse(str: ByteArrayInputStream) = {
		val parser = new SAXDocumentParser
		val fac = new NoBindingFactoryAdapter

		parser.setContentHandler(fac)
		try {
			parser.parse(str)
		} catch {
		case x: Exception =>
			x.printStackTrace
		}
		fac.rootElem
	}
	def attributes(d: MetaData) = {
		val attrs = new AttributesHolder

		if (d != null) {
			for (attr <- d) {
				val sb = new StringBuilder()
				Utility.sequenceToXML(attr.value, TopScope, sb, true)
				attrs.addAttribute(new QualifiedName("", "", attr.key.toLowerCase), sb.toString)
			}
		}
		attrs
	}
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala bug2364.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.