Package com.jacob.com

Examples of com.jacob.com.SafeArray


        }
        //RB 20051106: If the sheet is empty, getUsedRange returns $A$1 and
        // toSafeArray fails. Therefore we have to check that there is some text
        // to extract by comparing the address of the range to the string "$A$1".
        if (!extractRange.getAddress().equals("$A$1")) {
          SafeArray cellArray = extractRange.getValue().toSafeArray();

          int startRow = cellArray.getLBound(1);
          int startCol = cellArray.getLBound(2);
          int endRow   = cellArray.getUBound(1);
          int endCol   = cellArray.getUBound(2);
 
          for (int row = startRow; row <= endRow; row++) {
            for (int col = startCol; col <= endCol; col++) {
              String cellValue = cellArray.getString(row, col);
              if ((cellValue != null) && (cellValue.length() != 0)) {
                contentBuf.append(cellValue);
                contentBuf.append(" ");
              }
            }
View Full Code Here


  /**
   *
   */
  public void testFloatSafeArray() {
    float sourceData[] = new float[] { 1.5F, 2.5F, 3.5F };
    SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
    saUnderTest.fromFloatArray(sourceData);
    float[] extractedFromSafeArray = saUnderTest.toFloatArray();
    for (int i = 0; i < extractedFromSafeArray.length; i++) {
      assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
    }
  }
View Full Code Here

  /**
   *
   */
  public void testBooleanSafeArray() {
    boolean sourceData[] = new boolean[] { true, false, true, false };
    SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
    saUnderTest.fromBooleanArray(sourceData);
    boolean[] extractedFromSafeArray = saUnderTest.toBooleanArray();
    for (int i = 0; i < extractedFromSafeArray.length; i++) {
      assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
    }
    assertEquals("single get failed: ", sourceData[2], saUnderTest
        .getBoolean(2));

    // test conversion
    Variant extractedFromSafeArrayVariant[] = saUnderTest.toVariantArray();
    for (int i = 0; i < extractedFromSafeArrayVariant.length; i++) {
      assertEquals("" + i, sourceData[i],
          extractedFromSafeArrayVariant[i].getBoolean());
    }
  }
View Full Code Here

  /**
   *
   */
  public void testCharSafeArray() {
    char sourceData[] = new char[] { 'a', 'b', 'c', 'd' };
    SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
    saUnderTest.fromCharArray(sourceData);
    char[] extractedFromSafeArray = saUnderTest.toCharArray();
    for (int i = 0; i < extractedFromSafeArray.length; i++) {
      assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
    }
    assertEquals("single get failed: ", sourceData[2], saUnderTest
        .getChar(2));

  }
View Full Code Here

  /**
   *
   */
  public void testStringSaveArray() {
    String sourceData[] = new String[] { "hello", "from", "java", "com" };
    SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
    saUnderTest.fromStringArray(sourceData);
    String[] extractedFromSafeArray = saUnderTest.toStringArray();
    for (int i = 0; i < extractedFromSafeArray.length; i++) {
      assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
    }
    assertEquals("single get failed: ", sourceData[2], saUnderTest
        .getString(2));

    // test conversion
    Variant extractedFromSafeArrayVariant[] = saUnderTest.toVariantArray();
    for (int i = 0; i < extractedFromSafeArrayVariant.length; i++) {
      assertEquals("" + i, sourceData[i],
          extractedFromSafeArrayVariant[i].getString());
    }
  }
View Full Code Here

   */
  public void testVariantSafeArray() {
    Variant sourceData[] = new Variant[] { new Variant(1),
        new Variant(2.3), new Variant("hi") };

    SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
    saUnderTest.fromVariantArray(sourceData);
    Variant[] extractedFromSafeArray = saUnderTest.toVariantArray();
    for (int i = 0; i < extractedFromSafeArray.length; i++) {
      assertEquals("" + i, sourceData[i].toString(),
          extractedFromSafeArray[i].toString());
    }
    assertEquals("single get failed: ", sourceData[2].toString(),
        saUnderTest.getVariant(2).toString());

  }
View Full Code Here

   */
  public void testSafeArrayNumDimensions() {
    int[] lowerBounds = new int[] { 0, 0, 0 };
    int[] dimensionSizes = new int[] { 3, 3, 3 };

    SafeArray sa3x3 = new SafeArray(Variant.VariantVariant, lowerBounds,
        dimensionSizes);

    System.out.println("Num Dimensions = " + sa3x3.getNumDim());
    for (int safeArrayDimension = 1; safeArrayDimension <= sa3x3
        .getNumDim(); safeArrayDimension++) {
      int configArrayIndex = safeArrayDimension - 1;
      assertEquals("unexpected lower bound value ",
          lowerBounds[configArrayIndex], sa3x3
              .getLBound(safeArrayDimension));
      assertEquals("unexpeced upper bound value ",
          (dimensionSizes[configArrayIndex] - 1)
              + lowerBounds[configArrayIndex], sa3x3
              .getUBound(safeArrayDimension));
    }
  }
View Full Code Here

  public void testSafeArrayMultiDimension() {

    int[] lowerBounds = new int[] { 0, 0, 0 };
    int[] dimensionSizes = new int[] { 3, 3, 3 };

    SafeArray sa3x3 = new SafeArray(Variant.VariantVariant, lowerBounds,
        dimensionSizes);
    int[] indices = new int[] { 0, 0, 0 };

    for (int i = 0; i < 3; i++) {
      indices[0] = i;
      for (int j = 0; j < 3; j++) {
        indices[1] = j;
        for (int k = 0; k < 3; k++) {
          indices[2] = k;

          int fill = 0;
          fill = i * 100 + j * 10 + k;
          sa3x3.setInt(indices, fill);
          assertEquals(fill, sa3x3.getInt(indices));

          long fillLong = 0L;
          // Pick a number bigger than 2^31
          fillLong = 100000000000000L * fill;
          sa3x3.setLong(indices, fillLong);
          assertEquals(fillLong, sa3x3.getLong(indices));
        }
      }
    }
  }
View Full Code Here

  }

  public void testSafeArrayContents() {
    // int
    System.out.println("Int");
    SafeArray ia = new SafeArray(Variant.VariantInt, 4);
    System.out.println("elem size:" + ia.getElemSize());
    int iack[] = new int[] { 100000, 200000, 300000, 400000 };
    printArray(iack);
    ia.fromIntArray(iack);
    iack = ia.toIntArray();
    printArray(iack);

    int i4[] = new int[4];
    ia.getInts(0, 4, i4, 0);
    printArray(i4);

    SafeArray ia2 = new SafeArray(Variant.VariantInt, 4);
    ia2.setInts(0, 4, i4, 0);
    iack = ia2.toIntArray();
    printArray(iack);

    // double
    System.out.println("Double");
    SafeArray da = new SafeArray(Variant.VariantDouble, 4);
    System.out.println("elem size:" + da.getElemSize());
    double dack[] = new double[] { 123.456, 456.123, 1234567.89, 12.3456789 };
    printArray(dack);
    da.fromDoubleArray(dack);
    dack = da.toDoubleArray();
    printArray(dack);

    double d4[] = new double[4];
    da.getDoubles(0, 4, d4, 0);
    printArray(d4);

    SafeArray da2 = new SafeArray(Variant.VariantDouble, 4);
    da2.setDoubles(0, 4, d4, 0);
    dack = da2.toDoubleArray();
    printArray(dack);

    // float
    System.out.println("Float");
    SafeArray fa = new SafeArray(Variant.VariantFloat, 4);
    System.out.println("elem size:" + fa.getElemSize());
    float fack[] = new float[] { 123.456F, 456.123F, 1234567.89F,
        12.3456789F };
    printArray(fack);
    fa.fromFloatArray(fack);
    fack = fa.toFloatArray();
    printArray(fack);

    float f4[] = new float[4];
    fa.getFloats(0, 4, f4, 0);
    printArray(f4);

    SafeArray fa2 = new SafeArray(Variant.VariantFloat, 4);
    fa2.setFloats(0, 4, f4, 0);
    fack = fa2.toFloatArray();
    printArray(fack);

    // boolean
    System.out.println("Boolean");
    SafeArray ba = new SafeArray(Variant.VariantBoolean, 4);
    System.out.println("elem size:" + ba.getElemSize());
    boolean back[] = new boolean[] { true, false, true, false };
    printArray(back);
    ba.fromBooleanArray(back);
    back = ba.toBooleanArray();
    printArray(back);

    boolean b4[] = new boolean[4];
    ba.getBooleans(0, 4, b4, 0);
    printArray(b4);

    SafeArray ba2 = new SafeArray(Variant.VariantBoolean, 4);
    ba2.setBooleans(0, 4, b4, 0);
    back = ba2.toBooleanArray();
    printArray(back);

    // char
    System.out.println("Char");
    SafeArray ca = new SafeArray(Variant.VariantShort, 4);
    System.out.println("elem size:" + ca.getElemSize());
    char cack[] = new char[] { 'a', 'b', 'c', 'd' };
    printArray(cack);
    ca.fromCharArray(cack);
    cack = ca.toCharArray();
    printArray(cack);

    char c4[] = new char[4];
    ca.getChars(0, 4, c4, 0);
    printArray(c4);

    SafeArray ca2 = new SafeArray(Variant.VariantShort, 4);
    ca2.setChars(0, 4, c4, 0);
    cack = ca2.toCharArray();
    printArray(cack);

    // short
    System.out.println("Short");
    SafeArray sha = new SafeArray(Variant.VariantShort, 4);
    System.out.println("elem size:" + sha.getElemSize());
    short shack[] = new short[] { 1000, 2000, 3000, 4000 };
    printArray(shack);
    sha.fromShortArray(shack);
    shack = sha.toShortArray();
    printArray(shack);

    short sh4[] = new short[4];
    sha.getShorts(0, 4, sh4, 0);
    printArray(sh4);

    SafeArray sha2 = new SafeArray(Variant.VariantShort, 4);
    sha2.setShorts(0, 4, sh4, 0);
    shack = sha2.toShortArray();
    printArray(shack);

    // string
    System.out.println("String");
    SafeArray sa = new SafeArray(Variant.VariantString, 4);
    System.out.println("elem size:" + sa.getElemSize());
    String sack[] = new String[] { "aa", "bb", "cc", "dd" };
    printArray(sack);
    sa.fromStringArray(sack);
    sack = sa.toStringArray();
    printArray(sack);

    String s4[] = new String[4];
    sa.getStrings(0, 4, s4, 0);
    printArray(s4);

    SafeArray sa2 = new SafeArray(Variant.VariantString, 4);
    sa2.setStrings(0, 4, s4, 0);
    sack = sa2.toStringArray();
    printArray(sack);

    // variant
    System.out.println("Variant");
    SafeArray va = new SafeArray(Variant.VariantVariant, 4);
    System.out.println("elem size:" + va.getElemSize());
    Variant vack[] = new Variant[] { new Variant(1), new Variant(2.3),
        new Variant(true), new Variant("four"), };
    printArray(vack);
    va.fromVariantArray(vack);
    vack = va.toVariantArray();
    printArray(vack);

    Variant v4[] = new Variant[4];
    va.getVariants(0, 4, v4, 0);
    printArray(v4);

    SafeArray va2 = new SafeArray(Variant.VariantVariant, 4);
    va2.setVariants(0, 4, v4, 0);
    vack = va2.toVariantArray();
    printArray(vack);

    // byte
    System.out.println("Byte");
    SafeArray bba = new SafeArray(Variant.VariantByte, 4);
    System.out.println("elem size:" + bba.getElemSize());
    byte bback[] = new byte[] { 0x1, 0x2, 0x3, 0x4 };
    printArray(bback);
    bba.fromByteArray(bback);
    bback = bba.toByteArray();
    printArray(bback);

    byte bb4[] = new byte[4];
    bba.getBytes(0, 4, bb4, 0);
    printArray(bb4);

    SafeArray bba2 = new SafeArray(Variant.VariantByte, 4);
    bba2.setBytes(0, 4, bb4, 0);
    bback = bba2.toByteArray();
    printArray(bback);

    try {
      // this should throw ComException
      bba2.fromCharArray(new char[] { 'a' });
      fail("Failed to catch expected exception");
    } catch (ComFailException cfe) {
      // do nothing
      // cfe.printStackTrace();
    }
View Full Code Here

    Dispatch workbooks = null;
    Dispatch workbook = null;
    Dispatch workSheets = null;
    Dispatch sheet = null;
    Dispatch tabCells = null;
    SafeArray sa = null;

    // -Dcom.jacob.autogc=true
    System.out.println("Jacob version: " + JacobReleaseInfo.getBuildVersion());

    for (int t = 0; t < 10; t++) {
      // look at a large range of cells
      String position = "A7:DM8934";

      try {
        xl = new ActiveXComponent("Excel.Application");
        System.out
            .println("Excel version=" + xl.getProperty("Version"));

        xl.setProperty("Visible", new Variant(false));
        workbooks = xl.getProperty("Workbooks").toDispatch();

        workbook = Dispatch.get(workbooks, "Add").toDispatch();

        workSheets = Dispatch.get(workbook, "Worksheets").toDispatch();

        sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
        // grab the whole range specified above.
        tabCells = Dispatch.invoke(sheet, "Range", Dispatch.Get,
            new Object[] { position }, new int[1]).toDispatch();

        sa = Dispatch.get(tabCells, "Value").toSafeArray(true);

        System.out.println("Ub0=" + sa.getUBound(1)); // nbCol
        System.out.println("Ub1=" + sa.getUBound(2)); // nbLgn

        // number of rows
        int nbLgn = sa.getUBound(2);
        // number of columns
        int nbCol = sa.getUBound(1);

        int[] colLgn = new int[] { 0, 0 };

        // now set a value on every cell in the range we retrieved
        for (int i = 1; i <= nbLgn; i++) {
          colLgn[1] = i;

          for (int j = 1; j <= nbCol; j++) {
            colLgn[0] = j;
            // this one works with out a leak 1.13-M3
            // sa.setString(j, i, "test");
            // This one leaks with 1.13-M3 and earlier
            sa.setString(colLgn, "test");
          }
        }

        Dispatch.put(tabCells, "Value", sa);

        Variant f = new Variant(false);
        Dispatch.call(workbook, "Close", f);
        System.out.println("Close");
      } catch (Exception e) {
        e.printStackTrace();
      } finally {

        if (sa != null) {
          try {
            sa.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            sa = null;
          }
View Full Code Here

TOP

Related Classes of com.jacob.com.SafeArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.