FileDocumentFactory { public static Document createDocument(final SearchResourceContext leafResourceContext, final VFSLeaf leaf) throws IOException, DocumentException { final String fileName = leaf.getName(); final String suffix = getSuffix(fileName); if (log.isDebugEnabled()) { log.debug("suffix=" + suffix); } if (PDF_SUFFIX.indexOf(suffix) >= 0) { return PdfDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (HTML_SUFFIX.indexOf(suffix) >= 0) { return HtmlDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (XML_SUFFIX.indexOf(suffix) >= 0) { if (IMS_MANIFEST_FILE.equals(fileName)) { return IMSMetadataDocument.createDocument(leafResourceContext, leaf); } return XmlDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (TEXT_SUFFIX.indexOf(suffix) >= 0) { return TextDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (suffix.indexOf(WORD_X_SUFFIX) >= 0) { return WordOOXMLDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (suffix.indexOf(EXCEL_X_SUFFIX) >= 0) { if (excelFileEnabled) { return ExcelOOXMLDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } throw new DocumentNotEnabledException("Indexing of Excel files (xlsx) is not enabled."); } if (suffix.indexOf(POWERPOINT_X_SUFFIX) >= 0) { if (pptFileEnabled) { return PowerPointOOXMLDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } throw new DocumentNotEnabledException("Indexing of PowerPoint files (pptx) is not enabled."); } if (WORD_SUFFIX.indexOf(suffix) >= 0) { return WordDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } if (POWERPOINT_SUFFIX.indexOf(suffix) >= 0) { if (pptFileEnabled) { return PowerPointDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } throw new DocumentNotEnabledException("Indexing of PowerPoint files (ppt) is not enabled."); } if (EXCEL_SUFFIX.indexOf(suffix) >= 0) { if (excelFileEnabled) { return ExcelDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } throw new DocumentNotEnabledException("Indexing of Excel files (xls) is not enabled."); } if (OD_TEXT_SUFFIX.indexOf(suffix) >= 0 || OD_SPREADSHEET_SUFFIX.indexOf(suffix) >= 0 || OD_PRESENTATION_SUFFIX.indexOf(suffix) >= 0 || OD_FORMULA_SUFFIX.indexOf(suffix) >= 0 || OD_GRAPHIC_SUFFIX.indexOf(suffix) >= 0) { return OpenDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } return UnkownDocument.createDocument(leafResourceContext, leaf, mimeTypeProvider.getMimeType(suffix)); } FileDocumentFactory(final SearchModule searchModule, MimeTypeProvider mimeTypeProvider);  FileDocumentFactory(final SearchModule searchModule); static Document createDocument(final SearchResourceContext leafResourceContext, final VFSLeaf leaf); boolean isFileSupported(final VFSLeaf leaf); int getExcludedFileSizeCount(); void resetExcludedFileSizeCount();  }
@Test public void testCreateHtmlDocument() throws Exception { final String htmlFileName = "test.html"; final String htmlText = "<html><head><meta name=\"generator\" content=\"olat-tinymce-1\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>" + "<H1>Test HTML Seite fuer JUnit Test</H1>" + "Dies ist<br />der Test&nbsp;Text" + "</body></html>"; final String text = "Test HTML Seite fuer JUnit Test Dies ist der Test\u00A0Text"; final File htmlFile = new File(SEARCH_TEST_FOLDER, htmlFileName); if (htmlFile.exists()) { htmlFile.delete(); } String filePath = "anyFilePath"; FileUtils.save(htmlFile, htmlText, "utf-8"); try { final SearchResourceContext resourceContext = new SearchResourceContext(); resourceContext.setBusinessControlFor(OresHelper.createOLATResourceableType("FileDocumentFactoryITCase")); resourceContext.setFilePath(filePath + "/" + htmlFileName); final Document htmlDocument = FileDocumentFactory.createDocument(resourceContext, new LocalFileImpl(htmlFile)); final String content = htmlDocument.get(AbstractOlatDocument.CONTENT_FIELD_NAME); assertEquals("Wrong HTML content=" + content.trim() + " , must be =" + text.trim(), text.trim(), content.trim()); final String resourceUrl = htmlDocument.get(AbstractOlatDocument.RESOURCEURL_FIELD_NAME); assertEquals("Wrong ResourceUrl", "[FileDocumentFactoryITCase:0][path=" + filePath + "/" + htmlFileName + "]", resourceUrl); final String fileType = htmlDocument.get(AbstractOlatDocument.FILETYPE_FIELD_NAME); assertEquals("Wrong file-type", "type.file.html", fileType); } catch (final IOException e) { fail("IOException=" + e.getMessage()); } catch (final DocumentException e) { fail("DocumentException=" + e.getMessage()); } }
@Test public void testCreateXMLDocument() { try { final SearchResourceContext resourceContext = new SearchResourceContext(); resourceContext.setBusinessControlFor(olatResourcable); final Document xmlDocument = FileDocumentFactory.createDocument(resourceContext, new LocalFileImpl(getTestFile("XMLDocument.xml"))); final String content = xmlDocument.get(AbstractOlatDocument.CONTENT_FIELD_NAME); assertEquals("Wrong XML content", "content", content.trim()); } catch (final Exception e) { e.printStackTrace(); fail(e.getClass().getSimpleName() + ": " + e.getMessage()); } }
@Test public void testCreateExcelOOXMLDocument() { try { final SearchResourceContext resourceContext = new SearchResourceContext(); resourceContext.setBusinessControlFor(olatResourcable); final Document excelDocument = FileDocumentFactory.createDocument(resourceContext, new LocalFileImpl(getTestFile("ExcelOOXMLDocument.xlsx"))); final String content = excelDocument.get(AbstractOlatDocument.CONTENT_FIELD_NAME); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet1")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet1:A1")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet1:D4")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet1:H8")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet2")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet2:A1")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet2:D4")); assertTrue("Wrong Excel (xlsx) content", content.contains("Sheet2:H8")); } catch (final Exception e) { e.printStackTrace(); fail(e.getClass().getSimpleName() + ": " + e.getMessage()); } }
@Test public void testCreateExcelOOXMLDocumentSlow() { try { final SearchResourceContext resourceContext = new SearchResourceContext(); resourceContext.setBusinessControlFor(olatResourcable); final Thread excelTextExtractionThread = new Thread(new Runnable() { @Override public void run() { try { FileDocumentFactory.createDocument(resourceContext, new LocalFileImpl(getTestFile("ExcelOOXMLDocumentSlow.xlsx"))); } catch (Exception e) { } } }); excelTextExtractionThread.start(); Thread.sleep(5000); assertFalse("Text extraction for Excel (xlsx) must not take longer than 2s.", excelTextExtractionThread.getState().equals(State.RUNNABLE)); } catch (final Exception e) { e.printStackTrace(); fail(e.getClass().getSimpleName() + ": " + e.getMessage()); } }
@Test public void testCreatePowerPointOOXMLDocument() { try { final SearchResourceContext resourceContext = new SearchResourceContext(); resourceContext.setBusinessControlFor(olatResourcable); final Document powerPointDocument = FileDocumentFactory.createDocument(resourceContext, new LocalFileImpl(getTestFile("PowerPointDocument.pptx"))); final String content = powerPointDocument.get(AbstractOlatDocument.CONTENT_FIELD_NAME); assertTrue("Wrong PowerPoint (pptx) content", content.contains("Slide1")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("ContentSlide1")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("Oliver: CommentSlide1")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("NoteSlide1")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("Slide2")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("ContentSlide2")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("Oliver: CommentSlide2")); assertTrue("Wrong PowerPoint (pptx) content", content.contains("NoteSlide2")); } catch (final Exception e) { e.printStackTrace(); fail(e.getClass().getSimpleName() + ": " + e.getMessage()); } }