home *** CD-ROM | disk | FTP | other *** search
- package netscape.palomar.sgml;
-
- import java.io.InputStream;
- import java.util.Date;
- import java.util.Vector;
- import netscape.palomar.util.CascadedException;
-
- public class SGMLStream {
- InputStream _inputStream;
- byte[] _mainBuffer;
- int _fillMark;
- int _linePos;
- boolean _isText;
- boolean _endOfFile;
- SGMLParser _parseTables;
- int _lineNumber;
- int _tagNumber;
- Date _startTime;
- Date _closeTime;
- String _fileName;
- Vector _warnings;
-
- public SGMLStream(InputStream nf, SGMLParser parser) throws CascadedException {
- this(parser);
- this._inputStream = nf;
- this.loadBuffer();
- }
-
- SGMLStream(SGMLParser parser) throws CascadedException {
- this._isText = true;
- this._endOfFile = false;
- this._fileName = "(stream)";
- this._parseTables = parser;
- this._startTime = new Date();
- this._warnings = new Vector();
- }
-
- private boolean loadBuffer() throws CascadedException {
- try {
- if (this._endOfFile) {
- return true;
- } else {
- int addSize = this._inputStream.available();
- if (addSize < 3000) {
- addSize = 3000;
- }
-
- if (addSize > 0) {
- for(int actualRead = 0; actualRead == 0; this._fillMark += actualRead) {
- if (this._mainBuffer == null) {
- this._mainBuffer = new byte[addSize + 100];
- } else if (this._fillMark + addSize > this._mainBuffer.length) {
- byte[] oldBuffer = this._mainBuffer;
- this._mainBuffer = new byte[this._fillMark * 3 / 2 + addSize];
- System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
- }
-
- actualRead = this._inputStream.read(this._mainBuffer, this._fillMark, addSize);
- if (actualRead < 0) {
- return this._endOfFile = true;
- }
- }
-
- return false;
- } else {
- int waitedByte = this._inputStream.read();
- if (waitedByte == -1) {
- return this._endOfFile = true;
- } else {
- addSize = this._inputStream.available();
- if (this._mainBuffer == null) {
- this._mainBuffer = new byte[addSize + 100];
- } else if (this._fillMark + addSize + 1 > this._mainBuffer.length) {
- byte[] oldBuffer = this._mainBuffer;
- this._mainBuffer = new byte[this._fillMark + addSize + this._fillMark / 3 + 1000];
- System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
- }
-
- this._mainBuffer[this._fillMark++] = (byte)waitedByte;
- int actualRead = this._inputStream.read(this._mainBuffer, this._fillMark, addSize);
- this._fillMark += actualRead;
- return false;
- }
- }
- }
- } catch (Exception e) {
- throw new SGMLException(205, e);
- }
- }
-
- private final int indexOf(byte b, int offset) throws CascadedException {
- while(offset < this._fillMark || !this.loadBuffer()) {
- if (this._mainBuffer[offset] == b) {
- return offset;
- }
-
- ++offset;
- }
-
- return -1;
- }
-
- private final int indexOf(byte[] b, int offsetInit) throws CascadedException {
- int offset = offsetInit;
-
- do {
- int second = 0;
-
- while(this._mainBuffer[offset + second] == b[second]) {
- ++second;
- if (second >= b.length) {
- return offset;
- }
-
- if (offset + second >= this._fillMark && this.loadBuffer()) {
- return -1;
- }
- }
-
- ++offset;
- } while(offset < this._fillMark || !this.loadBuffer());
-
- return -1;
- }
-
- private String readComment() throws CascadedException {
- try {
- int scanner = this._linePos + 4;
-
- do {
- scanner = this.indexOf((byte)45, scanner);
- if (scanner < 0) {
- return new String(this._mainBuffer, 0, this._linePos, this._fillMark - this._linePos) + "-->";
- }
-
- scanner += 2;
- } while(this._mainBuffer[scanner - 1] != 45);
-
- scanner = this.indexOf((byte)62, scanner);
- if (scanner < 0) {
- return new String(this._mainBuffer, 0, this._linePos, this._fillMark - this._linePos) + ">";
- } else {
- this._isText = true;
- ++scanner;
- String retval = new String(this._mainBuffer, 0, this._linePos, scanner - this._linePos);
- this._linePos = scanner;
- return retval;
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(206, e);
- ((CascadedException)ce).addToken("line", Integer.toString(this._linePos));
- throw ce;
- }
- }
-
- public String readTag() throws CascadedException {
- try {
- if (this._endOfFile) {
- return null;
- } else if (this._isText) {
- SGMLException ce = new SGMLException(207);
- throw ce;
- } else {
- if (this._fillMark < this._linePos + 4) {
- this.loadBuffer();
- }
-
- if (this._fillMark > this._linePos + 3 && this._mainBuffer[this._linePos + 1] == 33 && this._mainBuffer[this._linePos + 2] == 45 && this._mainBuffer[this._linePos + 3] == 45) {
- return this.readComment();
- } else {
- ++this._tagNumber;
- int indexPos = this.indexOf((byte)62, this._linePos);
- if (indexPos < 0) {
- return null;
- } else {
- this._isText = true;
- String retval = new String(this._mainBuffer, 0, this._linePos, indexPos + 1 - this._linePos);
- this._linePos = indexPos + 1;
- return retval;
- }
- }
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(208, e);
- ((CascadedException)ce).addToken("line", Integer.toString(this._lineNumber));
- throw ce;
- }
- }
-
- public String readText() throws CascadedException {
- int indexPos = -2;
- String retVal = "unset";
-
- try {
- if (this._endOfFile) {
- return null;
- } else if (!this._isText) {
- SGMLException ce = new SGMLException(209);
- throw ce;
- } else {
- indexPos = this.indexOf((byte)60, this._linePos);
- if (indexPos < 0) {
- indexPos = this._fillMark;
- }
-
- this._isText = false;
- retVal = new String(this._mainBuffer, 0, this._linePos, indexPos - this._linePos);
- this._linePos = indexPos;
- return retVal;
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(210, e);
- ((CascadedException)ce).addToken("indexPos", Integer.toString(indexPos));
- ((CascadedException)ce).addToken("position", Integer.toString(this._linePos));
- ((CascadedException)ce).addToken("fillMark", Integer.toString(this._fillMark));
- ((CascadedException)ce).addToken("bufferSize", Integer.toString(this._mainBuffer.length));
- throw ce;
- }
- }
-
- private final int indexOfTag(String upperCaseEndToken) throws CascadedException {
- byte[] angleBracketSlash = new byte[]{60, 47};
- int pos = this._linePos;
- int tokenLen = upperCaseEndToken.length();
-
- while(true) {
- pos = this.indexOf(angleBracketSlash, pos);
- if (pos < 0) {
- return -1;
- }
-
- boolean found = true;
- int scanbase = pos + 1;
-
- for(int j = 1; j < tokenLen; ++j) {
- if (Character.toUpperCase((char)this._mainBuffer[scanbase + j]) != upperCaseEndToken.charAt(j)) {
- found = false;
- break;
- }
- }
-
- if (found) {
- byte followOnByte = this._mainBuffer[scanbase + tokenLen];
- if (followOnByte == 62) {
- return pos;
- }
-
- if (followOnByte <= 32) {
- return pos;
- }
- }
-
- ++pos;
- }
- }
-
- public String getTextTilToken(String endToken) throws CascadedException {
- try {
- String upperCaseEnd = endToken.toUpperCase();
- int indexPos = this.indexOfTag(upperCaseEnd);
- if (indexPos < 0) {
- indexPos = this._fillMark;
- }
-
- String retVal = new String(this._mainBuffer, 0, this._linePos, indexPos - this._linePos);
- this._linePos = indexPos;
- this._isText = false;
- return retVal;
- } catch (Exception e) {
- SGMLException ce = new SGMLException(211, e);
- ((CascadedException)ce).addToken("tag", endToken);
- throw ce;
- }
- }
-
- public SGMLTag generateTag(String rawValue) throws CascadedException {
- return this._parseTables.generateTag(rawValue);
- }
-
- public SGMLText generateText(String rawValue) throws CascadedException {
- return this._parseTables.generateText(rawValue);
- }
-
- public SGMLText generateScript(String rawValue) throws CascadedException {
- return this._parseTables.generateScript(rawValue);
- }
-
- public boolean textNext() {
- return this._isText;
- }
-
- public void close() throws CascadedException {
- this._closeTime = new Date();
-
- try {
- this._inputStream.close();
- } catch (Exception e) {
- SGMLException ce = new SGMLException(212, e);
- throw ce;
- }
- }
-
- public int getLineNumber() {
- return this._lineNumber;
- }
-
- public int getColumnNumber() {
- return this._linePos;
- }
-
- public int getTagNumber() {
- return this._tagNumber;
- }
-
- public Date getStartTime() {
- return this._startTime;
- }
-
- public long elapsedTime() {
- Date end = this._closeTime;
- if (end == null) {
- end = new Date();
- }
-
- return end.getTime() - this._startTime.getTime();
- }
-
- public void registerWarning(Exception e) {
- this._warnings.addElement(e);
- CascadedException.printException(e, "SGML Parser");
- }
-
- public Vector getWarnings() {
- return this._warnings;
- }
-
- public String getFileName() {
- return this._fileName;
- }
-
- public void setFileName(String newName) {
- this._fileName = newName;
- }
- }
-