From 04fe66b3066a95dadd22e3ebdf3d6a6aafe6e623 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Mon, 12 Dec 2005 10:26:49 +0000 Subject: attributes can hold any values now --- sources/scala/xml/MetaData.scala | 9 +++++---- sources/scala/xml/NodeSeq.scala | 2 +- sources/scala/xml/PrefixedAttribute.scala | 14 +++++++++----- sources/scala/xml/UnprefixedAttribute.scala | 12 +++++++----- sources/scala/xml/Utility.scala | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/sources/scala/xml/MetaData.scala b/sources/scala/xml/MetaData.scala index 285e3eb850..74b8e2d850 100644 --- a/sources/scala/xml/MetaData.scala +++ b/sources/scala/xml/MetaData.scala @@ -114,7 +114,7 @@ abstract class MetaData extends Iterable[MetaData] { def key: String; /** returns key of this MetaData item */ - def value: String; + def value: Any; /** maps this sequence of meta data */ def map(f: MetaData => Text): List[Text] = f(this)::(next map f); @@ -123,14 +123,15 @@ abstract class MetaData extends Iterable[MetaData] { def next: MetaData; /** gets value of unqualified (unprefixed) attribute with given key */ - def getValue(key: String): String; + def getValue(key: String): Any; /** gets value of qualified (prefixed) attribute with given key */ - def getValue(namespace: String, owner: Node, key: String): String = + def getValue(namespace: String, owner: Node, key: String): Any = getValue(namespace, owner.scope, key); /** gets value of qualified (prefixed) attribute with given key */ - def getValue(namespace: String, scope: NamespaceBinding, key: String): String; + def getValue(namespace: String, scope: NamespaceBinding, key: String): Any; + override def hashCode(): Int; def toString1(): String = { diff --git a/sources/scala/xml/NodeSeq.scala b/sources/scala/xml/NodeSeq.scala index 84d37d5790..105c2e70b4 100644 --- a/sources/scala/xml/NodeSeq.scala +++ b/sources/scala/xml/NodeSeq.scala @@ -50,7 +50,7 @@ abstract class NodeSeq extends Seq[Node] { val y = this(0); val v = y.attribute(k); if( v != null ) { - res = NodeSeq.fromSeq(Seq.single(Text(v))); + res = NodeSeq.fromSeq(Seq.single(new Atom(v))); } case _ => diff --git a/sources/scala/xml/PrefixedAttribute.scala b/sources/scala/xml/PrefixedAttribute.scala index 4eb947e2c1..4afb7c6c3a 100644 --- a/sources/scala/xml/PrefixedAttribute.scala +++ b/sources/scala/xml/PrefixedAttribute.scala @@ -14,14 +14,18 @@ package scala.xml; */ class PrefixedAttribute(val pre: String, val key: String, - val value: String, + val value: Any, val next: MetaData) extends MetaData { +/* + // verify that value is a proper attribute value (references, no <) - Utility.checkAttributeValue(value) match { + // update: this should happen before the attribute is constructed. + Utility.checkAttributeValue(value.toString()) match { case null => ; case msg => throw new MalformedAttributeException(msg); } +*/ /** Returns a copy of this unprefixed attribute with the given * next field. @@ -52,11 +56,11 @@ class PrefixedAttribute(val pre: String, owner.getNamespace(pre); /** forwards the call to next */ - def getValue(key: String): String = next.getValue(key); + def getValue(key: String): Any = next.getValue(key); /** gets attribute value of qualified (prefixed) attribute with given key */ - def getValue(namespace: String, scope: NamespaceBinding, key: String): String = { + def getValue(namespace: String, scope: NamespaceBinding, key: String): Any = { if (key == this.key && scope.getURI(pre) == namespace) value else @@ -75,7 +79,7 @@ class PrefixedAttribute(val pre: String, sb.append(':'); sb.append(key); sb.append('='); - Utility.appendQuoted(value, sb); + Utility.appendAttributeValue(value.toString(), sb); } def wellformed(scope: NamespaceBinding): Boolean = { diff --git a/sources/scala/xml/UnprefixedAttribute.scala b/sources/scala/xml/UnprefixedAttribute.scala index 82d30ac804..a705c81ab5 100644 --- a/sources/scala/xml/UnprefixedAttribute.scala +++ b/sources/scala/xml/UnprefixedAttribute.scala @@ -12,14 +12,16 @@ package scala.xml; /** unprefixed attributes have the null namespace */ -class UnprefixedAttribute(val key: String, val value: String, val next: MetaData) extends MetaData { +class UnprefixedAttribute(val key: String, val value: Any, val next: MetaData) extends MetaData { + /* // verify that value is a proper attribute value (references, no <) + // this should happen before the attribute is constructed. Utility.checkAttributeValue(value) match { case null => ; case msg => throw new MalformedAttributeException(msg); } - +*/ /** returns a copy of this unprefixed attribute with the given next field*/ def copy(next: MetaData) = new UnprefixedAttribute(key, value, next); @@ -37,7 +39,7 @@ class UnprefixedAttribute(val key: String, val value: String, val next: MetaData * @param key * @return .. */ - def getValue(key: String): String = + def getValue(key: String): Any = if (key == this.key) value else next.getValue(key); /** @@ -48,7 +50,7 @@ class UnprefixedAttribute(val key: String, val value: String, val next: MetaData * @param key * @return .. */ - def getValue(namespace: String, scope: NamespaceBinding, key: String): String = + def getValue(namespace: String, scope: NamespaceBinding, key: String): Any = next.getValue(namespace, scope, key); override def hashCode() = @@ -60,7 +62,7 @@ class UnprefixedAttribute(val key: String, val value: String, val next: MetaData def toString1(sb:StringBuffer): Unit = { sb.append(key); sb.append('='); - Utility.appendQuoted(value, sb); + Utility.appendAttributeValue(value.toString(), sb); } def wellformed(scope: NamespaceBinding): Boolean = diff --git a/sources/scala/xml/Utility.scala b/sources/scala/xml/Utility.scala index 2737e75d58..29314774e4 100644 --- a/sources/scala/xml/Utility.scala +++ b/sources/scala/xml/Utility.scala @@ -190,6 +190,27 @@ object Utility extends AnyRef with parsing.TokenTests { sb.append(ch).append(s).append(ch) } + + /** + * Appends "s" if s does not contain ", 's' + * otherwise, and replaces < and & + * + * @param s + * @param sb + */ + def appendAttributeValue(s: String, sb: StringBuffer) = { + val ch = if (s.indexOf('"') == -1) '"' else '\''; + sb.append(ch); + val ss: Seq[Char] = s; + val it = ss.elements; + while(it.hasNext) it.next match { + case '<' => sb.append("<"); + case '&' => sb.append("&"); + case x => sb.append(x); + } + sb.append(ch) + } + /** * Appends "s" and escapes and " i s with \" * -- cgit v1.2.3