diff options
author | Burak Emir <emir@epfl.ch> | 2005-11-16 10:17:16 +0000 |
---|---|---|
committer | Burak Emir <emir@epfl.ch> | 2005-11-16 10:17:16 +0000 |
commit | d7d3c75f7092bcce1e6853b2286454d9da21fbe5 (patch) | |
tree | 3e175b7980df9da0420c1891a2d17953d5a5cffb /sources | |
parent | c3acfba19771b05097c2fe0a6926ed464409d5e6 (diff) | |
download | scala-d7d3c75f7092bcce1e6853b2286454d9da21fbe5.tar.gz scala-d7d3c75f7092bcce1e6853b2286454d9da21fbe5.tar.bz2 scala-d7d3c75f7092bcce1e6853b2286454d9da21fbe5.zip |
bugfix in method '\' for attributes
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/xml/MetaData.scala | 29 | ||||
-rw-r--r-- | sources/scala/xml/NodeSeq.scala | 55 |
2 files changed, 54 insertions, 30 deletions
diff --git a/sources/scala/xml/MetaData.scala b/sources/scala/xml/MetaData.scala index ec0d18568f..285e3eb850 100644 --- a/sources/scala/xml/MetaData.scala +++ b/sources/scala/xml/MetaData.scala @@ -41,7 +41,7 @@ abstract class MetaData extends Iterable[MetaData] { def hasNext = (Null != next); - def length: Int = length(1); + def length: Int = length(0); def length(i: Int): Int = next.length(i + 1); @@ -66,7 +66,7 @@ abstract class MetaData extends Iterable[MetaData] { } } - /** bq: this can be done better */ + /** returns an iterator on attributes */ def elements = new Iterator[MetaData] { var x: MetaData = MetaData.this; def hasNext = Null != x; @@ -77,6 +77,31 @@ abstract class MetaData extends Iterable[MetaData] { } } + /* returns a sequences of "pseudo nodes" that contain attribute info. + not sure if this useful, and it violates contract for nodes... + def nodes = { + class NodeProxy(last:MetaData) extends Node { + override def prefix = last match { + case p:PrefixedAttribute => p.pre; + case _ => null + } + override def label = "@"+last.key; + override def child = Text(last.value); + override def text = last.value + } + val ns = new Array[Node](this.length); + var i = 0; + val it = elements; + while(it.hasNext) { + val a = it.next; + ns(i) = new NodeProxy(a); + i = i + 1; + } + val seq = array2seq(ns); + NodeSeq.fromSeq(seq); + } + */ + /** shallow equals method */ def equals1(that: MetaData): Boolean; diff --git a/sources/scala/xml/NodeSeq.scala b/sources/scala/xml/NodeSeq.scala index b03fb0f3e4..84d37d5790 100644 --- a/sources/scala/xml/NodeSeq.scala +++ b/sources/scala/xml/NodeSeq.scala @@ -28,8 +28,8 @@ abstract class NodeSeq extends Seq[Node] { def apply(f: Node => Boolean): NodeSeq = filter(f); /** structural equality */ - override def equals( x:Any ) = x match { - case z:Node => ( length == 1 ) && z == apply( 0 ) + override def equals(x: Any) = x match { + case z:Node => (length == 1) && z == apply(0) case z:Seq[Node] => sameElements( z ) case z:String => text == z case _ => false; @@ -39,21 +39,25 @@ abstract class NodeSeq extends Seq[Node] { * of all elements of this sequence that are labelled with "foo". * Use \ "_" as a wildcard. The document order is preserved. */ - def \(that: String):NodeSeq = that match { - case "_" => for( val x <- this; - 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 <- x.child: NodeSeq; - y.label == that ) - yield { y } + def \(that: String):NodeSeq = { + var res: NodeSeq = NodeSeq.Empty; + that match { + case "_" => + res = for( val x <- this; val y <- x.child: NodeSeq) yield { y } + + case _ if (that.charAt(0) == '@') && (this.length == 1) => + val k = that.substring(1); + val y = this(0); + val v = y.attribute(k); + if( v != null ) { + res = NodeSeq.fromSeq(Seq.single(Text(v))); + } + + case _ => + res = for( val x <- this; val y <- x.child: NodeSeq; y.label == that ) + yield { y } + } + res } /** projection function. Similar to XPath, use this \\ 'foo to get a list @@ -66,12 +70,11 @@ abstract class NodeSeq extends Seq[Node] { 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 + val attrib = that.substring(1); + (for(val x <- this; + val y <- x.descendant_or_self: NodeSeq; + val z <- y \ that) + yield { z }):NodeSeq case _ => for( val x <- this; val y <- x.descendant_or_self: NodeSeq; y.label == that) @@ -82,11 +85,7 @@ abstract class NodeSeq extends Seq[Node] { (s:String,x:Node) => s + x.toString() } - private var _asList:List[Node] = null; - def asList = { - if (_asList == null ) _asList = elements.toList; - _asList - } + def asList = elements.toList; def map(f: Node => Node): NodeSeq = { val x = asList map f; x } |