home *** CD-ROM | disk | FTP | other *** search
/ Popular Software (Premium Edition) / mycd.iso / INTERNET / NETSCAP4.06 / CP32E406.EXE / netcast.z / ncjava10.jar / netscape / palomar / sgml / SGMLStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  7.1 KB  |  345 lines

  1. package netscape.palomar.sgml;
  2.  
  3. import java.io.InputStream;
  4. import java.util.Date;
  5. import java.util.Vector;
  6. import netscape.palomar.util.CascadedException;
  7.  
  8. public class SGMLStream {
  9.    InputStream _inputStream;
  10.    byte[] _mainBuffer;
  11.    int _fillMark;
  12.    int _linePos;
  13.    boolean _isText;
  14.    boolean _endOfFile;
  15.    SGMLParser _parseTables;
  16.    int _lineNumber;
  17.    int _tagNumber;
  18.    Date _startTime;
  19.    Date _closeTime;
  20.    String _fileName;
  21.    Vector _warnings;
  22.  
  23.    public SGMLStream(InputStream nf, SGMLParser parser) throws CascadedException {
  24.       this(parser);
  25.       this._inputStream = nf;
  26.       this.loadBuffer();
  27.    }
  28.  
  29.    SGMLStream(SGMLParser parser) throws CascadedException {
  30.       this._isText = true;
  31.       this._endOfFile = false;
  32.       this._fileName = "(stream)";
  33.       this._parseTables = parser;
  34.       this._startTime = new Date();
  35.       this._warnings = new Vector();
  36.    }
  37.  
  38.    private boolean loadBuffer() throws CascadedException {
  39.       try {
  40.          if (this._endOfFile) {
  41.             return true;
  42.          } else {
  43.             int addSize = this._inputStream.available();
  44.             if (addSize < 3000) {
  45.                addSize = 3000;
  46.             }
  47.  
  48.             if (addSize > 0) {
  49.                for(int actualRead = 0; actualRead == 0; this._fillMark += actualRead) {
  50.                   if (this._mainBuffer == null) {
  51.                      this._mainBuffer = new byte[addSize + 100];
  52.                   } else if (this._fillMark + addSize > this._mainBuffer.length) {
  53.                      byte[] oldBuffer = this._mainBuffer;
  54.                      this._mainBuffer = new byte[this._fillMark * 3 / 2 + addSize];
  55.                      System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
  56.                   }
  57.  
  58.                   actualRead = this._inputStream.read(this._mainBuffer, this._fillMark, addSize);
  59.                   if (actualRead < 0) {
  60.                      return this._endOfFile = true;
  61.                   }
  62.                }
  63.  
  64.                return false;
  65.             } else {
  66.                int waitedByte = this._inputStream.read();
  67.                if (waitedByte == -1) {
  68.                   return this._endOfFile = true;
  69.                } else {
  70.                   addSize = this._inputStream.available();
  71.                   if (this._mainBuffer == null) {
  72.                      this._mainBuffer = new byte[addSize + 100];
  73.                   } else if (this._fillMark + addSize + 1 > this._mainBuffer.length) {
  74.                      byte[] oldBuffer = this._mainBuffer;
  75.                      this._mainBuffer = new byte[this._fillMark + addSize + this._fillMark / 3 + 1000];
  76.                      System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
  77.                   }
  78.  
  79.                   this._mainBuffer[this._fillMark++] = (byte)waitedByte;
  80.                   int actualRead = this._inputStream.read(this._mainBuffer, this._fillMark, addSize);
  81.                   this._fillMark += actualRead;
  82.                   return false;
  83.                }
  84.             }
  85.          }
  86.       } catch (Exception e) {
  87.          throw new SGMLException(205, e);
  88.       }
  89.    }
  90.  
  91.    private final int indexOf(byte b, int offset) throws CascadedException {
  92.       while(offset < this._fillMark || !this.loadBuffer()) {
  93.          if (this._mainBuffer[offset] == b) {
  94.             return offset;
  95.          }
  96.  
  97.          ++offset;
  98.       }
  99.  
  100.       return -1;
  101.    }
  102.  
  103.    private final int indexOf(byte[] b, int offsetInit) throws CascadedException {
  104.       int offset = offsetInit;
  105.  
  106.       do {
  107.          int second = 0;
  108.  
  109.          while(this._mainBuffer[offset + second] == b[second]) {
  110.             ++second;
  111.             if (second >= b.length) {
  112.                return offset;
  113.             }
  114.  
  115.             if (offset + second >= this._fillMark && this.loadBuffer()) {
  116.                return -1;
  117.             }
  118.          }
  119.  
  120.          ++offset;
  121.       } while(offset < this._fillMark || !this.loadBuffer());
  122.  
  123.       return -1;
  124.    }
  125.  
  126.    private String readComment() throws CascadedException {
  127.       try {
  128.          int scanner = this._linePos + 4;
  129.  
  130.          do {
  131.             scanner = this.indexOf((byte)45, scanner);
  132.             if (scanner < 0) {
  133.                return new String(this._mainBuffer, 0, this._linePos, this._fillMark - this._linePos) + "-->";
  134.             }
  135.  
  136.             scanner += 2;
  137.          } while(this._mainBuffer[scanner - 1] != 45);
  138.  
  139.          scanner = this.indexOf((byte)62, scanner);
  140.          if (scanner < 0) {
  141.             return new String(this._mainBuffer, 0, this._linePos, this._fillMark - this._linePos) + ">";
  142.          } else {
  143.             this._isText = true;
  144.             ++scanner;
  145.             String retval = new String(this._mainBuffer, 0, this._linePos, scanner - this._linePos);
  146.             this._linePos = scanner;
  147.             return retval;
  148.          }
  149.       } catch (Exception e) {
  150.          SGMLException ce = new SGMLException(206, e);
  151.          ((CascadedException)ce).addToken("line", Integer.toString(this._linePos));
  152.          throw ce;
  153.       }
  154.    }
  155.  
  156.    public String readTag() throws CascadedException {
  157.       try {
  158.          if (this._endOfFile) {
  159.             return null;
  160.          } else if (this._isText) {
  161.             SGMLException ce = new SGMLException(207);
  162.             throw ce;
  163.          } else {
  164.             if (this._fillMark < this._linePos + 4) {
  165.                this.loadBuffer();
  166.             }
  167.  
  168.             if (this._fillMark > this._linePos + 3 && this._mainBuffer[this._linePos + 1] == 33 && this._mainBuffer[this._linePos + 2] == 45 && this._mainBuffer[this._linePos + 3] == 45) {
  169.                return this.readComment();
  170.             } else {
  171.                ++this._tagNumber;
  172.                int indexPos = this.indexOf((byte)62, this._linePos);
  173.                if (indexPos < 0) {
  174.                   return null;
  175.                } else {
  176.                   this._isText = true;
  177.                   String retval = new String(this._mainBuffer, 0, this._linePos, indexPos + 1 - this._linePos);
  178.                   this._linePos = indexPos + 1;
  179.                   return retval;
  180.                }
  181.             }
  182.          }
  183.       } catch (Exception e) {
  184.          SGMLException ce = new SGMLException(208, e);
  185.          ((CascadedException)ce).addToken("line", Integer.toString(this._lineNumber));
  186.          throw ce;
  187.       }
  188.    }
  189.  
  190.    public String readText() throws CascadedException {
  191.       int indexPos = -2;
  192.       String retVal = "unset";
  193.  
  194.       try {
  195.          if (this._endOfFile) {
  196.             return null;
  197.          } else if (!this._isText) {
  198.             SGMLException ce = new SGMLException(209);
  199.             throw ce;
  200.          } else {
  201.             indexPos = this.indexOf((byte)60, this._linePos);
  202.             if (indexPos < 0) {
  203.                indexPos = this._fillMark;
  204.             }
  205.  
  206.             this._isText = false;
  207.             retVal = new String(this._mainBuffer, 0, this._linePos, indexPos - this._linePos);
  208.             this._linePos = indexPos;
  209.             return retVal;
  210.          }
  211.       } catch (Exception e) {
  212.          SGMLException ce = new SGMLException(210, e);
  213.          ((CascadedException)ce).addToken("indexPos", Integer.toString(indexPos));
  214.          ((CascadedException)ce).addToken("position", Integer.toString(this._linePos));
  215.          ((CascadedException)ce).addToken("fillMark", Integer.toString(this._fillMark));
  216.          ((CascadedException)ce).addToken("bufferSize", Integer.toString(this._mainBuffer.length));
  217.          throw ce;
  218.       }
  219.    }
  220.  
  221.    private final int indexOfTag(String upperCaseEndToken) throws CascadedException {
  222.       byte[] angleBracketSlash = new byte[]{60, 47};
  223.       int pos = this._linePos;
  224.       int tokenLen = upperCaseEndToken.length();
  225.  
  226.       while(true) {
  227.          pos = this.indexOf(angleBracketSlash, pos);
  228.          if (pos < 0) {
  229.             return -1;
  230.          }
  231.  
  232.          boolean found = true;
  233.          int scanbase = pos + 1;
  234.  
  235.          for(int j = 1; j < tokenLen; ++j) {
  236.             if (Character.toUpperCase((char)this._mainBuffer[scanbase + j]) != upperCaseEndToken.charAt(j)) {
  237.                found = false;
  238.                break;
  239.             }
  240.          }
  241.  
  242.          if (found) {
  243.             byte followOnByte = this._mainBuffer[scanbase + tokenLen];
  244.             if (followOnByte == 62) {
  245.                return pos;
  246.             }
  247.  
  248.             if (followOnByte <= 32) {
  249.                return pos;
  250.             }
  251.          }
  252.  
  253.          ++pos;
  254.       }
  255.    }
  256.  
  257.    public String getTextTilToken(String endToken) throws CascadedException {
  258.       try {
  259.          String upperCaseEnd = endToken.toUpperCase();
  260.          int indexPos = this.indexOfTag(upperCaseEnd);
  261.          if (indexPos < 0) {
  262.             indexPos = this._fillMark;
  263.          }
  264.  
  265.          String retVal = new String(this._mainBuffer, 0, this._linePos, indexPos - this._linePos);
  266.          this._linePos = indexPos;
  267.          this._isText = false;
  268.          return retVal;
  269.       } catch (Exception e) {
  270.          SGMLException ce = new SGMLException(211, e);
  271.          ((CascadedException)ce).addToken("tag", endToken);
  272.          throw ce;
  273.       }
  274.    }
  275.  
  276.    public SGMLTag generateTag(String rawValue) throws CascadedException {
  277.       return this._parseTables.generateTag(rawValue);
  278.    }
  279.  
  280.    public SGMLText generateText(String rawValue) throws CascadedException {
  281.       return this._parseTables.generateText(rawValue);
  282.    }
  283.  
  284.    public SGMLText generateScript(String rawValue) throws CascadedException {
  285.       return this._parseTables.generateScript(rawValue);
  286.    }
  287.  
  288.    public boolean textNext() {
  289.       return this._isText;
  290.    }
  291.  
  292.    public void close() throws CascadedException {
  293.       this._closeTime = new Date();
  294.  
  295.       try {
  296.          this._inputStream.close();
  297.       } catch (Exception e) {
  298.          SGMLException ce = new SGMLException(212, e);
  299.          throw ce;
  300.       }
  301.    }
  302.  
  303.    public int getLineNumber() {
  304.       return this._lineNumber;
  305.    }
  306.  
  307.    public int getColumnNumber() {
  308.       return this._linePos;
  309.    }
  310.  
  311.    public int getTagNumber() {
  312.       return this._tagNumber;
  313.    }
  314.  
  315.    public Date getStartTime() {
  316.       return this._startTime;
  317.    }
  318.  
  319.    public long elapsedTime() {
  320.       Date end = this._closeTime;
  321.       if (end == null) {
  322.          end = new Date();
  323.       }
  324.  
  325.       return end.getTime() - this._startTime.getTime();
  326.    }
  327.  
  328.    public void registerWarning(Exception e) {
  329.       this._warnings.addElement(e);
  330.       CascadedException.printException(e, "SGML Parser");
  331.    }
  332.  
  333.    public Vector getWarnings() {
  334.       return this._warnings;
  335.    }
  336.  
  337.    public String getFileName() {
  338.       return this._fileName;
  339.    }
  340.  
  341.    public void setFileName(String newName) {
  342.       this._fileName = newName;
  343.    }
  344. }
  345.