home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / util / Archiver.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  7.2 KB  |  271 lines

  1. package netscape.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5.  
  6. public class Archiver implements Encoder {
  7.    Archive archive;
  8.    ArchivingStack stack = new ArchivingStack();
  9.    IdHashtable idHash = new IdHashtable(false);
  10.    Object currentObject;
  11.    ClassTable currentTable;
  12.    int currentId;
  13.    int currentColumnCount;
  14.    int currentRow;
  15.    int currentColumn;
  16.  
  17.    public Archiver(Archive var1) {
  18.       this.archive = var1;
  19.    }
  20.  
  21.    public Archive archive() {
  22.       return this.archive;
  23.    }
  24.  
  25.    public static void writeObject(OutputStream var0, Object var1) throws IOException, CodingException {
  26.       Archive var2 = new Archive();
  27.       Archiver var3 = new Archiver(var2);
  28.       var3.archiveRootObject(var1);
  29.       var2.write(var0);
  30.    }
  31.  
  32.    public void archiveRootObject(Object var1) throws CodingException {
  33.       int var2 = this.identifierForObject(var1);
  34.       this.archive.addRootIdentifier(var2);
  35.    }
  36.  
  37.    private int identifierForObject(Object var1) throws CodingException {
  38.       if (var1 == null) {
  39.          return 0;
  40.       } else {
  41.          int var2 = this.idHash.get(var1);
  42.          if (var2 != 0) {
  43.             return var2;
  44.          } else {
  45.             String var6 = var1.getClass().getName();
  46.             ExternalCoder var5 = this.archive.externalCoderForName(var6);
  47.             ClassTable var3 = this.archive.classTableForName(var6);
  48.             if (var3 == null) {
  49.                ClassInfo var4 = new ClassInfo(var6);
  50.                if (var5 != null) {
  51.                   var5.describeClassInfo(var1, var4);
  52.                } else {
  53.                   ((Codable)var1).describeClassInfo(var4);
  54.                }
  55.  
  56.                var3 = new ClassTable(this.archive, var4);
  57.                var3.setUniqueStrings(true);
  58.                this.archive.addClassTable(var3);
  59.             }
  60.  
  61.             var2 = var3.newIdentifier();
  62.             this.idHash.putKnownAbsent(var1, var2);
  63.             this.pushArchivingState(var1, var2);
  64.             if (var5 != null) {
  65.                var5.encode(var1, this);
  66.             } else {
  67.                ((Codable)var1).encode(this);
  68.             }
  69.  
  70.             this.popArchivingState();
  71.             return var2;
  72.          }
  73.       }
  74.    }
  75.  
  76.    private void pushArchivingState(Object var1, int var2) {
  77.       this.stack.pushArchiver(this);
  78.       this.currentObject = var1;
  79.       this.currentTable = this.archive.classTableForIdentifier(var2);
  80.       this.currentId = var2;
  81.       this.currentRow = this.archive.rowForIdentifier(var2);
  82.       this.currentColumn = -1;
  83.       this.currentColumnCount = this.currentTable.fieldCount;
  84.    }
  85.  
  86.    private void popArchivingState() {
  87.       this.stack.popArchiver(this);
  88.    }
  89.  
  90.    private void prepareToArchiveField(String var1) throws CodingException {
  91.       int var3 = this.currentColumnCount;
  92.       String[] var4 = this.currentTable.fieldNames;
  93.  
  94.       for(int var2 = this.currentColumn + 1; var2 < var3; ++var2) {
  95.          if (var1 == var4[var2]) {
  96.             this.currentColumn = var2;
  97.             return;
  98.          }
  99.       }
  100.  
  101.       this.currentColumn = this.currentTable.columnForField(var1);
  102.       if (this.currentColumn < 0) {
  103.          throw new CodingException("Unknown field name: " + var1);
  104.       }
  105.    }
  106.  
  107.    public void encodeBoolean(String var1, boolean var2) throws CodingException {
  108.       this.prepareToArchiveField(var1);
  109.       this.currentTable.setBooleanAt(this.currentRow, this.currentColumn, var2);
  110.    }
  111.  
  112.    public void encodeBooleanArray(String var1, boolean[] var2, int var3, int var4) throws CodingException {
  113.       boolean[] var5 = null;
  114.       this.prepareToArchiveField(var1);
  115.       if (var2 != null) {
  116.          var5 = new boolean[var4];
  117.          System.arraycopy(var2, var3, var5, 0, var4);
  118.       }
  119.  
  120.       this.currentTable.setBooleanArrayAt(this.currentRow, this.currentColumn, var5);
  121.    }
  122.  
  123.    public void encodeChar(String var1, char var2) throws CodingException {
  124.       this.prepareToArchiveField(var1);
  125.       this.currentTable.setCharAt(this.currentRow, this.currentColumn, var2);
  126.    }
  127.  
  128.    public void encodeCharArray(String var1, char[] var2, int var3, int var4) throws CodingException {
  129.       char[] var5 = null;
  130.       this.prepareToArchiveField(var1);
  131.       if (var2 != null) {
  132.          var5 = new char[var4];
  133.          System.arraycopy(var2, var3, var5, 0, var4);
  134.       }
  135.  
  136.       this.currentTable.setCharArrayAt(this.currentRow, this.currentColumn, var5);
  137.    }
  138.  
  139.    public void encodeByte(String var1, byte var2) throws CodingException {
  140.       this.prepareToArchiveField(var1);
  141.       this.currentTable.setByteAt(this.currentRow, this.currentColumn, var2);
  142.    }
  143.  
  144.    public void encodeByteArray(String var1, byte[] var2, int var3, int var4) throws CodingException {
  145.       byte[] var5 = null;
  146.       this.prepareToArchiveField(var1);
  147.       if (var2 != null) {
  148.          var5 = new byte[var4];
  149.          System.arraycopy(var2, var3, var5, 0, var4);
  150.       }
  151.  
  152.       this.currentTable.setByteArrayAt(this.currentRow, this.currentColumn, var5);
  153.    }
  154.  
  155.    public void encodeShort(String var1, short var2) throws CodingException {
  156.       this.prepareToArchiveField(var1);
  157.       this.currentTable.setShortAt(this.currentRow, this.currentColumn, var2);
  158.    }
  159.  
  160.    public void encodeShortArray(String var1, short[] var2, int var3, int var4) throws CodingException {
  161.       short[] var5 = null;
  162.       this.prepareToArchiveField(var1);
  163.       if (var2 != null) {
  164.          var5 = new short[var4];
  165.          System.arraycopy(var2, var3, var5, 0, var4);
  166.       }
  167.  
  168.       this.currentTable.setShortArrayAt(this.currentRow, this.currentColumn, var5);
  169.    }
  170.  
  171.    public void encodeInt(String var1, int var2) throws CodingException {
  172.       this.prepareToArchiveField(var1);
  173.       this.currentTable.setIntAt(this.currentRow, this.currentColumn, var2);
  174.    }
  175.  
  176.    public void encodeIntArray(String var1, int[] var2, int var3, int var4) throws CodingException {
  177.       int[] var5 = null;
  178.       this.prepareToArchiveField(var1);
  179.       if (var2 != null) {
  180.          var5 = new int[var4];
  181.          System.arraycopy(var2, var3, var5, 0, var4);
  182.       }
  183.  
  184.       this.currentTable.setIntArrayAt(this.currentRow, this.currentColumn, var5);
  185.    }
  186.  
  187.    public void encodeLong(String var1, long var2) throws CodingException {
  188.       this.prepareToArchiveField(var1);
  189.       this.currentTable.setLongAt(this.currentRow, this.currentColumn, var2);
  190.    }
  191.  
  192.    public void encodeLongArray(String var1, long[] var2, int var3, int var4) throws CodingException {
  193.       long[] var5 = null;
  194.       this.prepareToArchiveField(var1);
  195.       if (var2 != null) {
  196.          var5 = new long[var4];
  197.          System.arraycopy(var2, var3, var5, 0, var4);
  198.       }
  199.  
  200.       this.currentTable.setLongArrayAt(this.currentRow, this.currentColumn, var5);
  201.    }
  202.  
  203.    public void encodeFloat(String var1, float var2) throws CodingException {
  204.       this.prepareToArchiveField(var1);
  205.       this.currentTable.setFloatAt(this.currentRow, this.currentColumn, var2);
  206.    }
  207.  
  208.    public void encodeFloatArray(String var1, float[] var2, int var3, int var4) throws CodingException {
  209.       float[] var5 = null;
  210.       this.prepareToArchiveField(var1);
  211.       if (var2 != null) {
  212.          var5 = new float[var4];
  213.          System.arraycopy(var2, var3, var5, 0, var4);
  214.       }
  215.  
  216.       this.currentTable.setFloatArrayAt(this.currentRow, this.currentColumn, var5);
  217.    }
  218.  
  219.    public void encodeDouble(String var1, double var2) throws CodingException {
  220.       this.prepareToArchiveField(var1);
  221.       this.currentTable.setDoubleAt(this.currentRow, this.currentColumn, var2);
  222.    }
  223.  
  224.    public void encodeDoubleArray(String var1, double[] var2, int var3, int var4) throws CodingException {
  225.       double[] var5 = null;
  226.       this.prepareToArchiveField(var1);
  227.       if (var2 != null) {
  228.          var5 = new double[var4];
  229.          System.arraycopy(var2, var3, var5, 0, var4);
  230.       }
  231.  
  232.       this.currentTable.setDoubleArrayAt(this.currentRow, this.currentColumn, var5);
  233.    }
  234.  
  235.    public void encodeString(String var1, String var2) throws CodingException {
  236.       this.prepareToArchiveField(var1);
  237.       this.currentTable.setStringAt(this.currentRow, this.currentColumn, var2);
  238.    }
  239.  
  240.    public void encodeStringArray(String var1, String[] var2, int var3, int var4) throws CodingException {
  241.       String[] var5 = null;
  242.       this.prepareToArchiveField(var1);
  243.       if (var2 != null) {
  244.          var5 = new String[var4];
  245.          System.arraycopy(var2, var3, var5, 0, var4);
  246.       }
  247.  
  248.       this.currentTable.setStringArrayAt(this.currentRow, this.currentColumn, var5);
  249.    }
  250.  
  251.    public void encodeObject(String var1, Object var2) throws CodingException {
  252.       this.prepareToArchiveField(var1);
  253.       this.currentTable.setIdentifierAt(this.currentRow, this.currentColumn, this.identifierForObject(var2));
  254.    }
  255.  
  256.    public void encodeObjectArray(String var1, Object[] var2, int var3, int var4) throws CodingException {
  257.       this.prepareToArchiveField(var1);
  258.       if (var2 == null) {
  259.          this.currentTable.setIdentifierArrayAt(this.currentRow, this.currentColumn, (int[])null);
  260.       } else {
  261.          int[] var6 = new int[var4];
  262.  
  263.          for(int var5 = var3; var5 < var4; ++var5) {
  264.             var6[var5] = this.identifierForObject(var2[var5]);
  265.          }
  266.  
  267.          this.currentTable.setIdentifierArrayAt(this.currentRow, this.currentColumn, var6);
  268.       }
  269.    }
  270. }
  271.