diff options
author | buraq <buraq@epfl.ch> | 2004-06-03 09:16:40 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-06-03 09:16:40 +0000 |
commit | e5a10b5d5f4dae5ba13006f0d3c1f8a456cf9390 (patch) | |
tree | 54496be1a301aed107e443ed2b4b42bab5f80212 /sources | |
parent | 53ee2c0a66faede5572b6658c9c6173056313112 (diff) | |
download | scala-e5a10b5d5f4dae5ba13006f0d3c1f8a456cf9390.tar.gz scala-e5a10b5d5f4dae5ba13006f0d3c1f8a456cf9390.tar.bz2 scala-e5a10b5d5f4dae5ba13006f0d3c1f8a456cf9390.zip |
dtd2scala attribute validation
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/tools/dtd2scala/DeclToScala.scala | 45 | ||||
-rw-r--r-- | sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml | 38 | ||||
-rw-r--r-- | sources/scala/xml/CharData.scala | 8 | ||||
-rw-r--r-- | sources/scala/xml/Comment.scala | 2 | ||||
-rw-r--r-- | sources/scala/xml/EntityRef.scala | 3 | ||||
-rw-r--r-- | sources/scala/xml/Node.scala | 1 | ||||
-rw-r--r-- | sources/scala/xml/ProcInstr.scala | 2 | ||||
-rw-r--r-- | sources/scala/xml/Text.scala | 2 |
8 files changed, 59 insertions, 42 deletions
diff --git a/sources/scala/tools/dtd2scala/DeclToScala.scala b/sources/scala/tools/dtd2scala/DeclToScala.scala index 074879b1ba..267bc11032 100644 --- a/sources/scala/tools/dtd2scala/DeclToScala.scala +++ b/sources/scala/tools/dtd2scala/DeclToScala.scala @@ -50,7 +50,7 @@ class DeclToScala(fOut:PrintWriter, sb.append( key ); sb.append("\", \""); sb.append(attValue); /* quotes??? */ - sb.append("\")"); + sb.append("\");"); } sb.append('\n'); } @@ -81,15 +81,15 @@ class DeclToScala(fOut:PrintWriter, sb.append(") =>"); df match { case DEFAULT( true, attValue ) => - sb.append("if( b._2 != \"+"); + sb.append("if( b._2 != \""); sb.append( attValue ); - sb.append("+\" ) error_FixedAttribute(b._1,"); + sb.append("\" ) error_FixedAttribute(b._1,\""); sb.append( attValue ); - sb.append(")"); + sb.append("\") else {};"); case REQUIRED => - sb.append(" req = req - 1; map = map.update( b._1, b._2 )"); + sb.append(" req = req - 1; map = map.update( b._1, b._2 );"); case _ => - sb.append(" map = map.update( b._1, b._2 )"); + sb.append(" map = map.update( b._1, b._2 );"); } } sb.append('\n'); @@ -129,44 +129,23 @@ class DeclToScala(fOut:PrintWriter, case ANY_ => "true"; case Eps => "ch.length == 0" case PCDATA_ | Sequ( PCDATA_ ) => - val sb = new StringBuffer("ch.elements.forall( x:scala.xml.Node => x match {\n"); - sb.append(" case _:scala.xml.Text => true \n"); - sb.append(" case _:scala.xml.EntityRef => true \n"); - sb.append(" case _:scala.xml.CharData => true \n"); - sb.append(" case _:scala.xml.Comment => true \n"); - sb.append(" case _:scala.xml.ProcInstr => true \n"); - sb.append("case _ => false })"); + val sb = new StringBuffer("tagIterator( ch ).forall( x => x < 0 )"); sb.toString(); case Star(Alt(PCDATA_, alts @ _*)) => { - val sb = new StringBuffer("ch.elements.forall( x:scala.xml.Node => x match {\n"); - sb.append(" case _:scala.xml.Text => true \n"); - sb.append(" case _:scala.xml.EntityRef => true \n"); - sb.append(" case _:scala.xml.CharData => true \n"); - sb.append(" case _:scala.xml.Comment => true \n"); - sb.append(" case _:scala.xml.ProcInstr => true \n"); - // classes created with mixin composition... types won't work - sb.append(" case _ => x.typeTag$ match {\n"); - // no type tag -> is scala.xml.Elem - sb.append(" case 0 => x.label match {"); - for( val alt <- alts.elements ) { - sb.append(" case \""); - alt match { case RNode( name ) => sb.append( name );} - sb.append("\" => true \n"); - } - sb.append("case _ => Console.println(\"ELEM\"); for( val z <- ch ) { Console.println( z.getClass() ); Console.println( z.label ); Console.println( z.toString() )}; false}\n"); + val sb = new StringBuffer("tagIterator( ch ).forall( x:Int => x match {"); for( val alt <- alts.elements ) { sb.append(" case $TagOf"); alt match { case RNode( name ) => sb.append( cookedCap( name ));} sb.append(" => true \n"); } - sb.append("case _ => Console.println(ch.toList); for( val z <- ch ) { Console.println( z.getClass() ); Console.println( z.label ); Console.println( z.toString() )}; false }})"); + sb.append("case _ => false })"); sb.toString(); } - case _ => val sb = new StringBuffer("(ch.elements.foldLeft (Nil:List[Int]) { (e:List[Int],x:scala.xml.Node) => val i = x.typeTag$; if( i!=0 ) i::e else e }).reverse match {"); + case _ => val sb = new StringBuffer("tagIterator( ch ).toList.match {"); sb.append(" case Seq( "); sb.append( shallowContentModel( r ) ); - sb.append( ") => true "); - sb.append( "case _ => Console.println(\"reg match fails for \"+ch.elements.foldLeft (Nil:List[Int]) { (e:List[Int],x:scala.xml.Node) => val i = x.typeTag$; if( i!=0 ) i::e else e }); false}"); + sb.append( ") => true \n"); + sb.append( "case _ => false }\n"); sb.toString(); } } diff --git a/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml b/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml index a8a948460f..2c8d50e7e2 100644 --- a/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml +++ b/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml @@ -43,9 +43,29 @@ abstract class <string ref="objectName"/>Node$ extends scala.xml.Node; + def tagIterator(ch:Seq[scala.xml.Node]):Iterator[Int] = new Iterator[Int] { + val it = ch.elements.buffered; + def hasNext = { + while (it.hasNext && it.head.typeTag$ < 0) it.next; + it.hasNext + } + def next = { + if( hasNext ) { + val n = it.next; + val i = n.typeTag$; + if( i > 0 ) i else $tagMap.get( n.label ) match { + case Some( j ) => j + case None => 0 + } + } else { + error("internal error"); + } + } + } + private val $tagMap = new mutable.HashMap[String,Int]; <elementBinding> val $TagOf&ccElementName;:int = <elementTag/>; - + $tagMap.update( &qElementName;, $TagOf&ccElementName; ); def constr_&ccElementName;( attrs:aT, ch:cT ) = &ccElementName;(attrs,ch:_*); case class &ccElementName;( attrs:Map[String,String], ch:scala.xml.Node* ) extends <string ref="objectName"/>Node$ { @@ -53,10 +73,10 @@ final override def typeTag$ = $TagOf&ccElementName;; - def shallowValidate&ccElementName;Children( ch:Seq[scala.xml.Node] ) = + final def shallowValidate&ccElementName;Children( ch:Seq[scala.xml.Node] ) = <shallowContentRegExp/>; - def validateAttributes( attrs:Map[String,String] ):aT = { + final def validateAttributes( attrs:Map[String,String] ):aT = { var req = 0; var map = scala.xml.Node.NoAttributes; <initAttributes/> @@ -75,10 +95,16 @@ if( !shallowValidate&ccElementName;Children( ch ) ) error("trying to construct invalid &elementName;"); - def label = &qElementName;; - def child = ch; + final def label = &qElementName;; + final def child = ch; - protected var hmap:aT = validateAttributes( attrs ); + protected var hmap:aT = _; + try { + hmap = validateAttributes( attrs ); + } catch { + case e:java.lang.Error => + error("trying to construct invalid &elementName;\n"+e.getMessage()); + } final def attribute:aT = hmap; <attributeBinding> diff --git a/sources/scala/xml/CharData.scala b/sources/scala/xml/CharData.scala index 512dbfa9ca..e929b4e41f 100644 --- a/sources/scala/xml/CharData.scala +++ b/sources/scala/xml/CharData.scala @@ -18,15 +18,17 @@ import scala.collection.immutable ; case class CharData( text:String ) extends Node { + final override def typeTag$:Int = -4; + if( text.indexOf( "]]>" ) != -1 ) throw new IllegalArgumentException(" CDATA text may not contain \"]]>\" "); - /** the constant "#PCDATA" + /** the constant "#CDATA" */ - def label = "#PI"; + def label = "#CDATA"; /** always empty */ - final def attribute = immutable.TreeMap.Empty[String,String]; + final def attribute = Node.NoAttributes; /** always empty */ final def child = Nil; diff --git a/sources/scala/xml/Comment.scala b/sources/scala/xml/Comment.scala index e89674396a..45665dcceb 100644 --- a/sources/scala/xml/Comment.scala +++ b/sources/scala/xml/Comment.scala @@ -19,6 +19,8 @@ import scala.collection.immutable ; case class Comment( text:String ) extends Node { + final override def typeTag$:Int = -3; + if( text.indexOf("--" ) != -1 ) throw new IllegalArgumentException("text containts \"--\""); diff --git a/sources/scala/xml/EntityRef.scala b/sources/scala/xml/EntityRef.scala index 117519e266..7cbb2f29f5 100644 --- a/sources/scala/xml/EntityRef.scala +++ b/sources/scala/xml/EntityRef.scala @@ -17,6 +17,9 @@ import scala.collection.immutable ; **/ case class EntityRef( entityName:String ) extends Node { + + final override def typeTag$:Int = -5; + /** the constant "#ENTITY" */ def label = "#ENTITY"; diff --git a/sources/scala/xml/Node.scala b/sources/scala/xml/Node.scala index f116214e57..31fd7174a5 100644 --- a/sources/scala/xml/Node.scala +++ b/sources/scala/xml/Node.scala @@ -24,6 +24,7 @@ object Node { */ trait Node { + /** used internally. Text = -1 PI = -2 Comment = -3 CDATA = -4 EntityRef = -5 */ def typeTag$:Int = 0; /** QName (the label of this node). I.e. "foo" for <foo/>) */ diff --git a/sources/scala/xml/ProcInstr.scala b/sources/scala/xml/ProcInstr.scala index d49125f702..fc2324f619 100644 --- a/sources/scala/xml/ProcInstr.scala +++ b/sources/scala/xml/ProcInstr.scala @@ -18,6 +18,8 @@ package scala.xml; case class ProcInstr( target:String, text:Option[String] ) extends Node { + final override def typeTag$:Int = -2; + val z:Seq[Char] = target; z match { case Seq('X'|'x','M'|'m','L'|'l') => throw new IllegalArgumentException(target+" is reserved"); diff --git a/sources/scala/xml/Text.scala b/sources/scala/xml/Text.scala index a8dcdebe53..7490389437 100644 --- a/sources/scala/xml/Text.scala +++ b/sources/scala/xml/Text.scala @@ -18,6 +18,8 @@ import scala.collection.immutable ; case class Text( text:String ) extends Node { + final override def typeTag$:Int = -1; + /** the constant "#PCDATA" */ def label = "#PCDATA"; |