home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / browser / net_linx / java40.jar / netscape / util / Unarchiver.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  7.5 KB  |  341 lines

  1. package netscape.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import netscape.application.AWTCompatibility;
  6. import netscape.application.FoundationApplet;
  7.  
  8. public class Unarchiver implements Decoder {
  9.    Archive archive;
  10.    ArchivingStack stack = new ArchivingStack();
  11.    Object[] objectForId;
  12.    boolean[] referenceGivenOut;
  13.    int unarchivedCount;
  14.    Object[] unarchivedObjects;
  15.    ExternalCoder[] unarchivedCoders;
  16.    Object currentObject;
  17.    ClassTable currentTable;
  18.    int currentId;
  19.    int currentColumnCount;
  20.    int currentRow;
  21.    int currentColumn;
  22.    FoundationApplet applet;
  23.    boolean appletInitialized;
  24.  
  25.    public Unarchiver(Archive var1) {
  26.       this.archive = var1;
  27.    }
  28.  
  29.    public Archive archive() {
  30.       return this.archive;
  31.    }
  32.  
  33.    public static Object readObject(InputStream var0) throws IOException, CodingException {
  34.       Archive var1 = new Archive();
  35.       var1.read(var0);
  36.       Unarchiver var2 = new Unarchiver(var1);
  37.       int[] var3 = var1.rootIdentifiers();
  38.       return var3 != null && var3.length != 0 ? var2.unarchiveIdentifier(var3[0]) : null;
  39.    }
  40.  
  41.    public Object unarchiveIdentifier(int var1) throws CodingException {
  42.       if (var1 == 0) {
  43.          return null;
  44.       } else {
  45.          if (this.objectForId == null) {
  46.             this.referenceGivenOut = new boolean[this.archive.identifierCount()];
  47.             this.objectForId = new Object[this.referenceGivenOut.length];
  48.             this.unarchivedObjects = new Object[this.referenceGivenOut.length];
  49.             this.unarchivedCoders = new ExternalCoder[this.referenceGivenOut.length];
  50.          }
  51.  
  52.          this.clearFinishList();
  53.  
  54.          Object var2;
  55.          try {
  56.             var2 = this.objectForIdentifier(var1);
  57.             this.processFinishList();
  58.          } finally {
  59.             this.clearFinishList();
  60.          }
  61.  
  62.          return var2;
  63.       }
  64.    }
  65.  
  66.    protected Class classForName(String var1) throws CodingException {
  67.       Class var2 = null;
  68.       if (!this.appletInitialized) {
  69.          this.applet = (FoundationApplet)AWTCompatibility.awtApplet();
  70.          this.appletInitialized = true;
  71.       }
  72.  
  73.       try {
  74.          if (this.applet != null) {
  75.             var2 = this.applet.classForName(var1);
  76.          } else {
  77.             var2 = Class.forName(var1);
  78.          }
  79.       } catch (ClassNotFoundException var4) {
  80.          this.creationException(((Throwable)var4).toString(), var1);
  81.       } catch (NoSuchMethodError var5) {
  82.          this.creationException(((Throwable)var5).toString(), var1);
  83.       }
  84.  
  85.       return var2;
  86.    }
  87.  
  88.    protected Object newInstance(String var1) throws CodingException {
  89.       Object var3 = null;
  90.       Class var2 = this.classForName(var1);
  91.  
  92.       try {
  93.          var3 = var2.newInstance();
  94.       } catch (InstantiationException var5) {
  95.          this.creationException(((Throwable)var5).toString(), var1);
  96.       } catch (IllegalAccessException var6) {
  97.          this.creationException(((Throwable)var6).toString(), var1);
  98.       } catch (NoSuchMethodError var7) {
  99.          this.creationException(((Throwable)var7).toString(), var1);
  100.       }
  101.  
  102.       return var3;
  103.    }
  104.  
  105.    private void creationException(String var1, String var2) throws CodingException {
  106.       throw new CodingException(var1 + ".  Class " + var2 + " must be public and define a constructor taking no arguments.");
  107.    }
  108.  
  109.    private Object objectForIdentifier(int var1) throws CodingException {
  110.       Object var3 = this.objectForId[var1];
  111.       if (var3 != null) {
  112.          this.referenceGivenOut[var1] = true;
  113.          return var3;
  114.       } else if (var1 == 0) {
  115.          return null;
  116.       } else {
  117.          ClassTable var4 = this.archive.classTableForIdentifier(var1);
  118.          String var5 = var4.className();
  119.          ExternalCoder var2 = this.archive.externalCoderForName(var5);
  120.          if (var2 != null) {
  121.             var3 = var2.newInstance(var5);
  122.          } else {
  123.             var3 = this.newInstance(var5);
  124.          }
  125.  
  126.          if (!var4.hasUniqueStrings()) {
  127.             ClassInfo var6 = new ClassInfo(var5);
  128.             if (var2 != null) {
  129.                var2.describeClassInfo(var3, var6);
  130.             } else {
  131.                ((Codable)var3).describeClassInfo(var6);
  132.             }
  133.  
  134.             var4.uniqueStrings(var6);
  135.          }
  136.  
  137.          this.addToFinishList(var2, var3);
  138.          this.objectForId[var1] = var3;
  139.          this.pushUnarchivingState(var3, var1);
  140.          if (var2 != null) {
  141.             var2.decode(var3, this);
  142.          } else {
  143.             ((Codable)var3).decode(this);
  144.          }
  145.  
  146.          this.popUnarchivingState();
  147.          return this.objectForId[var1];
  148.       }
  149.    }
  150.  
  151.    private void pushUnarchivingState(Object var1, int var2) {
  152.       this.stack.pushUnarchiver(this);
  153.       this.currentObject = var1;
  154.       this.currentTable = this.archive.classTableForIdentifier(var2);
  155.       this.currentId = var2;
  156.       this.currentRow = this.archive.rowForIdentifier(var2);
  157.       this.currentColumn = -1;
  158.       this.currentColumnCount = this.currentTable.fieldCount;
  159.    }
  160.  
  161.    private void popUnarchivingState() {
  162.       this.stack.popUnarchiver(this);
  163.    }
  164.  
  165.    private void addToFinishList(ExternalCoder var1, Object var2) {
  166.       this.unarchivedCoders[this.unarchivedCount] = var1;
  167.       this.unarchivedObjects[this.unarchivedCount] = var2;
  168.       ++this.unarchivedCount;
  169.    }
  170.  
  171.    private void processFinishList() throws CodingException {
  172.       int var2 = this.unarchivedCount;
  173.  
  174.       for(int var1 = 0; var1 < var2; ++var1) {
  175.          ExternalCoder var3 = this.unarchivedCoders[var1];
  176.          Object var4 = this.unarchivedObjects[var1];
  177.          if (var3 != null) {
  178.             var3.finishDecoding(var4);
  179.          } else {
  180.             ((Codable)var4).finishDecoding();
  181.          }
  182.  
  183.          this.unarchivedCoders[var1] = null;
  184.          this.unarchivedObjects[var1] = null;
  185.       }
  186.  
  187.       this.unarchivedCount = 0;
  188.    }
  189.  
  190.    private void clearFinishList() {
  191.       int var2 = this.unarchivedCount;
  192.  
  193.       for(int var1 = 0; var1 < var2; ++var1) {
  194.          this.unarchivedCoders[var1] = null;
  195.          this.unarchivedObjects[var1] = null;
  196.       }
  197.  
  198.       this.unarchivedCount = 0;
  199.    }
  200.  
  201.    private void prepareToUnarchiveField(String var1) throws CodingException {
  202.       int var3 = this.currentColumnCount;
  203.       String[] var4 = this.currentTable.fieldNames;
  204.  
  205.       for(int var2 = this.currentColumn + 1; var2 < var3; ++var2) {
  206.          if (var1 == var4[var2]) {
  207.             this.currentColumn = var2;
  208.             return;
  209.          }
  210.       }
  211.  
  212.       this.currentColumn = this.currentTable.columnForField(var1);
  213.       if (this.currentColumn < 0) {
  214.          throw new CodingException("Unknown field name: " + var1);
  215.       }
  216.    }
  217.  
  218.    public int versionForClassName(String var1) throws CodingException {
  219.       return this.currentTable.versionForClassName(var1);
  220.    }
  221.  
  222.    public boolean decodeBoolean(String var1) throws CodingException {
  223.       this.prepareToUnarchiveField(var1);
  224.       return this.currentTable.booleanAt(this.currentRow, this.currentColumn);
  225.    }
  226.  
  227.    public boolean[] decodeBooleanArray(String var1) throws CodingException {
  228.       this.prepareToUnarchiveField(var1);
  229.       return this.currentTable.booleanArrayAt(this.currentRow, this.currentColumn);
  230.    }
  231.  
  232.    public char decodeChar(String var1) throws CodingException {
  233.       this.prepareToUnarchiveField(var1);
  234.       return this.currentTable.charAt(this.currentRow, this.currentColumn);
  235.    }
  236.  
  237.    public char[] decodeCharArray(String var1) throws CodingException {
  238.       this.prepareToUnarchiveField(var1);
  239.       return this.currentTable.charArrayAt(this.currentRow, this.currentColumn);
  240.    }
  241.  
  242.    public byte decodeByte(String var1) throws CodingException {
  243.       this.prepareToUnarchiveField(var1);
  244.       return this.currentTable.byteAt(this.currentRow, this.currentColumn);
  245.    }
  246.  
  247.    public byte[] decodeByteArray(String var1) throws CodingException {
  248.       this.prepareToUnarchiveField(var1);
  249.       return this.currentTable.byteArrayAt(this.currentRow, this.currentColumn);
  250.    }
  251.  
  252.    public short decodeShort(String var1) throws CodingException {
  253.       this.prepareToUnarchiveField(var1);
  254.       return this.currentTable.shortAt(this.currentRow, this.currentColumn);
  255.    }
  256.  
  257.    public short[] decodeShortArray(String var1) throws CodingException {
  258.       this.prepareToUnarchiveField(var1);
  259.       return this.currentTable.shortArrayAt(this.currentRow, this.currentColumn);
  260.    }
  261.  
  262.    public int decodeInt(String var1) throws CodingException {
  263.       this.prepareToUnarchiveField(var1);
  264.       return this.currentTable.intAt(this.currentRow, this.currentColumn);
  265.    }
  266.  
  267.    public int[] decodeIntArray(String var1) throws CodingException {
  268.       this.prepareToUnarchiveField(var1);
  269.       return this.currentTable.intArrayAt(this.currentRow, this.currentColumn);
  270.    }
  271.  
  272.    public long decodeLong(String var1) throws CodingException {
  273.       this.prepareToUnarchiveField(var1);
  274.       return this.currentTable.longAt(this.currentRow, this.currentColumn);
  275.    }
  276.  
  277.    public long[] decodeLongArray(String var1) throws CodingException {
  278.       this.prepareToUnarchiveField(var1);
  279.       return this.currentTable.longArrayAt(this.currentRow, this.currentColumn);
  280.    }
  281.  
  282.    public float decodeFloat(String var1) throws CodingException {
  283.       this.prepareToUnarchiveField(var1);
  284.       return this.currentTable.floatAt(this.currentRow, this.currentColumn);
  285.    }
  286.  
  287.    public float[] decodeFloatArray(String var1) throws CodingException {
  288.       this.prepareToUnarchiveField(var1);
  289.       return this.currentTable.floatArrayAt(this.currentRow, this.currentColumn);
  290.    }
  291.  
  292.    public double decodeDouble(String var1) throws CodingException {
  293.       this.prepareToUnarchiveField(var1);
  294.       return this.currentTable.doubleAt(this.currentRow, this.currentColumn);
  295.    }
  296.  
  297.    public double[] decodeDoubleArray(String var1) throws CodingException {
  298.       this.prepareToUnarchiveField(var1);
  299.       return this.currentTable.doubleArrayAt(this.currentRow, this.currentColumn);
  300.    }
  301.  
  302.    public String decodeString(String var1) throws CodingException {
  303.       this.prepareToUnarchiveField(var1);
  304.       return this.currentTable.stringAt(this.currentRow, this.currentColumn);
  305.    }
  306.  
  307.    public String[] decodeStringArray(String var1) throws CodingException {
  308.       this.prepareToUnarchiveField(var1);
  309.       return this.currentTable.stringArrayAt(this.currentRow, this.currentColumn);
  310.    }
  311.  
  312.    public Object decodeObject(String var1) throws CodingException {
  313.       this.prepareToUnarchiveField(var1);
  314.       return this.objectForIdentifier(this.currentTable.identifierAt(this.currentRow, this.currentColumn));
  315.    }
  316.  
  317.    public Object[] decodeObjectArray(String var1) throws CodingException {
  318.       this.prepareToUnarchiveField(var1);
  319.       int[] var3 = this.currentTable.identifierArrayAt(this.currentRow, this.currentColumn);
  320.       if (var3 == null) {
  321.          return null;
  322.       } else {
  323.          Object[] var4 = new Object[var3.length];
  324.  
  325.          for(int var2 = 0; var2 < var3.length; ++var2) {
  326.             var4[var2] = this.objectForIdentifier(var3[var2]);
  327.          }
  328.  
  329.          return var4;
  330.       }
  331.    }
  332.  
  333.    public void replaceObject(Object var1) throws CodingException {
  334.       if (this.referenceGivenOut[this.currentId]) {
  335.          throw new CodingException("Circular replacement exception");
  336.       } else {
  337.          this.objectForId[this.currentId] = var1;
  338.       }
  339.    }
  340. }
  341.