diff options
author | buraq <buraq@epfl.ch> | 2004-06-08 18:06:10 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-06-08 18:06:10 +0000 |
commit | 4a3559d00506505a2eb571c92b263cf5b080a669 (patch) | |
tree | 01ba34c63a24c2deaca2515b2bc43ffbd23b53d2 /sources | |
parent | 2c7b4a9d136c53011d65d46893fddc754e6231d9 (diff) | |
download | scala-4a3559d00506505a2eb571c92b263cf5b080a669.tar.gz scala-4a3559d00506505a2eb571c92b263cf5b080a669.tar.bz2 scala-4a3559d00506505a2eb571c92b263cf5b080a669.zip |
xml changes - nodeseq supports comprehension, p...
xml changes - nodeseq supports comprehension, prettyprinter.format
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/xml/Node.scala | 2 | ||||
-rw-r--r-- | sources/scala/xml/NodeSeq.scala | 59 | ||||
-rw-r--r-- | sources/scala/xml/PrettyPrinter.scala | 6 |
3 files changed, 42 insertions, 25 deletions
diff --git a/sources/scala/xml/Node.scala b/sources/scala/xml/Node.scala index 31fd7174a5..ac00ea1fce 100644 --- a/sources/scala/xml/Node.scala +++ b/sources/scala/xml/Node.scala @@ -37,7 +37,7 @@ trait Node { def child: Seq[Node]; /** descendant axis (all descendants of this node) */ - def descendant: List[Node] = child.toList.flatMap { + def descendant:List[Node] = child.toList.flatMap { x => x::x.descendant } ; diff --git a/sources/scala/xml/NodeSeq.scala b/sources/scala/xml/NodeSeq.scala index 73d15843ea..5b5941b4ed 100644 --- a/sources/scala/xml/NodeSeq.scala +++ b/sources/scala/xml/NodeSeq.scala @@ -9,34 +9,47 @@ package scala.xml ; -/* a wrapper that adds some XPath navigation method */ +/** a wrapper around Seq[Node] that adds XPath and comprehension methods */ class NodeSeq( theSeq:Seq[Node] ) extends Seq[Node] { + def length = theSeq.length; def elements = theSeq.elements ; def apply( i:int ) = theSeq.apply( i ); - /** projection function. Similar to XPath, use this./'foo to get a list + /** structural equality */ + override def equals( x:Any ) = x match { + case z:Node => ( length == 1 ) && z == apply( 0 ) + case z:Seq[Node] => sameElements( z ) + case _ => false; + } + + /** projection function. Similar to XPath, use this \ "foo" to get a list + * 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 <- new NodeSeq( x.child ) ) + yield y + case _ => for( val x <- this; + val y <- new NodeSeq( x.child ); + y.label == that ) + yield { y } + } + + /** projection function. Similar to XPath, use this \\ 'foo to get a list * of all elements of this sequence that are labelled with "foo". - * Use /'_ as a wildcard. The document order is preserved. + * Use \ "_" as a wildcard. The document order is preserved. */ - def \ (that: String):NodeSeq = { - that match { - case "_" => - val it = elements.flatMap { x:Node => x.child.elements }; - new NodeSeq( it.toList ); - case _ => - val list = elements.flatMap( y => - y.child.elements.filter{ z:Node => z.label == that }).toList; - new NodeSeq( list ); - } - } - - def \\ ( that:String ):NodeSeq = { - val list = (elements.flatMap { x:Node => x.descendant_or_self.elements }).toList; - that match { - case "_" => new NodeSeq( list ); - case _ => new NodeSeq( list.filter { z:Node => z.label == that }); - } + + def \\ ( that:String ):NodeSeq = that match { + case "_" => for( val x <- this; + val y <- new NodeSeq( x.descendant_or_self )) + yield { y } + case _ => for( val x <- this; + val y <- new NodeSeq( x.descendant_or_self ); + y.label == that) + yield { y } } override def toString() = theSeq.elements.foldLeft ("") { @@ -57,4 +70,8 @@ class NodeSeq( theSeq:Seq[Node] ) extends Seq[Node] { new NodeSeq( asList flatMap { x => f(x).asList }) } + def filter( f:Node => boolean ):NodeSeq = { + new NodeSeq( asList filter f ) + } + } diff --git a/sources/scala/xml/PrettyPrinter.scala b/sources/scala/xml/PrettyPrinter.scala index e62139b3aa..b6a6e4a525 100644 --- a/sources/scala/xml/PrettyPrinter.scala +++ b/sources/scala/xml/PrettyPrinter.scala @@ -111,7 +111,7 @@ class PrettyPrinter( width:Int, step:Int ) { /* returns a formatted string containing well-formed XML **/ - def toPrettyXML( n:Node ):String = { + def format( n:Node ):String = { reset(); traverse( n, 0 ); val sb = new StringBuffer(); @@ -135,10 +135,10 @@ class PrettyPrinter( width:Int, step:Int ) { /* returns a formatted string containing well-formed XML nodes. **/ - def toPrettyXML( ns:Seq[Node] ):String = { + def format( ns:Seq[Node] ):String = { var sb2 = new StringBuffer(); for( val n <- ns.elements ) { - sb2.append( toPrettyXML( n )) + sb2.append( format( n )) } sb2.toString(); } |