diff options
author | Burak Emir <emir@epfl.ch> | 2006-05-23 15:54:36 +0000 |
---|---|---|
committer | Burak Emir <emir@epfl.ch> | 2006-05-23 15:54:36 +0000 |
commit | 2a2f543db6e903d249671edca48d50fc4fde0be3 (patch) | |
tree | a711c1c0b64d2449cc7171ef6c04d273052ce0b4 /src | |
parent | 673fb7e02b704b9cc288b1b591f982b3131b16c9 (diff) | |
download | scala-2a2f543db6e903d249671edca48d50fc4fde0be3.tar.gz scala-2a2f543db6e903d249671edca48d50fc4fde0be3.tar.bz2 scala-2a2f543db6e903d249671edca48d50fc4fde0be3.zip |
added some methods
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/xml/XML.scala | 109 |
1 files changed, 68 insertions, 41 deletions
diff --git a/src/library/scala/xml/XML.scala b/src/library/scala/xml/XML.scala index b61f94772b..2291fa70cc 100644 --- a/src/library/scala/xml/XML.scala +++ b/src/library/scala/xml/XML.scala @@ -31,71 +31,98 @@ object XML { // functions for generic xml loading, saving - /** loads XML from given file */ - final def loadFile( file: File ): scala.xml.Elem = + /** loads XML from given file, using XML parser in JDK. */ + final def loadFile(file: File): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( new FileInputStream( file ) )); - /** loads XML from given file descriptor */ - final def loadFile( fileDesc: FileDescriptor ): scala.xml.Elem = + /** loads XML from given file descriptor, using XML parser in JDK. */ + final def loadFile(fileDesc: FileDescriptor): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( new FileInputStream( fileDesc ) )); - /** loads XML from given file */ - final def loadFile( fileName:String ): scala.xml.Elem = + /** loads XML from given file, using XML parser in JDK. */ + final def loadFile(fileName: String): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( new FileInputStream( fileName ) )); - /** loads XML from given InputStream */ + /** loads XML from given InputStream, using XML parser in JDK. */ final def load( is:InputStream ): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( is )); - /** loads XML from given Reader */ - final def load( reader:Reader ): scala.xml.Elem = + /** loads XML from given Reader, using XML parser in JDK. */ + final def load(reader: Reader): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( reader )); - /** loads XML from given sysID */ - final def load( sysID:String ): scala.xml.Elem = + /** loads XML from given sysID, using XML parser in JDK. */ + final def load(sysID: String): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( new InputSource( sysID )); - /** loads XML from a given input source */ - final def load( source:InputSource ): scala.xml.Elem = + /** loads XML from a given input source, using XML parser in JDK. */ + final def load(source: InputSource): scala.xml.Elem = new NoBindingFactoryAdapter().loadXML( source ); - /** loads XML from a string */ + /** loads XML from a string, using XML parser in JDK. */ final def loadString( string:String ): scala.xml.Elem = load(new StringReader(string)) - /** saves XML to filename with encoding ISO-8859-1. Does not write an xml-decl or doctype. */ - final def save(filename: String, doc: Node): Unit = - save(filename, doc, "ISO-8859-1"); - - /** saves XML to filename with given encoding. Does not write an xml-decl or doctype. */ - final def save(filename: String, doc: Node, enc: String): Unit = - saveFull(filename, doc, enc, false, null); - - final def saveFull(filename: String, doc: Node, xmlDecl: Boolean, doctype: dtd.DocType): Unit = - saveFull(filename, doc, "ISO-8859-1", xmlDecl, doctype); - - final def saveFull(filename: String, doc: Node, enc: String, xmlDecl: Boolean, doctype: dtd.DocType): Unit = { - /* using NIO classes of JDK 1.4 */ - import java.io.{FileOutputStream,Writer}; - import java.nio.channels.{Channels,FileChannel}; - - val fos = new FileOutputStream( filename ); - val w: Writer = Channels.newWriter( fos.getChannel(), enc ); - - /* 2do: optimize by giving writer parameter to toXML*/ - if(xmlDecl) w.write( "<?xml version='1.0' encoding='"+enc+"'?>\n"); - if(doctype!=null) w.write( doctype.toString()+"\n"); - w.write( Utility.toXML( doc )); - - w.close(); - fos.close(); - + /** saves XML to filename with encoding ISO-8859-1 without xml-decl without doctype. */ + final def save(filename: String, node: Node): Unit = + save(filename, node, "ISO-8859-1"); + + /** saves XML to filename with given encoding, without xml-decl without doctype. */ + final def save(filename: String, node: Node, enc: String): Unit = + saveFull(filename, node, enc, false, null); + + /** saves a node to a file with given filename using encoding iso-8859-1 optionally with xmldecl and doctype declaration + * @param filename the filename + * @param node the xml node we want to write + * @param xmlDecl if true, write xml declaration + * @param doctype if not null, write doctype declaration + */ + final def saveFull(filename: String, node: Node, xmlDecl: Boolean, doctype: dtd.DocType): Unit = + saveFull(filename, node, "ISO-8859-1", xmlDecl, doctype); + + /** saves a node to a file with given filename using given encoding optionally with xmldecl and doctype declaration. + * @param filename the filename + * @param node the xml node we want to write + * @param enc encoding to use + * @param xmlDecl if true, write xml declaration + * @param doctype if not null, write doctype declaration + */ + + final def saveFull(filename: String, node: Node, enc: String, xmlDecl: Boolean, doctype: dtd.DocType): Unit = { + var fos: FileOutputStream = null; + var w: Writer = null; + try { + /* using NIO classes of JDK 1.4 */ + import java.io.{FileOutputStream,Writer}; + import java.nio.channels.{Channels,FileChannel}; + + fos = new FileOutputStream( filename ) + w = Channels.newWriter( fos.getChannel(), enc ) + write(w, node, enc, xmlDecl, doctype) + } finally { + w.close(); + fos.close(); + } } + /** writes the given node using writer, optionally with xml decl and doctype. + * It's the caller's responsibility to close the writer. + * @param w the writer + * @param node the xml node we want to write + * @param enc the string to be used in xmlDecl + * @param xmlDecl if true, write xml declaration + * @param doctype if not null, write doctype declaration + */ + final def write(w: java.io.Writer, node: Node, enc: String, xmlDecl: Boolean, doctype: dtd.DocType): Unit = { + /* 2do: optimize by giving writer parameter to toXML*/ + if(xmlDecl) w.write( "<?xml version='1.0' encoding='"+enc+"'?>\n"); + if(doctype!=null) w.write( doctype.toString()+"\n"); + w.write( Utility.toXML( node )); + } } |