diff options
author | buraq <buraq@epfl.ch> | 2005-04-19 14:09:15 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2005-04-19 14:09:15 +0000 |
commit | 5662d8f94ee22defd6539290e339a45ee1586274 (patch) | |
tree | dd2baa6d1664a8ebedd8dd5b0c5aba6dfd9586f6 /sources | |
parent | d02399bd06ea475e7b6774bb1b4a8eb95e129bc7 (diff) | |
download | scala-5662d8f94ee22defd6539290e339a45ee1586274.tar.gz scala-5662d8f94ee22defd6539290e339a45ee1586274.tar.bz2 scala-5662d8f94ee22defd6539290e339a45ee1586274.zip |
fixed bug in new XML API related to namespaces
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/Predef.scala | 2 | ||||
-rw-r--r-- | sources/scala/xml/Elem.scala | 3 | ||||
-rw-r--r-- | sources/scala/xml/FactoryAdapter.scala | 16 | ||||
-rw-r--r-- | sources/scala/xml/NamespaceBinding.scala | 30 | ||||
-rw-r--r-- | sources/scala/xml/Node.scala | 2 | ||||
-rw-r--r-- | sources/scala/xml/PrettyPrinter.scala | 16 | ||||
-rw-r--r-- | sources/scala/xml/TopScope.scala | 10 | ||||
-rw-r--r-- | sources/scala/xml/Utility.scala | 134 |
8 files changed, 56 insertions, 157 deletions
diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala index d9ce59f82b..14959efc5e 100644 --- a/sources/scala/Predef.scala +++ b/sources/scala/Predef.scala @@ -43,7 +43,7 @@ object Predef { def scd[a](x: Any, y: a): a = y; val namespace$default = ""; - val $scope = null; + val $scope = scala.xml.TopScope; type Function[-a,+b] = Function1[a,b]; diff --git a/sources/scala/xml/Elem.scala b/sources/scala/xml/Elem.scala index 8f9208a6ee..747f0d5757 100644 --- a/sources/scala/xml/Elem.scala +++ b/sources/scala/xml/Elem.scala @@ -23,6 +23,9 @@ import scala.collection.mutable.ArrayBuffer; // "val" is redundant for non-overriding arguments case class Elem(override val prefix:String, val label: String, override val attributes: MetaData, override val scope: NamespaceBinding, val child: Node*) extends Node { + if(null== scope) + error("scope is null"); + //@todo: copy the children, // setting namespace scope if necessary // cleaning adjacent text nodes if necessary diff --git a/sources/scala/xml/FactoryAdapter.scala b/sources/scala/xml/FactoryAdapter.scala index 9b08612cfc..e120811269 100644 --- a/sources/scala/xml/FactoryAdapter.scala +++ b/sources/scala/xml/FactoryAdapter.scala @@ -103,8 +103,6 @@ abstract class FactoryAdapter extends DefaultHandler() { } } - protected def scope() = scopeStack.top; - //var elemCount = 0; //STATISTICS /* ContentHandler methods */ @@ -133,7 +131,7 @@ abstract class FactoryAdapter extends DefaultHandler() { hStack.push( null ); var m: MetaData = Null; - var scpe = scope(); + var scpe = scopeStack.top; for( val i <- List.range( 0, attributes.getLength() )) { //val attrType = attributes.getType(i); // unused for now val qname = attributes.getQName(i); @@ -141,12 +139,13 @@ abstract class FactoryAdapter extends DefaultHandler() { val colon = qname.indexOf(':'); if(-1 != colon) { // prefixed attribute val pre = qname.substring(0, colon); - val key = attributes.getLocalName(i); + val key = qname.substring(colon+1, qname.length()); if("xmlns" == pre) scpe = value.length() match { case 0 => new NamespaceBinding(key, null, scpe); case _ => new NamespaceBinding(key, value, scpe); - } else + } + else m = new PrefixedAttribute(pre, key, value, m) } else if("xmlns" == qname) scpe = value.length() match { @@ -196,11 +195,12 @@ abstract class FactoryAdapter extends DefaultHandler() { val colon = qname.indexOf(':'); val localName = if(-1 == colon) qname else qname.substring(colon+1,qname.length()); + val scp = scopeStack.pop; // create element rootElem = if(-1 == colon) - createNode( null, localName, metaData, scopeStack.pop, v ); + createNode( null, localName, metaData, scp, v ); else - createNode( qname.substring(0,colon), localName, metaData, scopeStack.pop, v ); + createNode( qname.substring(0,colon), localName, metaData, scp, v ); hStack.push(rootElem); @@ -293,7 +293,7 @@ abstract class FactoryAdapter extends DefaultHandler() { // parse file try { //System.err.println("[parsing \"" + source + "\"]"); - scopeStack.push(null); + scopeStack.push(TopScope); parser.parse( source, this ); scopeStack.pop; } catch { diff --git a/sources/scala/xml/NamespaceBinding.scala b/sources/scala/xml/NamespaceBinding.scala index 85091dc3a1..9437a3a6ff 100644 --- a/sources/scala/xml/NamespaceBinding.scala +++ b/sources/scala/xml/NamespaceBinding.scala @@ -7,6 +7,9 @@ package scala.xml; */ class NamespaceBinding(val prefix: String, val uri: String, val parent: NamespaceBinding) { + if(null != prefix && 0 == prefix.length()) + error("zero length prefix not allowed"); + def getURI(_prefix: String): String = { if(prefix == _prefix) uri @@ -23,5 +26,32 @@ class NamespaceBinding(val prefix: String, val uri: String, val parent: Namespac parent.getURI(_uri); } + override def toString(): String = { + val sb = new StringBuffer(); + toString(sb, TopScope); + sb.toString(); + } + + def toString(stop: NamespaceBinding): String = { + val sb = new StringBuffer(); + toString(sb, stop); + sb.toString(); + } + + def toString(sb:StringBuffer, stop:NamespaceBinding): Unit = { + if(this ne stop) { // contains? + sb.append(" xmlns"); + if(prefix != null) { + sb.append(':').append(prefix) + } + sb.append('=') + .append('"') + .append(uri) + .append('"'); + parent.toString(sb, stop); // copy(ignore) + } + } + + } diff --git a/sources/scala/xml/Node.scala b/sources/scala/xml/Node.scala index 0ef443f3f6..7f73bc4260 100644 --- a/sources/scala/xml/Node.scala +++ b/sources/scala/xml/Node.scala @@ -41,7 +41,7 @@ abstract class Node extends NodeSeq { def typeTag$: Int = 0; /** the namespace bindings */ - def scope: NamespaceBinding = null; + def scope: NamespaceBinding = TopScope; def namespace = getNamespace(prefix); diff --git a/sources/scala/xml/PrettyPrinter.scala b/sources/scala/xml/PrettyPrinter.scala index 7901c7c8ab..aa5181326f 100644 --- a/sources/scala/xml/PrettyPrinter.scala +++ b/sources/scala/xml/PrettyPrinter.scala @@ -109,21 +109,7 @@ class PrettyPrinter( width:Int, step:Int ) { n.nameToString(sb); //Utility.appendPrefixedName( n.prefix, n.label, pmap, sb ); val i = sb.length() + 1; n.attributes.toString(sb); - var scp = n.scope; - while( scp != TopScope && !scp.eq(pscope)) { - sb.append(' '); - sb.append("xmlns"); - if(scp.prefix != null) { - sb.append(':'); - sb.append(scp.prefix); - } - sb.append("=\""); - if(scp.uri != null) sb.append(scp.uri); - sb.append('\"'); - scp = scp.parent; - } - - //Utility.attr2xml( n.namespace, n.attributes, pmap, sb ); + n.scope.toString(sb, pscope); sb.append('>'); Pair(sb.toString(), i); } diff --git a/sources/scala/xml/TopScope.scala b/sources/scala/xml/TopScope.scala index 6dca9dfff8..200fb3b21f 100644 --- a/sources/scala/xml/TopScope.scala +++ b/sources/scala/xml/TopScope.scala @@ -2,9 +2,19 @@ package scala.xml; case object TopScope extends NamespaceBinding(null,null,null) { + /* + override def contains(pre:String) = false; + */ override def getURI(_prefix: String) = null; override def getPrefix(_uri: String) = null; + override def toString() = ""; + + override def toString(stop: NamespaceBinding) = ""; + + override def toString(sb: StringBuffer, ignore: NamespaceBinding) = { + } + } diff --git a/sources/scala/xml/Utility.scala b/sources/scala/xml/Utility.scala index cc6d50589c..3dc9ee7b01 100644 --- a/sources/scala/xml/Utility.scala +++ b/sources/scala/xml/Utility.scala @@ -82,54 +82,6 @@ object Utility { val noPrefixes: Map[String,String] = immutable.ListMap.Empty[String,String].update("",""); - /** - * Returns a default prefix mapping for a set of namespaces. - * the empty namespace is mapped to the empty prefix - * - * @param rootns - * @param nset - def defaultPrefixes(rootns: String, nset: mutable.Set[String]): Map[String,String] = { - val map = new mutable.HashMap[String,String](); - if (nset.contains("http://www.w3.org/XML/1998/namespace")) - map.update("http://www.w3.org/XML/1998/namespace", "xml"); - if (nset.contains("")) - map.update("", ""); - var i = 0; - for (val ns <- nset) - map.get(ns) match { - case None => map.update( ns, "ns"+i ); i = i + 1; - case Some( _ ) => - } - // Console.println("defaultPrefixes:"+map); // DEBUG - return map; - } - */ - - /** - * @see "defaultPrefixes(String, mutable.Set[String])" - def defaultPrefixes(n: Node): Map[String,String] = - defaultPrefixes(n.namespace, collectNamespaces( n )); - */ - - /** - * @see "defaultPrefixes(String, mutable.Set[String])" - def defaultPrefixes(nodes: Seq[Node]): Map[String,String] = - defaultPrefixes("", collectNamespaces(nodes)); - */ - - - def scope2map(n:NamespaceBinding): immutable.Map[String,String] = - scope2map(n, immutable.ListMap.Empty[String,String]); - - def scope2map(n:NamespaceBinding, map:immutable.Map[String,String]): immutable.Map[String,String] = { - if(TopScope == n) - map - else if(null == n.uri) - scope2map(n.parent, map - n.prefix) - else - scope2map(n.parent, map.update(n.prefix, n.uri)) - } - /** string representation of an XML node, with comments stripped the comments * @see "toXML(Node, Boolean)" */ @@ -146,35 +98,10 @@ object Utility { */ def toXML(n: Node, stripComment: Boolean): String = { val sb = new StringBuffer(); - toXML(n, null, sb, stripComment); - sb.toString(); - } - - /** - * @see "toXML(Node, Map[String,String], Boolean)" - def toXML(x: Node, pmap: Map[String,String]): String = - toXML(x, pmap, false); - */ - - /** - * Serializes a node with given namespace prefix mapping. The prefix - * mapping may not map the empty namespace "" to some non-empty prefix. - * - * @param x the node to serialize - * @param pmap a mapping from namespace URIs to prefixes - * @param stripComment - def toXML(x: Node, pmap: Map[String,String], stripComment: Boolean): String = { - val sb = new StringBuffer(); - toXML(x, pmap, sb, stripComment); + toXML(n, TopScope, sb, stripComment); sb.toString(); } - */ - /** - * @see "toXML(Node, Map[String,String], StringBuffer, Boolean)" - def toXML(x: Node, pmap: Map[String,String], sb: StringBuffer): Unit = - toXML(x, pmap, sb, false); - */ /** serializes a tree to the given stringbuffer * with the given namespace prefix mapping. @@ -210,19 +137,7 @@ object Utility { if (x.attributes != null) { x.attributes.toString(sb) } - var scp = x.scope; - while( scp != null && !scp.eq(pscope)) { - sb.append(' '); - sb.append("xmlns"); - if(scp.prefix != null) { - sb.append(':'); - sb.append(scp.prefix); - } - sb.append("=\""); - if(scp.uri != null) sb.append(scp.uri); - sb.append('\"'); - scp = scp.parent; - } + x.scope.toString(sb, pscope); sb.append('>'); for (val c <- x.child.elements) { toXML(c, x.scope, sb, stripComment); @@ -242,31 +157,6 @@ object Utility { } /** - * For a Node n, returns string representation of <code>n.attributes</code> - * - * @param ns - * @param attrib - * @param pmap - * @param sb - def attr2xml(attrib: Iterator[MetaData], sb: StringBuffer ) = { - val md = attrib; - while(null != md) { - sb.append(' '); - md.match { - case _:UnprefixedAttribute => - sb.append(x.key); - case p:PrefixedAttribute => - sb.append(p.pre); - sb.append(':'); - sb.append(x.key); - } - sb.append("="); - appendQuoted(x.value, sb) - } - } - */ - - /** * Returns a hashcode for the given constituents of a node * * @param uri @@ -303,26 +193,6 @@ object Utility { } /** - * Appends prefixed name to given stringbuffer using - * namespace-to-prefix mapping pmap. - * - * precondition: pmap.contains(ns) - * - * @param ns - * @param name - * @param pmap - * @param sb - def appendPrefixedName(pref: String, name: String, pmap: Map[String, String], sb: StringBuffer): Unit = { - //val pref = pmap( ns ); - if (pref.length() > 0) { - sb.append(pref); - sb.append(':') - } - sb.append(name) - } - */ - - /** * Appends "s" if s does not contain ", 's' * otherwise * |