summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-09-23 03:02:10 +0000
committerPaul Phillips <paulp@improving.org>2009-09-23 03:02:10 +0000
commite5464bcb42ee86cbd57c37c0a7963e3b8d6d4d0c (patch)
tree1b89638dba315caa288d652808132683d02f923f
parenta7697326cf7584debb61461691b01c520743df32 (diff)
downloadscala-e5464bcb42ee86cbd57c37c0a7963e3b8d6d4d0c.tar.gz
scala-e5464bcb42ee86cbd57c37c0a7963e3b8d6d4d0c.tar.bz2
scala-e5464bcb42ee86cbd57c37c0a7963e3b8d6d4d0c.zip
Fix for #2364, and another XML rewrite.
-rw-r--r--src/library/scala/testing/SUnit.scala9
-rw-r--r--src/library/scala/xml/parsing/FactoryAdapter.scala130
2 files changed, 52 insertions, 87 deletions
diff --git a/src/library/scala/testing/SUnit.scala b/src/library/scala/testing/SUnit.scala
index 2cb8517527..b4d2636c5e 100644
--- a/src/library/scala/testing/SUnit.scala
+++ b/src/library/scala/testing/SUnit.scala
@@ -150,12 +150,9 @@ object SUnit {
/** an AssertFailed is thrown for a failed assertion */
case class AssertFailed(msg: String, stackTrace: Boolean) extends RuntimeException {
- private val msg0 = if (stackTrace) {
- import java.io._
- val wrt = new StringWriter
- printStackTrace(new PrintWriter(wrt))
- wrt.toString
- } else msg
+ private val msg0 =
+ if (stackTrace) super.getStackTrace().map(_.toString + "\n").mkString
+ else msg
override def toString() =
if (msg0 eq null) "failed assertion: " + msg else msg0
}
diff --git a/src/library/scala/xml/parsing/FactoryAdapter.scala b/src/library/scala/xml/parsing/FactoryAdapter.scala
index 8353f4177f..31ace71145 100644
--- a/src/library/scala/xml/parsing/FactoryAdapter.scala
+++ b/src/library/scala/xml/parsing/FactoryAdapter.scala
@@ -69,7 +69,7 @@ abstract class FactoryAdapter extends DefaultHandler with factory.XMLLoader[Node
* @return a new XML element.
*/
def createNode(pre: String, elemName: String, attribs: MetaData,
- scope: NamespaceBinding, chIter: List[Node]): Node //abstract
+ scope: NamespaceBinding, chIter: List[Node]): Node // abstract
/** creates a Text node.
* @param text
@@ -94,22 +94,27 @@ abstract class FactoryAdapter extends DefaultHandler with factory.XMLLoader[Node
*/
override def characters(ch: Array[Char], offset: Int, length: Int): Unit = {
if (!capture) return
- if (!normalizeWhitespace) {
- // compliant: report every character
- return buffer.appendAll(ch, offset, length)
- }
-
+ // compliant: report every character
+ else if (!normalizeWhitespace) buffer.appendAll(ch, offset, length)
// normalizing whitespace is not compliant, but useful
- var i: Int = offset
- while (i < offset + length) {
- val c = if (ch(i).isWhitespace) ' ' else ch(i)
- buffer append c
- i += 1
- // if that was whitespace, drop until non whitespace
- if (c == ' ') while (ch(i).isWhitespace) i += 1
+ else {
+ var it = ch.slice(offset, offset + length).iterator
+ while (it.hasNext) {
+ val c = it.next
+ val isSpace = c.isWhitespace
+ buffer append (if (isSpace) ' ' else c)
+ if (isSpace)
+ it = it dropWhile (_.isWhitespace)
+ }
}
}
+ private def splitName(s: String) = {
+ val idx = s indexOf ':'
+ if (idx < 0) (null, s)
+ else (s take idx, s drop (idx + 1))
+ }
+
/* ContentHandler methods */
/* Start element. */
@@ -119,61 +124,42 @@ abstract class FactoryAdapter extends DefaultHandler with factory.XMLLoader[Node
qname: String,
attributes: Attributes): Unit =
{
- /*elemCount = elemCount + 1; STATISTICS */
captureText()
- //Console.println("FactoryAdapter::startElement("+uri+","+_localName+","+qname+","+attributes+")");
- tagStack.push(curTag)
- curTag = qname; //localName ;
-
- val colon = qname.indexOf(':'.asInstanceOf[Int])
- val localName = if(-1 == colon) qname else qname.substring(colon+1,qname.length())
-
- //Console.println("FactoryAdapter::startElement - localName ="+localName);
+ tagStack push curTag
+ curTag = qname
+ val localName = splitName(qname)._2
capture = nodeContainsText(localName)
- hStack.push(null)
+ hStack push null
var m: MetaData = Null
+ var scpe: NamespaceBinding = TopScope
+
+ for (i <- (0 until attributes.getLength()).toList) {
+ val qname = attributes getQName i
+ val value = (attributes getValue i) match { case "" => null ; case x => x }
+ val (pre, key) = splitName(qname)
- var scpe = scopeStack.top
- for (i <- List.range(0, attributes.getLength())) {
- //val attrType = attributes.getType(i); // unused for now
- val qname = attributes.getQName(i)
- val value = attributes.getValue(i)
- val colon = qname.indexOf(':'.asInstanceOf[Int])
- if (-1 != colon) { // prefixed attribute
- val pre = qname.substring(0, colon)
- val key = qname.substring(colon+1, qname.length())
- if ("xmlns" /*XML.xmlns*/ == pre)
- scpe = value.length() match {
- case 0 => new NamespaceBinding(key, null, scpe)
- case _ => new NamespaceBinding(key, value, scpe)
- }
- else
- m = new PrefixedAttribute(pre, key, Text(value), m)
- } else if ("xmlns" /*XML.xmlns*/ == qname)
- scpe = value.length() match {
- case 0 => new NamespaceBinding(null, null, scpe)
- case _ => new NamespaceBinding(null, value, scpe)
- }
+ if (pre == "xmlns" || (pre == null && qname == "xmlns")) {
+ val arg = if (pre == null) null else key
+ scpe = new NamespaceBinding(arg, value, scpe)
+ }
else
- m = new UnprefixedAttribute(qname, Text(value), m)
+ m = Attribute(Option(pre), key, Text(value), m)
}
- scopeStack.push(scpe)
- attribStack.push(m)
- ()
- } // startElement(String,String,String,Attributes)
+
+ scopeStack push scpe
+ attribStack push m
+ }
/** captures text, possibly normalizing whitespace
*/
def captureText(): Unit = {
- if (capture) {
- val text = buffer.toString()
- if (text.length() > 0)
- hStack.push(createText(text))
- }
- buffer.setLength(0)
+ if (capture && buffer.length > 0)
+ hStack push createText(buffer.toString)
+
+ buffer.clear()
}
/** End element.
@@ -182,43 +168,25 @@ abstract class FactoryAdapter extends DefaultHandler with factory.XMLLoader[Node
* @param qname
* @throws org.xml.sax.SAXException if ..
*/
- override def endElement(uri: String , _localName: String , qname: String): Unit = {
+ override def endElement(uri: String , _localName: String, qname: String): Unit = {
captureText()
-
val metaData = attribStack.pop
// reverse order to get it right
- var v: List[Node] = Nil
- var child: Node = hStack.pop
- while (child ne null) {
- v = child::v
- child = hStack.pop
- }
-
- val colon = qname.indexOf(':'.asInstanceOf[Int])
- val localName =
- if (-1 == colon) qname
- else qname.substring(colon+1, qname.length())
-
+ val v = (Iterator continually hStack.pop takeWhile (_ != null)).toList.reverse
+ val (pre, localName) = splitName(qname)
val scp = scopeStack.pop
+
// create element
- val pre = if (-1 == colon) null else qname.substring(0, colon)
rootElem = createNode(pre, localName, metaData, scp, v)
-
- hStack.push(rootElem)
-
- // set
+ hStack push rootElem
curTag = tagStack.pop
-
- capture =
- if (curTag ne null) nodeContainsText(curTag) // root level
- else false
- } // endElement(String,String,String)
+ capture = curTag != null && nodeContainsText(curTag) // root level
+ }
/** Processing instruction.
*/
override def processingInstruction(target: String, data: String) {
- for (pi <- createProcInstr(target, data))
- hStack.push(pi)
+ hStack pushAll createProcInstr(target, data)
}
}