Exemplo n.º 1
1
  /**
   * Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and
   * be readable, and may be password protected
   *
   * <p>Note that in order to properly release resources the Workbook should be closed after use.
   *
   * @throws EncryptedDocumentException If the wrong password is given for a protected file
   * @throws EmptyFileException If an empty stream is given
   */
  public static Workbook create(File file, String password)
      throws IOException, InvalidFormatException, EncryptedDocumentException {
    if (!file.exists()) {
      throw new FileNotFoundException(file.toString());
    }

    try {
      NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
      return create(fs, password);
    } catch (OfficeXmlFileException e) {
      // opening as .xls failed => try opening as .xlsx
      OPCPackage pkg = OPCPackage.open(file);
      try {
        return new XSSFWorkbook(pkg);
      } catch (IOException ioe) {
        // ensure that file handles are closed (use revert() to not re-write the file)
        pkg.revert();
        // pkg.close();

        // rethrow exception
        throw ioe;
      } catch (IllegalArgumentException ioe) {
        // ensure that file handles are closed (use revert() to not re-write the file)
        pkg.revert();
        // pkg.close();

        // rethrow exception
        throw ioe;
      }
    }
  }
Exemplo n.º 2
0
  /**
   * public Result getDataBinding(String postcode) throws PostcodeNotFoundException {
   *
   * <p>Result response = null; try { String dir =
   * this.getClass().getClassLoader().getResource("").getPath(); response = mapper.readValue(new
   * File(dir.toString()+POSTCODE_FOLDER+File.separator+postcode+".json"), Result.class);
   * if(response == null){ throw new PostcodeNotFoundException("Postcode " +
   * postcode.toUpperCase()+" cannot be found."); } } catch (JsonParseException e) {
   * System.out.println("JsonParseException: "+ e.toString()); } catch (JsonMappingException e) {
   * System.out.println("JsonMappingException: "+ e.toString()); } catch (IOException e) {
   * System.out.println("IOException: "+ e.toString()); } countNum++; return response; }
   *
   * <p>public Result getDataBindingFirstTry(String postcode) {
   *
   * <p>Result response = null; try { String dir =
   * this.getClass().getClassLoader().getResource("").getPath(); response = mapper.readValue(new
   * File(dir.toString()+POSTCODE_FOLDER+File.separator+postcode+".json"), Result.class); }catch
   * (JsonParseException e) { System.out.println("JsonParseException: "+ e.toString()); } catch
   * (JsonMappingException e) { System.out.println("JsonMappingException: "+ e.toString()); } catch
   * (IOException e) { // System.out.println("IOException: "+ e.toString()); } return response; }
   *
   * <p>public Result getDataBindingReTry(String postcode, String bindingName){
   *
   * <p>Result response = null; try { String dir =
   * this.getClass().getClassLoader().getResource("").getPath(); response = mapper.readValue(new
   * File(dir.toString()+POSTCODE_FOLDER+File.separator+bindingName+".json"), Result.class); }catch
   * (JsonParseException e) { System.out.println("JsonParseException: "+ e.toString()); } catch
   * (JsonMappingException e) { System.out.println("JsonMappingException: "+ e.toString()); } catch
   * (IOException e) { System.out.println("IOException: "+ e.toString()); } countNum++; return
   * response;
   *
   * <p>}
   *
   * <p>public boolean saveJsonFileByPostcode(String postcode, String url) throws
   * PostcodeNotFoundException{
   *
   * <p>// System.setProperty("http.proxySet", "true"); //
   * System.setProperty("http.proxyHost","proxy.abdn.ac.uk"); //
   * System.setProperty("http.proxyPort", "8080"); URL website = null; ReadableByteChannel rbc =
   * null; FileOutputStream fos = null; try { website = new URL(url); rbc =
   * Channels.newChannel(website.openStream()); String dir =
   * this.getClass().getClassLoader().getResource("").getPath(); // System.out.println("dir:
   * "+dir+""+ POSTCODE_FOLDER+File.separator+postcode); File directory = new File(dir+""+
   * POSTCODE_FOLDER); if(!directory.exists()){ directory.mkdirs(); } fos = new FileOutputStream(new
   * File(dir.toString()+POSTCODE_FOLDER+File.separator+postcode+".json"));
   * fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.flush(); fos.close(); return true;
   * }catch (MalformedURLException e) { e.printStackTrace(); }catch(FileNotFoundException e){ //
   * e.printStackTrace(); throw new PostcodeNotFoundException("Postcode "+postcode.toUpperCase()+"
   * cannot be found."); }catch (IOException e) { e.printStackTrace(); } return false; }
   *
   * <p>public String getURLByPostcode(String postcode){
   *
   * <p>String url = "http://uk-postcodes.com/postcode/"; url += postcode; url += ".json"; return
   * url; }
   */
  public void readExcel() {

    String path = this.getClass().getClassLoader().getResource("").getPath();
    File file = new File(path + File.separator + POSTCODE_TABLE);
    OPCPackage opcPackage = null;
    try {
      opcPackage = OPCPackage.open(file);
    } catch (InvalidFormatException e) {
      e.printStackTrace();
    }
    // Create Workbook instance for xlsx/xls file input stream
    Workbook workbook = null;
    Sheet sheet = null;
    if (POSTCODE_TABLE.toLowerCase().endsWith("xlsx")) {
      try {
        workbook = new XSSFWorkbook(opcPackage);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // Get the number of sheets in the xlsx file
    int numberOfSheets = workbook.getNumberOfSheets();
    if (numberOfSheets > 0) {
      //			System.out.println("read the first sheet from the workbook...");
      sheet = workbook.getSheetAt(0);
    }
    read(sheet);
  }
  public void testSheets() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));

    XSSFReader r = new XSSFReader(pkg);
    byte[] data = new byte[4096];

    // By r:id
    assertNotNull(r.getSheet("rId2"));
    int read = IOUtils.readFully(r.getSheet("rId2"), data);
    assertEquals(974, read);

    // All
    Iterator<InputStream> it = r.getSheetsData();

    int count = 0;
    while (it.hasNext()) {
      count++;
      InputStream inp = it.next();
      assertNotNull(inp);
      read = IOUtils.readFully(inp, data);
      inp.close();

      assertTrue(read > 400);
      assertTrue(read < 1500);
    }
    assertEquals(3, count);
  }
