/** * Add a file to the Lucene index (and generate a xref file) * * @param file The file to add * @param path The path to the file (from source root) * @throws java.io.IOException if an error occurs */ private void addFile(File file, String path) throws IOException { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, path); for (IndexChangedListener listener : listeners) { listener.fileAdd(path, fa.getClass().getSimpleName()); } fa.setCtags(ctags); fa.setProject(Project.getProject(path)); Document d; try { d = analyzerGuru.getDocument(file, in, path, fa); } catch (Exception e) { log.log( Level.INFO, "Skipped file ''{0}'' because the analyzer didn''t " + "understand it.", path); StringBuilder stack = new StringBuilder(); for (StackTraceElement ste : e.getStackTrace()) { stack.append(ste.toString()).append(System.lineSeparator()); } StringBuilder sstack = new StringBuilder(); for (Throwable t : e.getSuppressed()) { for (StackTraceElement ste : t.getStackTrace()) { sstack.append(ste.toString()).append(System.lineSeparator()); } } log.log( Level.FINE, "Exception from analyzer {0}: {1} {2}{3}{4}{5}{6}", new String[] { fa.getClass().getName(), e.toString(), System.lineSeparator(), stack.toString(), System.lineSeparator(), sstack.toString() }); return; } writer.addDocument(d, fa); Genre g = fa.getFactory().getGenre(); if (xrefDir != null && (g == Genre.PLAIN || g == Genre.XREFABLE)) { File xrefFile = new File(xrefDir, path); // If mkdirs() returns false, the failure is most likely // because the file already exists. But to check for the // file first and only add it if it doesn't exists would // only increase the file IO... if (!xrefFile.getParentFile().mkdirs()) { assert xrefFile.getParentFile().exists(); } fa.writeXref(xrefDir, path); } setDirty(); for (IndexChangedListener listener : listeners) { listener.fileAdded(path, fa.getClass().getSimpleName()); } } }
@Override public void analyze(Document doc, InputStream in) throws IOException { if (in.read() != 'B') { throw new IOException("Not BZIP2 format"); } if (in.read() != 'Z') { throw new IOException("Not BZIP2 format"); } BufferedInputStream gzis = new BufferedInputStream(new CBZip2InputStream(in)); String path = doc.get("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); fa = AnalyzerGuru.getAnalyzer(gzis, newname); if (fa instanceof BZip2Analyzer) { fa = null; } else { if (fa.getGenre() == Genre.PLAIN || fa.getGenre() == Genre.XREFABLE) { this.g = Genre.XREFABLE; } else { this.g = Genre.DATA; } fa.analyze(doc, gzis); if (doc.get("t") != null) { doc.removeField("t"); if (g == Genre.XREFABLE) { doc.add(new Field("t", g.typeName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); } } } } }
/** * Get a writer to which the xref can be written, or null if no xref should be produced for files * of this type. */ private Writer getXrefWriter(FileAnalyzer fa, String path) throws IOException { Genre g = fa.getFactory().getGenre(); if (xrefDir != null && (g == Genre.PLAIN || g == Genre.XREFABLE)) { File xrefFile = new File(xrefDir, path); // If mkdirs() returns false, the failure is most likely // because the file already exists. But to check for the // file first and only add it if it doesn't exists would // only increase the file IO... if (!xrefFile.getParentFile().mkdirs()) { assert xrefFile.getParentFile().exists(); } RuntimeEnvironment env = RuntimeEnvironment.getInstance(); boolean compressed = env.isCompressXref(); File file = new File(xrefDir, path + (compressed ? ".gz" : "")); return new BufferedWriter( new OutputStreamWriter( compressed ? new GZIPOutputStream(new FileOutputStream(file)) : new FileOutputStream(file))); } // no Xref for this analyzer return null; }
@Override public TokenStream overridableTokenStream(String fieldName, Reader reader) { if (fa != null) { return fa.overridableTokenStream(fieldName, reader); } return super.overridableTokenStream(fieldName, reader); }
/** * Add a file to the Lucene index (and generate a xref file) * * @param file The file to add * @param path The path to the file (from source root) * @throws java.io.IOException if an error occurs */ private void addFile(File file, String path) throws IOException { FileAnalyzer fa; try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { fa = AnalyzerGuru.getAnalyzer(in, path); } for (IndexChangedListener listener : listeners) { listener.fileAdd(path, fa.getClass().getSimpleName()); } fa.setCtags(ctags); fa.setProject(Project.getProject(path)); fa.setScopesEnabled(RuntimeEnvironment.getInstance().isScopesEnabled()); fa.setFoldingEnabled(RuntimeEnvironment.getInstance().isFoldingEnabled()); Document doc = new Document(); try (Writer xrefOut = getXrefWriter(fa, path)) { analyzerGuru.populateDocument(doc, file, path, fa, xrefOut); } catch (Exception e) { LOGGER.log( Level.INFO, "Skipped file ''{0}'' because the analyzer didn''t " + "understand it.", path); LOGGER.log(Level.FINE, "Exception from analyzer " + fa.getClass().getName(), e); cleanupResources(doc); return; } try { writer.addDocument(doc); } catch (Throwable t) { cleanupResources(doc); throw t; } setDirty(); for (IndexChangedListener listener : listeners) { listener.fileAdded(path, fa.getClass().getSimpleName()); } }
/** * Write a cross referenced HTML file. * * @param out Writer to store HTML cross-reference */ @Override public void writeXref(Writer out) throws IOException { if ((fa != null) && (fa.getGenre() == Genre.PLAIN || fa.getGenre() == Genre.XREFABLE)) { fa.writeXref(out); } }