Search in sources :

Example 1 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class Results method prettyPrint.

/**
     * Prints out results in html form. The following search helper fields are
     * required to be properly initialized: <ul>
     * <li>{@link SearchHelper#dataRoot}</li>
     * <li>{@link SearchHelper#contextPath}</li>
     * <li>{@link SearchHelper#searcher}</li> <li>{@link SearchHelper#hits}</li>
     * <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
     * <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
     * <li>{@link SearchHelper#summarizer} (if sourceContext is not
     * {@code null})</li> <li>{@link SearchHelper#compressed} (if sourceContext
     * is not {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
     * sourceContext or historyContext is not {@code null})</li> </ul>
     *
     * @param out write destination
     * @param sh search helper which has all required fields set
     * @param start index of the first hit to print
     * @param end index of the last hit to print
     * @throws HistoryException
     * @throws IOException
     * @throws ClassNotFoundException
     */
public static void prettyPrint(Writer out, SearchHelper sh, int start, int end) throws HistoryException, IOException, ClassNotFoundException {
    Project p;
    String ctxE = Util.URIEncodePath(sh.contextPath);
    String xrefPrefix = sh.contextPath + Prefix.XREF_P;
    String morePrefix = sh.contextPath + Prefix.MORE_P;
    String xrefPrefixE = ctxE + Prefix.XREF_P;
    File xrefDataDir = new File(sh.dataRoot, Prefix.XREF_P.toString());
    for (Map.Entry<String, ArrayList<Document>> entry : createMap(sh.searcher, sh.hits, start, end).entrySet()) {
        String parent = entry.getKey();
        out.write("<tr class=\"dir\"><td colspan=\"3\"><a href=\"");
        out.write(xrefPrefixE);
        out.write(Util.URIEncodePath(parent));
        out.write("/\">");
        // htmlize ???
        out.write(parent);
        out.write("/</a>");
        if (sh.desc != null) {
            out.write(" - <i>");
            // htmlize ???
            out.write(sh.desc.get(parent));
            out.write("</i>");
        }
        JSONArray messages;
        if ((p = Project.getProject(parent)) != null && (messages = Util.messagesToJson(p, RuntimeEnvironment.MESSAGES_MAIN_PAGE_TAG)).size() > 0) {
            out.write(" <a ");
            out.write("href=\"" + xrefPrefix + "/" + p.getName() + "\">");
            out.write("<span class=\"important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
            out.write("</a>");
        }
        out.write("</td></tr>");
        for (Document doc : entry.getValue()) {
            String rpath = doc.get(QueryBuilder.PATH);
            String rpathE = Util.URIEncodePath(rpath);
            DateFormat df;
            out.write("<tr>");
            Util.writeHAD(out, sh.contextPath, rpathE, false);
            out.write("<td class=\"f\"><a href=\"");
            out.write(xrefPrefixE);
            out.write(rpathE);
            out.write("\"");
            if (RuntimeEnvironment.getInstance().isLastEditedDisplayMode()) {
                try {
                    // insert last edited date if possible
                    df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                    String dd = df.format(DateTools.stringToDate(doc.get("date")));
                    out.write(" class=\"result-annotate\" title=\"");
                    out.write("Last modified: ");
                    out.write(dd);
                    out.write("\"");
                } catch (ParseException ex) {
                    LOGGER.log(Level.WARNING, "An error parsing date information", ex);
                }
            }
            out.write(">");
            // htmlize ???
            out.write(rpath.substring(rpath.lastIndexOf('/') + 1));
            out.write("</a>");
            out.write("</td><td><tt class=\"con\">");
            if (sh.sourceContext != null) {
                Genre genre = Genre.get(doc.get("t"));
                Definitions tags = null;
                IndexableField tagsField = doc.getField(QueryBuilder.TAGS);
                if (tagsField != null) {
                    tags = Definitions.deserialize(tagsField.binaryValue().bytes);
                }
                Scopes scopes;
                IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
                if (scopesField != null) {
                    scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
                } else {
                    scopes = new Scopes();
                }
                if (Genre.XREFABLE == genre && sh.summarizer != null) {
                    String xtags = getTags(xrefDataDir, rpath, sh.compressed);
                    // FIXME use Highlighter from lucene contrib here,
                    // instead of summarizer, we'd also get rid of
                    // apache lucene in whole source ...
                    out.write(sh.summarizer.getSummary(xtags).toString());
                } else if (Genre.HTML == genre && sh.summarizer != null) {
                    String htags = getTags(sh.sourceRoot, rpath, false);
                    out.write(sh.summarizer.getSummary(htags).toString());
                } else {
                    FileReader r = genre == Genre.PLAIN ? new FileReader(new File(sh.sourceRoot, rpath)) : null;
                    sh.sourceContext.getContext(r, out, xrefPrefix, morePrefix, rpath, tags, true, sh.builder.isDefSearch(), null, scopes);
                }
            }
            if (sh.historyContext != null) {
                sh.historyContext.getContext(new File(sh.sourceRoot, rpath), rpath, out, sh.contextPath);
            }
            out.write("</tt></td></tr>\n");
        }
    }
}
Also used : Definitions(org.opensolaris.opengrok.analysis.Definitions) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) Project(org.opensolaris.opengrok.configuration.Project) Scopes(org.opensolaris.opengrok.analysis.Scopes) DateFormat(java.text.DateFormat) FileReader(java.io.FileReader) ParseException(java.text.ParseException) Genre(org.opensolaris.opengrok.analysis.FileAnalyzer.Genre) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class JavaAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     *
     * @throws java.lang.Exception
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/java/Sample.java";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("java testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    // foo, bar, main
    assertEquals(5, scopes.size());
    for (int i = 0; i < 74; ++i) {
        if (i >= 29 && i <= 31) {
            assertEquals("Sample", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i >= 33 && i <= 41) {
            assertEquals("Method", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i == 43) {
            assertEquals("AbstractMethod", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i >= 47 && i <= 56) {
            assertEquals("InnerMethod", scopes.getScope(i).getName());
            assertEquals("Sample.InnerClass", scopes.getScope(i).getNamespace());
        } else if (i >= 60 && i <= 72) {
            assertEquals("main", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 3 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class CxxAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/c/sample.cxx";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("cxx testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    System.out.println(path);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    assertEquals(9, scopes.size());
    for (int i = 0; i < 50; ++i) {
        if (i >= 11 && i <= 15) {
            assertEquals("SomeClass", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 17 && i <= 20) {
            assertEquals("~SomeClass", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 22 && i <= 25) {
            assertEquals("MemberFunc", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 27 && i <= 29) {
            assertEquals("operator ++", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 32 && i <= 34) {
            assertEquals("TemplateMember", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 44 && i <= 46) {
            assertEquals("SomeFunc", scopes.getScope(i).getName());
            assertEquals("ns1::NamespacedClass", scopes.getScope(i).getNamespace());
        } else if (i >= 51 && i <= 54) {
            assertEquals("foo", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 59 && i <= 73) {
            assertEquals("bar", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 76 && i <= 87) {
            assertEquals("main", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 4 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class CSharpAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CSharpAnalyzerFactory.
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/csharp/Sample.cs";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("csharp testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    //TODO 5
    assertEquals(4, scopes.size());
    for (int i = 0; i < 41; ++i) {
        if (i >= 10 && i <= 10) {
            assertEquals("M1", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        } else if (i >= 12 && i <= 14) {
            assertEquals("M2", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        } else if (i >= 19 && i <= 25) {
            assertEquals("M3", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        //TODO add support for generic classes                
        //            } else if (i >= 28 && i <= 30) { 
        //                assertEquals("M4", scopes.getScope(i).name);
        //                assertEquals("MyNamespace.TopClass", scopes.getScope(i).namespace);
        } else if (i >= 34 && i <= 36) {
            assertEquals("M5", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass.InnerClass", scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 5 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class PlainAnalyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
    doc.add(new TextField(QueryBuilder.FULL, getReader(src.getStream())));
    String fullpath = doc.get(QueryBuilder.FULLPATH);
    if (fullpath != null && ctags != null) {
        defs = ctags.doCtags(fullpath + "\n");
        if (defs != null && defs.numberOfSymbols() > 0) {
            doc.add(new TextField(QueryBuilder.DEFS, new IteratorReader(defs.getSymbols())));
            //this is to explicitely use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search 
            TextField ref = new TextField(QueryBuilder.REFS, this.SymbolTokenizer);
            this.SymbolTokenizer.setReader(getReader(src.getStream()));
            doc.add(ref);
            byte[] tags = defs.serialize();
            doc.add(new StoredField(QueryBuilder.TAGS, tags));
        }
    }
    if (xrefOut != null) {
        try (Reader in = getReader(src.getStream())) {
            writeXref(in, xrefOut);
        }
        Scopes scopes = xref.getScopes();
        if (scopes.size() > 0) {
            byte[] scopesSerialized = scopes.serialize();
            doc.add(new StoredField(QueryBuilder.SCOPES, scopesSerialized));
        }
    }
}
Also used : StoredField(org.apache.lucene.document.StoredField) IteratorReader(org.opensolaris.opengrok.analysis.IteratorReader) Scopes(org.opensolaris.opengrok.analysis.Scopes) TextField(org.apache.lucene.document.TextField) ExpandTabsReader(org.opensolaris.opengrok.analysis.ExpandTabsReader) Reader(java.io.Reader) IteratorReader(org.opensolaris.opengrok.analysis.IteratorReader)

Aggregations

Scopes (org.opensolaris.opengrok.analysis.Scopes)7 Document (org.apache.lucene.document.Document)6 IndexableField (org.apache.lucene.index.IndexableField)6 File (java.io.File)5 StringWriter (java.io.StringWriter)4 Field (org.apache.lucene.document.Field)4 Test (org.junit.Test)4 Scope (org.opensolaris.opengrok.analysis.Scopes.Scope)4 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 Definitions (org.opensolaris.opengrok.analysis.Definitions)2 Genre (org.opensolaris.opengrok.analysis.FileAnalyzer.Genre)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1