home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 September / pcp155a.iso / linux / ldp / FolderData.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-05-02  |  5.4 KB  |  196 lines

  1. import java.applet.Applet;
  2. import java.awt.Image;
  3. import java.io.BufferedInputStream;
  4. import java.io.DataInputStream;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8.  
  9. public class FolderData {
  10.    private Applet theApplet;
  11.    private String dataFile = "";
  12.    private Folder rootFolder;
  13.    private String defaultTarget;
  14.    private Image defaultRoot;
  15.    private Image defaultNode;
  16.    private Image defaultOpen;
  17.    private Image defaultClosed;
  18.    private static final String OPENER = "<";
  19.    private static final String CLOSER = ">";
  20.    private String buffer = "";
  21.  
  22.    public FolderData(String dataFile, Image defaultRoot, Image defaultNode, Image defaultOpen, Image defaultClosed, String defaultTarget, Applet theApplet) {
  23.       this.dataFile = dataFile;
  24.       this.defaultRoot = defaultRoot;
  25.       this.defaultNode = defaultNode;
  26.       this.defaultOpen = defaultOpen;
  27.       this.defaultClosed = defaultClosed;
  28.       this.defaultTarget = defaultTarget;
  29.       this.theApplet = theApplet;
  30.       DataInputStream in = this.openDataFile(this.dataFile);
  31.       String tag = "";
  32.  
  33.       while((tag = this.readSegment(in)) != null) {
  34.          if (tag.equals("<FOLDER>")) {
  35.             this.readFolder(in, (Folder)null);
  36.          }
  37.       }
  38.  
  39.       this.getRootFolder().setOpen(true);
  40.    }
  41.  
  42.    private void readFolder(DataInputStream in, Folder parent) {
  43.       String tag = "";
  44.       boolean closedIconGiven = false;
  45.       boolean openIconGiven = false;
  46.       Folder f1 = new Folder(parent);
  47.       f1.setIconOpen(this.defaultNode);
  48.       f1.setIconClosed(this.defaultNode);
  49.       f1.setTarget(this.defaultTarget);
  50.  
  51.       for(String var7 = this.readSegment(in); var7 != null && !var7.equals("</FOLDER>"); var7 = this.readSegment(in)) {
  52.          if (var7.equals("<HREF>")) {
  53.             f1.setHRef(this.createURL(this.readSimpleField(in, "HREF")));
  54.          } else if (var7.equals("<TEXT>")) {
  55.             f1.setText(this.readSimpleField(in, "TEXT"));
  56.          } else if (var7.equals("<ICONOPEN>")) {
  57.             f1.setIconOpen(this.theApplet.getImage(this.theApplet.getDocumentBase(), this.readSimpleField(in, "ICONOPEN")));
  58.             openIconGiven = true;
  59.          } else if (var7.equals("<ICON>")) {
  60.             f1.setIconClosed(this.theApplet.getImage(this.theApplet.getDocumentBase(), this.readSimpleField(in, "ICON")));
  61.             closedIconGiven = true;
  62.          } else if (var7.equals("<FOLDER>")) {
  63.             if (!openIconGiven) {
  64.                f1.setIconOpen(this.defaultOpen);
  65.             }
  66.  
  67.             if (!closedIconGiven) {
  68.                f1.setIconClosed(this.defaultClosed);
  69.             }
  70.  
  71.             this.readFolder(in, f1);
  72.          } else if (var7.equals("<TARGET>")) {
  73.             f1.setTarget(this.readSimpleField(in, "TARGET"));
  74.          } else if (var7.equals("<WARNING>")) {
  75.             f1.setWarning(this.readSimpleField(in, "WARNING"));
  76.          } else if (var7.equals("<NOJAVA>")) {
  77.             this.readSimpleField(in, "NOJAVA");
  78.          } else {
  79.             this.alert(String.valueOf("Unknown text/tag: ").concat(String.valueOf(var7)));
  80.          }
  81.       }
  82.  
  83.       if (parent == null) {
  84.          this.rootFolder = f1;
  85.          this.rootFolder.setIconOpen(this.defaultRoot);
  86.          this.rootFolder.setIconClosed(this.defaultRoot);
  87.       }
  88.  
  89.    }
  90.  
  91.    private URL createURL(String u) {
  92.       URL createURL = null;
  93.       if (u != null) {
  94.          try {
  95.             createURL = new URL(this.theApplet.getDocumentBase(), u);
  96.          } catch (MalformedURLException var4) {
  97.             System.out.println(String.valueOf("Bad URL: ").concat(String.valueOf(u)));
  98.             createURL = null;
  99.          }
  100.       }
  101.  
  102.       return createURL;
  103.    }
  104.  
  105.    public Folder getRootFolder() {
  106.       return this.rootFolder;
  107.    }
  108.  
  109.    private String readSimpleField(DataInputStream in, String field) {
  110.       String tag = "";
  111.       String text = "";
  112.  
  113.       for(String var5 = this.readSegment(in); var5 != null && !var5.equals(String.valueOf(String.valueOf("</").concat(String.valueOf(field))).concat(String.valueOf(">"))); var5 = this.readSegment(in)) {
  114.          if (var5.startsWith("<")) {
  115.             this.alert(String.valueOf(String.valueOf(String.valueOf("Error found this in ").concat(String.valueOf(field))).concat(String.valueOf(" tag: "))).concat(String.valueOf(var5)));
  116.          } else {
  117.             text = String.valueOf(text).concat(String.valueOf(var5));
  118.          }
  119.       }
  120.  
  121.       return text;
  122.    }
  123.  
  124.    private DataInputStream openDataFile(String dataFile) {
  125.       DataInputStream in = null;
  126.  
  127.       try {
  128.          URL fileURL = new URL(this.theApplet.getDocumentBase(), dataFile);
  129.          in = new DataInputStream(new BufferedInputStream(fileURL.openStream()));
  130.       } catch (IOException e) {
  131.          this.alert(String.valueOf(String.valueOf(String.valueOf("Cannot open ").concat(String.valueOf(dataFile))).concat(String.valueOf(": "))).concat(String.valueOf(((Throwable)e).getMessage())));
  132.       }
  133.  
  134.       return in;
  135.    }
  136.  
  137.    public String readSegment(DataInputStream in) {
  138.       String temp = "";
  139.       String temp2 = "";
  140.       String text = "";
  141.       if (!this.buffer.equals("")) {
  142.          temp = this.buffer;
  143.       } else {
  144.          try {
  145.             temp = in.readLine();
  146.          } catch (IOException e) {
  147.             this.alert(String.valueOf("IOException: ").concat(String.valueOf(((Throwable)e).getMessage())));
  148.          }
  149.  
  150.          if (temp == null) {
  151.             return temp;
  152.          }
  153.       }
  154.  
  155.       int pos1 = temp.indexOf("<");
  156.       int pos2 = temp.indexOf(">");
  157.       if (pos1 == -1) {
  158.          this.buffer = "";
  159.          text = temp;
  160.       } else {
  161.          if (pos2 == -1) {
  162.             for(String var10 = " "; !var10.equals("") && pos2 == -1; pos2 = temp.indexOf(">")) {
  163.                try {
  164.                   var10 = in.readLine();
  165.                   temp = String.valueOf(String.valueOf(temp).concat(String.valueOf(" "))).concat(String.valueOf(var10));
  166.                } catch (IOException e) {
  167.                   this.alert(String.valueOf("IOException: ").concat(String.valueOf(((Throwable)e).getMessage())));
  168.                }
  169.             }
  170.  
  171.             if (pos2 == -1) {
  172.                this.alert("No closing '>' on line");
  173.             }
  174.          }
  175.  
  176.          if (pos1 == 0) {
  177.             text = temp.substring(0, pos2 + ">".length()).toUpperCase();
  178.             this.buffer = temp.substring(pos2 + ">".length());
  179.          } else {
  180.             text = temp.substring(0, pos1);
  181.             this.buffer = temp.substring(pos1);
  182.          }
  183.       }
  184.  
  185.       if (text.startsWith("<?") || text.startsWith("<!")) {
  186.          text = "";
  187.       }
  188.  
  189.       return text.trim();
  190.    }
  191.  
  192.    private void alert(String msg) {
  193.       this.theApplet.showStatus(msg);
  194.    }
  195. }
  196.