From 61622e42558ae96080b29af82b284316ed8021b8 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Wed, 11 Oct 2006 09:17:56 +0000 Subject: attribute nillable --- src/library/scala/xml/MetaData.scala | 13 +++---- src/library/scala/xml/Null.scala | 3 ++ src/library/scala/xml/PrefixedAttribute.scala | 48 +++++++++++++------------ src/library/scala/xml/UnprefixedAttribute.scala | 29 ++++++++------- 4 files changed, 52 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/library/scala/xml/MetaData.scala b/src/library/scala/xml/MetaData.scala index a2542f1c0c..64541ad3ca 100644 --- a/src/library/scala/xml/MetaData.scala +++ b/src/library/scala/xml/MetaData.scala @@ -54,12 +54,6 @@ abstract class MetaData extends Iterable[MetaData] { def isPrefixed: Boolean - //def containedIn(m:MetaData): Boolean - - //def deepCopy: MetaData - - //def deepCopy(tail:MetaData): MetaData - /** deep equals method */ override def equals(that: Any) = { that match { @@ -146,4 +140,11 @@ abstract class MetaData extends Iterable[MetaData] { def wellformed(scope: NamespaceBinding): Boolean + def remove(key:String): MetaData + + def remove(namespace: String, scope: NamespaceBinding, key: String): MetaData + + final def remove(namespace: String, owner: Node, key: String): MetaData = + remove(namespace, owner.scope, key) + } diff --git a/src/library/scala/xml/Null.scala b/src/library/scala/xml/Null.scala index 2cb81c4e3e..e5a4290055 100644 --- a/src/library/scala/xml/Null.scala +++ b/src/library/scala/xml/Null.scala @@ -73,4 +73,7 @@ case object Null extends MetaData { override def wellformed(scope: NamespaceBinding) = true + def remove(key: String) = this + + def remove(namespace: String, scope: NamespaceBinding, key: String) = this } diff --git a/src/library/scala/xml/PrefixedAttribute.scala b/src/library/scala/xml/PrefixedAttribute.scala index 7f8aefb313..96ba45c73c 100644 --- a/src/library/scala/xml/PrefixedAttribute.scala +++ b/src/library/scala/xml/PrefixedAttribute.scala @@ -13,23 +13,27 @@ package scala.xml import compat.StringBuilder -/** prefixed attributes always have a non-null namespace +/** prefixed attributes always have a non-null namespace. + * @param value the attribute value, which may not be null */ class PrefixedAttribute(val pre: String, val key: String, val value: Seq[Node], val next: MetaData) extends MetaData { + if(value == null) + throw new UnsupportedOperationException("value is null") + /** same as this(key, Utility.parseAttributeValue(value), next) */ def this(pre: String, key: String, value: String, next: MetaData) = this(pre, key, Utility.parseAttributeValue(value), next) - /* verify that value is a proper attribute value (references, no <) - Utility.checkAttributeValue(value) match { - case null => - case msg => throw new MalformedAttributeException(msg) - } - */ + /* + // the problem here is the fact that we cannot remove the proper attribute from + // next, and thus cannot guarantee that hashcodes are computed properly + def this(pre: String, key: String, value: scala.AllRef, next: MetaData) = + throw new UnsupportedOperationException("can't construct prefixed nil attributes") + */ /** Returns a copy of this unprefixed attribute with the given * next field. @@ -37,20 +41,6 @@ class PrefixedAttribute(val pre: String, def copy(next: MetaData) = new PrefixedAttribute(pre, key, value, next) - //** duplicates the MetaData (deep copy), not preserving order */ - //def deepCopy: MetaData = deepCopy(null) - - //** duplicates the MetaData (deep copy), prepending it to tail */ - /* - def deepCopy(tail: MetaData): MetaData = { - val md = copy(tail) - if (null == next) - md - else - next.deepCopy(md) - } - */ - def equals1(m: MetaData) = (m.isPrefixed && (m.asInstanceOf[PrefixedAttribute].pre == pre) && @@ -74,11 +64,14 @@ class PrefixedAttribute(val pre: String, /** returns true */ final def isPrefixed = true + /** returns the hashcode. + */ override def hashCode() = - pre.hashCode() * 41 + key.hashCode() * 7 + value.hashCode() * 3 + next.hashCode() + pre.hashCode() * 41 + key.hashCode() * 7 + next.hashCode() - def toString1(sb:StringBuilder): Unit = { + /** appends string representation of only this attribute to stringbuffer */ + def toString1(sb:StringBuilder): Unit = if(value!=null) { sb.append(pre) sb.append(':') sb.append(key) @@ -95,5 +88,14 @@ class PrefixedAttribute(val pre: String, next.wellformed(scope)) } + def remove(key: String) = + copy(next.remove(key)) + + def remove(namespace: String, scope: NamespaceBinding, key: String): MetaData = + if (key == this.key && scope.getURI(pre) == namespace) + next + else + next.remove(namespace, scope, key) + } diff --git a/src/library/scala/xml/UnprefixedAttribute.scala b/src/library/scala/xml/UnprefixedAttribute.scala index 103bf4a7de..cc5d002e84 100644 --- a/src/library/scala/xml/UnprefixedAttribute.scala +++ b/src/library/scala/xml/UnprefixedAttribute.scala @@ -13,20 +13,16 @@ package scala.xml import compat.StringBuilder -/** unprefixed attributes have the null namespace +/** unprefixed attributes have the null namespace, and no prefix field + * */ -class UnprefixedAttribute(val key: String, val value: Seq[Node], val next: MetaData) extends MetaData { +class UnprefixedAttribute(val key: String, val value: Seq[Node], next1: MetaData) extends MetaData { + + val next = if(value != null) next1 else next1.remove(key) /** same as this(key, Utility.parseAttributeValue(value), next) */ def this(key: String, value: String, next: MetaData) = - this(key, Utility.parseAttributeValue(value), next) - - /* verify that value is a proper attribute value (references, no <) - Utility.checkAttributeValue(value) match { - case null => - case msg => throw new MalformedAttributeException(msg) - } -*/ + this(key, if(value!=null) Utility.parseAttributeValue(value) else {val z:NodeSeq=null;z}, next) /** returns a copy of this unprefixed attribute with the given next field*/ def copy(next: MetaData) = @@ -59,13 +55,16 @@ class UnprefixedAttribute(val key: String, val value: Seq[Node], val next: MetaD def apply(namespace: String, scope: NamespaceBinding, key: String): Seq[Node] = next(namespace, scope, key) + /** returns the hashcode. + */ override def hashCode() = - key.hashCode() * 7 + value.hashCode() * 53 + next.hashCode() + key.hashCode() * 7 + { if(value!=null) value.hashCode() * 53 else 0 } + next.hashCode() /** returns false */ final def isPrefixed = false - def toString1(sb:StringBuilder): Unit = { + /** appends string representation of only this attribute to stringbuffer */ + def toString1(sb:StringBuilder): Unit = if(value!=null) { sb.append(key) sb.append('=') val sb2 = new StringBuilder() @@ -78,5 +77,11 @@ class UnprefixedAttribute(val key: String, val value: Seq[Node], val next: MetaD def wellformed(scope: NamespaceBinding): Boolean = (null == next(null, scope, key)) && next.wellformed(scope) + def remove(key: String) = + if(this.key == key) next else copy(next.remove(key)) + + def remove(namespace: String, scope: NamespaceBinding, key: String): MetaData = + next.remove(namespace, scope, key) + } -- cgit v1.2.3