Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Moved QName prefix ahead of name in parameter ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed May 11, 2011
1 parent d7c527f commit d13ab35
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/codecommit/antixml/Attributes.scala
Expand Up @@ -138,7 +138,7 @@ object Attributes {
new Builder[(String, String), Attributes] {
def +=(pair: (String, String)) = {
val (key, value) = pair
delegate += (QName(None, key, None) -> value)
delegate += (QName(None, None, key) -> value)
this
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/codecommit/antixml/NodeSeqSAXHandler.scala
Expand Up @@ -93,7 +93,7 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
if (back == localName) None else Some(back.substring(0, back.length - localName.length -1))
}

map + (QName(ns, localName, prefix) -> attrs.getValue(i))
map + (QName(ns, prefix, localName) -> attrs.getValue(i))
}

builders ::= VectorCase.newBuilder
Expand All @@ -103,7 +103,7 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
}
val ns = if (uri == "") None else Some(uri)

Elem(QName(ns, localName, prefix), map, scopes.top, children)
Elem(QName(ns, prefix, localName), map, scopes.top, children)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/com/codecommit/antixml/QName.scala
Expand Up @@ -28,10 +28,10 @@

package com.codecommit.antixml

case class QName(ns: Option[String], name: String, prefix: Option[String]) {
case class QName(ns: Option[String], prefix: Option[String], name: String) {
override def toString = (prefix map { _.toString + ':' } getOrElse "") + name
}

object QName extends ((Option[String], String, Option[String]) => QName) {
implicit def stringToQName(str: String) = QName(None, str, None)
object QName extends ((Option[String], Option[String], String) => QName) {
implicit def stringToQName(str: String) = QName(None, None, str)
}
4 changes: 2 additions & 2 deletions src/main/scala/com/codecommit/antixml/StAXParser.scala
Expand Up @@ -75,7 +75,7 @@ class StAXParser extends XMLParser {
children += Text(text.result)
text.clear()
}
ancestors.head += Elem(QName(elem.ns, elem.name, elem.prefix), elem.attrs, prefixMapping.pop, Group fromSeq children.result)
ancestors.head += Elem(QName(elem.ns, elem.prefix, elem.name), elem.attrs, prefixMapping.pop, Group fromSeq children.result)
elems = parents
results = ancestors
}
Expand All @@ -101,7 +101,7 @@ class StAXParser extends XMLParser {
val back = xmlReader.getAttributePrefix(i)
if (back == null || back == "") None else Some(back)
}
attrs = attrs + (QName(ns, localName, prefix) -> xmlReader.getAttributeValue(i))
attrs = attrs + (QName(ns, prefix, localName) -> xmlReader.getAttributeValue(i))
i = i + 1
}
val uri = xmlReader.getNamespaceURI
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/codecommit/antixml/conversion.scala
Expand Up @@ -119,13 +119,13 @@ object XMLConvertable extends SecondPrecedenceConvertables {
val ns = if (e.namespace == null) None else Some(e.namespace)

val attrs = (Attributes() /: e.attributes) {
case (attrs, pa: xml.PrefixedAttribute) => attrs + (QName(Some(e.scope.getURI(pa.pre)), pa.key, Some(pa.pre)) -> pa.value.mkString)
case (attrs, pa: xml.PrefixedAttribute) => attrs + (QName(Some(e.scope.getURI(pa.pre)), Some(pa.pre), pa.key) -> pa.value.mkString)
case (attrs, ua: xml.UnprefixedAttribute) => attrs + (ua.key -> ua.value.mkString)
case (attrs, _) => attrs
}

val children = NodeSeqConvertable(xml.NodeSeq fromSeq e.child)
Elem(QName(ns, e.label, prefix), attrs, Map(), children)
Elem(QName(ns, prefix, e.label), attrs, Map(), children)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/codecommit/antixml/package.scala
Expand Up @@ -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(QName(_, _, `name`), _, _, _) => e }, Some(name))

