X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FXML.java;h=29e798daea833c890532ad954728284f2276626d;hb=1f80106afa0f0eb6f12544c75304f2084aca6499;hp=5837b04ce7831606cd0e8f1f41be4485995bc1f4;hpb=1c3169cf69a08e5920e5f1c3e3e153b4da5a2de3;p=org.ibex.util.git diff --git a/src/org/ibex/util/XML.java b/src/org/ibex/util/XML.java index 5837b04..29e798d 100644 --- a/src/org/ibex/util/XML.java +++ b/src/org/ibex/util/XML.java @@ -1,14 +1,10 @@ -// Copyright (C) 2003 Adam Megacz all rights reserved. -// -// You may modify, copy, and redistribute this code under the terms of -// the GNU Library Public License version 2.1, with the exception of -// the portion of clause 6a after the semicolon (aka the "obnoxious -// relink clause") +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. package org.ibex.util; import java.io.Reader; -import java.io.Writer; import java.io.IOException; import java.io.EOFException; @@ -48,8 +44,8 @@ import java.io.EOFException; * @see XML Specification * @see XML Namespaces */ -public abstract class XML { - +public abstract class XML +{ ///////////////////////////////////////////////////////////////////////////////////////////// // XML Parser ///////////////////////////////////////////////////////////////////////////////////////////// @@ -65,16 +61,16 @@ public abstract class XML { private static final char[] single_lt = new char[] { '<' }; private static final char[] single_quot = new char[] { '"' }; - int line; - int col; + private int line; + private int col; - Reader in; - char[] buf; - int off; - int base; // base+off == distance into the stream - int len; + private Reader in; + private char[] buf; + private int off; + private int base; // base+off == distance into the stream + private int len; - Element current = null; + private Element current; // used in readEntity() to process a single character without creating a new array private char[] singlechar = new char[1]; @@ -84,8 +80,9 @@ public abstract class XML { public XML(int bSize) { buf = new char[bSize]; - //current = (Element)elements.remove(false); - if (current == null) current = newElement(); + + current = (Element)elements.remove(false); + if (current == null) current = new Element(); } /** Returns the line number at the beginning of the last process call. */ @@ -97,8 +94,6 @@ public abstract class XML { /** Returns the global file offset at the beginning of the last process call. */ public int getGlobalOffset() { return base + off; } - Element newElement() { return new Element(); } - /** * Parse given input and call the abstract event functions. * @@ -125,39 +120,8 @@ public abstract class XML { } finally { clear(); } // clean up elements } - // Stuff below here is Adam's hack ////////////////////////////////////////////////////////////////////////////// - - boolean done = false; - public static class Pull extends XML { - public Pull(Reader in) { this.in = in; off = len = 0; line = col = 1; clear(); } - StringBuffer sb = new StringBuffer(); - Element pending = null; - boolean emptytag = true; - public int level = 0; - public final void startElement(Element e) throws Exn { emptytag = false; level++; pending = e; } - public final void endElement(Element e) throws Exn, IOException { emptytag=pending!=null; level--; } - public final void whitespace(char[] ch, int start, int length) throws Exn, IOException { } - public final void characters(char[] ch, int start, int length) throws Exn, IOException { - emptytag=false; sb.append(ch,start,length);} - public Object read() throws Exn, IOException { - while(!done) { - if (pending != null) { Element ret = pending; pending = null; ret.level = level-(emptytag?0:1); return ret; } - if (sb.length() > 0) { String ret = sb.toString(); sb.setLength(0); return ret; } - if (!buffer(1)) { - if (done) return null; - throw new Exn("reached eof without closing <"+current.qName+"> element", Exn.WFC, getLine(), getCol()); - } - if (buf[off] == '<') readTag(); else readChars(!done); - } - return null; - } - } - - - // Stuff above here is Adam's hack ////////////////////////////////////////////////////////////////////////////// - /** remove any leftover elements from the linked list and queue them */ - final void clear() { + private final void clear() { for (Element last = current; current.parent != null; ) { current = current.parent; last.clear(); @@ -167,7 +131,7 @@ public abstract class XML { } /** reads in a tag. expects buf[off] == '<' */ - final void readTag() throws IOException, Exn { + private final void readTag() throws IOException, Exn { // Start Tag '<' Name (S Attribute)* S? '>' boolean starttag = true; @@ -329,7 +293,8 @@ public abstract class XML { // create the in-memory element representation of this beast // if current.qName == null then this is the root element we're dealing with if (current.qName != null) { - Element next = newElement(); + Element next = (Element)elements.remove(false); + if (next == null) next = new Element(); //next.clear(); // TODO: remove as elements now checked as they're added to the queue next.parent = current; current = next; @@ -390,18 +355,17 @@ public abstract class XML { // we just closed an element, so remove it from the element 'stack' if (current.getParent() == null) { // we just finished the root element - done = true; + current.clear(); } else { Element last = current; current = current.parent; - //last.clear(); FIXME + last.clear(); elements.append(last); } } } } - /** reads in an attribute of an element. expects Name(buf[off]) */ private final void readAttribute() throws IOException, Exn { int ref = 0; @@ -571,7 +535,7 @@ public abstract class XML { } /** reads until the passed string is encountered. */ - final void readChars(boolean p, String match, boolean entities) throws IOException, Exn { + private final void readChars(boolean p, String match, boolean entities) throws IOException, Exn { int ref; char[] end = match.toCharArray(); @@ -626,7 +590,7 @@ public abstract class XML { * reads until a < symbol is encountered * @param p If true call the characters(char[],int,int) funciton for the processed characters */ - final void readChars(boolean p) throws IOException, Exn { + private final void readChars(boolean p) throws IOException, Exn { int ref; for (boolean more = true; more;) { @@ -719,7 +683,7 @@ public abstract class XML { * @param min Minimum number of characters to read (even if we have to block to do it). * @return return false if min can't be reached. */ - final boolean buffer(int min) throws IOException { + private final boolean buffer(int min) throws IOException { if (len > min) return true; if (buf.length - (off+len) >= min) { @@ -779,6 +743,7 @@ public abstract class XML { /** Represents the end of an Element. */ public abstract void endElement(Element e) throws Exn, IOException; + ///////////////////////////////////////////////////////////////////////////////////////////// // Inner Classes for Parser Support ///////////////////////////////////////////////////////////////////////////////////////////// @@ -801,7 +766,6 @@ public abstract class XML { protected String localName = null; protected String qName = null; protected String prefix = null; - public int level = 0; protected Hash urimap = new Hash(3,3);