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 / SGMLParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  9.1 KB  |  338 lines

  1. package netscape.palomar.sgml;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.FilterInputStream;
  9. import java.io.FilterOutputStream;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.util.Hashtable;
  13. import java.util.Vector;
  14. import netscape.palomar.util.CascadedException;
  15.  
  16. public class SGMLParser {
  17.    protected String[] charEscape = new String[256];
  18.    protected Hashtable parseTable = new Hashtable(50);
  19.    protected Hashtable paramSets = new Hashtable(50);
  20.    protected Hashtable charXlate = new Hashtable(350);
  21.    protected Class _unknownTagClass;
  22.    protected Class _textClass;
  23.    protected Class _scriptClass;
  24.    protected Class _treeTopClass;
  25.  
  26.    public SGMLParser() throws CascadedException {
  27.       try {
  28.          this._unknownTagClass = Class.forName("netscape.palomar.sgml.SGMLTagImpl");
  29.          this._textClass = Class.forName("netscape.palomar.sgml.SGMLTextImpl");
  30.          this._scriptClass = Class.forName("netscape.palomar.sgml.SGMLTextImpl");
  31.          this._treeTopClass = Class.forName("netscape.palomar.sgml.SGMLTagTop");
  32.       } catch (Exception ex) {
  33.          SGMLException ce = new SGMLException(200, ex);
  34.          throw ce;
  35.       }
  36.    }
  37.  
  38.    public SGMLTag parseFile(String filename) throws CascadedException {
  39.       try {
  40.          FileInputStream fis = new FileInputStream(filename);
  41.          BufferedInputStream bis = new BufferedInputStream(fis);
  42.          SGMLTag rslt = this.parseStream(bis);
  43.          ((FilterInputStream)bis).close();
  44.          return rslt;
  45.       } catch (Exception e) {
  46.          SGMLException ce = new SGMLException(201, e);
  47.          ((CascadedException)ce).addToken("filename", filename);
  48.          throw ce;
  49.       }
  50.    }
  51.  
  52.    public void writeFile(String filename, SGMLTag content) throws CascadedException {
  53.       try {
  54.          FileOutputStream fos = new FileOutputStream(filename);
  55.          BufferedOutputStream bos = new BufferedOutputStream(fos);
  56.          this.writeStream(bos, content);
  57.          ((FilterOutputStream)bos).close();
  58.       } catch (Exception e) {
  59.          SGMLException ce = new SGMLException(202, e);
  60.          ((CascadedException)ce).addToken("filename", filename);
  61.          throw ce;
  62.       }
  63.    }
  64.  
  65.    public SGMLTag parseStream(InputStream is) throws CascadedException {
  66.       try {
  67.          SGMLStream fis = new SGMLStream(is, this);
  68.          SGMLTag res = (SGMLTag)this._treeTopClass.newInstance();
  69.          SGMLParseTableEntry phony = (SGMLParseTableEntry)this.parseTable.get("%UNKNOWN");
  70.          if (phony == null) {
  71.             phony = new SGMLParseTableEntry(this, this._unknownTagClass);
  72.             phony.contentEmpty = true;
  73.             this.parseTable.put("%UNKNOWN", phony);
  74.          }
  75.  
  76.          res.preParseSetup("<TOP>", phony);
  77.          res.parseChildren(fis, new Vector(), new Vector());
  78.          return res;
  79.       } catch (Exception e) {
  80.          SGMLException ce = new SGMLException(201, e);
  81.          ((CascadedException)ce).addToken("filename", "STREAM");
  82.          throw ce;
  83.       }
  84.    }
  85.  
  86.    public void writeStream(OutputStream os, SGMLTag content) throws CascadedException {
  87.       DataOutputStream outData = new DataOutputStream(os);
  88.       content.writeYourself(outData);
  89.    }
  90.  
  91.    public SGMLTag generateTag(String lineImage) throws CascadedException {
  92.       String tokenType = null;
  93.  
  94.       try {
  95.          tokenType = getTagType(lineImage);
  96.          SGMLParseTableEntry pte = (SGMLParseTableEntry)this.parseTable.get(tokenType);
  97.          if (pte == null) {
  98.             SGMLTag newT = (SGMLTag)this._unknownTagClass.newInstance();
  99.             newT.preParseSetup(lineImage, (SGMLParseTableEntry)this.parseTable.get("%UNKNOWN"));
  100.             return newT;
  101.          } else {
  102.             SGMLTag newT = (SGMLTag)pte.generatedClass.newInstance();
  103.             newT.preParseSetup(lineImage, pte);
  104.             return newT;
  105.          }
  106.       } catch (Exception e) {
  107.          SGMLException pe = new SGMLException(203, e);
  108.          ((CascadedException)pe).addToken("tag", tokenType);
  109.          throw pe;
  110.       }
  111.    }
  112.  
  113.    public SGMLText generateText(String lineImage) throws CascadedException {
  114.       try {
  115.          SGMLText retText = (SGMLText)this._textClass.newInstance();
  116.          retText.setParser(this);
  117.          retText.setRawValue(lineImage);
  118.          return retText;
  119.       } catch (Exception e) {
  120.          SGMLException pe = new SGMLException(204, e);
  121.          throw pe;
  122.       }
  123.    }
  124.  
  125.    public SGMLText generateScript(String rawValue) throws CascadedException {
  126.       try {
  127.          SGMLText retText = (SGMLText)this._scriptClass.newInstance();
  128.          retText.setParser(this);
  129.          retText.setRawValue(rawValue);
  130.          return retText;
  131.       } catch (Exception e) {
  132.          SGMLException pe = new SGMLException(204, e);
  133.          throw pe;
  134.       }
  135.    }
  136.  
  137.    public static String getTagType(String lineImage) {
  138.       int i = 1;
  139.       int len = lineImage.length();
  140.       if (len < 3) {
  141.          return "";
  142.       } else {
  143.          while(i < len) {
  144.             char ch = lineImage.charAt(i);
  145.             if (ch <= ' ') {
  146.                break;
  147.             }
  148.  
  149.             ++i;
  150.          }
  151.  
  152.          return i < len ? lineImage.substring(1, i).toUpperCase() : lineImage.substring(1, len - 1).toUpperCase();
  153.       }
  154.    }
  155.  
  156.    public static int findWhite(String s, int start) {
  157.       int i = start;
  158.  
  159.       for(int last = s.length(); i < last; ++i) {
  160.          char ch = s.charAt(i);
  161.          if (ch <= ' ') {
  162.             return i;
  163.          }
  164.       }
  165.  
  166.       return -1;
  167.    }
  168.  
  169.    public static int spanWhite(String s, int start) {
  170.       int i = start;
  171.  
  172.       for(int last = s.length(); i < last; ++i) {
  173.          char ch = s.charAt(i);
  174.          if (ch > ' ') {
  175.             return i;
  176.          }
  177.       }
  178.  
  179.       return -1;
  180.    }
  181.  
  182.    public void setChar(String escape, int value) {
  183.       charHolder hold = new charHolder(escape, value);
  184.       this.charXlate.put(escape, hold);
  185.       if (value >= 0 & value < 256) {
  186.          this.charEscape[value] = "&" + escape + ";";
  187.       }
  188.  
  189.    }
  190.  
  191.    public int lookUpChar(String entitiy) {
  192.       charHolder hold = (charHolder)this.charXlate.get(entitiy);
  193.       return hold == null ? -1 : hold.getValue();
  194.    }
  195.  
  196.    public String escapeChar(char ch) {
  197.       return ch > 255 ? "&#" + ch + ";" : this.charEscape[ch];
  198.    }
  199.  
  200.    public String escapeText(String input) {
  201.       if (input == null) {
  202.          return null;
  203.       } else {
  204.          StringBuffer out = new StringBuffer();
  205.          int len = input.length();
  206.  
  207.          for(int i = 0; i < len; ++i) {
  208.             out.append(this.escapeChar(input.charAt(i)));
  209.          }
  210.  
  211.          return out.toString();
  212.       }
  213.    }
  214.  
  215.    public String unEscapeText(String input) {
  216.       if (input == null) {
  217.          return null;
  218.       } else {
  219.          StringBuffer out = new StringBuffer();
  220.          int last = input.length();
  221.  
  222.          for(int pos = 0; pos < last; ++pos) {
  223.             char ch = input.charAt(pos);
  224.             switch (ch) {
  225.                case '\r':
  226.                   break;
  227.                case '&':
  228.                   int semiPos = input.indexOf(";", pos + 1);
  229.                   if (semiPos < 0) {
  230.                      out.append('&');
  231.                   } else {
  232.                      if (input.charAt(pos + 1) == '#') {
  233.                         String num = input.substring(pos + 2, semiPos);
  234.  
  235.                         try {
  236.                            int val = Integer.parseInt(num);
  237.                            out.append((char)val);
  238.                         } catch (NumberFormatException var9) {
  239.                            out.append(input.substring(pos, semiPos + 1));
  240.                         }
  241.                      } else {
  242.                         String esc = input.substring(pos + 1, semiPos);
  243.                         int escVal = this.lookUpChar(esc);
  244.                         if (escVal > 0) {
  245.                            out.append((char)escVal);
  246.                         } else {
  247.                            out.append(input.substring(pos, semiPos + 1));
  248.                         }
  249.                      }
  250.  
  251.                      pos = semiPos;
  252.                   }
  253.                   break;
  254.                default:
  255.                   out.append(ch);
  256.             }
  257.          }
  258.  
  259.          return out.toString();
  260.       }
  261.    }
  262.  
  263.    private String unEscapeText_OLD_OLD_OLD(String input) {
  264.       if (input == null) {
  265.          return null;
  266.       } else {
  267.          StringBuffer out = new StringBuffer();
  268.          int oldPos = 0;
  269.  
  270.          for(int pos = input.indexOf("&"); pos >= 0; pos = input.indexOf("&", oldPos)) {
  271.             out.append(input.substring(oldPos, pos));
  272.             int semiPos = input.indexOf(";", pos + 1);
  273.             if (semiPos < 0) {
  274.                out.append(input.substring(pos));
  275.                return out.toString();
  276.             }
  277.  
  278.             if (input.charAt(pos + 1) == '#') {
  279.                String num = input.substring(pos + 2, semiPos);
  280.  
  281.                try {
  282.                   int val = Integer.parseInt(num);
  283.                   out.append((char)val);
  284.                } catch (NumberFormatException var8) {
  285.                   out.append(input.substring(pos, semiPos + 1));
  286.                }
  287.             } else {
  288.                String esc = input.substring(pos + 1, semiPos);
  289.                int escVal = this.lookUpChar(esc);
  290.                if (escVal > 0) {
  291.                   out.append((char)escVal);
  292.                } else {
  293.                   out.append(input.substring(pos, semiPos + 1));
  294.                }
  295.             }
  296.  
  297.             oldPos = semiPos + 1;
  298.          }
  299.  
  300.          out.append(input.substring(oldPos));
  301.          return out.toString();
  302.       }
  303.    }
  304.  
  305.    public void setUnknownTagClass(Class utc) {
  306.       this._unknownTagClass = utc;
  307.    }
  308.  
  309.    public void setTextClass(Class tc) {
  310.       this._textClass = tc;
  311.    }
  312.  
  313.    public void setScriptClass(Class tc) {
  314.       this._scriptClass = tc;
  315.    }
  316.  
  317.    public void setTopClass(Class tc) {
  318.       this._treeTopClass = tc;
  319.    }
  320.  
  321.    public SGMLParseTableEntry getParseTableEntry(String tagName) {
  322.       if (tagName == null) {
  323.          tagName = "%UNKNOWN";
  324.       }
  325.  
  326.       SGMLParseTableEntry pte = (SGMLParseTableEntry)this.parseTable.get(tagName);
  327.       if (pte == null) {
  328.          pte = (SGMLParseTableEntry)this.parseTable.get("%UNKNOWN");
  329.       }
  330.  
  331.       return pte;
  332.    }
  333.  
  334.    public String[] getStandardAttributeList(String tagName) {
  335.       return (String[])this.paramSets.get(tagName);
  336.    }
  337. }
  338.