コード例 #1
0
 // Save the user configuration
 private void saveConfig() {
   if (sfa2 != null && sConfigFileName != null) {
     try {
       // Remove the file if it exists
       if (sfa2.exists(sConfigFileName)) {
         sfa2.kill(sConfigFileName);
       }
       // Then write the new contents
       XOutputStream xOs = sfa2.openFileWrite(sConfigFileName);
       if (xOs != null) {
         OutputStream os = new XOutputStreamToOutputStreamAdapter(xOs);
         config.write(os);
         os.close();
         xOs.closeOutput();
       }
     } catch (IOException e) {
       // ignore
     } catch (NotConnectedException e) {
       // ignore
     } catch (CommandAbortedException e) {
       // ignore
     } catch (com.sun.star.uno.Exception e) {
       // ignore
     }
   }
 }
コード例 #2
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean WriteBytesToStream(
      XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes) {
    // get output stream of substream
    XOutputStream xOutput = xStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
      xPropSet.setPropertyValue("Compressed", new Boolean(bCompressed));
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    return true;
  }
コード例 #3
0
ファイル: SignedDoc.java プロジェクト: beppec56/odf-xades
  /**
   * This method writes the signatures to the XOutputStream given as parameter, wrapping them into
   * the
   *
   * @param os output file stream, to be converted to XStream used in Store interface
   * @throws SignedDocException
   */
  public void writeSignaturesToXStream(XOutputStream os) throws SignedDocException {

    try {
      os.writeBytes(xmlSignatureHeader().getBytes());
      for (int i = 0; i < countSignatures(); i++) {
        Signature sig = getSignature(i);
        os.writeBytes(sig.toXML());
        os.writeBytes("\n".getBytes());
      }
      os.writeBytes(xmlSignatureTrailer().getBytes());
    } catch (SignedDocException ex) {
      throw ex; // already handled
    } catch (Exception ex) {
      SignedDocException.handleException(ex, SignedDocException.ERR_WRITE_FILE);
    }
  }
コード例 #4
0
  /**
   * Test calls the method and checks return value and that no exceptions were thrown.
   * <b>XSimpleFileAccess.txt</b> file used.
   *
   * <p>Has <b> OK </b> status if the method returns not <code>null</code> value and no exceptions
   * were thrown.
   *
   * <p>
   */
  public void _openFileWrite() {
    try {
      String tmpdirname = util.utils.getOfficeTemp(tParam.getMSF());

      String copiedFile = tmpdirname + "XSimpleFileAccess_openWrite.txt";

      if (oObj.exists(copiedFile)) oObj.kill(copiedFile);

      com.sun.star.io.XOutputStream oStream = oObj.openFileWrite(copiedFile);

      oStream.closeOutput();
      oObj.kill(copiedFile);

      tRes.tested("openFileWrite()", true);
    } catch (com.sun.star.uno.Exception ex) {
      log.println("Exception occurred while testing 'openFileWrite()'");
      ex.printStackTrace(log);
      tRes.tested("openFileWrite()", false);
    }
  } // EOF openFileWrite()
コード例 #5
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean closeOutput(XStream xStream) {
    XOutputStream xOutTemp = null;
    try {
      xOutTemp = xStream.getOutputStream();
      if (xOutTemp == null) {
        Error("Can't get the output part of a stream!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get the output part of a stream, exception :" + e);
      return false;
    }

    try {
      xOutTemp.closeOutput();
    } catch (Exception e) {
      Error("Can't close output part of a stream, exception :" + e);
      return false;
    }

    return true;
  }
コード例 #6
0
 // Save a text file, return true on success
 protected boolean saveFile(String sFileName, String sText) {
   killFile(sFileName);
   try {
     XOutputStream xOs = sfa2.openFileWrite(sFileName);
     if (xOs != null) {
       OutputStream os = new XOutputStreamToOutputStreamAdapter(xOs);
       try {
         OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
         osw.write(sText);
         osw.flush();
         os.close();
       } catch (IOException e) {
         xOs.closeOutput();
         return false;
       }
       xOs.closeOutput();
       return true;
     }
   } catch (com.sun.star.uno.Exception e) {
   }
   return false;
 }
コード例 #7
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public String CreateTempFile(XMultiServiceFactory xMSF) {
    String sResult = null;

    // try to get temporary file representation
    XPropertySet xTempFileProps = null;
    try {
      Object oTempFile = xMSF.createInstance("com.sun.star.io.TempFile");
      xTempFileProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oTempFile);
    } catch (Exception e) {
    }

    if (xTempFileProps != null) {
      try {
        xTempFileProps.setPropertyValue("RemoveFile", new Boolean(false));
        sResult = AnyConverter.toString(xTempFileProps.getPropertyValue("Uri"));
      } catch (Exception e) {
        Error("Can't control TempFile properties, exception: " + e);
      }
    } else {
      Error("Can't create temporary file representation!");
    }

    // close temporary file explicitly
    try {
      XStream xStream = (XStream) UnoRuntime.queryInterface(XStream.class, xTempFileProps);
      if (xStream != null) {
        XOutputStream xOut = xStream.getOutputStream();
        if (xOut != null) xOut.closeOutput();

        XInputStream xIn = xStream.getInputStream();
        if (xIn != null) xIn.closeInput();
      } else Error("Can't close TempFile!");
    } catch (Exception e) {
      Error("Can't close TempFile, exception: " + e);
    }

    return sResult;
  }
コード例 #8
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean WriteBytesToSubstreamDefaultCompressed(
      XStorage xStorage, String sStreamName, String sMediaType, byte[] pBytes) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
      return false;
    }

    // get output stream of substream
    XOutputStream xOutput = xSubStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet =
        (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    // do not set the compressed property
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamName)) return false;

    return true;
  }