Search in sources :

Example 1 with AnalyzerFactory

use of org.opengrok.indexer.analysis.AnalyzerFactory in project OpenGrok by OpenGrok.

the class DocumentMatcherTest method testMandocDocument.

/**
 * Tests a mandoc(5)-style document.
 * @throws IOException I/O exception
 */
@Test
public void testMandocDocument() throws IOException {
    InputStream res = getClass().getClassLoader().getResourceAsStream("analysis/document/catman.1m");
    assertNotNull(res, "despite inclusion locally,");
    byte[] buf = readSignature(res);
    AnalyzerFactory fac;
    // assert that it is mandoc-like
    fac = MandocAnalyzerFactory.MATCHER.isMagic(buf, res);
    assertNotNull(fac, "though catman.1m is mandoc(5),");
    assertSame(MandocAnalyzerFactory.DEFAULT_INSTANCE, fac, "though catman.1m is mandoc(5)");
    // assert that it is also troff-like (though mandoc will win in the
    // AnalyzerGuru)
    fac = TroffAnalyzerFactory.MATCHER.isMagic(buf, res);
    assertNotNull(fac, "though catman.1m is mandoc(5),");
    assertSame(TroffAnalyzerFactory.DEFAULT_INSTANCE, fac, "though catman.1m is mandoc(5)");
}
Also used : InputStream(java.io.InputStream) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory) Test(org.junit.jupiter.api.Test)

Example 2 with AnalyzerFactory

use of org.opengrok.indexer.analysis.AnalyzerFactory in project OpenGrok by OpenGrok.

the class JavaClassAnalyzerFactoryTest method testDylibCafebabeWrtAnalyzerGuru.

/**
 * Tests a dylib with spurious CAFEBABE.
 * @throws IOException I/O exception
 */
@Test
public void testDylibCafebabeWrtAnalyzerGuru() throws IOException {
    InputStream res = getClass().getClassLoader().getResourceAsStream("analysis/executables/fat.dylib");
    assertNotNull(res, "despite inclusion locally,");
    AnalyzerFactory fac = AnalyzerGuru.find(res);
    if (fac != null) {
        assertNotSame(fac.getClass(), JavaClassAnalyzerFactory.class, "should not be JavaClassAnalyzerFactory");
    }
}
Also used : InputStream(java.io.InputStream) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory) Test(org.junit.jupiter.api.Test)

Example 3 with AnalyzerFactory

use of org.opengrok.indexer.analysis.AnalyzerFactory in project OpenGrok by OpenGrok.

the class IndexDatabase method checkSettings.

/**
 * Verify TABSIZE, and evaluate AnalyzerGuru version together with ZVER --
 * or return a value to indicate mismatch.
 * @param file the source file object
 * @param path the source file path
 * @return {@code false} if a mismatch is detected
 */
