Permalink
Browse files

Merge remote-tracking branch 'jespersm/namespaces'

  • Loading branch information...
djspiewak committed May 13, 2011
2 parents 97cb9bf + 10d11d6 commit 0e885a3db4ac5d6c82ab4668c9b79e3b43261686
@@ -44,7 +44,7 @@ import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}
* a valid `Group` could be as follows:
*
* {{{
* Group(EntityRef("quot"), Text("Daniel is "), Elem(QName(None, "em", None), Map(), Group(Text("delusional!"))), EntityRef("quot"))
* Group(EntityRef("quot"), Text("Daniel is "), Elem(None, "em", None), Map(), Group(Text("delusional!"))), EntityRef("quot")
* }}}
*
* This would correspond to the following XML fragment (note: not actually well-formed
@@ -199,8 +199,8 @@ class Group[+A <: Node] private[antixml] (private[antixml] val nodes: VectorCase
for (node <- nodes) {
node match {
case Elem(name, _, _, children) => {
names += name.name
case Elem(_, name, _, _, children) => {
names += name
childFilter = if (childFilter == null)
children.bloomFilter
@@ -104,7 +104,7 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
val ns = if (uri == "") None else Some(uri)
Elem(QName(prefix, localName), map, scopes.headOption getOrElse Map(), children)
Elem(prefix, localName, map, scopes.headOption getOrElse Map(), children)
}
}
@@ -109,7 +109,7 @@ trait Selectable[+A <: Node] {
for (node <- toGroup) {
node match {
case e @ Elem(_, _, _, children) if children.matches(selector) => {
case e @ Elem(_, _, _, _, children) if children.matches(selector) => {
var childMap = Map[Int, Set[Int]]()
var currentChunk = 0
@@ -205,7 +205,7 @@ trait Selectable[+A <: Node] {
def \\[B, That](selector: Selector[B])(implicit cbf: CanBuildFromWithZipper[Group[_], B, That]): That = {
if (matches(selector)) {
val recursive = toGroup collect {
case Elem(_, _, _, children) => children \\ selector
case Elem(_, _, _, _, children) => children \\ selector
case _ => cbf().result
}
@@ -79,7 +79,7 @@ class StAXParser extends XMLParser {
children += Text(text.result)
text.clear()
}
ancestors.head += Elem(QName(elem.prefix, elem.name), elem.attrs, mapping, Group fromSeq children.result)
ancestors.head += Elem(elem.prefix, elem.name, elem.attrs, mapping, Group fromSeq children.result)
elems = parents
results = ancestors
}
@@ -125,7 +125,7 @@ object XMLConvertable extends SecondPrecedenceConvertables {
}
val children = NodeSeqConvertable(xml.NodeSeq fromSeq e.child)
Elem(QName(prefix, e.label), attrs, Map(), children)
Elem(prefix, e.label, attrs, Map(), children)
}
}
@@ -91,8 +91,9 @@ case class ProcInstr(target: String, data: String) extends Node {
}
/**
* An XML element consisting of an optional namespace, a name (or identifier), a
* set of attributes, a mapping of namespace prefixes and a sequence of child nodes.
* An XML element consisting of an optional namespace prefix, a name (or identifier), a
* set of attributes, a namespace prefix scope (mapping of prefixes to namespace URIs),
* and a sequence of child nodes.
* For example:
*
* {{{
@@ -102,10 +103,10 @@ case class ProcInstr(target: String, data: String) extends Node {
* This would result in the following node:
*
* {{{
* Elem(QName(None, "span"), Map("id" -> "foo", "class" -> "bar"), Map(), Group(Text("Lorem ipsum")))
* Elem(None, "span", Map("id" -> "foo", "class" -> "bar"), Map(), Group(Text("Lorem ipsum")))
* }}}
*/
case class Elem(name: QName, attrs: Attributes, scope: Map[String, String], children: Group[Node]) extends Node with Selectable[Elem] {
case class Elem(prefix: Option[String], name: String, attrs: Attributes, scope: Map[String, String], children: Group[Node]) extends Node with Selectable[Elem] {
override def toString = {
import Node._
@@ -118,12 +119,13 @@ case class Elem(name: QName, attrs: Attributes, scope: Map[String, String], chil
""
else
" " + (scope map { case (key, value) => (if (key == "") "xmlns" else "xmlns:" + escapeText(key)) + "=\"" + escapeText(value) + '"' } mkString " ")
val partial = "<" + name.toString + attrStr + prefixesStr
val qname = (prefix map { _ + ":" } getOrElse "") + name
val partial = "<" + qname + attrStr + prefixesStr
if (children.isEmpty)
partial + "/>"
else
partial + '>' + children.toString + "</" + name.toString + '>'
partial + '>' + children.toString + "</" + qname + '>'
}
def toGroup = Group(this)
@@ -59,7 +59,7 @@ package object antixml {
* For example: `ns \ "name"`
*/
implicit def stringToSelector(name: String): Selector[Elem] =
Selector({ case e @ Elem(QName(_, `name`), _, _, _) => e }, Some(name))
Selector({ case e @ Elem(_, `name`, _, _, _) => e }, Some(name))
/**
* Implicitly lifts a [[scala.Symbol]] into an instance of [[com.codecommit.antixml.Selector]]
@@ -76,14 +76,14 @@ class ConversionSpecs extends Specification with ScalaCheck {
"convert elem names without namespaces" in {
val e = <test/>.anti
e.name.prefix mustEqual None
e.name.name mustEqual "test"
e.prefix mustEqual None
e.name mustEqual "test"
}
"convert elem names with namespaces" in {
val e = <w:test/>.anti
e.name.prefix mustEqual Some("w")
e.name.name mustEqual "test"
e.prefix mustEqual Some("w")
e.name mustEqual "test"
}
"convert elem attributes" in {
@@ -95,16 +95,16 @@ class ConversionSpecs extends Specification with ScalaCheck {
val e = <test>Text1<child/>Text2</test>.anti
e.children must have size(3)
e.children(0) mustEqual Text("Text1")
e.children(1) mustEqual Elem("child", Attributes(), Map(), Group())
e.children(1) mustEqual Elem(None, "child", Attributes(), Map(), Group())
e.children(2) mustEqual Text("Text2")
}
"convert NodeSeq" in {
xml.NodeSeq.fromSeq(Nil).anti mustEqual Group()
val result = xml.NodeSeq.fromSeq(List(<test1/>, <test2/>, xml.Text("text"))).anti
val expected = Group(Elem("test1", Attributes(), Map(), Group()),
Elem("test2", Attributes(), Map(), Group()),
val expected = Group(Elem(None, "test1", Attributes(), Map(), Group()),
Elem(None, "test2", Attributes(), Map(), Group()),
Text("text"))
result mustEqual expected
@@ -119,5 +119,7 @@ class GroupSpecs extends Specification with ScalaCheck with XMLGenerators with U
}
}
def elem(name: String, children: Node*) = Elem(name, Attributes(), Map(), Group(children: _*))
def elem(name: String, children: Node*) = Elem(None, name, Attributes(), Map(), Group(children: _*))
def elem(qname : QName, children: Node*) = Elem(qname.prefix, qname.name, Attributes(), Map(), Group(children: _*))
}
@@ -44,7 +44,7 @@ class NodeSpecs extends Specification with DataTables {
"&" !! "<foo bar=\"&amp;\"/>" |
"'" !! "<foo bar=\"&apos;\"/>" |
"<" !! "<foo bar=\"&lt;\"/>" |
">" !! "<foo bar=\"&gt;\"/>" | { (c, r) => Elem("foo", Attributes("bar" -> c), Map(), Group()).toString mustEqual r }
">" !! "<foo bar=\"&gt;\"/>" | { (c, r) => Elem(None, "foo", Attributes("bar" -> c), Map(), Group()).toString mustEqual r }
}
"select against self" in {
@@ -35,15 +35,15 @@ class SAXSpecs extends Specification {
"SAXParser" should {
"parse a simpleString and generate a single Elem" in {
SAXParser.fromString("<a/>") mustEqual Elem("a", Attributes(), Map(), Group())
SAXParser.fromString("<a/>") mustEqual Elem(None, "a", Attributes(), Map(), Group())
}
"parse a simpleString and generate a single Elem even with namespaces" in {
SAXParser.fromString("<pf:a xmlns:pf='urn:a'/>") mustEqual Elem(QName(Some("pf"), "a"), Attributes(), Map("pf" -> "urn:a"), Group())
SAXParser.fromString("<pf:a xmlns:pf='urn:a'/>") mustEqual Elem(Some("pf"), "a", Attributes(), Map("pf" -> "urn:a"), Group())
}
"parse a String and generate an Elem" in {
SAXParser.fromString("<p:a xmlns:p='ns'>hi<b attr='value' /> there</p:a>") mustEqual Elem(QName(Some("p"), "a"), Attributes(), Map("p"->"ns"), Group(Text("hi"), Elem("b", Attributes("attr" -> "value"), Map("p"->"ns"), Group()), Text(" there")))
SAXParser.fromString("<p:a xmlns:p='ns'>hi<b attr='value' /> there</p:a>") mustEqual Elem(Some("p"), "a", Attributes(), Map("p"->"ns"), Group(Text("hi"), Elem(None, "b", Attributes("attr" -> "value"), Map("p"->"ns"), Group()), Text(" there")))
}
}
}
@@ -36,7 +36,7 @@ class StAXSpecs extends Specification {
"StAXParser" should {
"parse a StreamSource and generate an Elem" in {
StAXParser.fromString("<a:a xmlns:a='a'>hi<b attr='value' /> there</a:a>") mustEqual Elem(QName(Some("a"), "a"), Attributes(), Map("a" -> "a"), Group(Text("hi"), Elem("b", Attributes("attr" -> "value"), Map("a" -> "a"), Group()), Text(" there")))
StAXParser.fromString("<a:a xmlns:a='a'>hi<b attr='value' /> there</a:a>") mustEqual Elem(Some("a"), "a", Attributes(), Map("a" -> "a"), Group(Text("hi"), Elem(None, "b", Attributes("attr" -> "value"), Map("a" -> "a"), Group()), Text(" there")))
}
}
}
@@ -85,7 +85,7 @@ trait XMLGenerators {
attrs <- genAttributes
bindings <- genBindings
children <- if (depth > MaxGroupDepth) value(Group()) else (listOf(nodeGenerator(depth + 1)) map Group.fromSeq)
} yield Elem(QName(prefix, name), attrs, bindings, children)
} yield Elem(prefix, name, attrs, bindings, children)
lazy val textGenerator: Gen[Text] = genSaneString map Text
@@ -61,11 +61,11 @@ class XMLSpecs extends Specification {
"preserve prefixes" in {
val ns = "urn:my-urn:quux";
fromString("<my:test xmlns:my='urn:my-urn:quux'/>") mustEqual Elem(QName(Some("my"), "test"), Attributes(), Map("my" -> ns), Group[Node]())
fromString("<my:test xmlns:my='urn:my-urn:quux'/>") mustEqual Elem(Some("my"), "test", Attributes(), Map("my" -> ns), Group[Node]())
}
"parse prefixes" in {
fromString("<my:test xmlns:my='urn:my-urn:quux'></my:test>").name.name mustEqual "test"
fromString("<my:test xmlns:my='urn:my-urn:quux'></my:test>").name mustEqual "test"
}
"serialize prefixes" in {
@@ -87,5 +87,5 @@ class XMLSpecs extends Specification {
}
}
def elem(name: QName, children: Node*) = Elem(name, Attributes(), Map(), Group(children: _*))
def elem(name: QName, children: Node*) = Elem(name.prefix, name.name, Attributes(), Map(), Group(children: _*))
}
@@ -65,8 +65,8 @@ class ZipperSpecs extends Specification {
val bookstore2: Group[Node] = authors.updated(0, author0).updated(2, author2).updated(3, author3).unselect.unselect
// find afresh without using \
bookstore2.head.asInstanceOf[Elem].name.name mustEqual "bookstore"
bookstore2.head.asInstanceOf[Elem].children(0).asInstanceOf[Elem].name.name mustEqual "book"
bookstore2.head.asInstanceOf[Elem].name mustEqual "bookstore"
bookstore2.head.asInstanceOf[Elem].children(0).asInstanceOf[Elem].name mustEqual "book"
bookstore2.head.asInstanceOf[Elem].children(0).asInstanceOf[Elem].children must haveSize(2)
bookstore2.head.asInstanceOf[Elem].children(0).asInstanceOf[Elem].children(1).asInstanceOf[Elem].attrs mustEqual Attributes("updated" -> "yes")
@@ -95,7 +95,7 @@ class ZipperSpecs extends Specification {
"rebuild after a flatMap at the first level" in {
val books = bookstore \ "book"
val books2 = books flatMap {
case e @ Elem(_, _, _, children) if children.length > 2 =>
case e @ Elem(_, _, _, _, children) if children.length > 2 =>
List(e.copy(attrs=Attributes("updated" -> "yes")), e.copy(attrs=Attributes("updated" -> "yes")))
case _ => Nil
@@ -117,7 +117,7 @@ class ZipperSpecs extends Specification {
bookElem <- bookstore \ "book"
title <- bookElem \ "title" \ text
if !title.trim.isEmpty
val filteredChildren = bookElem.children filter { case Elem(QName(None, "title"), _, _, _) => false case _ => true }
val filteredChildren = bookElem.children filter { case Elem(None, "title", _, _, _) => false case _ => true }
} yield bookElem.copy(attrs=(bookElem.attrs + ("title" -> title)), children=filteredChildren)
val bookstore2 = titledBooks.unselect

0 comments on commit 0e885a3

Please sign in to comment.