Exemplo n.º 4
0
  @Test
  public void testImportExcel2007() throws Exception {

    long beginTime = System.currentTimeMillis();

    String fileName = "D:\\Backup\\test.xlsx";

    List<ExcelData> dataList = Lists.newArrayList();

    OPCPackage pkg = OPCPackage.open(fileName);
    XSSFReader r = new XSSFReader(pkg);

    XMLReader parser = XMLReaderFactory.createXMLReader();
    ContentHandler handler = new Excel2007SheetHandler(dataList);
    parser.setContentHandler(handler);

    Iterator<InputStream> sheets = r.getSheetsData();
    while (sheets.hasNext()) {
      InputStream sheet = sheets.next();
      InputSource sheetSource = new InputSource(sheet);
      parser.parse(sheetSource);
      sheet.close();
    }

    // 把最后剩下的不足batchSize大小
    if (dataList.size() > 0) {
      doBatchSave(dataList);
    }

    long endTime = System.currentTimeMillis();
    log.info("耗时(秒):" + (endTime - beginTime) / 1000);
  }
 /**
  * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start
  * tag when no following text exists. The tested tag is <{m:diagram provider:"noExistingProvider"
  * width:"500" height:"500" title="representationTitle"}> The expected tag is : <{m:diagram
  * provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The
  * image tag is referencing an unknown diagram provider : 'noExistingProvider' > After the run
  * with the end '}' char, the following runs must be present : A run must contains blanks char.
  * The next one must contains the error message. The next one is a blank separator. The next one
  * must contains the other error message. The next one must contains blank char and the next one
  * the static content after the tag in the original template.
  *
  * @throws InvalidFormatException
  * @throws IOException
  * @throws DocumentParserException
  * @throws DocumentGenerationException
  */
 @Test
 public void testErrorInSimpleTagWithoutFollowing()
     throws InvalidFormatException, IOException, DocumentParserException,
         DocumentGenerationException {
   FileInputStream is =
       new FileInputStream("templates/testParsingErrorSimpleTagWithoutFollowingText.docx");
   OPCPackage oPackage = OPCPackage.open(is);
   XWPFDocument document = new XWPFDocument(oPackage);
   BodyParser parser = new BodyParser(document, env);
   Template template = parser.parseTemplate();
   TemplateValidationGenerator validator = new TemplateValidationGenerator();
   validator.doSwitch(template);
   createDestinationDocument(
       document, "results/testParsingErrorSimpleTagWithoutFollowingText.docx");
   // scan the destination document
   assertEquals(1, document.getParagraphs().size());
   assertEquals(11, document.getParagraphs().get(0).getRuns().size());
   assertEquals("    ", document.getParagraphs().get(0).getRuns().get(2).getText(0));
   assertEquals("<---", document.getParagraphs().get(0).getRuns().get(3).getText(0));
   assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(3).getColor());
   assertEquals(16, document.getParagraphs().get(0).getRuns().get(3).getFontSize());
   assertEquals(
       STHighlightColor.LIGHT_GRAY,
       document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
   assertEquals(
       "The image tag is referencing an unknown diagram provider : 'noExistingProvider'",
       document.getParagraphs().get(0).getRuns().get(5).getText(0));
   assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(5).getColor());
   assertEquals(16, document.getParagraphs().get(0).getRuns().get(5).getFontSize());
   assertEquals(
       STHighlightColor.LIGHT_GRAY,
       document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
 }
 /** Test M4.1 rule. */
 public void testOnlyOneCorePropertiesPart_AddRelationship() {
   InputStream is =
       OpenXML4JTestDataSamples.openComplianceSampleStream(
           "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
   OPCPackage pkg;
   try {
     pkg = OPCPackage.open(is);
   } catch (InvalidFormatException e) {
     throw new RuntimeException(e);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   URI partUri = createURI("/docProps/core2.xml");
   try {
     pkg.addRelationship(
         PackagingURIHelper.createPartName(partUri),
         TargetMode.INTERNAL,
         PackageRelationshipTypes.CORE_PROPERTIES);
     fail("expected OPC compliance exception was not thrown");
   } catch (InvalidFormatException e) {
     throw new RuntimeException(e);
   } catch (InvalidOperationException e) {
     // expected during successful test
     assertEquals(
         "OPC Compliance error [M4.1]: can't add another core properties part ! Use the built-in package method instead.",
         e.getMessage());
   }
   pkg.revert();
 }
  public void testStyles() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));

    XSSFReader r = new XSSFReader(pkg);

    assertEquals(3, r.getStylesTable().getFonts().size());
    assertEquals(0, r.getStylesTable()._getNumberFormatSize());
  }
  public void testStrings() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));

    XSSFReader r = new XSSFReader(pkg);

    assertEquals(11, r.getSharedStringsTable().getItems().size());
    assertEquals(
        "Test spreadsheet",
        new XSSFRichTextString(r.getSharedStringsTable().getEntryAt(0)).toString());
  }