private boolean checkSettings(File file, String path) throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // potential xref writer
    boolean outIsXrefWriter = false;
    int reqTabSize = project != null && project.hasTabSizeSetting() ? project.getTabSize() : 0;
    Integer actTabSize = settings.getTabSize();
    if (actTabSize != null && !actTabSize.equals(reqTabSize)) {
        LOGGER.log(Level.FINE, "Tabsize mismatch: {0}", path);
        return false;
    }
    int n = 0;
    postsIter = uidIter.postings(postsIter);
    while (postsIter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
        ++n;
        // Read a limited-fields version of the document.
        Document doc = reader.document(postsIter.docID(), CHECK_FIELDS);
        if (doc == null) {
            LOGGER.log(Level.FINER, "No Document: {0}", path);
            continue;
        }
        long reqGuruVersion = AnalyzerGuru.getVersionNo();
        Long actGuruVersion = settings.getAnalyzerGuruVersion();
        /*
             * For an older OpenGrok index that does not yet have a defined,
             * stored analyzerGuruVersion, break so that no extra work is done.
             * After a re-index, the guru version check will be active.
             */
        if (actGuruVersion == null) {
            break;
        }
        AbstractAnalyzer fa = null;
        String fileTypeName;
        if (actGuruVersion.equals(reqGuruVersion)) {
            fileTypeName = doc.get(QueryBuilder.TYPE);
            if (fileTypeName == null) {
                // (Should not get here, but break just in case.)
                LOGGER.log(Level.FINEST, "Missing TYPE field: {0}", path);
                break;
            }
            AnalyzerFactory fac = AnalyzerGuru.findByFileTypeName(fileTypeName);
            if (fac != null) {
                fa = fac.getAnalyzer();
            }
        } else {
            /*
                 * If the stored guru version does not match, re-verify the
                 * selection of analyzer or return a value to indicate the
                 * analyzer is now mis-matched.
                 */
            LOGGER.log(Level.FINER, "Guru version mismatch: {0}", path);
            fa = getAnalyzerFor(file, path);
            fileTypeName = fa.getFileTypeName();
            String oldTypeName = doc.get(QueryBuilder.TYPE);
            if (!fileTypeName.equals(oldTypeName)) {
                if (LOGGER.isLoggable(Level.FINE)) {
                    LOGGER.log(Level.FINE, "Changed {0} to {1}: {2}", new Object[] { oldTypeName, fileTypeName, path });
                }
                return false;
            }
        }
        // Verify Analyzer version, or return a value to indicate mismatch.
        long reqVersion = AnalyzerGuru.getAnalyzerVersionNo(fileTypeName);
        Long actVersion = settings.getAnalyzerVersion(fileTypeName);
        if (actVersion == null || !actVersion.equals(reqVersion)) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.log(Level.FINE, "{0} version mismatch: {1}", new Object[] { fileTypeName, path });
            }
            return false;
        }
        if (fa != null) {
            outIsXrefWriter = true;
        }
        // The versions checks have passed.
        break;
    }
    if (n < 1) {
        LOGGER.log(Level.FINER, "Missing index Documents: {0}", path);
        return false;
    }
    // If the economy mode is on, this should be treated as a match.
    if (!env.isGenerateHtml()) {
        if (xrefExistsFor(path)) {
            LOGGER.log(Level.FINEST, "Extraneous {0} , removing its xref file", path);
            removeXrefFile(path);
        }
        return true;
    }
    return (!outIsXrefWriter || xrefExistsFor(path));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) Document(org.apache.lucene.document.Document) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory)

Example 4 with AnalyzerFactory

use of org.opengrok.indexer.analysis.AnalyzerFactory in project OpenGrok by OpenGrok.

the class IndexerTest method testXref.

@Test
void testXref() throws IOException {
    List<File> files = new ArrayList<>();
    FileUtilities.getAllFiles(new File(repository.getSourceRoot()), files, false);
    for (File f : files) {
        AnalyzerFactory factory = AnalyzerGuru.find(f.getAbsolutePath());
        if (factory == null) {
            continue;
        }
        try (FileReader in = new FileReader(f);
            StringWriter out = new StringWriter()) {
            try {
                AnalyzerGuru.writeXref(factory, in, out, null, null, null);
            } catch (UnsupportedOperationException exp) {
            // ignore
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) File(java.io.File) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory) Test(org.junit.jupiter.api.Test)

Example 5 with AnalyzerFactory

use of org.opengrok.indexer.analysis.AnalyzerFactory in project OpenGrok by OpenGrok.

the class JarAnalyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
    JFieldBuilder jfbuilder = new JFieldBuilder();
    try (ZipInputStream zis = new ZipInputStream(src.getStream())) {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String ename = entry.getName();
            if (xrefOut != null) {
                xrefOut.append("<br/><b>");
                Util.htmlize(ename, xrefOut);
                xrefOut.append("</b>");
            }
            StringWriter fout = jfbuilder.write(QueryBuilder.FULL);
            fout.write(ename);
            fout.write("\n");
            AnalyzerFactory fac = AnalyzerGuru.find(ename);
            if (fac instanceof JavaClassAnalyzerFactory) {
                if (xrefOut != null) {
                    xrefOut.append("<br/>");
                }
                JavaClassAnalyzer jca = (JavaClassAnalyzer) fac.getAnalyzer();
                jca.analyze(doc, new BufferedInputStream(zis), xrefOut, jfbuilder);
            }
        }
    }
    for (String name : FIELD_NAMES) {
        if (jfbuilder.hasField(name)) {
            String fstr = jfbuilder.write(name).toString();
            doc.add(new OGKTextField(name, fstr, Store.NO));
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) OGKTextField(org.opengrok.indexer.analysis.OGKTextField) StringWriter(java.io.StringWriter) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory)

Aggregations

AnalyzerFactory (org.opengrok.indexer.analysis.AnalyzerFactory)10 Test (org.junit.jupiter.api.Test)8 InputStream (java.io.InputStream)7 StringWriter (java.io.StringWriter)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Document (org.apache.lucene.document.Document)1 AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)1 OGKTextField (org.opengrok.indexer.analysis.OGKTextField)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1