You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POIDataSamples.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. /**
  23. * Centralises logic for finding/opening sample files
  24. */
  25. public final class POIDataSamples {
  26. /**
  27. * Name of the system property that defined path to the test data.
  28. */
  29. public static final String TEST_PROPERTY = "POI.testdata.path";
  30. private static POIDataSamples _instSlideshow;
  31. private static POIDataSamples _instSpreadsheet;
  32. private static POIDataSamples _instDocument;
  33. private static POIDataSamples _instDiagram;
  34. private static POIDataSamples _instOpenxml4j;
  35. private static POIDataSamples _instPOIFS;
  36. private static POIDataSamples _instDDF;
  37. private static POIDataSamples _instHMEF;
  38. private static POIDataSamples _instHPSF;
  39. private static POIDataSamples _instHPBF;
  40. private static POIDataSamples _instHSMF;
  41. private static POIDataSamples _instXmlDSign;
  42. private File _resolvedDataDir;
  43. /** <code>true</code> if standard system propery is not set,
  44. * but the data is available on the test runtime classpath */
  45. private boolean _sampleDataIsAvaliableOnClassPath;
  46. private String _moduleDir;
  47. /**
  48. *
  49. * @param moduleDir the name of the directory containing the test files
  50. */
  51. private POIDataSamples(String moduleDir){
  52. _moduleDir = moduleDir;
  53. initialise();
  54. }
  55. public static POIDataSamples getSpreadSheetInstance(){
  56. if(_instSpreadsheet == null) _instSpreadsheet = new POIDataSamples("spreadsheet");
  57. return _instSpreadsheet;
  58. }
  59. public static POIDataSamples getDocumentInstance(){
  60. if(_instDocument == null) _instDocument = new POIDataSamples("document");
  61. return _instDocument;
  62. }
  63. public static POIDataSamples getSlideShowInstance(){
  64. if(_instSlideshow == null) _instSlideshow = new POIDataSamples("slideshow");
  65. return _instSlideshow;
  66. }
  67. public static POIDataSamples getDiagramInstance(){
  68. if(_instOpenxml4j == null) _instOpenxml4j = new POIDataSamples("diagram");
  69. return _instOpenxml4j;
  70. }
  71. public static POIDataSamples getOpenXML4JInstance(){
  72. if(_instDiagram == null) _instDiagram = new POIDataSamples("openxml4j");
  73. return _instDiagram;
  74. }
  75. public static POIDataSamples getPOIFSInstance(){
  76. if(_instPOIFS == null) _instPOIFS = new POIDataSamples("poifs");
  77. return _instPOIFS;
  78. }
  79. public static POIDataSamples getDDFInstance(){
  80. if(_instDDF == null) _instDDF = new POIDataSamples("ddf");
  81. return _instDDF;
  82. }
  83. public static POIDataSamples getHPSFInstance(){
  84. if(_instHPSF == null) _instHPSF = new POIDataSamples("hpsf");
  85. return _instHPSF;
  86. }
  87. public static POIDataSamples getPublisherInstance(){
  88. if(_instHPBF == null) _instHPBF = new POIDataSamples("publisher");
  89. return _instHPBF;
  90. }
  91. public static POIDataSamples getHMEFInstance(){
  92. if(_instHMEF == null) _instHMEF = new POIDataSamples("hmef");
  93. return _instHMEF;
  94. }
  95. public static POIDataSamples getHSMFInstance(){
  96. if(_instHSMF == null) _instHSMF = new POIDataSamples("hsmf");
  97. return _instHSMF;
  98. }
  99. public static POIDataSamples getXmlDSignInstance(){
  100. if(_instXmlDSign == null) _instXmlDSign = new POIDataSamples("xmldsign");
  101. return _instXmlDSign;
  102. }
  103. /**
  104. * Opens a sample file from the test data directory
  105. *
  106. * @param sampleFileName the file to open
  107. * @return an open <tt>InputStream</tt> for the specified sample file
  108. */
  109. public InputStream openResourceAsStream(String sampleFileName) {
  110. if (_sampleDataIsAvaliableOnClassPath) {
  111. InputStream result = sampleFileName == null ? null :
  112. openClasspathResource(sampleFileName);
  113. if(result == null) {
  114. throw new RuntimeException("specified test sample file '" + sampleFileName
  115. + "' not found on the classpath");
  116. }
  117. // wrap to avoid temp warning method about auto-closing input stream
  118. return new NonSeekableInputStream(result);
  119. }
  120. if (_resolvedDataDir == null) {
  121. throw new RuntimeException("Must set system property '"
  122. + TEST_PROPERTY
  123. + "' properly before running tests");
  124. }
  125. File f = getFile(sampleFileName);
  126. try {
  127. return new FileInputStream(f);
  128. } catch (FileNotFoundException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. /**
  133. *
  134. * @param sampleFileName the name of the test file
  135. * @return
  136. * @throws RuntimeException if the file was not found
  137. */
  138. public File getFile(String sampleFileName) {
  139. File f = new File(_resolvedDataDir, sampleFileName);
  140. if (!f.exists()) {
  141. throw new RuntimeException("Sample file '" + sampleFileName
  142. + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
  143. }
  144. try {
  145. if(sampleFileName.length() > 0) {
  146. String fn = sampleFileName;
  147. if(fn.indexOf('/') > 0) {
  148. fn = fn.substring(fn.indexOf('/')+1);
  149. }
  150. if(!fn.equals(f.getCanonicalFile().getName())){
  151. throw new RuntimeException("File name is case-sensitive: requested '" + fn
  152. + "' but actual file is '" + f.getCanonicalFile().getName() + "'");
  153. }
  154. }
  155. } catch (IOException e){
  156. throw new RuntimeException(e);
  157. }
  158. return f;
  159. }
  160. private void initialise() {
  161. String dataDirName = System.getProperty(TEST_PROPERTY);
  162. if (dataDirName == null) {
  163. // check to see if we can just get the resources from the classpath
  164. InputStream is = openClasspathResource("");
  165. if (is != null) {
  166. try {
  167. is.close(); // be nice
  168. } catch (IOException e) {
  169. throw new RuntimeException(e);
  170. }
  171. _sampleDataIsAvaliableOnClassPath = true;
  172. return;
  173. }
  174. throw new RuntimeException("Must set system property '" +
  175. TEST_PROPERTY + "' before running tests");
  176. }
  177. File dataDir = new File(dataDirName, _moduleDir);
  178. if (!dataDir.exists()) {
  179. throw new RuntimeException("Data dir '" + _moduleDir + " does not exist");
  180. }
  181. // convert to canonical file, to make any subsequent error messages
  182. // clearer.
  183. try {
  184. _resolvedDataDir = dataDir.getCanonicalFile();
  185. } catch (IOException e) {
  186. throw new RuntimeException(e);
  187. }
  188. }
  189. /**
  190. * Opens a test sample file from the 'data' sub-package of this class's package.
  191. *
  192. * @param sampleFileName the file to open
  193. * @return <code>null</code> if the sample file is not deployed on the classpath.
  194. */
  195. private InputStream openClasspathResource(String sampleFileName) {
  196. return getClass().getResourceAsStream("/" + _moduleDir + "/" + sampleFileName);
  197. }
  198. private static final class NonSeekableInputStream extends InputStream {
  199. private final InputStream _is;
  200. public NonSeekableInputStream(InputStream is) {
  201. _is = is;
  202. }
  203. @Override
  204. public int read() throws IOException {
  205. return _is.read();
  206. }
  207. @Override
  208. public int read(byte[] b, int off, int len) throws IOException {
  209. return _is.read(b, off, len);
  210. }
  211. @Override
  212. public boolean markSupported() {
  213. return false;
  214. }
  215. @Override
  216. public void close() throws IOException {
  217. _is.close();
  218. }
  219. }
  220. /**
  221. * @param fileName the file to open
  222. * @return byte array of sample file content from file found in standard hssf test data dir
  223. */
  224. public byte[] readFile(String fileName) {
  225. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  226. try {
  227. InputStream fis = openResourceAsStream(fileName);
  228. byte[] buf = new byte[512];
  229. while (true) {
  230. int bytesRead = fis.read(buf);
  231. if (bytesRead < 1) {
  232. break;
  233. }
  234. bos.write(buf, 0, bytesRead);
  235. }
  236. fis.close();
  237. } catch (IOException e) {
  238. throw new RuntimeException(e);
  239. }
  240. return bos.toByteArray();
  241. }
  242. }