/**
* Implicitly lifts a [[scala.Symbol]] into an instance of [[com.codecommit.antixml.Selector]]
Expand All @@ -85,7 +85,7 @@ package object antixml {
// I feel justified in this global implicit since it doesn't pimp anything
implicit def stringTupleToQNameTuple(pair: (String, String)): (QName, String) = {
val (key, value) = pair
(QName(None, key, None), value)
(QName(None, None, key), value)
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/com/codecommit/antixml/AttributesSpecs.scala
Expand Up @@ -56,16 +56,16 @@ class AttributesSpecs extends Specification with ScalaCheck with XMLGenerators {
val attrsSafe = attrs - name
val attrs2 = attrsSafe + (name -> value)
attrs2 must havePairs(attrsSafe.toSeq: _*)
attrs2 must havePair(QName(None, name, None) -> value)
attrs2 must havePair(QName(None, None, name) -> value)
}

"produce most specific Map with non-String value" in check { attrs: Attributes =>
val value = new AnyRef
val attrsSafe = attrs - "foo"
val attrs2 = attrsSafe + (QName(None, "foo", None) -> value)
val attrs2 = attrsSafe + (QName(None, None, "foo") -> value)
validate[Map[QName, AnyRef]](attrs2)
attrs2 must havePairs(attrsSafe.toSeq: _*)
attrs2 must havePair(QName(None, "foo", None) -> value)
attrs2 must havePair(QName(None, None, "foo") -> value)
}

"support removal of qname attrs" in {
Expand Down Expand Up @@ -94,21 +94,21 @@ class AttributesSpecs extends Specification with ScalaCheck with XMLGenerators {

"support retrieval of attributes by string" in check { (attrs: Attributes, key: String) =>
val result = attrs get key
result mustEqual (attrs find { case (QName(None, `key`, None), _) => true case _ => false })
result mustEqual (attrs find { case (QName(None, None, `key`), _) => true case _ => false })
}

"produce Attributes from collection utility methods returning compatible results" in {
val attrs = Attributes("foo" -> "bar", "baz" -> "bin")
val attrs2 = attrs map { case (k, v) => k -> (v + "42") }
validate[Attributes](attrs2)
attrs2 must havePairs(QName(None, "foo", None) -> "bar42", QName(None, "baz", None) -> "bin42")
attrs2 must havePairs(QName(None, None, "foo") -> "bar42", QName(None, None, "baz") -> "bin42")
}

"produce Map from collection utility methods returning incompatible results" in {
val attrs = Attributes("foo" -> "bar", "baz" -> "bin")
val attrs2 = attrs map { case (k, v) => k -> 42 }
validate[Map[QName, Int]](attrs2)
attrs2 must havePairs(QName(None, "foo", None) -> 42, QName(None, "baz", None) -> 42)
attrs2 must havePairs(QName(None, None, "foo") -> 42, QName(None, None, "baz") -> 42)
}
}

Expand Down
Expand Up @@ -91,7 +91,7 @@ class ConversionSpecs extends Specification with ScalaCheck {

"convert elem attributes" in {
(<test/>).anti.attrs mustEqual Map()
(<test a:c="1" b="foo" xmlns:a="a"/>).anti.attrs mustEqual Attributes(QName(Some("a"), "c", Some("a")) -> "1", "b" -> "foo")
(<test a:c="1" b="foo" xmlns:a="a"/>).anti.attrs mustEqual Attributes(QName(Some("a"), Some("a"), "c") -> "1", "b" -> "foo")
}

"convert elem children" in {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/codecommit/antixml/GroupSpecs.scala
Expand Up @@ -119,5 +119,5 @@ class GroupSpecs extends Specification with ScalaCheck with XMLGenerators with U
}
}

def elem(name: String, children: Node*) = Elem(QName(None, name, None), Attributes(), Map(), Group(children: _*))
def elem(name: String, children: Node*) = Elem(name, Attributes(), Map(), Group(children: _*))
}
4 changes: 2 additions & 2 deletions src/test/scala/com/codecommit/antixml/SAXSpecs.scala
Expand Up @@ -42,11 +42,11 @@ class SAXSpecs extends Specification {
}

"parse a simpleString and generate a single Elem even with namespaces" in {
SAXParser.fromString("<pf:a xmlns:pf='urn:a'/>") mustEqual Elem(QName(Some("urn:a"), "a", Some("pf")), Attributes(), Map("pf" -> "urn:a"), Group())
SAXParser.fromString("<pf:a xmlns:pf='urn:a'/>") mustEqual Elem(QName(Some("urn:a"), 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("ns"), "a", Some("p")), 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(QName(Some("ns"), Some("p"), "a"), Attributes(), Map("p"->"ns"), Group(Text("hi"), Elem("b", Attributes("attr" -> "value"), Map("p"->"ns"), Group()), Text(" there")))
}
}
}
2 changes: 1 addition & 1 deletion src/test/scala/com/codecommit/antixml/StAXSpecs.scala
Expand Up @@ -38,7 +38,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", Some("a")), Attributes(), Map("a" -> "a"), Group(Text("hi"), Elem(QName(None, "b", None), 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(QName(Some("a"), Some("a"), "a"), Attributes(), Map("a" -> "a"), Group(Text("hi"), Elem("b", Attributes("attr" -> "value"), Map("a" -> "a"), Group()), Text(" there")))
}
}
}
4 changes: 2 additions & 2 deletions src/test/scala/com/codecommit/antixml/XMLGenerators.scala
Expand Up @@ -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(ns, name, prefix), attrs, bindings, children)
} yield Elem(QName(ns, prefix, name), attrs, bindings, children)

lazy val textGenerator: Gen[Text] = genSaneString map Text

Expand Down Expand Up @@ -117,5 +117,5 @@ trait XMLGenerators {
private lazy val genQName: Gen[QName] = for {
ns <- genSaneOptionString
name <- genSaneString
} yield QName(ns, name, None)
} yield QName(ns, None, name)
}
2 changes: 1 addition & 1 deletion src/test/scala/com/codecommit/antixml/XMLSpecs.scala
Expand Up @@ -63,7 +63,7 @@ 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(ns), "test", Some("my")), Attributes(), Map("my" -> ns), Group[Node]());
fromString("<my:test xmlns:my='urn:my-urn:quux'/>") mustEqual Elem(QName(Some(ns), Some("my"), "test"), Attributes(), Map("my" -> ns), Group[Node]())
}

"parse prefixes" in {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/codecommit/antixml/ZipperSpecs.scala
Expand Up @@ -120,7 +120,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", None), _, _, _) => false case _ => true }
val filteredChildren = bookElem.children filter { case Elem(QName(None, None, "title"), _, _, _) => false case _ => true }
} yield bookElem.copy(attrs=(bookElem.attrs + ("title" -> title)), children=filteredChildren)

val bookstore2 = titledBooks.unselect
Expand Down

2 comments on commit d13ab35

@jespersm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I thought about doing this, but didn't get around to it before I got too tired.

@djspiewak
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-) I know that feeling…

Please sign in to comment.