home *** CD-ROM | disk | FTP | other *** search
/ Popular Software (Premium Edition) / mycd.iso / INTERNET / NETSCAP4.06 / CP32E406.EXE / nav40.z / java40.jar / netscape / util / Unarchiver.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-08-13  |  8.5 KB  |  340 lines

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