diff options
-rw-r--r-- | sources/scala/tools/dtd2scala/DeclToScala.scala | 121 | ||||
-rw-r--r-- | sources/scala/tools/dtd2scala/MainHandler.scala | 127 | ||||
-rw-r--r-- | sources/scala/tools/dtd2scala/XMLDecl.scala | 5 |
3 files changed, 118 insertions, 135 deletions
diff --git a/sources/scala/tools/dtd2scala/DeclToScala.scala b/sources/scala/tools/dtd2scala/DeclToScala.scala index d32063b21c..8b82edafa5 100644 --- a/sources/scala/tools/dtd2scala/DeclToScala.scala +++ b/sources/scala/tools/dtd2scala/DeclToScala.scala @@ -9,9 +9,9 @@ import java.io.OutputStreamWriter ; import java.io.IOException ; -import java.util.Map ; -import java.util.Iterator ; - +//import java.util.Map ; +//import java.util.Iterator ; +import scala.collection.Map ; import scalac.util.Name ; import scalac.ast.parser.Scanner ; @@ -24,7 +24,7 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { final val COMPRESS_DEFAULT:String = "true"; // do hash-consing on load final val ATTRIBS_VARDEF:String = - "var _at:scala.xml.javaAdapter.Map[String,String] = new HashMap[String,String];"; + "var _at:Map[String,String] = new HashMap[String,String];"; //static final String ATTRIB_MAP = "attribs"; //static final String ATTRIB_T = "scala.Map[String,String]"; @@ -37,8 +37,8 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { final val RAW_NAME_DEF:String = "def getName:String = "; final val GET_CHILDREN_DEF:String = "def getChildren:scala.Seq[scala.xml.Element] = _ch ;"; - final val GET_ATTRIBS_DEF:String = "def getAttribs:scala.xml.javaAdapter.Map[String,String] = _at ;"; - final val SET_ATTRIBS_DEF:String = "def setAttribs( m:scala.xml.javaAdapter.Map[String,String] ):Unit = {_at = m};"; + final val GET_ATTRIBS_DEF:String = "def getAttribs:Map[String,String] = _at ;"; + final val SET_ATTRIBS_DEF:String = "def setAttribs( m:Map[String,String] ):Unit = {_at = m};"; //static final String HASHCODE_DEF = "override def hashCode():int = { getChildren.hashCode() + getAttribs.hashCode() + getName.hashCode() }"; @@ -60,8 +60,8 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { fIndent = IND_STEP; printIndent(); fOut.println("import scala.xml._ ;"); - fOut.println("import scala.xml.javaAdapter.Map ;"); - fOut.println("import scala.xml.javaAdapter.HashMap ;"); + fOut.println("import scala.collection.Map ;"); + fOut.println("import scala.collection.mutable.HashMap ;"); } def end():Unit = { @@ -151,7 +151,7 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { fOut.print( "def " ); fOut.print( cooked( name )); - fOut.print( "( arg:String ):Unit = _at.put(\"" ); + fOut.print( "( arg:String ):Unit = _at.update(\"" ); fOut.print( name ); fOut.println( "\", arg ) ;"); } @@ -166,30 +166,28 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { fOut.print( cooked( name )); fOut.print( ":String = _at.get( "); fOut.print( '"' );fOut.print( name );fOut.print( '"' ); - fOut.println( " ) ;"); + fOut.println( " ).match { Some(x) => x };"); } - def printFactory( elemMap:Map ):Unit = { + def printFactory( elemMap:Map[String,ElemDecl] ):Unit = { printIndent(); fOut.println( - "val _factory: scala.xml.javaAdapter.Map[String, scala.Seq[scala.xml.Element] => scala.xml.Element] = {"); + "val _factory: Map[String, scala.Seq[scala.xml.Element] => scala.xml.Element] = {"); fIndent = fIndent + IND_STEP; printIndent(); fOut.println( //"val res = new scala.HashMap[String,(scala.Map[String,String],scala.Seq[Element])=>Element] ;"); - "val res = new scala.xml.javaAdapter.HashMap[String, scala.Seq[scala.xml.Element] => scala.xml.Element] ;"); + "val res = new HashMap[String, scala.Seq[scala.xml.Element] => scala.xml.Element] ;"); //JAVA: for(Iterator it = elemMap.keySet().iterator(); it.hasNext(); ) - val it:Iterator = elemMap.keySet().iterator(); - while( it.hasNext() ) { - val decl:ElemDecl = elemMap.get( it.next() ).asInstanceOf[ ElemDecl ]; - printIndent(); - fOut.print( "res.put(\"" ); - fOut.print( decl.name ); - fOut.print( "\",(x:scala.Seq[scala.xml.Element] => "); - fOut.print( cookedCap( decl.name )); - fOut.println("( x:_* ) ));"); - } + elemMap.values.foreach( decl => { + printIndent(); + fOut.print( "res.update(\"" ); + fOut.print( decl.name ); + fOut.print( "\",(x:scala.Seq[scala.xml.Element] => "); + fOut.print( cookedCap( decl.name )); + fOut.println("( x:_* ) ));"); + }); printIndent(); fOut.println("res"); printIndent(); @@ -198,28 +196,24 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { } - def printContainsTextDef( elemMap:Map ):Unit = { + def printContainsTextDef( elemMap:Map[String,ElemDecl] ):Unit = { printIndent(); - fOut.println("val _containsMap: scala.xml.javaAdapter.Map[scala.String, boolean] = {"); + fOut.println("val _containsMap: Map[scala.String, boolean] = {"); fIndent = fIndent + IND_STEP; printIndent(); - fOut.println("val res = new scala.xml.javaAdapter.HashMap[scala.String, boolean] ;"); - - val it:Iterator = elemMap.keySet().iterator(); - while( it.hasNext() ) { - val decl:ElemDecl = elemMap.get( it.next() ).asInstanceOf[ ElemDecl ]; - printIndent(); - fOut.print( "res.put(\"" ); - fOut.print( decl.name ); - fOut.print( "\","); - - if( decl.contentModel.indexOf("#PCDATA") != -1 ) - fOut.print("true"); - else - fOut.print("false"); - - fOut.println(");"); - } + fOut.println("val res = new HashMap[scala.String, boolean] ;"); + + elemMap.values.foreach( decl => { + printIndent(); + fOut.print( "res.update(\"" ); + fOut.print( decl.name ); + fOut.print( "\","); + if( decl.contentModel.indexOf("#PCDATA") != -1 ) + fOut.print("true") + else + fOut.print("false"); + fOut.println(");"); + }); printIndent(); fOut.println("res"); fIndent = fIndent - IND_STEP; @@ -248,37 +242,29 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { fOut.println("};"); fIndent = fIndent - IND_STEP; printIndent(); - fOut.println("val b:scala.Object = fAdapter.loadXML( filename );"); - printIndent(); - fOut.println("b.asInstanceOf[Element]"); + fOut.println("fAdapter.loadXML( filename );"); printIndent(); fOut.println("};"); fIndent = fIndent - IND_STEP; }; - def toScala( elemMap:Map ):Unit = { + def toScala( elemMap:Map[String,ElemDecl] ):Unit = { begin(); fOut.println("/** the following elements are there"); - val it:Iterator = elemMap.keySet().iterator(); - while( it.hasNext() ) { - val decl:ElemDecl = elemMap.get( it.next() ).asInstanceOf[ ElemDecl ]; - fOut.print(" * "); - fOut.print( decl.name ); - fOut.print(" : ["); - fOut.print( decl.contentModel ); - fOut.println(']'); - fOut.print(" * "); - fOut.println( "attribs: "+decl.attribs.keySet() ); - } + elemMap.values.foreach( decl => { + fOut.print(" * "); + fOut.print( decl.name ); + fOut.print(" : ["); + fOut.print( decl.contentModel ); + fOut.println(']'); + fOut.print(" * "); + fOut.println( "attribs: "+decl.attribs ); + } ); fOut.println("*/"); - val it2:Iterator = elemMap.keySet().iterator(); - while( it2.hasNext() ) { - val decl:ElemDecl = elemMap.get( it2.next() ).asInstanceOf[ ElemDecl ]; - toScala( decl ); - } + elemMap.values.foreach( decl => toScala( decl ) ); printContainsTextDef( elemMap ); printFactory( elemMap ); @@ -287,14 +273,12 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { end(); } - def toScala( decl:XMLDecl ):Unit = { - decl.match { - case ElemDecl( _, _, _) => + def toScala( decl:XMLDecl ):Unit = decl.match { - printClassDef( decl.asInstanceOf[ ElemDecl ] ); + case x:ElemDecl => + printClassDef( x ); case AttrDecl( name, attrType ) => - if( attrType.equals("CDATA") ) { //printSetMethod(name); printGetMethod(name); @@ -307,8 +291,7 @@ class DeclToScala(fOut:PrintWriter, moduleName:String) { } case _ => System.err.println("unexpected XMLDecl"); System.exit(-1); - } -} + } // toScala // diff --git a/sources/scala/tools/dtd2scala/MainHandler.scala b/sources/scala/tools/dtd2scala/MainHandler.scala index ff8963d111..6bc5135dc3 100644 --- a/sources/scala/tools/dtd2scala/MainHandler.scala +++ b/sources/scala/tools/dtd2scala/MainHandler.scala @@ -7,11 +7,14 @@ import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.ext.DeclHandler; +/* import java.util.Map ; import java.util.HashMap ; import java.util.TreeMap ; import java.util.Iterator ; - +*/ +//import scala.collection.Map ; +import scala.collection.mutable.HashMap ; /** 2 do : - handle modes of attributes (add #REQUIRED ones, fill in default of #IMPLIED) - allow package prefix to be given ! @@ -20,71 +23,67 @@ import java.util.Iterator ; class MainHandler extends DefaultHandler with DeclHandler { - var elemMap:Map = new HashMap(); // elementName -> elementDecl - - // DTDHandler methods - - /** encountered element declaration - */ - def elementDecl( name:String, contentModel:String ):Unit - /* throws SAXException */ ={ - - val decl:ElemDecl = elemMap.get( name ).asInstanceOf[ ElemDecl ]; + var elemMap:HashMap[String,ElemDecl] = new HashMap[String,ElemDecl]; - if( decl == null ) { - val _ = elemMap.put( name, new ElemDecl( name, - contentModel, - new HashMap() )); - } else { - val newDecl = ElemDecl( decl.name, contentModel, decl.attribs ); - val _ = elemMap.put( name, newDecl ); - } + // DTDHandler methods - } // elementDecl(String,String) - - /** encountered attribute declaration. - */ - def attributeDecl(elementName:String, - attributeName:String, - tpe:String, - valueDefault:String, - value:String ) /*throws SAXException*/ = { - var attribs:Map = null.asInstanceOf[ Map ]; + /** encountered element declaration + */ + def elementDecl( name:String, contentModel:String ):Unit + /* throws SAXException */ = { - val decl:ElemDecl = elemMap.get( elementName ).asInstanceOf[ ElemDecl ]; + elemMap.get( name ).match { - if( decl == null ) - { - attribs = new TreeMap(); - elemMap.put( elementName, - new ElemDecl( elementName, - null, - attribs )); - } - else - attribs = decl.attribs; - - - val _ = attribs.put( attributeName, new AttrDecl(attributeName, - tpe )); - - } // attributeDecl(String,String,String,String,String) - - - /** Internal entity declaration. - */ + case Some(decl) => // was added because of ATTLIST decl before + elemMap.update( name, ElemDecl( decl.name, + contentModel, + decl.attribs ) ) + case None => + elemMap.update( name, ElemDecl( name, + contentModel, + new HashMap[String,AttrDecl]() )); + } + } // elementDecl(String,String) + + /** encountered attribute declaration. + */ + def attributeDecl(elementName:String, + attributeName:String, + tpe:String, + valueDefault:String, + value:String ) /*throws SAXException*/ = { + + var attribs:HashMap[String,AttrDecl] = + elemMap.get( elementName ).match { + case None => + val amap = new HashMap[String,AttrDecl]; + elemMap.update( elementName, + ElemDecl( elementName, + null:String, + amap )); + amap; + + case Some(decl) => + decl.attribs; + }; + attribs.update( attributeName, AttrDecl(attributeName, tpe )); + } // attributeDecl(String,String,String,String,String) + + + /** Internal entity declaration. + */ def internalEntityDecl( name:String, text:String ):Unit - /*throws SAXException*/ = { - // ignore + /*throws SAXException*/ = { + // ignore } /** External entity declaration. */ def externalEntityDecl( name:String, - publicId:String , - systemId:String ):Unit - /*throws SAXException*/ = { - // ignore + publicId:String , + systemId:String ):Unit + /*throws SAXException*/ = { + // ignore } /* @@ -98,14 +97,14 @@ class MainHandler extends DefaultHandler with DeclHandler { AttrDecl adecl = null; System.out.print("{"); for(Iterator it2 = decl.attribs.keySet().iterator(); - it2.hasNext(); ) { - if(adecl != null) - System.out.print(","); - adecl = (AttrDecl) decl.attribs.get( it2.next() ); - System.out.print(adecl.name+":"+adecl.type); - } - System.out.println("}"); - } + it2.hasNext(); ) { + if(adecl != null) + System.out.print(","); + adecl = (AttrDecl) decl.attribs.get( it2.next() ); + System.out.print(adecl.name+":"+adecl.type); + } + System.out.println("}"); + } } diff --git a/sources/scala/tools/dtd2scala/XMLDecl.scala b/sources/scala/tools/dtd2scala/XMLDecl.scala index a13cad461d..31c861fbf8 100644 --- a/sources/scala/tools/dtd2scala/XMLDecl.scala +++ b/sources/scala/tools/dtd2scala/XMLDecl.scala @@ -1,12 +1,13 @@ package scala.tools.dtd2scala ; -import java.util.Map ; +import scala.collection.mutable.HashMap ; abstract class XMLDecl ; case class ElemDecl( name:String , contentModel:String , - attribs:Map ) extends XMLDecl; /*AttrDecl[]*/ + attribs:HashMap[String,AttrDecl] ) + extends XMLDecl; /*AttrDecl[]*/ // ignore default values 4 now case class AttrDecl( name:String, |