Search in sources :

Example 11 with AbstractAnalyzer

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

the class SearchAndContextFormatterTest method getFirstFragments.

private String[] getFirstFragments(SearchEngine instance) throws IOException {
    ContextArgs args = new ContextArgs((short) 1, (short) 10);
    /*
         * The following `anz' should go unused, but UnifiedHighlighter demands
         * an analyzer "even if in some circumstances it isn't used."
         */
    PlainAnalyzerFactory fac = PlainAnalyzerFactory.DEFAULT_INSTANCE;
    AbstractAnalyzer anz = fac.getAnalyzer();
    ContextFormatter formatter = new ContextFormatter(args);
    OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, instance.getSearcher(), anz);
    uhi.setBreakIterator(StrictLineBreakIterator::new);
    uhi.setFormatter(formatter);
    ScoreDoc[] docs = instance.scoreDocs();
    for (ScoreDoc scoreDoc : docs) {
        int docid = scoreDoc.doc;
        Document doc = instance.doc(docid);
        String path = doc.get(QueryBuilder.PATH);
        System.out.println(path);
        formatter.setUrl("/source" + path);
        for (String contextField : instance.getQueryBuilder().getContextFields()) {
            Map<String, String[]> res = uhi.highlightFields(new String[] { contextField }, instance.getQueryObject(), new int[] { docid }, new int[] { 10 });
            String[] frags = res.getOrDefault(contextField, null);
            if (frags != null) {
                return frags;
            }
        }
    }
    return null;
}
Also used : PlainAnalyzerFactory(org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer)

Example 12 with AbstractAnalyzer

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

the class SearchAndContextFormatterTest2 method getFirstFragments.

private String[] getFirstFragments(SearchEngine instance) throws IOException, InvalidTokenOffsetsException {
    ContextArgs args = new ContextArgs((short) 1, (short) 10);
    /*
         * The following `anz' should go unused, but UnifiedHighlighter demands
         * an analyzer "even if in some circumstances it isn't used."
         */
    PlainAnalyzerFactory fac = PlainAnalyzerFactory.DEFAULT_INSTANCE;
    AbstractAnalyzer anz = fac.getAnalyzer();
    ContextFormatter formatter = new ContextFormatter(args);
    OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, instance.getSearcher(), anz);
    uhi.setBreakIterator(StrictLineBreakIterator::new);
    uhi.setFormatter(formatter);
    uhi.setTabSize(TABSIZE);
    ScoreDoc[] docs = instance.scoreDocs();
    for (ScoreDoc scoreDoc : docs) {
        int docid = scoreDoc.doc;
        Document doc = instance.doc(docid);
        String path = doc.get(QueryBuilder.PATH);
        System.out.println(path);
        formatter.setUrl("/source" + path);
        for (String contextField : instance.getQueryBuilder().getContextFields()) {
            Map<String, String[]> res = uhi.highlightFields(new String[] { contextField }, instance.getQueryObject(), new int[] { docid }, new int[] { 10 });
            String[] frags = res.getOrDefault(contextField, null);
            if (frags != null) {
                return frags;
            }
        }
    }
    return null;
}
Also used : PlainAnalyzerFactory(org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer)

Example 13 with AbstractAnalyzer

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

the class BZip2Analyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
    AbstractAnalyzer fa;
    StreamSource bzSrc = wrap(src);
    String path = doc.get(QueryBuilder.PATH);
    if (path != null && (path.endsWith(".bz2") || path.endsWith(".BZ2") || path.endsWith(".bz"))) {
        String newname = path.substring(0, path.lastIndexOf('.'));
        // System.err.println("BZIPPED OF = " + newname);
        try (InputStream in = bzSrc.getStream()) {
            fa = AnalyzerGuru.getAnalyzer(in, newname);
        }
        if (!(fa instanceof BZip2Analyzer)) {
            if (fa.getGenre() == Genre.PLAIN || fa.getGenre() == Genre.XREFABLE) {
                this.g = Genre.XREFABLE;
            } else {
                this.g = Genre.DATA;
            }
            fa.analyze(doc, bzSrc, xrefOut);
            if (doc.get(QueryBuilder.T) != null) {
                doc.removeField(QueryBuilder.T);
                if (g == Genre.XREFABLE) {
                    doc.add(new Field(QueryBuilder.T, g.typeName(), AnalyzerGuru.string_ft_stored_nanalyzed_norms));
                }
            }
        }
    }
}
Also used : Field(org.apache.lucene.document.Field) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) InputStream(java.io.InputStream) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) StreamSource(org.opengrok.indexer.analysis.StreamSource)

