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)");
}
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");
}
}
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));
}
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
}
}
}
}
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));
}
}
}
Aggregations