Exemplo n.º 9
0
 public void ReadAll(String path, String filename) {
   try {
     FileInputStream fis = new FileInputStream(path + filename + ".doc");
     XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
     XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
     System.out.println(extractor.getText());
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Exemplo n.º 10
0
 static final OPCPackage empty() {
   InputStream is = XMLSlideShow.class.getResourceAsStream("empty.pptx");
   if (is == null) {
     throw new RuntimeException("Missing resource 'empty.pptx'");
   }
   try {
     return OPCPackage.open(is);
   } catch (Exception e) {
     throw new POIXMLException(e);
   }
 }
  public void testGetBits() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));

    XSSFReader r = new XSSFReader(pkg);

    assertNotNull(r.getWorkbookData());
    assertNotNull(r.getSharedStringsData());
    assertNotNull(r.getStylesData());

    assertNotNull(r.getSharedStringsTable());
    assertNotNull(r.getStylesTable());
  }
Exemplo n.º 12
0
 @Override
 public Reader extractText(final InputStream stream, final String type, final String encoding)
     throws IOException {
   try {
     final OPCPackage opcPackage = OPCPackage.open(stream);
     final XSSFExcelExtractor xw = new XSSFExcelExtractor(opcPackage);
     return new StringReader(xw.getText());
   } catch (Exception e) {
     logger.warn("Failed to extract Excel text content", e);
     return new StringReader("");
   }
 }
