From 51fcef17d61451116c6349ba4bffacc5aa82e48c Mon Sep 17 00:00:00 2001 From: buraq Date: Mon, 11 Apr 2005 17:10:12 +0000 Subject: hello --- sources/scala/xml/MetaData.scala | 110 + sources/scala/xml/NamespaceBinding.scala | 27 + sources/scala/xml/Null.scala | 59 + sources/scala/xml/PrefixedAttribute.scala | 63 + sources/scala/xml/TopScope.scala | 10 + sources/scala/xml/UnprefixedAttribute.scala | 44 + test/files/jvm/xml01.check | 20 + test/files/jvm/xml01.scala | 174 ++ test/files/xml/lnk.dtd | 13 - test/files/xml/xhtml.dtd | 3087 --------------------------- 10 files changed, 507 insertions(+), 3100 deletions(-) create mode 100644 sources/scala/xml/MetaData.scala create mode 100644 sources/scala/xml/NamespaceBinding.scala create mode 100644 sources/scala/xml/Null.scala create mode 100644 sources/scala/xml/PrefixedAttribute.scala create mode 100644 sources/scala/xml/TopScope.scala create mode 100644 sources/scala/xml/UnprefixedAttribute.scala create mode 100644 test/files/jvm/xml01.check create mode 100644 test/files/jvm/xml01.scala delete mode 100644 test/files/xml/lnk.dtd delete mode 100644 test/files/xml/xhtml.dtd diff --git a/sources/scala/xml/MetaData.scala b/sources/scala/xml/MetaData.scala new file mode 100644 index 0000000000..e3d0ee06b9 --- /dev/null +++ b/sources/scala/xml/MetaData.scala @@ -0,0 +1,110 @@ +package scala.xml; + +/** Attribute information item, and linked list of attribute information items. + * These are triples consisting of prefix,key,value. To obtain the namespace, + * getNamespace must be called with the parent. If next is null, this is + * the last attribute in the MetaData list. + * + * either an UnprefixedAttribute or a PrefixedAttribute + * + * @todo _vlue should be a normalized attribute value + */ +abstract class MetaData extends Iterable[MetaData] with java.io.Serializable { + + /** appends given MetaData items to this MetaData list */ + def append(m: MetaData): MetaData = + next.append(copy(m)); + + def containedIn1(m:MetaData): Boolean = + m.equals1(this) || containedIn1(m.next); + + /** returns a copy of this MetaData item with next field set to argument */ + def copy(next: MetaData): MetaData; + + /** if owner is the element of this metadata item, returns namespace */ + def getNamespace(owner: Node): String; + + def hasNext = (Null != next); + + def length: Int = length(1); + + def length(i:Int): Int = next.length(i+1); + + 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 { + case m:MetaData => + var res = (this.length == m.length) && (this.hashCode() == m.hashCode()); + val it = this.elements; + while (res && it.hasNext) { res = it.next.containedIn1(m) } + res + + case _ => false; + } + } + + def elements = new Iterator[MetaData] { + var x = MetaData.this; + def hasNext = x.hasNext; + def next = { + x = x.next; + x + } + } + + /** shallow equals method */ + def equals1(that:MetaData): Boolean; + + /** returns key of this MetaData item */ + def key: String; + + /** returns key of this MetaData item */ + def value: String; + + /** returns Null or the next MetaData item */ + def next: MetaData; + + /** gets value of unqualified (unprefixed) attribute with given key */ + def getValue(key: String): String; + + /** gets value of qualified (prefixed) attribute with given key */ + def getValue(namespace: String, owner: Node, key: String): String = + getValue(namespace, owner.scope, key); + + /** gets value of qualified (prefixed) attribute with given key */ + def getValue(namespace: String, scope: NamespaceBinding, key: String): String; + override def hashCode(): Int; + + def toString1(): String = { + val sb = new StringBuffer(); + toString1(sb); + sb.toString(); + } + + //appends string representations of single attribute to StringBuffer + def toString1(sb:StringBuffer): Unit; + + override def toString(): String = { + val sb = new StringBuffer(); + toString(sb); + sb.toString(); + } + + def toString(sb: StringBuffer): Unit = { + sb.append(' '); + toString1(sb); + next.toString(sb); + } + + def wellformed(scope: NamespaceBinding): Boolean; + +} + diff --git a/sources/scala/xml/NamespaceBinding.scala b/sources/scala/xml/NamespaceBinding.scala new file mode 100644 index 0000000000..85091dc3a1 --- /dev/null +++ b/sources/scala/xml/NamespaceBinding.scala @@ -0,0 +1,27 @@ +package scala.xml; + +/** 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. + */ +class NamespaceBinding(val prefix: String, val uri: String, val parent: NamespaceBinding) { + + def getURI(_prefix: String): String = { + if(prefix == _prefix) + uri + else + parent.getURI(_prefix); + } + + /** returns some prefix that is mapped to the prefix, + */ + def getPrefix(_uri: String): String = { + if(_uri == uri) + uri + else + parent.getURI(_uri); + } + +} + diff --git a/sources/scala/xml/Null.scala b/sources/scala/xml/Null.scala new file mode 100644 index 0000000000..544b05c743 --- /dev/null +++ b/sources/scala/xml/Null.scala @@ -0,0 +1,59 @@ +package scala.xml; + +case object Null extends MetaData { + + /** appends given MetaData items to this MetaData list */ + override def append(m: MetaData): MetaData = m; + + override def containedIn1(m:MetaData): Boolean = false; + + /** returns a copy of this MetaData item with next field set to argument */ + def copy(next: MetaData) = next; + + /** returns null */ + def getNamespace(owner: Node) = null; + + final override def hasNext = false; + + final override def length = 0; + + final override def length(i:Int) = i; + + def isPrefixed = false; + + /** deep equals method */ + override def equals(that: Any) = that match { + case m:MetaData => m.length == 0 + case _ => false; + } + + def equals1(that:MetaData) = that.length == 0; + + def key = null; + + def value = null; + + def next = null; + + /** null */ + def getValue(key: String) = null; + + /** gets value of qualified (prefixed) attribute with given key */ + def getValue(namespace: String, scope: NamespaceBinding, key: String) = + null; + + override def hashCode(): Int = 0; + + override def toString1(): String = ""; + + //appends string representations of single attribute to StringBuffer + def toString1(sb:StringBuffer) = {}; + + override def toString(): String = ""; + + override def toString(sb: StringBuffer): Unit = {} + + override def wellformed(scope: NamespaceBinding) = true; + + +} diff --git a/sources/scala/xml/PrefixedAttribute.scala b/sources/scala/xml/PrefixedAttribute.scala new file mode 100644 index 0000000000..7be7ffe338 --- /dev/null +++ b/sources/scala/xml/PrefixedAttribute.scala @@ -0,0 +1,63 @@ +package scala.xml; + +/** prefixed attributes always have a non-null namespace + */ +class PrefixedAttribute(val pre: String, val key: String, val value: String, val next: MetaData) extends MetaData { + + /** returns a copy of this unprefixed attribute with the given next field*/ + 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) && (m.key == key) && (m.value == value); + + def getNamespace(owner: Node) = + owner.getNamespace(pre); + + /** forwards the call to next */ + def getValue(key: String): String = next.getValue(key); + + /** gets attribute value of qualified (prefixed) attribute with given key + */ + def getValue(namespace: String, scope: NamespaceBinding, key: String): String = { + if(key == this.key && scope.getURI(pre) == namespace) + value + else + next.getValue(namespace, scope, key); + } + + /** returns true */ + final def isPrefixed = true; + + override def hashCode() = + pre.hashCode() * 41 + key.hashCode() * 7 + value.hashCode() * 3 + next.hashCode(); + + + def toString1(sb:StringBuffer): Unit = { + sb.append(pre); + sb.append(':'); + sb.append(key); + sb.append('='); + Utility.appendQuoted(value, sb); + } + + def wellformed(scope: NamespaceBinding): Boolean = { + (null == next.getValue(scope.getURI(pre), scope, key) + && next.wellformed(scope)); + } + +} + diff --git a/sources/scala/xml/TopScope.scala b/sources/scala/xml/TopScope.scala new file mode 100644 index 0000000000..6dca9dfff8 --- /dev/null +++ b/sources/scala/xml/TopScope.scala @@ -0,0 +1,10 @@ +package scala.xml; + +case object TopScope extends NamespaceBinding(null,null,null) { + + override def getURI(_prefix: String) = null; + + override def getPrefix(_uri: String) = null; + +} + diff --git a/sources/scala/xml/UnprefixedAttribute.scala b/sources/scala/xml/UnprefixedAttribute.scala new file mode 100644 index 0000000000..90081909a6 --- /dev/null +++ b/sources/scala/xml/UnprefixedAttribute.scala @@ -0,0 +1,44 @@ +package scala.xml; + +/** unprefixed attributes have the null namespace + */ +class UnprefixedAttribute(val key: String, val value: String, val next: MetaData) extends MetaData { + + /** returns a copy of this unprefixed attribute with the given next field*/ + def copy(next: MetaData) = + new UnprefixedAttribute(key, value, next); + + def equals1(m:MetaData) = !m.isPrefixed && (m.key == key) && (m.value == value); + + /** returns null */ + final def getNamespace(owner: Node): String = + null; + + /** gets value of unqualified (unprefixed) attribute with given key */ + def getValue(key: String): String = + if(key == this.key) + value + else + next.getValue(key); + + /** forwards the call to next */ + def getValue(namespace: String, scope: NamespaceBinding, key: String): String = + next.getValue(namespace, scope, key); + + override def hashCode() = + key.hashCode() * 7 + value.hashCode() * 53 + next.hashCode(); + + /** returns false */ + final def isPrefixed = false; + + def toString1(sb:StringBuffer): Unit = { + sb.append(key); + sb.append('='); + Utility.appendQuoted(value, sb); + } + + def wellformed(scope: NamespaceBinding): Boolean = + (null == next.getValue(null, scope, key)) && next.wellformed(scope); + +} + diff --git a/test/files/jvm/xml01.check b/test/files/jvm/xml01.check new file mode 100644 index 0000000000..57a788c6a1 --- /dev/null +++ b/test/files/jvm/xml01.check @@ -0,0 +1,20 @@ +passed ok +equality +passed ok +passed ok +passed ok +passed ok +passed ok +xpath \ +passed ok +passed ok +passed ok +passed ok +passed ok +passed ok +passed ok +passed ok +xpath \\ DESCENDANTS +passed ok +passed ok +passed ok diff --git a/test/files/jvm/xml01.scala b/test/files/jvm/xml01.scala new file mode 100644 index 0000000000..bd73a0fd7d --- /dev/null +++ b/test/files/jvm/xml01.scala @@ -0,0 +1,174 @@ +import java.io.StringReader; +import org.xml.sax.InputSource; +import scala.xml._; +import scala.xml.nobinding._; +import scala.util.logging._; + +import scala.testing.UnitTest._ ; + +object Test with Application { + val e: scala.xml.MetaData = Null; //Node.NoAttributes; + val sc: scala.xml.NamespaceBinding = null; + + val xmlFile1 = ""; + val isrc1 = new InputSource( new StringReader( xmlFile1 ) ); + val parsedxml1 = XML.load( isrc1 ); + val isrc11 = new InputSource( new StringReader( xmlFile1 ) ); + val parsedxml11 = XML.load( isrc11 ); + + val c = new Node { + def label = "hello"; + //def namespace = ""; + def child = List(Elem("","world",e,sc)); + //def attributes = e; + }; + + assertSameElements( List( 3 ), List( 3 )); + + Console.println("equality"); + assertEquals( c, parsedxml11 ); + assertEquals( parsedxml1, parsedxml11 ); + assertSameElements( List(parsedxml1), List(parsedxml11)); + assertSameElements( Iterator.fromArray(Predef.Array(parsedxml1)).toList, List(parsedxml11)); + + val x2 = "Peter BunemanDan SuciuData on ze web"; + + val i = new InputSource( new StringReader( x2 )); + val x2p = XML.load( i ); + + assertEquals(x2p, Elem("","book",e,sc, + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web")))); + + val xmlFile2 = "Peter BunemanDan SuciuData on ze webJohn MitchellFoundations of Programming Languages"; + val isrc2 = new InputSource( new StringReader( xmlFile2 ) ); + val parsedxml2 = XML.load( isrc2 ); + + // xmlFile2/book -> book,book + Console.println("xpath \\"); + + + assertSameElements( parsedxml1 \ "_" , List( Elem("","world",e,sc) ) ); + + assertSameElements( parsedxml1 \ "world", List( Elem("","world",e,sc) ) ); + +/* + Console.println( parsedxml2 \ "_" ); + Console.println( (parsedxml2 \ "_" ).elements); + for( val i <- (parsedxml2 \ "_" ).elements) { + Console.println( i ); + }; + */ + + assertSameElements( + parsedxml2 \ "_" , + + List( + Elem("","book", e,sc, + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web"))), + Elem("","book",e,sc, + Elem("","author",e,sc,Text("John Mitchell")), + Elem("","title",e,sc,Text("Foundations of Programming Languages")))) + ); + assertEquals( (parsedxml2 \ "author").length, 0 ); + + assertSameElements( + parsedxml2 \ "book", + + List( + Elem("","book",e,sc, + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web"))), + Elem("","book",e,sc, + Elem("","author",e,sc,Text("John Mitchell")), + Elem("","title",e,sc,Text("Foundations of Programming Languages"))) + ) + ); + + assertSameElements( + + parsedxml2 \ "_" \ "_", + + List( + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web")), + Elem("","author",e,sc,Text("John Mitchell")), + Elem("","title",e,sc,Text("Foundations of Programming Languages")) + ) + ); + + assertSameElements( + + parsedxml2 \ "_" \ "author", + + List( + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","author",e,sc,Text("John Mitchell")) + ) + + ); + + assertSameElements( (parsedxml2 \ "_" \ "_" \ "author"), List() ); + + Console.println("xpath \\\\ DESCENDANTS"); + + assertSameElements( + + parsedxml2 \\ "author", + + List( + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","author",e,sc,Text("John Mitchell")) + ) + + ); + assertEquals( + + (new NodeSeq { val theSeq = List( parsedxml2 ) }) \\ "_", + + List( + Elem("","bib",e,sc, + Elem("","book",e,sc, + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web"))), + Elem("","book",e,sc, + Elem("","author",e,sc,Text("John Mitchell")), + Elem("","title",e,sc,Text("Foundations of Programming Languages")))), + Elem("","book",e,sc, + Elem("","author",e,sc,Text("Peter Buneman")), + Elem("","author",e,sc,Text("Dan Suciu")), + Elem("","title",e,sc,Text("Data on ze web"))), + Elem("","author",e,sc,Text("Peter Buneman")), + Text("Peter Buneman"), + Elem("","author",e,sc,Text("Dan Suciu")), + Text("Dan Suciu"), + Elem("","title",e,sc,Text("Data on ze web")), + Text("Data on ze web"), + Elem("","book",e,sc, + Elem("","author",e,sc,Text("John Mitchell")), + Elem("","title",e,sc,Text("Foundations of Programming Languages"))), + Elem("","author",e,sc,Text("John Mitchell")), + Text("John Mitchell"), + Elem("","title",e,sc,Text("Foundations of Programming Languages")), + Text("Foundations of Programming Languages") + ) + ); + + + assertSameElements( + + parsedxml2 \\ "title", + + List( + Elem("","title",e,sc,Text("Data on ze web")), + Elem("","title",e,sc,Text("Foundations of Programming Languages"))) + ); +} diff --git a/test/files/xml/lnk.dtd b/test/files/xml/lnk.dtd deleted file mode 100644 index e0ede56aba..0000000000 --- a/test/files/xml/lnk.dtd +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - diff --git a/test/files/xml/xhtml.dtd b/test/files/xml/xhtml.dtd deleted file mode 100644 index 8f9aebc175..0000000000 --- a/test/files/xml/xhtml.dtd +++ /dev/null @@ -1,3087 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - -]]> - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - -%xhtml-events.mod;]]> - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - -]]> - - -]]> - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - -]]> - - - -]]> - - - - - - - - -]]> - - - -]]> - - - - -]]> - - - -]]> - - - - -]]> - - - -]]> - - - - -]]> - - - -]]> - - - - -]]> - - - -]]> - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - -]]> - - - - - - - - - - - - - - - - -]]> - - - -]]> - - - - - - - -]]> - - - - - - -]]> - - - - - - - -]]> - - - -]]> - - - - - - - -]]> - - - - - - - -]]> - - - - - -- cgit v1.2.3