diff options
author | Burak Emir <emir@epfl.ch> | 2005-10-10 00:44:53 +0000 |
---|---|---|
committer | Burak Emir <emir@epfl.ch> | 2005-10-10 00:44:53 +0000 |
commit | 5af15214f17fb47eb8030899162111260333915a (patch) | |
tree | f40e57229a34d607df6b9e90b5d8079d4093a282 | |
parent | 265f05b5d7e515ff6e5a0e1b8f7d87948aaa9118 (diff) | |
download | scala-5af15214f17fb47eb8030899162111260333915a.tar.gz scala-5af15214f17fb47eb8030899162111260333915a.tar.bz2 scala-5af15214f17fb47eb8030899162111260333915a.zip |
new \ and \\ method behaviour added (attribute ...
new \ and \\ method behaviour added (attribute selection)
-rw-r--r-- | sources/scala/xml/MetaData.scala | 8 | ||||
-rw-r--r-- | sources/scala/xml/NodeSeq.scala | 40 | ||||
-rw-r--r-- | sources/scala/xml/Null.scala | 4 |
3 files changed, 36 insertions, 16 deletions
diff --git a/sources/scala/xml/MetaData.scala b/sources/scala/xml/MetaData.scala index e8ce25e884..aa2df08ec8 100644 --- a/sources/scala/xml/MetaData.scala +++ b/sources/scala/xml/MetaData.scala @@ -67,12 +67,20 @@ abstract class MetaData extends Iterable[MetaData] { /** shallow equals method */ def equals1(that:MetaData): Boolean; + /** filters this sequence of meta data */ + def filter(f:MetaData => Boolean): MetaData = { + if(f(this)) copy(next filter f) else next filter f; + } + /** returns key of this MetaData item */ def key: String; /** returns key of this MetaData item */ def value: String; + /** maps this sequence of meta data */ + def map(f: MetaData => Text): List[Text] = f(this)::(next map f); + /** returns Null or the next MetaData item */ def next: MetaData; diff --git a/sources/scala/xml/NodeSeq.scala b/sources/scala/xml/NodeSeq.scala index 56ab68a130..5dc85e88e5 100644 --- a/sources/scala/xml/NodeSeq.scala +++ b/sources/scala/xml/NodeSeq.scala @@ -19,7 +19,7 @@ object NodeSeq { /** a wrapper around Seq[Node] that adds XPath and comprehension methods */ abstract class NodeSeq extends Seq[Node] { - + import NodeSeq.view; // import view magic for NodeSeq wrappers def theSeq: Seq[Node]; def length = theSeq.length; def elements = theSeq.elements ; @@ -38,10 +38,17 @@ abstract class NodeSeq extends Seq[Node] { */ def \(that: String):NodeSeq = that match { case "_" => for( val x <- this; - val y <- new NodeSeq { val theSeq = x.child; }) - yield y + val y <- x.child: NodeSeq) + yield { y } + case _ if that.charAt(0) == '@' => + val attrib = that.substring(1); + (for( val x <- this; + val y <- x.child: NodeSeq; + val z <- y.attributes; + z.key == attrib ) + yield { Text(z.value) }): NodeSeq case _ => for( val x <- this; - val y <- new NodeSeq { val theSeq = x.child; }; + val y <- x.child: NodeSeq; y.label == that ) yield { y } } @@ -51,12 +58,19 @@ abstract class NodeSeq extends Seq[Node] { * Use \ "_" as a wildcard. The document order is preserved. */ - def \\ ( that:String ):NodeSeq = that match { + def \\ ( that:String ): NodeSeq = that match { case "_" => for( val x <- this; - val y <- new NodeSeq { val theSeq = x.descendant_or_self }) + val y <- x.descendant_or_self: NodeSeq ) yield { y } + case _ if that.charAt(0) == '@' => + val attrib = that.substring(1); + (for( val x <- this; + val y <- x.descendant_or_self: NodeSeq; + val z <- y.attributes; + z.key == attrib ) + yield { Text(z.value) }): NodeSeq case _ => for( val x <- this; - val y <- new NodeSeq { val theSeq = x.descendant_or_self }; + val y <- x.descendant_or_self: NodeSeq; y.label == that) yield { y } } @@ -71,16 +85,10 @@ abstract class NodeSeq extends Seq[Node] { _asList } - def map( f:Node => Node ):NodeSeq = { - new NodeSeq{ final def theSeq = NodeSeq.this.asList map f } - } + def map(f: Node => Node): NodeSeq = { val x = asList map f; x } - def flatMap( f:Node => NodeSeq ):NodeSeq = { - new NodeSeq{ final def theSeq = NodeSeq.this.asList flatMap { x => f(x).asList }} - } + def flatMap(f:Node => NodeSeq): NodeSeq = { val y = asList flatMap { x => f(x).asList }; y } - def filter( f:Node => boolean ):NodeSeq = { - new NodeSeq{ val theSeq = NodeSeq.this.asList filter f } - } + def filter(f:Node => Boolean): NodeSeq = { val x = asList filter f; x } } diff --git a/sources/scala/xml/Null.scala b/sources/scala/xml/Null.scala index 517fd53854..b6a7499c1d 100644 --- a/sources/scala/xml/Null.scala +++ b/sources/scala/xml/Null.scala @@ -10,6 +10,8 @@ case object Null extends MetaData { /** returns its argument */ def copy(next: MetaData) = next; + override def filter(f:MetaData => Boolean): MetaData = this; + /** returns null */ def getNamespace(owner: Node) = null; @@ -33,6 +35,8 @@ case object Null extends MetaData { def value = null; + override def map(f: MetaData => Text): List[Text] = Nil; + def next = null; /** null */ |