diff options
-rw-r--r-- | sources/scala/xml/NodeFactory.scala | 24 | ||||
-rw-r--r-- | sources/scala/xml/Parsing.scala | 24 | ||||
-rw-r--r-- | sources/scala/xml/TextBuffer.scala | 48 |
3 files changed, 96 insertions, 0 deletions
diff --git a/sources/scala/xml/NodeFactory.scala b/sources/scala/xml/NodeFactory.scala new file mode 100644 index 0000000000..8355db36ec --- /dev/null +++ b/sources/scala/xml/NodeFactory.scala @@ -0,0 +1,24 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ +package scala.xml ; + +abstract class NodeFactory( config:NodeFactoryConfig ) { + + def makeNode( elemName:String, attribs:Map[String,String], chIter:Seq[Node] ):Node; + + def makeText( s:String ) = Text( s ); + def makeComment( s:String ):Seq[Comment] = + if(config.ignodeComments) Nil else List( Comment( s ) ); + def makeProcInstr( t:String, s:String ) = ProcInstr( t, s ); + def makeCharData( s:String ) = CharData( s ); + +} + + +case class NodeFactoryConfig(ignoreComments:boolean,namespace:Namespace ) ; diff --git a/sources/scala/xml/Parsing.scala b/sources/scala/xml/Parsing.scala new file mode 100644 index 0000000000..7a34c3a174 --- /dev/null +++ b/sources/scala/xml/Parsing.scala @@ -0,0 +1,24 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ +package scala.xml ; + +object Parsing { + + /** (#x20 | #x9 | #xD | #xA) */ + final def isSpace( ch:Char ):Boolean = ch match { + case '\u0009' | '\u000A' | '\u000D' | '\u0020' => true + case _ => false; + } + + /** (#x20 | #x9 | #xD | #xA)+ */ + final def isSpace( cs:Seq[Char] ):Boolean = { + val it = cs.elements; + it.hasNext && it.forall { isSpace }; + } +} diff --git a/sources/scala/xml/TextBuffer.scala b/sources/scala/xml/TextBuffer.scala new file mode 100644 index 0000000000..207bba560d --- /dev/null +++ b/sources/scala/xml/TextBuffer.scala @@ -0,0 +1,48 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ +package scala.xml ; + +/** this classes is for creating text nodes with trimmed whitespace. + * all occurrences of one or more whitespace in strings appended (using the + * append method) will be replaced by a single space character + */ +class TextBuffer { + + val sb = new StringBuffer(); + var ws = true; + + def appendSpace = if( !ws ) { ws = true; sb.append(' ');} else {}; + def appendChar(c:char) = { ws = false; sb.append( c );} + + /** appends this string to the text buffer, trimming whitespaces as needed */ + def append( cs:Seq[Char] ):TextBuffer = { + for( val c <- cs ) { + if( Parsing.isSpace( c ) ) + appendSpace; + else + appendChar( c ) + } + this + } + + /** returns an empty sequence if text is only whitespace */ + def toText:Seq[Text] = { + var len = sb.length(); /* invariant */ + if( len == 0 ) return Nil; + + if( Parsing.isSpace( sb.charAt( len - 1 ) )) { + len = len - 1; + sb.setLength( len ) + } + if( len == 0 ) return Nil; + + List( Text( sb.toString() ) ); + } + +} |