home *** CD-ROM | disk | FTP | other *** search
- package netscape.palomar.sgml;
-
- import java.io.InputStream;
- import java.util.Date;
- import netscape.palomar.util.CascadedException;
-
- public class SGMLStreamB extends SGMLStream {
- InputStream _inputStreamB;
- byte[] _mainBuffer;
- int _fillMark;
-
- public SGMLStreamB(InputStream nf, SGMLParser parser) throws CascadedException {
- super(parser);
- this._inputStreamB = nf;
- this.loadBuffer();
- }
-
- private boolean loadBuffer() throws CascadedException {
- try {
- if (super._endOfFile) {
- return true;
- } else {
- int addSize = this._inputStreamB.available();
- if (addSize > 0) {
- 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 + addSize + 1000];
- System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
- }
-
- int actualRead = this._inputStreamB.read(this._mainBuffer, this._fillMark, addSize);
- this._fillMark += actualRead;
- return false;
- } else {
- int waitedByte = this._inputStreamB.read();
- if (waitedByte == -1) {
- return super._endOfFile = true;
- } else {
- addSize = this._inputStreamB.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._inputStreamB.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 offset) throws CascadedException {
- while(true) {
- 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;
- }
- }
-
- private String readComment() throws CascadedException {
- try {
- int scanner = super._linePos + 3;
-
- do {
- scanner = this.indexOf((byte)45, scanner);
- if (scanner < 0) {
- return new String(this._mainBuffer, 0, super._linePos, this._fillMark - super._linePos) + "-->";
- }
-
- scanner += 2;
- } while(this._mainBuffer[scanner - 1] == 45);
-
- scanner = this.indexOf((byte)62, scanner);
- if (scanner < 0) {
- return new String(this._mainBuffer, 0, super._linePos, this._fillMark - super._linePos) + ">";
- } else {
- super._isText = true;
- ++scanner;
- String retval = new String(this._mainBuffer, 0, super._linePos, scanner - super._linePos);
- super._linePos = scanner;
- return retval;
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(206, e);
- ((CascadedException)ce).addToken("line", Integer.toString(super._linePos));
- throw ce;
- }
- }
-
- public String readTag() throws CascadedException {
- try {
- if (super._endOfFile) {
- return null;
- } else if (super._isText) {
- SGMLException ce = new SGMLException(207);
- throw ce;
- } else {
- if (this._fillMark < super._linePos + 4) {
- this.loadBuffer();
- }
-
- if (this._fillMark > super._linePos + 3 && this._mainBuffer[super._linePos + 1] == 33 && this._mainBuffer[super._linePos + 1] == 45 && this._mainBuffer[super._linePos + 1] == 45) {
- return this.readComment();
- } else {
- ++super._tagNumber;
- int indexPos = this.indexOf((byte)62, super._linePos);
- if (indexPos < 0) {
- return null;
- } else {
- super._isText = true;
- String retval = new String(this._mainBuffer, 0, super._linePos, indexPos + 1 - super._linePos);
- super._linePos = indexPos + 1;
- return retval;
- }
- }
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(208, e);
- ((CascadedException)ce).addToken("line", Integer.toString(super._lineNumber));
- throw ce;
- }
- }
-
- public String readText() throws CascadedException {
- try {
- if (super._endOfFile) {
- return null;
- } else if (!super._isText) {
- SGMLException ce = new SGMLException(209);
- throw ce;
- } else {
- int indexPos = this.indexOf((byte)60, super._linePos);
- if (indexPos < 0) {
- indexPos = this._fillMark;
- }
-
- super._isText = false;
- String retVal = new String(this._mainBuffer, 0, super._linePos, indexPos - super._linePos);
- super._linePos = indexPos;
- return retVal;
- }
- } catch (Exception e) {
- SGMLException ce = new SGMLException(210, e);
- ((CascadedException)ce).addToken("line", Integer.toString(super._lineNumber));
- throw ce;
- }
- }
-
- private final int indexOfTag(String upperCaseEndToken) throws CascadedException {
- byte[] angleBracketSlash = new byte[]{60, 47};
- int pos = super._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;
- }
-
- if (followOnByte == 9) {
- return pos;
- }
-
- if (followOnByte == 10) {
- 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, super._linePos, indexPos - super._linePos);
- super._linePos = indexPos;
- super._isText = false;
- return retVal;
- } catch (Exception e) {
- SGMLException ce = new SGMLException(211, e);
- ((CascadedException)ce).addToken("tag", endToken);
- throw ce;
- }
- }
-
- public void close() throws CascadedException {
- super._closeTime = new Date();
-
- try {
- this._inputStreamB.close();
- } catch (Exception e) {
- SGMLException ce = new SGMLException(212, e);
- throw ce;
- }
- }
- }
-