summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-04-30 22:09:48 +0000
committerPaul Phillips <paulp@improving.org>2009-04-30 22:09:48 +0000
commit8be38d4395c649746b46f6f10748fd561a672472 (patch)
tree60cc42ed53a40d538fca8cced06c9db38f3edca3 /src/library
parent8a9a104f79ba2edbb91c27f6eb3bdb98d510fbad (diff)
downloadscala-8be38d4395c649746b46f6f10748fd561a672472.tar.gz
scala-8be38d4395c649746b46f6f10748fd561a672472.tar.bz2
scala-8be38d4395c649746b46f6f10748fd561a672472.zip
Factored common interface of UnprefixedAttribut...
Factored common interface of UnprefixedAttribute and PrefixedAttribute out into an Attribute trait; added Attribute object with various convenience methods for creations; created extractors for all three.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/xml/Attribute.scala61
-rw-r--r--src/library/scala/xml/PrefixedAttribute.scala18
-rw-r--r--src/library/scala/xml/UnprefixedAttribute.scala12
3 files changed, 81 insertions, 10 deletions
diff --git a/src/library/scala/xml/Attribute.scala b/src/library/scala/xml/Attribute.scala
new file mode 100644
index 0000000000..261e9d9c86
--- /dev/null
+++ b/src/library/scala/xml/Attribute.scala
@@ -0,0 +1,61 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+// $Id$
+
+package scala.xml
+
+/** Attribute defines the interface shared by both
+ * PrefixedAttribute and UnprefixedAttribute
+ */
+
+object Attribute {
+ def unapply(x: Attribute) = x match {
+ case PrefixedAttribute(_, key, value, next) => Some(key, value, next)
+ case UnprefixedAttribute(key, value, next) => Some(key, value, next)
+ case _ => None
+ }
+
+ /** Convenience functions which choose Un/Prefixedness appropriately */
+ def apply(key: String, value: Seq[Node], next: MetaData): Attribute =
+ new UnprefixedAttribute(key, value, next)
+
+ def apply(pre: String, key: String, value: String, next: MetaData): Attribute =
+ if (pre == null || pre == "") new UnprefixedAttribute(key, value, next)
+ else new PrefixedAttribute(pre, key, value, next)
+
+ def apply(pre: String, key: String, value: Seq[Node], next: MetaData): Attribute =
+ if (pre == null || pre == "") new UnprefixedAttribute(key, value, next)
+ else new PrefixedAttribute(pre, key, value, next)
+
+ def apply(pre: Option[String], key: String, value: Seq[Node], next: MetaData): Attribute =
+ pre match {
+ case None => new UnprefixedAttribute(key, value, next)
+ case Some(p) => new PrefixedAttribute(p, key, value, next)
+ }
+}
+
+abstract trait Attribute extends MetaData
+{
+ val key: String
+ val value: Seq[Node]
+ val next: MetaData
+
+ def apply(key: String): Seq[Node]
+ def apply(namespace: String, scope: NamespaceBinding, key: String): Seq[Node]
+ def copy(next: MetaData): Attribute
+ def remove(key: String): MetaData
+ def remove(namespace: String, scope: NamespaceBinding, key: String): MetaData
+
+ def isPrefixed: Boolean
+ def getNamespace(owner: Node): String
+ def wellformed(scope: NamespaceBinding): Boolean
+
+ def equals1(m: MetaData): Boolean
+ def toString1(sb: StringBuilder): Unit
+} \ No newline at end of file
diff --git a/src/library/scala/xml/PrefixedAttribute.scala b/src/library/scala/xml/PrefixedAttribute.scala
index 11f4f2dab3..13ed201634 100644
--- a/src/library/scala/xml/PrefixedAttribute.scala
+++ b/src/library/scala/xml/PrefixedAttribute.scala
@@ -18,12 +18,14 @@ package scala.xml
* @param value the attribute value, which may not be null
* @param next ...
*/
-class PrefixedAttribute(val pre: String,
- val key: String,
- val value: Seq[Node],
- val next: MetaData) extends MetaData {
-
- if(value eq null)
+class PrefixedAttribute(
+ val pre: String,
+ val key: String,
+ val value: Seq[Node],
+ val next: MetaData)
+extends Attribute
+{
+ if (value eq null)
throw new UnsupportedOperationException("value is null")
/** same as this(key, Utility.parseAttributeValue(value), next) */
@@ -97,4 +99,6 @@ class PrefixedAttribute(val pre: String,
next.remove(namespace, scope, key)
}
-
+object PrefixedAttribute {
+ def unapply(x: PrefixedAttribute) = Some(x.pre, x.key, x.value, x.next)
+}
diff --git a/src/library/scala/xml/UnprefixedAttribute.scala b/src/library/scala/xml/UnprefixedAttribute.scala
index 4b4de6bb01..59d43d6856 100644
--- a/src/library/scala/xml/UnprefixedAttribute.scala
+++ b/src/library/scala/xml/UnprefixedAttribute.scala
@@ -15,8 +15,12 @@ package scala.xml
*
* @author Burak Emir
*/
-class UnprefixedAttribute(val key: String, val value: Seq[Node], next1: MetaData) extends MetaData {
-
+class UnprefixedAttribute(
+ val key: String,
+ val value: Seq[Node],
+ next1: MetaData)
+extends Attribute
+{
val next = if (value ne null) next1 else next1.remove(key)
/** same as this(key, Text(value), next) */
@@ -88,4 +92,6 @@ class UnprefixedAttribute(val key: String, val value: Seq[Node], next1: MetaData
next.remove(namespace, scope, key)
}
-
+object UnprefixedAttribute {
+ def unapply(x: UnprefixedAttribute) = Some(x.key, x.value, x.next)
+}