Exemplo n.º 13
0
 /** @param args the command line arguments */
 public void ReadParagraph(String path, String filename) {
   try {
     FileInputStream fis = new FileInputStream(path + filename + ".docx");
     XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
     List<XWPFParagraph> paragraphList = xdoc.getParagraphs();
     for (XWPFParagraph paragraph : paragraphList) {
       System.out.println(paragraph.getText());
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
 private String sampleDocx(String resourceName) {
   String content = null;
   try {
     InputStream input = new BOMInputStream(TestUtil.class.getResourceAsStream(resourceName));
     XWPFDocument document = new XWPFDocument(OPCPackage.open(input));
     @SuppressWarnings("resource")
     XWPFWordExtractor wordExtractor = new XWPFWordExtractor(document);
     content = wordExtractor.getText();
   } catch (Exception exep) {
     exep.printStackTrace();
   }
   return content;
 }
Exemplo n.º 15
0
  public MediaType detect(TikaInputStream input, Metadata metadata) throws IOException {
    ZipFile zip = new ZipFile(input.getFile());
    for (ZipEntry entry : Collections.list(zip.entries())) {
      // Is it an Open Document file?
      if (entry.getName().equals("mimetype")) {
        InputStream stream = zip.getInputStream(entry);
        try {
          return fromString(IOUtils.toString(stream, "UTF-8"));
        } finally {
          stream.close();
        }
      } else if (entry.getName().equals("_rels/.rels")
          || entry.getName().equals("[Content_Types].xml")) {
        // Office Open XML File
        // As POI to open and investigate it for us
        try {
          OPCPackage pkg = OPCPackage.open(input.getFile().toString());
          input.setOpenContainer(pkg);

          PackageRelationshipCollection core =
              pkg.getRelationshipsByType(ExtractorFactory.CORE_DOCUMENT_REL);
          if (core.size() != 1) {
            throw new IOException(
                "Invalid OOXML Package received - expected 1 core document, found " + core.size());
          }

          // Get the type of the core document part
          PackagePart corePart = pkg.getPart(core.getRelationship(0));
          String coreType = corePart.getContentType();

          // Turn that into the type of the overall document
          String docType = coreType.substring(0, coreType.lastIndexOf('.'));
          return fromString(docType);
        } catch (InvalidFormatException e) {
          throw new IOException("Office Open XML File detected, but corrupted - " + e.getMessage());
        }
      } else if (entry.getName().equals("buildVersionHistory.plist")) {
        // This is an iWork document

        // Reset and ask
        zip.close();
        zip = new ZipFile(input.getFile());
        return IWorkPackageParser.identifyType(zip);
      } else if (entry.getName().equals("META-INF/")) {
        // Java Jar
        return MediaType.application("java-archive");
      }
    }

    return MediaType.APPLICATION_ZIP;
  }
 public void testCorePropertiesPart() {
   OPCPackage pkg;
   try {
     InputStream is =
         OpenXML4JTestDataSamples.openComplianceSampleStream(
             "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
     pkg = OPCPackage.open(is);
   } catch (InvalidFormatException e) {
     throw new RuntimeException(e);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   pkg.revert();
 }
Exemplo n.º 17
0
 public String readFileToString(File file) throws XmlException, OpenXML4JException, IOException {
   String rst = "";
   OPCPackage opcPackage = null;
   XSLFPowerPointExtractor extractor = null;
   try {
     opcPackage = OPCPackage.open(file.getPath(), PackageAccess.READ);
     extractor = new XSLFPowerPointExtractor(opcPackage);
     rst = extractor.getText();
   } finally {
     if (extractor != null) extractor.close();
     if (opcPackage != null) opcPackage.close();
   }
   return rst;
 }
Exemplo n.º 18
0
  private XlsxConverter(String filePath, String output)
      throws IOException, InvalidFormatException, ParserConfigurationException {

    OPCPackage op = OPCPackage.open(filePath);
    x = new XSSFWorkbook(op);
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    this.htmlDocumentFacade = new HtmlDocumentFacade(document);

    Element window = htmlDocumentFacade.createBlock();
    window.setAttribute("id", "window");
    page = htmlDocumentFacade.createBlock();
    page.setAttribute("id", "page");

    window.appendChild(page);
    htmlDocumentFacade.getBody().appendChild(window);
  }
Exemplo n.º 19
0
  /**
   * Excelファイルとワークブックの値取得定義を基に、Excelから取得した値をMapに設定し返します。
   *
   * @param excelFile Excelファイル
   * @return Excelの値を設定したMap
   * @throws IOException ファイルの取得に失敗
   */
  public static List<Table<String>> read(File excelFile) throws IOException {

    if (!excelFile.isFile()) {
      throw new JadegreenException("excelFile=" + excelFile.getPath() + ":ファイルが存在しません");
    }

    try {
      // ファイルを開く
      OPCPackage pkg = OPCPackage.open(excelFile);
      XSSFReader reader = new XSSFReader(pkg);
      // 値の収集
      return correctValues(excelFile.getPath(), reader, correctSheetIds(reader));
    } catch (OpenXML4JException | IOException e) {
      throw new IOException(e);
    }
  }
Exemplo n.º 20
0
 protected void initExport() throws QueryException {
   if (hasTemplate()) {
     try {
       if (bean.getReportLayout().getTemplateName().endsWith(".xlsm")) {
         wb = new XSSFWorkbook(OPCPackage.open(getTemplateInputStream()));
       } else {
         wb = new XSSFWorkbook(getTemplateInputStream());
       }
     } catch (Exception e) {
       e.printStackTrace();
       LOG.error(e.getMessage(), e);
       wb = new XSSFWorkbook();
     }
   } else {
     wb = new XSSFWorkbook();
   }
 }
Exemplo n.º 21
0
  /** Creates a Workbook from the given NPOIFSFileSystem, which may be password protected */
  private static Workbook create(NPOIFSFileSystem fs, String password)
      throws IOException, InvalidFormatException {
    DirectoryNode root = fs.getRoot();

    // Encrypted OOXML files go inside OLE2 containers, is this one?
    if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
      EncryptionInfo info = new EncryptionInfo(fs);
      Decryptor d = Decryptor.getInstance(info);

      boolean passwordCorrect = false;
      InputStream stream = null;
      try {
        if (password != null && d.verifyPassword(password)) {
          passwordCorrect = true;
        }
        if (!passwordCorrect && d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) {
          passwordCorrect = true;
        }
        if (passwordCorrect) {
          stream = d.getDataStream(root);
        }
      } catch (GeneralSecurityException e) {
        throw new IOException(e);
      }

      if (!passwordCorrect) {
        if (password != null) throw new EncryptedDocumentException("Password incorrect");
        else
          throw new EncryptedDocumentException(
              "The supplied spreadsheet is protected, but no password was supplied");
      }

      OPCPackage pkg = OPCPackage.open(stream);
      return create(pkg);
    }

    // If we get here, it isn't an encrypted XLSX file
    // So, treat it as a regular HSSF XLS one
    if (password != null) {
      Biff8EncryptionKey.setCurrentUserPassword(password);
    }
    Workbook wb = new HSSFWorkbook(root, true);
    Biff8EncryptionKey.setCurrentUserPassword(null);
    return wb;
  }
  private static String extractInvalidFormatMessage(String sampleNameSuffix) {

    InputStream is =
        OpenXML4JTestDataSamples.openComplianceSampleStream(
            "OPCCompliance_CoreProperties_" + sampleNameSuffix);
    OPCPackage pkg;
    try {
      pkg = OPCPackage.open(is);
    } catch (InvalidFormatException e) {
      // expected during successful test
      return e.getMessage();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    pkg.revert();
    // Normally must thrown an InvalidFormatException exception.
    throw new AssertionFailedError("expected OPC compliance exception was not thrown");
  }
Exemplo n.º 23
0
 public void ReadTable(String path, String filename) {
   try {
     FileInputStream fis = new FileInputStream(path + filename + ".docx");
     XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
     Iterator<IBodyElement> bodyElementIterator = xdoc.getBodyElementsIterator();
     while (bodyElementIterator.hasNext()) {
       IBodyElement element = bodyElementIterator.next();
       if ("TABLE".equalsIgnoreCase(element.getElementType().name())) {
         List<XWPFTable> tableList = element.getBody().getTables();
         for (XWPFTable table : tableList) {
           System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows());
           System.out.println(table.getText());
         }
       }
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
  /**
   * Check that the sheet iterator returns sheets in the logical order (as they are defined in the
   * workbook.xml)
   */
  public void testOrderOfSheets() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("reordered_sheets.xlsx"));

    XSSFReader r = new XSSFReader(pkg);

    String[] sheetNames = {"Sheet4", "Sheet2", "Sheet3", "Sheet1"};
    XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();

    int count = 0;
    while (it.hasNext()) {
      InputStream inp = it.next();
      assertNotNull(inp);
      inp.close();

      assertEquals(sheetNames[count], it.getSheetName());
      count++;
    }
    assertEquals(4, count);
  }
Exemplo n.º 25
0
  /**
   * Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream, which may be
   * password protected.
   *
   * <p>Your input stream MUST either support mark/reset, or be wrapped as a {@link
   * PushbackInputStream}! Note that using an {@link InputStream} has a higher memory footprint than
   * using a {@link File}.
   *
   * <p>Note that in order to properly release resources the Workbook should be closed after use.
   * Note also that loading from an InputStream requires more memory than loading from a File, so
   * prefer {@link #create(File)} where possible.
   *
   * @throws EncryptedDocumentException If the wrong password is given for a protected file
   * @throws EmptyFileException If an empty stream is given
   */
  public static Workbook create(InputStream inp, String password)
      throws IOException, InvalidFormatException, EncryptedDocumentException {
    // If clearly doesn't do mark/reset, wrap up
    if (!inp.markSupported()) {
      inp = new PushbackInputStream(inp, 8);
    }

    // Ensure that there is at least some data there
    byte[] header8 = IOUtils.peekFirst8Bytes(inp);

    // Try to create
    if (POIFSFileSystem.hasPOIFSHeader(header8)) {
      NPOIFSFileSystem fs = new NPOIFSFileSystem(inp);
      return create(fs, password);
    }
    if (POIXMLDocument.hasOOXMLHeader(inp)) {
      return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException(
        "Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  }
Exemplo n.º 26
0
  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;
    }

    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();
  }
  /**
   * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start
   * tag. The tested tag is <{m:wrong->.}ajout de value1{m:endif}> The expected tag is :
   * <{m:wrong->.} Expression wrong->. is invalid ajout de value1{m:endif}> After the run with the
   * end '}' char, the following runs must be present : A run must contains blanks char. The next
   * one must contains the error message. The next one must contains blank char and the next one the
   * static content of the conditional.
   *
   * @throws InvalidFormatException
   * @throws IOException
   * @throws DocumentParserException
   * @throws DocumentGenerationException
   */
  @Test
  public void testErrorInStartTag()
      throws InvalidFormatException, IOException, DocumentParserException,
          DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorStartTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorStartTag.docx");

    // scan the destination document
    assertEquals(2, document.getParagraphs().size());
    assertEquals(16, document.getParagraphs().get(0).getRuns().size());
    assertEquals(1, document.getParagraphs().get(1).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(5).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(6).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(6).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(6).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document.getParagraphs().get(0).getRuns().get(7).getCTR().getRPr().getHighlight().getVal());
    assertEquals(
        "Expression \"wrong->.\" is invalid: missing collection service call",
        document.getParagraphs().get(0).getRuns().get(7).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(7).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(7).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document.getParagraphs().get(0).getRuns().get(7).getCTR().getRPr().getHighlight().getVal());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(8).getText(0));
    assertEquals("ajout de value1", document.getParagraphs().get(0).getRuns().get(9).getText(0));
    assertEquals(
        "Unexpected tag m:endif at this location",
        document.getParagraphs().get(0).getRuns().get(13).getText(0));
  }
  /** Test M4.1 rule. */
  public void testOnlyOneCorePropertiesPart_AddPart() {
    String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx";
    OPCPackage pkg = null;
    try {
      pkg =
          OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    URI partUri = createURI("/docProps/core2.xml");
    try {
      pkg.createPart(PackagingURIHelper.createPartName(partUri), ContentTypes.CORE_PROPERTIES_PART);
      fail("expected OPC compliance exception was not thrown");
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (InvalidOperationException e) {
      // expected during successful test
      assertEquals(
          "OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !",
          e.getMessage());
    }
    pkg.revert();
  }
  /**
   * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start
   * tag. The tested tag is <{m:diagram provider:"noExistingProvider" width:"500" height:"500"
   * title="representationTitle"}Some text> The expected tag is : <{m:diagram
   * provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The
   * image tag is referencing an unknown diagram provider : 'noExistingProvider' Some text> After
   * the run with the end '}' char, the following runs must be present : A run must contains blanks
   * char. The next one must contains the error message. The next one is a blank separator. The next
   * one must contains the other error message. The next one must contains blank char and the next
   * one the static content after the tag in the original template.
   *
   * @throws InvalidFormatException
   * @throws IOException
   * @throws DocumentParserException
   * @throws DocumentGenerationException
   */
  @Test
  public void testErrorInEndTag()
      throws InvalidFormatException, IOException, DocumentParserException,
          DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorEndTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorEndTag.docx");
    // scan the destination document
    assertEquals(1, document.getParagraphs().size());
    assertEquals(24, document.getParagraphs().get(0).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(9).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(10).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(10).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(10).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(10)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());
    assertEquals(
        "Unexpected tag m:endlet at this location",
        document.getParagraphs().get(0).getRuns().get(11).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(11).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(11).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(11)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());

    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(10).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(10).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(10).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(10)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());
    assertEquals(
        "Unexpected tag m:endlet at this location",
        document.getParagraphs().get(0).getRuns().get(11).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(11).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(11).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(11)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());

    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(15).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(16).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(16).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(16).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(16)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());
    assertEquals(
        "gd:elseif, gd:else or gd:endif expected here.",
        document.getParagraphs().get(0).getRuns().get(17).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(17).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(17).getFontSize());
    assertEquals(
        STHighlightColor.LIGHT_GRAY,
        document
            .getParagraphs()
            .get(0)
            .getRuns()
            .get(17)
            .getCTR()
            .getRPr()
            .getHighlight()
            .getVal());

    assertEquals("Some", document.getParagraphs().get(0).getRuns().get(18).getText(0));
    assertEquals(" t", document.getParagraphs().get(0).getRuns().get(19).getText(0));
    assertEquals("ext", document.getParagraphs().get(0).getRuns().get(20).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(22).getText(0));
    assertEquals(
        "Unexpected tag EOF at this location",
        document.getParagraphs().get(0).getRuns().get(23).getText(0));
  }
Exemplo n.º 30
0
  public Object call() throws Exception {
    OPCPackage pkg = null;
    try {
      if (fileURL != null) {
        pkg = OPCPackage.open(fileURL);
      } else {
        pkg = PackageHelper.open(is);
      }
      XSSFReader r = new XSSFReader(pkg);

      StylesTable styles = r.getStylesTable();
      ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
      sheetContentsHandler = new DefaultTalendSheetContentsHandler(cache);
      DataFormatter formatter = new DataFormatter();
      boolean formulasNotResults = false;

      XMLReader parser = XMLReaderFactory.createXMLReader();
      ContentHandler handler =
          new TalendXSSFSheetXMLHandler(
              styles, strings, sheetContentsHandler, formatter, formulasNotResults);
      parser.setContentHandler(handler);

      XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) r.getSheetsData();
      List<InputStream> iss = new ArrayList<InputStream>();
      while (sheets.hasNext()) {
        InputStream sheet = sheets.next();
        String sheetName = sheets.getSheetName();

        boolean match = false;

        for (int i = 0; i < sheetNames.size(); i++) {
          if ((asRegexs.get(i) && sheetName.matches(sheetNames.get(i)))
              || (!asRegexs.get(i) && sheetName.equals(sheetNames.get(i)))) {
            match = true;
            iss.add(sheet);
            break;
          }
        }

        if (!match) {
          sheet.close();
        }
      }

      if (iss.size() < 1) {
        throw new RuntimeException("No match sheets");
      }

      for (InputStream is : iss) {
        try {
          InputSource sheetSource = new InputSource(is);
          sheetSource.setEncoding(charset);
          parser.parse(sheetSource);
        } finally {
          is.close();
        }
      }

    } finally {
      if (pkg != null) {
        pkg.revert();
      }
      cache.notifyErrorOccurred();
    }
    return null;
  }