Example 14 with AbstractAnalyzer

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

the class XMLAnalyzerTest method bug806.

@Test
public void bug806() throws IOException {
    String xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<server>\n" + "  <mbean code=\"QuartzBean\" \n" + "      name=\"user:service=QuartzService,name=QuartzService\">\n" + "    <depends>jboss.jca:service=DataSourceBinding,name=ServerDS</depends>\n" + "    <attribute name=\"JndiName\">Quartz</attribute>\n" + "    <attribute name=\"Properties\">\n" + "      org.quartz.plugin.jobInitializer.fileNames = ../server/default/conf/scheduler/quartz_jobs.xml\n" + "    </attribute>\n" + "  </mbean>\n" + "</server>";
    StringReader sr = new StringReader(xmlText);
    StringWriter sw = new StringWriter();
    XMLAnalyzerFactory fac = new XMLAnalyzerFactory();
    AbstractAnalyzer analyzer = fac.getAnalyzer();
    analyzer.writeXref(new WriteXrefArgs(sr, sw));
    String[] xref = sw.toString().split("\n");
    // don't remove ../
    assertTrue(xref[7].contains("org.quartz.plugin.jobInitializer.fileNames</a> = <a href=\"/source/s?path=../\">..</a>/"));
}
Also used : StringWriter(java.io.StringWriter) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) StringReader(java.io.StringReader) WriteXrefArgs(org.opengrok.indexer.analysis.WriteXrefArgs) Test(org.junit.jupiter.api.Test)

Example 15 with AbstractAnalyzer

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

the class PhpXrefTest method basicTest.

@Test
public void basicTest() throws IOException {
    String s = "<?php foo bar";
    Writer w = new StringWriter();
    PhpAnalyzerFactory fac = new PhpAnalyzerFactory();
    AbstractAnalyzer analyzer = fac.getAnalyzer();
    WriteXrefArgs xargs = new WriteXrefArgs(new StringReader(s), w);
    Xrefer xref = analyzer.writeXref(xargs);
    assertEquals("<a class=\"l\" name=\"1\" href=\"#1\">1</a><strong>&lt;?php</strong> <a href=\"/" + "source/s?defs=foo\" class=\"intelliWindow-symbol\" data-definition-place=\"undefined-in-file\">foo</a>" + " <a href=\"/source/s?defs=bar\" class=\"intelliWindow-symbol\" data-definition-place=\"undefined-in-file\">bar</a>", w.toString());
    assertEquals(1, xref.getLOC(), "PHP LOC");
}
Also used : StringWriter(java.io.StringWriter) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) StringReader(java.io.StringReader) Xrefer(org.opengrok.indexer.analysis.Xrefer) WriteXrefArgs(org.opengrok.indexer.analysis.WriteXrefArgs) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)15 StringWriter (java.io.StringWriter)8 WriteXrefArgs (org.opengrok.indexer.analysis.WriteXrefArgs)8 StringReader (java.io.StringReader)6 Test (org.junit.jupiter.api.Test)6 Writer (java.io.Writer)5 Document (org.apache.lucene.document.Document)5 Xrefer (org.opengrok.indexer.analysis.Xrefer)5 PlainAnalyzerFactory (org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Field (org.apache.lucene.document.Field)2 ScoreDoc (org.apache.lucene.search.ScoreDoc)2 StreamSource (org.opengrok.indexer.analysis.StreamSource)2 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 ExecutionException (java.util.concurrent.ExecutionException)1