Search in sources :

Example 1 with DirectoryListingEntry

use of org.apache.tika.parser.chm.accessor.DirectoryListingEntry in project tika by apache.

the class TestChmBlockInfo method testGetChmBlockInfo.

@Test
public void testGetChmBlockInfo() {
    for (DirectoryListingEntry directoryListingEntry : chmDirListCont.getDirectoryListingEntryList()) {
        chmBlockInfo = ChmBlockInfo.getChmBlockInfoInstance(directoryListingEntry, (int) clrt.getBlockLen(), chmLzxcControlData);
        // Assert.assertTrue(!directoryListingEntry.getName().isEmpty() &&
        // chmBlockInfo.toString() != null);
        assertTrue(!ChmCommons.isEmpty(directoryListingEntry.getName()) && chmBlockInfo.toString() != null);
    }
}
Also used : DirectoryListingEntry(org.apache.tika.parser.chm.accessor.DirectoryListingEntry) Test(org.junit.Test)

Example 2 with DirectoryListingEntry

use of org.apache.tika.parser.chm.accessor.DirectoryListingEntry in project tika by apache.

the class TestChmExtraction method testExtractChmEntry.

protected void testExtractChmEntry(InputStream stream) throws TikaException, IOException {
    ChmExtractor chmExtractor = new ChmExtractor(stream);
    ChmDirectoryListingSet entries = chmExtractor.getChmDirList();
    final Pattern htmlPairP = Pattern.compile("\\Q<html\\E.+\\Q</html>\\E", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
    Set<String> names = new HashSet<String>();
    for (DirectoryListingEntry directoryListingEntry : entries.getDirectoryListingEntryList()) {
        byte[] data = chmExtractor.extractChmEntry(directoryListingEntry);
        //Entry names should be nice. Disable this if the test chm do have bad looking but valid entry names.
        if (!niceAscFileName(directoryListingEntry.getName())) {
            throw new TikaException("Warning: File name contains a non ascii char : " + directoryListingEntry.getName());
        }
        final String lowName = directoryListingEntry.getName().toLowerCase(Locale.ROOT);
        //check duplicate entry name which is seen before.
        if (names.contains(lowName)) {
            throw new TikaException("Duplicate File name detected : " + directoryListingEntry.getName());
        }
        names.add(lowName);
        if (lowName.endsWith(".html") || lowName.endsWith(".htm") || lowName.endsWith(".hhk") || lowName.endsWith(".hhc")) //|| name.endsWith(".bmp")
        {
            if (findZero(data)) {
                throw new TikaException("Xhtml/text file contains '\\0' : " + directoryListingEntry.getName());
            }
            //validate html
            String html = new String(data, ISO_8859_1);
            if (!htmlPairP.matcher(html).find()) {
                System.err.println(lowName + " is invalid.");
                System.err.println(html);
                throw new TikaException("Invalid xhtml file : " + directoryListingEntry.getName());
            }
        //                else {
        //                    System.err.println(directoryListingEntry.getName() + " is valid.");
        //                }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) TikaException(org.apache.tika.exception.TikaException) ChmDirectoryListingSet(org.apache.tika.parser.chm.accessor.ChmDirectoryListingSet) ChmExtractor(org.apache.tika.parser.chm.core.ChmExtractor) HashSet(java.util.HashSet) DirectoryListingEntry(org.apache.tika.parser.chm.accessor.DirectoryListingEntry)

Example 3 with DirectoryListingEntry

use of org.apache.tika.parser.chm.accessor.DirectoryListingEntry in project tika by apache.

the class TestChmExtractor method testExtractChmEntry.

@Test
public void testExtractChmEntry() throws TikaException {
    ChmDirectoryListingSet entries = chmExtractor.getChmDirList();
    int count = 0;
    for (DirectoryListingEntry directoryListingEntry : entries.getDirectoryListingEntryList()) {
        chmExtractor.extractChmEntry(directoryListingEntry);
        ++count;
    }
    assertEquals(TestParameters.VP_CHM_ENTITIES_NUMBER, count);
}
Also used : ChmDirectoryListingSet(org.apache.tika.parser.chm.accessor.ChmDirectoryListingSet) DirectoryListingEntry(org.apache.tika.parser.chm.accessor.DirectoryListingEntry) Test(org.junit.Test) TikaTest(org.apache.tika.TikaTest)

Example 4 with DirectoryListingEntry

use of org.apache.tika.parser.chm.accessor.DirectoryListingEntry in project tika by apache.

the class ChmParser method parse.

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    ChmExtractor chmExtractor = new ChmExtractor(stream);
    // metadata
    metadata.set(Metadata.CONTENT_TYPE, "application/vnd.ms-htmlhelp");
    // content
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    Parser htmlParser = EmbeddedDocumentUtil.tryToFindExistingLeafParser(HtmlParser.class, context);
    if (htmlParser == null) {
        htmlParser = new HtmlParser();
    }
    for (DirectoryListingEntry entry : chmExtractor.getChmDirList().getDirectoryListingEntryList()) {
        final String entryName = entry.getName();
        if (entryName.endsWith(".html") || entryName.endsWith(".htm")) {
            //                AttributesImpl attrs = new AttributesImpl();
            //                attrs.addAttribute("", "name", "name", "String", entryName);
            //                xhtml.startElement("", "document", "document", attrs);
            byte[] data = chmExtractor.extractChmEntry(entry);
            parsePage(data, htmlParser, xhtml, context);
        //                xhtml.endElement("", "", "document");
        }
    }
    xhtml.endDocument();
}
Also used : HtmlParser(org.apache.tika.parser.html.HtmlParser) ChmExtractor(org.apache.tika.parser.chm.core.ChmExtractor) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) Parser(org.apache.tika.parser.Parser) AbstractParser(org.apache.tika.parser.AbstractParser) HtmlParser(org.apache.tika.parser.html.HtmlParser) DirectoryListingEntry(org.apache.tika.parser.chm.accessor.DirectoryListingEntry)

Aggregations

DirectoryListingEntry (org.apache.tika.parser.chm.accessor.DirectoryListingEntry)4 ChmDirectoryListingSet (org.apache.tika.parser.chm.accessor.ChmDirectoryListingSet)2 ChmExtractor (org.apache.tika.parser.chm.core.ChmExtractor)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 TikaTest (org.apache.tika.TikaTest)1 TikaException (org.apache.tika.exception.TikaException)1 AbstractParser (org.apache.tika.parser.AbstractParser)1 Parser (org.apache.tika.parser.Parser)1 HtmlParser (org.apache.tika.parser.html.HtmlParser)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1