List of usage examples for org.apache.poi.openxml4j.opc OPCPackage close
@Override public void close() throws IOException
From source file:org.roda.common.certification.OOXMLSignatureUtils.java
public static String runDigitalSignatureVerify(Path input) throws IOException, GeneralSecurityException { boolean isValid = true; try {/*www. java2 s . c o m*/ OPCPackage pkg = OPCPackage.open(input.toString(), PackageAccess.READ); SignatureConfig sic = new SignatureConfig(); sic.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); si.setSignatureConfig(sic); Iterable<SignaturePart> it = si.getSignatureParts(); if (it != null) { for (SignaturePart sp : it) { isValid = isValid && sp.validate(); Set<Certificate> trustedRootCerts = new HashSet<Certificate>(); Set<Certificate> intermediateCerts = new HashSet<Certificate>(); List<X509Certificate> certChain = sp.getCertChain(); for (X509Certificate c : certChain) { c.checkValidity(); if (SignatureUtils.isCertificateSelfSigned(c)) trustedRootCerts.add(c); else intermediateCerts.add(c); } SignatureUtils.verifyCertificateChain(trustedRootCerts, intermediateCerts, certChain.get(0)); } } pkg.close(); } catch (InvalidFormatException e) { return "Error opening a document file"; } catch (CertificateExpiredException e) { return "Contains expired certificates"; } catch (CertificateNotYetValidException e) { return "Contains certificates not yet valid"; } return isValid ? "Passed" : "Not passed"; }
From source file:org.roda.common.certification.OOXMLSignatureUtils.java
public static void runDigitalSignatureStrip(Path input, Path output) throws IOException, InvalidFormatException { CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING }; Files.copy(input, output, copyOptions); OPCPackage pkg = OPCPackage.open(output.toString(), PackageAccess.READ_WRITE); ArrayList<PackagePart> pps = pkg.getPartsByContentType(SIGN_CONTENT_TYPE_OOXML); for (PackagePart pp : pps) { pkg.removePart(pp);// w ww. ja v a 2 s . com } ArrayList<PackagePart> ppct = pkg.getPartsByRelationshipType(SIGN_REL_TYPE_OOXML); for (PackagePart pp : ppct) { pkg.removePart(pp); } for (PackageRelationship r : pkg.getRelationships()) { if (r.getRelationshipType().equals(SIGN_REL_TYPE_OOXML)) { pkg.removeRelationship(r.getId()); } } pkg.close(); }
From source file:org.roda.common.certification.OOXMLSignatureUtils.java
public static Path runDigitalSignatureSign(Path input, String keystore, String alias, String password, String fileFormat)/*from w ww . ja v a2 s . c o m*/ throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, InvalidFormatException, XMLSignatureException, MarshalException { Path output = Files.createTempFile("signed", "." + fileFormat); CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING }; Files.copy(input, output, copyOptions); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); InputStream is = new FileInputStream(keystore); ks.load(is, password.toCharArray()); PrivateKey pk = (PrivateKey) ks.getKey(alias, password.toCharArray()); X509Certificate x509 = (X509Certificate) ks.getCertificate(alias); SignatureConfig signatureConfig = new SignatureConfig(); signatureConfig.setKey(pk); signatureConfig.setSigningCertificateChain(Collections.singletonList(x509)); OPCPackage pkg = OPCPackage.open(output.toString(), PackageAccess.READ_WRITE); signatureConfig.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); si.setSignatureConfig(signatureConfig); si.confirmSignature(); // boolean b = si.verifySignature(); pkg.close(); IOUtils.closeQuietly(is); return output; }
From source file:org.specrunner.source.excel.SourceFactoryExcel.java
License:Open Source License
/** * Load a document from a target.//from ww w .j av a2s . c om * * @param uri * The target corresponding uri (if any). * @param target * The target. * @param encoding * The encoding. * @return The document, if exists, null, otherwise. * @throws SourceException * On load error. */ @Override protected Document fromTarget(URI uri, String target, String encoding) throws SourceException { Element html = new Element("html"); Document result = new Document(html); OPCPackage pkg = null; InputStream in = null; POIFSFileSystem fsys = null; try { Workbook wb = null; if (isFile(uri, target)) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug("Source from file:" + target); } in = new FileInputStream(new File(target)); } else { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug("Source from URI:" + uri); } in = uri.toURL().openStream(); } if (target.trim().toLowerCase().endsWith(XLSX)) { pkg = OPCPackage.open(in); wb = new XSSFWorkbook(pkg); } else { fsys = new POIFSFileSystem(in); wb = new HSSFWorkbook(fsys); } for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i); Map<String, Dimension> spanMap = new HashMap<String, Dimension>(); Set<String> ignoreMap = new HashSet<String>(); for (int j = 0; j < sheet.getNumMergedRegions(); j++) { CellRangeAddress region = sheet.getMergedRegion(j); for (int x = region.getFirstRow(); x <= region.getLastRow(); x++) { for (int y = region.getFirstColumn(); y <= region.getLastColumn(); y++) { if (x == region.getFirstRow() && y == region.getFirstColumn()) { spanMap.put(x + "," + y, new Dimension(region.getLastRow() - x + 1, region.getLastColumn() - y + 1)); } else { ignoreMap.add(x + "," + y); } } } } Element table = new Element("table"); table.addAttribute(new Attribute("border", "1")); html.appendChild(table); Element caption = readCaption(table, sheet); Iterator<Row> ite = sheet.iterator(); readBody(table, caption, spanMap, ignoreMap, ite, headers(table, caption, spanMap, ignoreMap, ite)); } } catch (Exception e) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug(e.getMessage(), e); } throw new SourceException(e); } finally { if (pkg != null) { try { pkg.close(); } catch (IOException e) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug(e.getMessage(), e); } throw new SourceException(e); } } if (in != null) { try { in.close(); } catch (IOException e) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug(e.getMessage(), e); } throw new SourceException(e); } } } return result; }
From source file:org.talend.repository.ui.wizards.metadata.connection.files.excel.ExcelReader.java
License:Open Source License
private void init() throws BiffException, IOException { // hywang modified for excel 2007 // if (excelPath.endsWith(".xls")) { //$NON-NLS-1$ // isXlsx = false; // } else if (excelPath.endsWith(".xlsx")) { //$NON-NLS-1$ // isXlsx = true; // }/* ww w .j av a2 s . co m*/ if (!isXlsx) { WorkbookSettings worksetting = new WorkbookSettings(); //worksetting.setEncoding("ISO-8859-15"); //$NON-NLS-1$ worksetting.setCellValidationDisabled(true); worksetting.setSuppressWarnings(true); workbook = Workbook.getWorkbook(new File(excelPath), worksetting); } else { // modify for bug 12174. File file = new File(excelPath); OPCPackage clone = null; try { FileInputStream in = new FileInputStream(file); OPCPackage open = OPCPackage.open(in); clone = PackageHelper.clone(open, createTempFile()); open.close(); // Package createPackage = Package.openOrCreate(file); // clone = PackageHelper.clone(createPackage); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (OpenXML4JException e) { e.printStackTrace(); } if (clone != null) { List<String> sheetlist = new ArrayList<String>(); // modified for bug TDI-26614, Use XSSF and SAX (Event API) to parse excel 2007, only need small memory // footprint if (isXlsx && (EVENT_MODE).equals(generationMode)) { try { XSSFReader xssfReader = new XSSFReader(clone); XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (sheets.hasNext()) { sheets.next(); String sheetName = sheets.getSheetName(); sheetlist.add(sheetName); } } catch (OpenXML4JException e) { CommonExceptionHandler.process(e); } } else { xwb = new XSSFWorkbook(clone); for (XSSFSheet sheet : xwb) { sheetlist.add(sheet.getSheetName()); } } sheetNamesForXlsx = new String[sheetlist.size()]; for (int i = 0; i < sheetlist.size(); i++) { sheetNamesForXlsx[i] = sheetlist.get(i); } sheetlist.clear(); } } }
From source file:org.ujmp.poi.MatrixXLSXImporter.java
License:Open Source License
public DenseObjectMatrix2D importFromXLSX(final File file, final int sheetNumber) throws InvalidFormatException, IOException { final OPCPackage pkg = OPCPackage.open(file); final XSSFWorkbook workbook = new XSSFWorkbook(pkg); final DenseObjectMatrix2D matrix = importFromWorkbook(workbook, sheetNumber); pkg.close(); return matrix; }
From source file:org.ujmp.poi.MatrixXLSXImporter.java
License:Open Source License
public DenseObjectMatrix2D importFromXLSX(final InputStream inputStream, final int sheetNumber) throws InvalidFormatException, IOException { final OPCPackage pkg = OPCPackage.open(inputStream); final XSSFWorkbook workbook = new XSSFWorkbook(pkg); final DenseObjectMatrix2D matrix = importFromWorkbook(workbook, sheetNumber); pkg.close(); return matrix; }
From source file:packtest.CustomXMLMapping.java
License:Apache License
public static void main(String[] args) throws Exception { OPCPackage pkg = OPCPackage.open(args[0]); XSSFWorkbook wb = new XSSFWorkbook(pkg); for (XSSFMap map : wb.getCustomXMLMappings()) { XSSFExportToXml exporter = new XSSFExportToXml(map); ByteArrayOutputStream os = new ByteArrayOutputStream(); exporter.exportToXML(os, true);/*ww w . j a v a2s.co m*/ String xml = os.toString("UTF-8"); System.out.println(xml); } pkg.close(); }
From source file:packtest.FromHowTo.java
License:Apache License
public void processFirstSheet(String filename) throws Exception { OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ); try {// w w w . j a v a 2 s . c o m XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); // process the first sheet InputStream sheet2 = r.getSheetsData().next(); InputSource sheetSource = new InputSource(sheet2); parser.parse(sheetSource); sheet2.close(); } finally { pkg.close(); } }
From source file:packtest.XLSX2CSV.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("Use:"); System.err.println(" XLSX2CSV <xlsx file> [min columns]"); return;/*w w w.j ava 2 s .c o m*/ } File xlsxFile = new File(args[0]); if (!xlsxFile.exists()) { System.err.println("Not found or not a file: " + xlsxFile.getPath()); return; } int minColumns = -1; if (args.length >= 2) minColumns = Integer.parseInt(args[1]); // The package open is instantaneous, as it should be. OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ); XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns); xlsx2csv.process(); p.close(); }