kiwiwings

XSSFTestDataSamples

Nov 9th, 2014
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.68 KB | None | 0 0
  1. /* ====================================================================
  2.    Licensed to the Apache Software Foundation (ASF) under one or more
  3.    contributor license agreements.  See the NOTICE file distributed with
  4.    this work for additional information regarding copyright ownership.
  5.    The ASF licenses this file to You under the Apache License, Version 2.0
  6.    (the "License"); you may not use this file except in compliance with
  7.    the License.  You may obtain a copy of the License at
  8.  
  9.        http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.    Unless required by applicable law or agreed to in writing, software
  12.    distributed under the License is distributed on an "AS IS" BASIS,
  13.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.    See the License for the specific language governing permissions and
  15.    limitations under the License.
  16. ==================================================================== */
  17.  
  18. package org.apache.poi.xssf;
  19.  
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27.  
  28. import org.apache.poi.hssf.HSSFTestDataSamples;
  29. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  30. import org.apache.poi.openxml4j.opc.OPCPackage;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33.  
  34. /**
  35.  * Centralises logic for finding/opening sample files in the test-data/spreadsheet folder.
  36.  *
  37.  * @author Josh Micich
  38.  */
  39. public class XSSFTestDataSamples {
  40.    /**
  41.     * Used by {@link writeOutAndReadBack(R wb, String testName)}.  If a
  42.     * value is set for this in the System Properties, the xlsx file
  43.     * will be written out to that directory.
  44.     */
  45.    public static final String TEST_OUTPUT_DIR = "poi.test.xssf.output.dir";
  46.  
  47.    public static OPCPackage openSamplePackage(String sampleName) {
  48.       try {
  49.          return OPCPackage.open(
  50.                HSSFTestDataSamples.openSampleFileStream(sampleName)
  51.          );
  52.       } catch(Exception e) {
  53.          throw new RuntimeException(e);
  54.       }
  55.    }
  56.     public static XSSFWorkbook openSampleWorkbook(String sampleName) {
  57.         InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);
  58.         try {
  59.             return new XSSFWorkbook(is);
  60.         } catch (IOException e) {
  61.             throw new RuntimeException(e);
  62.         }
  63.     }
  64.     public static <R extends Workbook> R writeOutAndReadBack(R wb) {
  65.         Workbook result;
  66.         try {
  67.             ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
  68.             wb.write(baos);
  69.             wb.close();
  70.             InputStream is = new ByteArrayInputStream(baos.toByteArray());
  71.             if (wb instanceof HSSFWorkbook) {
  72.                 result = new HSSFWorkbook(is);
  73.             } else if (wb instanceof XSSFWorkbook) {
  74.                 result = new XSSFWorkbook(is);
  75.             } else {
  76.                 throw new RuntimeException("Unexpected workbook type ("
  77.                         + wb.getClass().getName() + ")");
  78.             }
  79.         } catch (IOException e) {
  80.             throw new RuntimeException(e);
  81.         }
  82.         @SuppressWarnings("unchecked")
  83.         R r = (R) result;
  84.         return r;
  85.     }
  86.    
  87.     /**
  88.      * Writes the Workbook either into a file or into a byte array, depending on presence of
  89.      * the system property {@value #TEST_OUTPUT_DIR}, and reads it in a new instance of the Workbook back.
  90.      * @param wb workbook to write
  91.      * @param testName file name to be used if writing into a file. The old file with the same name will be overridden.
  92.      * @return new instance read from the stream written by the wb parameter.
  93.      */
  94.     public static <R extends Workbook> R writeOutAndReadBack(R wb, String testName) {
  95.         XSSFWorkbook result = null;
  96.         if (System.getProperty(TEST_OUTPUT_DIR) != null) {
  97.             try {
  98.                 File file = new File(System.getProperty(TEST_OUTPUT_DIR), testName + ".xlsx");
  99.                 if (file.exists()) {
  100.                     file.delete();
  101.                 }
  102.                 FileOutputStream out = new FileOutputStream(file);
  103.                 try {
  104.                     wb.write(out);
  105.                 } finally {
  106.                     out.close();
  107.                 }
  108.                 FileInputStream in = new FileInputStream(file);
  109.                 try {
  110.                     result = new XSSFWorkbook(in);
  111.                 } finally {
  112.                     in.close();
  113.                 }
  114.                 wb.close();
  115.             } catch (IOException e) {
  116.                 throw new RuntimeException(e);
  117.             }
  118.         } else {
  119.             return writeOutAndReadBack(wb);
  120.         }
  121.         @SuppressWarnings("unchecked")
  122.         R r = (R) result;
  123.         return r;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment