@@ -73,58 +73,35 @@ abstract class NodeSeq extends immutable.Sequence[Node] with SequenceTemplate[No
73
73
* @param that ...
74
74
* @return ...
75
75
*/
76
- def \ (that : String ): NodeSeq = that match {
77
- case " _" =>
78
- var zs : List [Node ] = Nil
79
- val it = this .iterator
80
- while (it.hasNext) {
81
- val x = it.next
82
- val jt = x.child.iterator
83
- while (jt.hasNext) {
84
- val y = jt.next
85
- if (y.typeTag$ != - 1 )
86
- zs = y:: zs
76
+ def \ (that : String ): NodeSeq = {
77
+ def atResult = {
78
+ def fail = throw new IllegalArgumentException (that)
79
+ lazy val y = this (0 )
80
+ val attr =
81
+ if (that.length == 1 ) fail
82
+ else if (that(1 ) == '{' ) {
83
+ val i = that indexOf '}'
84
+ if (i == - 1 ) fail
85
+ val (uri, key) = (that.substring(2 ,i), that.substring(i+ 1 , that.length()))
86
+ if (uri == " " || key == " " ) fail
87
+ else y.attribute(uri, key)
87
88
}
88
- }
89
- NodeSeq .fromSeq(zs.reverse)
90
-
91
- case _ if (that.charAt(0 ) == '@' ) && (this .length == 1 ) =>
92
- if (that.length() == 1 )
93
- throw new IllegalArgumentException (that)
94
- if (that.charAt(1 ) == '{' ) {
95
- val i = that.indexOf('}' )
96
- if (i == - 1 )
97
- throw new IllegalArgumentException (that)
98
- val (uri, key) = (that.substring(2 ,i), that.substring(i+ 1 , that.length()))
99
- if (uri == " " || key == " " )
100
- throw new IllegalArgumentException (that)
101
- val y = this (0 )
102
- y.attribute(uri, key) match {
103
- case Some (x) => Group (x)
104
- case _ => NodeSeq .Empty
105
- }
106
- } else {
107
- val k = that.substring(1 )
108
- val y = this (0 )
109
- y.attribute(k) match {
110
- case Some (x) => Group (x)
111
- case _ => NodeSeq .Empty
112
- }
113
- }
89
+ else y.attribute(that.substring(1 ))
114
90
115
- case _ =>
116
- var zs : List [Node ] = Nil
117
- val it = this .iterator
118
- while (it.hasNext) {
119
- val x = it.next
120
- val jt = x.child.iterator
121
- while (jt.hasNext) {
122
- val y = jt.next
123
- if (y.label == that)
124
- zs = y:: zs
125
- }
91
+ attr match {
92
+ case Some (x) => Group (x)
93
+ case _ => NodeSeq .Empty
126
94
}
127
- NodeSeq .fromSeq(zs.reverse)
95
+ }
96
+
97
+ def makeSeq (cond : (Node ) => Boolean ) =
98
+ NodeSeq fromSeq (this flatMap (_.child) filter cond)
99
+
100
+ that match {
101
+ case " _" => makeSeq(! _.isAtom)
102
+ case _ if (that(0 ) == '@' && this .length == 1 ) => atResult
103
+ case _ => makeSeq(_.label == that)
104
+ }
128
105
}
129
106
130
107
/** projection function. Similar to XPath, use <code>this \\ 'foo</code>
@@ -142,52 +119,13 @@ abstract class NodeSeq extends immutable.Sequence[Node] with SequenceTemplate[No
142
119
* @param that ...
143
120
* @return ...
144
121
*/
145
- def \\ (that : String ): NodeSeq = that match {
146
- case " _" =>
147
- var zs : List [Node ] = Nil
148
- val it = this .iterator
149
- while (it.hasNext) {
150
- val x = it.next
151
- val jt = x.descendant_or_self.iterator
152
- while (jt.hasNext) {
153
- val y = jt.next
154
- if (y.typeTag$ != - 1 )
155
- zs = y:: zs
156
- }
157
- }
158
- zs.reverse
159
-
160
- case _ if that.charAt(0 ) == '@' =>
161
- var zs : List [Node ] = Nil
162
- val it = this .iterator
163
- while (it.hasNext) {
164
- val x = it.next
165
- val jt = x.descendant_or_self.iterator
166
- while (jt.hasNext) {
167
- val y = jt.next
168
- if (y.typeTag$ != - 1 ) {
169
- val kt = (y \ that).iterator
170
- while (kt.hasNext) {
171
- zs = (kt.next):: zs
172
- }
173
- }
174
- }
175
- }
176
- zs.reverse
177
-
178
- case _ =>
179
- var zs : List [Node ] = Nil
180
- val it = this .iterator
181
- while (it.hasNext) {
182
- val x = it.next
183
- val jt = x.descendant_or_self.iterator
184
- while (jt.hasNext) {
185
- val y = jt.next
186
- if (y.typeTag$ != - 1 && y.label == that)
187
- zs = y:: zs
188
- }
189
- }
190
- zs.reverse
122
+ def \\ (that : String ): NodeSeq = {
123
+ def filt (cond : (Node ) => Boolean ) = this flatMap (_.descendant_or_self) filter cond
124
+ that match {
125
+ case " _" => filt(! _.isAtom)
126
+ case _ if that(0 ) == '@' => filt(! _.isAtom) flatMap (_ \ that)
127
+ case _ => filt(x => ! x.isAtom && x.label == that)
128
+ }
191
129
}
192
130
193
131
override def toString (): String = theSeq.iterator.foldLeft (" " ) {
0 commit comments