summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/NodeSeq.scala
blob: 1316c515785cccf71be5de302ea23f1a8061d2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.xml


/** This object ...
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
object NodeSeq {
  final val Empty = new NodeSeq { def theSeq = Nil }
  def fromSeq(s: Seq[Node]): NodeSeq = new NodeSeq {
    def theSeq = s
  }
  implicit def view(s: Seq[Node]): NodeSeq = fromSeq(s)
}

/** This class implements a wrapper around <code>Seq[Node]</code> that
 *  adds XPath and comprehension methods.
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
abstract class NodeSeq extends Seq[Node] {
  import NodeSeq.view // import view magic for NodeSeq wrappers
  def theSeq: Seq[Node]
  def length = theSeq.length
  override def elements = theSeq.elements
  def apply(i: Int): Node = theSeq.apply(i)

  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)
    case z:Seq[_]    => sameElements(z)
    case z:String    => text == z
    case _           => false;
  }

  /** Projection function. Similar to XPath, use <code>this \ "foo"</code>
   *  to get a list of all elements of this sequence that are labelled with
   *  <code>"foo"</code>. Use <code>\ "_"</code> as a wildcard. Use
   *  <code>ns \ "@foo"</code> to get the unprefixed attribute "foo".
   *  Use <code>ns \ "@{uri}foo"</code> to get the prefixed attribute
   *  "pre:foo" whose prefix "pre" is resolved to the namespace "uri".
   *  For attribute projections, the resulting NodeSeq attribute values are
   *  wrapped in a Group.
   *  There is no support for searching a prefixed attribute by
   *  its literal prefix.
   *  The document order is preserved.
   *
   *  @param that ...
   *  @return     ...
   */
  def \(that: String): NodeSeq = that match {
    case "_" =>
      var zs: List[Node] = Nil
      val it = this.elements
      while (it.hasNext) {
        val x = it.next
        val jt = x.child.elements
        while (jt.hasNext) {
          val y = jt.next
          if (y.typeTag$ != -1)
            zs = y::zs
        }
      }
      NodeSeq.fromSeq(zs.reverse)

    case _ if (that.charAt(0) == '@') && (this.length == 1) =>
      if (that.length() == 1)
        throw new IllegalArgumentException(that)
      if (that.charAt(1) == '{') {
        val i = that.indexOf('}')
        if (i == -1)
          throw new IllegalArgumentException(that)
        val (uri, key) = (that.substring(2,i), that.substring(i+1, that.length()))
        if (uri == "" || key == "")
          throw new IllegalArgumentException(that)
        val y = this(0)
        y.attribute(uri, key) match {
          case Some(x) => Group(x)
          case _       => NodeSeq.Empty
        }
      } else {
        val k = that.substring(1)
        val y = this(0)
        y.attribute(k) match {
          case Some(x) => Group(x)
          case _       => NodeSeq.Empty
        }
      }

    case _   =>
      var zs: List[Node] = Nil
      val it = this.elements
      while (it.hasNext) {
        val x = it.next
        val jt = x.child.elements
        while (jt.hasNext) {
          val y = jt.next
          if (y.label == that)
            zs = y::zs
        }
      }
      NodeSeq.fromSeq(zs.reverse)
  }

  /** projection function. Similar to XPath, use <code>this \\ 'foo</code>
   *  to get a list of all elements of this sequence that are labelled with
   *  <code>"foo"</code>. Use <code>\\ "_"</code> as a wildcard.  Use
   *  <code>ns \\ "@foo"</code> to get the unprefixed attribute "foo".
   *  Use <code>ns \\ "@{uri}foo"</code> to get each prefixed attribute
   *  "pre:foo" whose prefix "pre" is resolved to the namespace "uri".
   *  For attribute projections, the resulting NodeSeq attribute values are
   *  wrapped in a Group.
   *  There is no support for searching a prefixed attribute by
   *  its literal prefix.
   *  The document order is preserved.
   *
   *  @param that ...
   *  @return     ...
   */
  def \\ (that: String): NodeSeq = that match {
    case "_" =>
      var zs: List[Node] = Nil
      val it = this.elements
      while (it.hasNext) {
        val x = it.next
        val jt = x.descendant_or_self.elements
        while (jt.hasNext) {
          val y = jt.next
          if (y.typeTag$ != -1)
            zs = y::zs
        }
      }
      zs.reverse

    case _ if that.charAt(0) == '@' =>
      var zs: List[Node] = Nil
      val it = this.elements
      while (it.hasNext) {
        val x = it.next
        val jt = x.descendant_or_self.elements
        while (jt.hasNext) {
          val y = jt.next
          if (y.typeTag$ != -1) {
            val kt = (y \ that).elements
            while (kt.hasNext) {
              zs = (kt.next)::zs
            }
          }
        }
      }
      zs.reverse

    case _ =>
      var zs: List[Node] = Nil
      val it = this.elements
      while (it.hasNext) {
        val x = it.next
        val jt = x.descendant_or_self.elements
        while (jt.hasNext) {
          val y = jt.next
          if (y.typeTag$ != -1 && y.label == that)
            zs = y::zs
        }
      }
    zs.reverse
  }

  override def toString(): String = theSeq.elements.foldLeft ("") {
    (s: String, x: Node) => s + x.toString()
  }
/*
  def map(f: Node => NodeSeq): NodeSeq = flatMap(f)

  def flatMap(f: Node => NodeSeq): NodeSeq = {
    val y = toList flatMap { x => f(x).toList }
    y
  }

  override def filter(f: Node => Boolean): NodeSeq = {
    val x = toList filter f
    x
  }
*/

  def text: String = {
    val sb = new StringBuilder()
    val it = elements
    while (it.hasNext) {
      sb.append(it.next.text)
    }
    sb.toString()
  }
}