summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorburaq <buraq@epfl.ch>2005-05-26 14:02:47 +0000
committerburaq <buraq@epfl.ch>2005-05-26 14:02:47 +0000
commite389932a0948127ed9b34eb2c7c57aa9817a4368 (patch)
tree6ea6a8b3fde870f4ba4f732654241b38517f9f91
parent2a99a8010f8b129893d046976195997bc9feb363 (diff)
downloadscala-e389932a0948127ed9b34eb2c7c57aa9817a4368.tar.gz
scala-e389932a0948127ed9b34eb2c7c57aa9817a4368.tar.bz2
scala-e389932a0948127ed9b34eb2c7c57aa9817a4368.zip
even better dtd parsing! handled URLs now
-rw-r--r--config/list/library.lst1
-rw-r--r--sources/scala/xml/parsing/ConstructingParser.scala28
-rw-r--r--sources/scala/xml/parsing/ExternalSources.scala76
-rw-r--r--sources/scala/xml/parsing/MarkupParser.scala73
4 files changed, 128 insertions, 50 deletions
diff --git a/config/list/library.lst b/config/list/library.lst
index f20bba95ac..d0c694e253 100644
--- a/config/list/library.lst
+++ b/config/list/library.lst
@@ -256,6 +256,7 @@ xml/factory/LoggedNodeFactory.scala
xml/parsing/ConstructingHandler.scala
xml/parsing/ConstructingParser.scala
+xml/parsing/ExternalSources.scala
xml/parsing/FatalError.scala
xml/parsing/MarkupHandler.scala
xml/parsing/MarkupParser.scala
diff --git a/sources/scala/xml/parsing/ConstructingParser.scala b/sources/scala/xml/parsing/ConstructingParser.scala
index 51299ec916..791d3b2f2c 100644
--- a/sources/scala/xml/parsing/ConstructingParser.scala
+++ b/sources/scala/xml/parsing/ConstructingParser.scala
@@ -14,24 +14,7 @@ import scala.io.Source;
object ConstructingParser {
def fromFile(inp: java.io.File, preserveWS: Boolean) = {
- /* DEBUG
- val src = Source.fromFile(inp);
- while(src.hasNext) {
- Console.print(src.next);
- if(!src.hasNext) {
- Console.print("last character!");
- Console.print(src.ch);
- }
- }
- */
val p = new ConstructingParser(Source.fromFile(inp), preserveWS);
- /*
- {
- override def externalSource(systemLiteral: String): Source = {
- Source.fromFile(new java.io.File(inp.getParent(), systemLiteral));
- }
- }
- */
p.nextch;
p
}
@@ -47,19 +30,12 @@ object ConstructingParser {
*/
class ConstructingParser(inp: Source, presWS:Boolean)
extends ConstructingHandler
+with ExternalSources
with MarkupParser {
override val isValidating = true;
val preserveWS = presWS;
val input = inp;
val handle = this;
-
- override def externalSource(systemLiteral: String): Source = {
- var fileStr = inp.descr;
- if(inp.descr.startsWith("file:")) {
- fileStr = inp.descr.substring(5, inp.descr.length());
- }
- fileStr = fileStr.substring(0,fileStr.lastIndexOf(java.io.File.separator)+1);
- Source.fromFile(fileStr + systemLiteral);
- }
}
+
diff --git a/sources/scala/xml/parsing/ExternalSources.scala b/sources/scala/xml/parsing/ExternalSources.scala
new file mode 100644
index 0000000000..7d42d6ec6a
--- /dev/null
+++ b/sources/scala/xml/parsing/ExternalSources.scala
@@ -0,0 +1,76 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala.xml.parsing ;
+
+import scala.io.Source;
+import java.net._;
+import java.io._;
+
+trait ExternalSources : (MarkupParser with MarkupHandler) {
+
+
+ private def externalSourceFromURL(url:URL): Source = {
+ val in =
+ new BufferedReader(
+ new InputStreamReader(
+ url.openStream()));
+
+ //@todo: replace this hack with proper Source implementation
+
+ val str = new StringBuffer();
+ var inputLine:String = _;
+
+ //while (inputLine = in.readLine()) != null) {
+ while ({inputLine = in.readLine(); inputLine} != null) {
+ // Console.println(inputLine); // DEBUG
+ str.append(inputLine);
+ str.append('\n'); // readable output
+ }
+ in.close();
+
+ class MyClass extends Source {
+
+ def newIter = new Iterator[Char] {
+ var i = -1;
+ private val len = str.length()-1;
+ def hasNext = i < len;
+ def next = {
+ i = i + 1;
+ str.charAt(i);
+ }
+ }
+
+ val iter = newIter;
+
+ def reset: Source = new MyClass;
+
+ override var descr = url.toExternalForm();
+ }
+
+ return new MyClass;
+ }
+
+ def externalSource(systemId: String): Source = {
+ //Console.println("in external source("+systemId+")");
+ if(systemId.startsWith("http:")) {
+ return externalSourceFromURL(new URL(systemId));
+ }
+
+ var fileStr = input.descr;
+
+ if(input.descr.startsWith("file:")) {
+ fileStr = input.descr.substring(5, input.descr.length());
+ } else
+ fileStr = fileStr.substring(0,
+ fileStr.lastIndexOf(java.io.File.separator)+1);
+ Source.fromFile(fileStr + systemId);
+ }
+
+}
diff --git a/sources/scala/xml/parsing/MarkupParser.scala b/sources/scala/xml/parsing/MarkupParser.scala
index 8b52bd8db4..95c7b3d440 100644
--- a/sources/scala/xml/parsing/MarkupParser.scala
+++ b/sources/scala/xml/parsing/MarkupParser.scala
@@ -45,8 +45,8 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
var pos: Int = _;
- /* true if reading external subset */
- var inExtSubSet = false;
+ /* used when reading external subset */
+ var extIndex = -1;
/** holds temporary values of pos */
var tmppos: Int = _;
@@ -226,9 +226,9 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
ch = curInput.next;
pos = curInput.pos;
} else {
- //Console.println("nextch, curInput.hasNext == false ") ;
- //Console.println("nextch, Nil != inpStack == "+(Nil!=inpStack));
- if (Nil != inpStack) {
+ val ilen = inpStack.length;
+ //Console.println(" ilen = "+ilen+ " extIndex = "+extIndex);
+ if ((ilen != extIndex) && (ilen > 0)) {
/** for external source, inpStack == Nil ! need notify of eof! */
pop();
} else {
@@ -580,9 +580,11 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
*/
if(null != extID) {
- val extSubsetSrc = externalSource( extID.systemId );
- inExtSubSet = true;
+ pushExternal(extID.systemId);
+ //val extSubsetSrc = externalSource( extID.systemId );
+
+ extIndex = inpStack.length;
/*
.indexOf(':') != -1) { // assume URI
Source.fromFile(new java.net.URI(extID.systemLiteral));
@@ -591,20 +593,23 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
}
*/
//Console.println("I'll print it now");
- val old = curInput;
- tmppos = curInput.pos;
- val oldch = ch;
- curInput = extSubsetSrc;
- pos = 0;
- nextch;
+ //val old = curInput;
+ //tmppos = curInput.pos;
+ //val oldch = ch;
+ //curInput = extSubsetSrc;
+ //pos = 0;
+ //nextch;
+
extSubset();
- inExtSubSet = false;
+ pop();
- curInput = old;
- pos = curInput.pos;
- ch = curInput.ch;
- eof = false;
+ extIndex = -1;
+
+ //curInput = old;
+ //pos = curInput.pos;
+ //ch = curInput.ch;
+ //eof = false;
//while(extSubsetSrc.hasNext)
//Console.print(extSubsetSrc.next);
@@ -806,7 +811,7 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
} else
markupDecl1();
}
- while(curInput.hasNext) {
+ while(!eof) {
markupDecl();
}
}
@@ -843,7 +848,7 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
nextch;
notationDecl();
- case '[' if inExtSubSet =>
+ case '[' if inpStack.length >= extIndex =>
nextch;
xSpaceOpt;
ch match {
@@ -949,9 +954,11 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
val n = xName;
xSpace;
while ('>' != ch) {
+ //Console.println("["+ch+"]");
putChar(ch);
nextch;
}
+ //Console.println("END["+ch+"]");
nextch;
val cmstr = cbuf.toString();
cbuf.setLength(0);
@@ -1102,10 +1109,10 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
def push(entityName:String) = {
//Console.println("BEFORE PUSHING "+ch);
//Console.println("BEFORE PUSHING "+pos);
- //Console.println("PUSHING "+entityName);
- if(!eof) {
+ //Console.print("[PUSHING "+entityName+"]");
+ if(!eof)
inpStack = curInput :: inpStack;
- }
+
curInput = replacementText(entityName);
nextch;
}
@@ -1116,13 +1123,31 @@ abstract class MarkupParser: (MarkupParser with MarkupHandler) extends AnyRef wi
nextch;
}
*/
+
+ def pushExternal(systemId:String) = {
+ //Console.print("BEFORE PUSH, curInput = $"+curInput.descr);
+ //Console.println(" stack = "+inpStack.map { x => "$"+x.descr });
+
+ //Console.print("[PUSHING EXTERNAL "+systemId+"]");
+ if(!eof)
+ inpStack = curInput :: inpStack;
+
+ curInput = externalSource(systemId);
+
+ //Console.print("AFTER PUSH, curInput = $"+curInput.descr);
+ //Console.println(" stack = "+inpStack.map { x => "$"+x.descr });
+
+ nextch;
+ }
def pop() = {
- //Console.println("POPPING");
+
curInput = inpStack.head;
inpStack = inpStack.tail;
ch = curInput.ch;
pos = curInput.pos;
eof = false; // must be false, because of places where entity refs occur
+ //Console.println("\n AFTER POP, curInput = $"+curInput.descr);
+ //Console.println(inpStack.map { x => x.descr });
}
}