summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/NamespaceBinding.scala
blob: 618cb78dcc771588cb933ce12ddfa14456c35a8b (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2007, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.xml

import Predef._

/** The class <code>NamespaceBinding</code> represents namespace bindings
 *  and scopes. The binding for the default namespace is treated as a null
 *  prefix. the absent namespace is represented with the null uri. Neither
 *  prefix nor uri may be empty, which is not checked.
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
@serializable
class NamespaceBinding(val prefix: String,
                       val uri: String,
                       val parent: NamespaceBinding) extends AnyRef {

  private val serialVersionUID = 0 - 2518644165573446725L

  if (null != prefix && 0 == prefix.length())
    throw new IllegalArgumentException("zero length prefix not allowed")

  def getURI(_prefix: String): String =
    if (prefix == _prefix) uri else parent.getURI(_prefix)

  /** Returns some prefix that is mapped to the prefix.
   *
   *  @param _uri
   *  @return
   */
  def getPrefix(_uri: String): String =
    if (_uri == uri) uri else parent.getURI(_uri)

  override def toString(): String = {
    val sb = new StringBuilder()
    toString(sb, TopScope)
    sb.toString()
  }

  def toString(stop: NamespaceBinding): String = {
    val sb = new StringBuilder()
    toString(sb, stop)
    sb.toString()
  }

  def toString(sb: StringBuilder, stop: NamespaceBinding): Unit = {
    if (this ne stop) { // contains?
      sb.append(" xmlns")
      if (prefix ne null) {
        sb.append(':').append(prefix)
      }
      sb.append('=')
      .append('"')
      .append(uri)
      .append('"');
      parent.toString(sb, stop) // copy(ignore)
    }
  }

}