Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to reduce sonar code smell fixes #4485

Merged
merged 18 commits into from
Nov 30, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
import org.opengrok.indexer.search.QueryBuilder;
import org.opengrok.indexer.util.IOUtils;
import org.opengrok.indexer.util.Statistics;
import org.opengrok.indexer.util.WrapperIOException;
import org.opengrok.indexer.web.Util;

/**
Expand Down Expand Up @@ -261,7 +262,7 @@ public class AnalyzerGuru {
new IgnorantAnalyzerFactory(),
new BZip2AnalyzerFactory(),
new XMLAnalyzerFactory(),
YamlAnalyzerFactory.DEFAULT_INSTANCE,
new YamlAnalyzerFactory(),
vladak marked this conversation as resolved.
Show resolved Hide resolved
MandocAnalyzerFactory.DEFAULT_INSTANCE,
TroffAnalyzerFactory.DEFAULT_INSTANCE,
new ELFAnalyzerFactory(),
Expand Down Expand Up @@ -552,7 +553,7 @@ public static AbstractAnalyzer getAnalyzer(String fileTypeName) {
*/
public static AbstractAnalyzer getAnalyzer(InputStream in, String file) throws IOException {
AnalyzerFactory factory = find(in, file);
if (factory == null) {
if (Objects.isNull(factory)) {
AbstractAnalyzer defaultAnalyzer = getAnalyzer();
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': fallback {1}",
Expand Down Expand Up @@ -616,7 +617,7 @@ public void populateDocument(Document doc, File file, String path, AbstractAnaly
*/
File fpath = new File(path);
String fileParent = fpath.getParent();
if (fileParent != null && fileParent.length() > 0) {
if (fileParent != null && !fileParent.isEmpty()) {
String normalizedPath = QueryBuilder.normalizeDirPath(fileParent);
StringField npstring = new StringField(QueryBuilder.DIRPATH, normalizedPath, Store.NO);
doc.add(npstring);
Expand Down Expand Up @@ -704,7 +705,7 @@ public static void writeXref(AnalyzerFactory factory, Reader in,
* @param file file object, used for logging only
* @throws java.io.IOException if an error occurs while creating the output
*/
@SuppressWarnings("java:S5443")
@SuppressWarnings({"java:S5443", "java:S107"})
public static void writeDumpedXref(String contextPath,
AnalyzerFactory factory, Reader in, Writer out,
@Nullable Definitions defs, Annotation annotation, Project project, File file) throws IOException {
Expand Down Expand Up @@ -902,46 +903,60 @@ public static AnalyzerFactory find(InputStream in, String file) throws IOExcepti
* @return the analyzer factory to use
*/
public static AnalyzerFactory find(String file) {
String path = file;
int i;
String path;
var i = file.lastIndexOf(File.separatorChar);

// Get basename of the file first.
if (((i = path.lastIndexOf(File.separatorChar)) > 0)
&& (i + 1 < path.length())) {
path = path.substring(i + 1);
if (i > 0 && (i + 1 < file.length())) {
path = file.substring(i + 1);
} else {
path = file;
}

int dotpos = path.lastIndexOf('.');
if (dotpos >= 0) {
AnalyzerFactory factory;

// Try matching the prefix.
if (dotpos > 0) {
factory = pre.get(path.substring(0, dotpos).toUpperCase(Locale.ROOT));
if (factory != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by prefix: {1}",
new Object[]{file, factory.getClass().getSimpleName()});
}
return factory;
}
}

// Now try matching the suffix. We kind of consider this order (first
var dotPos = path.lastIndexOf('.');
Optional<AnalyzerFactory> optionalFactory = Optional.empty();
if (dotPos >= 0) {
// Try matching the prefix and then by suffix.
// We kind of consider this order (first
// prefix then suffix) to be workable although for sure there can be
// cases when this does not work.
factory = ext.get(path.substring(dotpos + 1).toUpperCase(Locale.ROOT));
if (factory != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by suffix: {1}",
new Object[]{file, factory.getClass().getSimpleName()});
}
return factory;
}
optionalFactory = findByPrefix(file, path, dotPos)
.or(() -> findBySuffix(file, path, dotPos));

}

// file doesn't have any of the prefix or extensions we know, try full match
return FILE_NAMES.get(path.toUpperCase(Locale.ROOT));
return optionalFactory
.orElseGet(() -> FILE_NAMES.get(path.toUpperCase(Locale.ROOT)));
}

private static Optional<AnalyzerFactory> findBySuffix(String file, String path, int dotPos) {
var suffixFactory = Optional.of(dotPos)
.map(pos -> path.substring(dotPos + 1))
.map(suffix -> suffix.toUpperCase(Locale.ROOT))
.map(ext::get);
suffixFactory.ifPresent(factory -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by suffix: {1}",
new Object[]{file, factory.getClass().getSimpleName()});
}
});
return suffixFactory;
}

private static Optional<AnalyzerFactory> findByPrefix(String file, String path, int dotPos) {
var prefixFactory = Optional.of(dotPos)
.map(pos -> path.substring(0, dotPos))
.filter(prefix -> !prefix.isEmpty())
.map(prefix -> prefix.toUpperCase(Locale.ROOT))
.map(pre::get);
prefixFactory.ifPresent(factory -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by prefix: {1}",
new Object[]{file, factory.getClass().getSimpleName()});
}
});
return prefixFactory;
}

/**
Expand Down Expand Up @@ -982,89 +997,84 @@ private static AnalyzerFactory findForStream(InputStream in,
}
content = Arrays.copyOf(content, len);
}

AnalyzerFactory fac;

// First, do precise-magic Matcher matching
for (FileAnalyzerFactory.Matcher matcher : matchers) {
if (matcher.isPreciseMagic()) {
fac = matcher.isMagic(content, in);
if (fac != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"''{0}'': chosen by precise magic: {1}",
new Object[]{file, fac.getClass().getSimpleName()});
}
return fac;
}
}
}

// Next, look for magic strings
String opening = readOpening(in, content);
fac = findMagicString(opening, file);
if (fac != null) {
return fac;
var finalContent = Arrays.copyOf(content, len);
try {
return findFactoryUsingMatcher(finalContent, in, file, true)
.or(() -> findUsingMagicString(finalContent, in, file))
.or(() -> findFactoryUsingMatcher(finalContent, in, file, false))
.orElse(null);
} catch (WrapperIOException ex) {
throw ex.getParentIOException();
}
}

// Last, do imprecise-magic Matcher matching
for (FileAnalyzerFactory.Matcher matcher : matchers) {
if (!matcher.isPreciseMagic()) {
fac = matcher.isMagic(content, in);
if (fac != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"''{0}'': chosen by imprecise magic: {1}",
new Object[]{file, fac.getClass().getSimpleName()});
private static Optional<AnalyzerFactory> findFactoryUsingMatcher(byte[] content, InputStream in,
String file,
boolean isPrecise) {
var optionalFactory = matchers.stream()
ginoaugustine marked this conversation as resolved.
Show resolved Hide resolved
.filter(matcher -> isPrecise == matcher.isPreciseMagic())
.map(matcher -> {
try {
return matcher.isMagic(content, in);
} catch (IOException e) {
throw new WrapperIOException(e);
}
return fac;
}
})
.filter(Objects::nonNull)
.findFirst();

optionalFactory.ifPresent(factory -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"''{0}'': chosen by {1} magic: {2}",
new Object[]{file, isPrecise ? "precise" : "imprecise",
factory.getClass().getSimpleName()});
}
}
});
return optionalFactory;

return null;
}

private static AnalyzerFactory findMagicString(String opening, String file) {
private static Optional<AnalyzerFactory> findUsingMagicString(byte[] content, InputStream in, String file) {
String opening;
try {
opening = readOpening(in, content);
} catch (IOException e) {
throw new WrapperIOException(e);
}

// first, try to look up two words in magics
String fragment = getWords(opening, 2);
AnalyzerFactory fac = magics.get(fragment);
if (fac != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by magic {2}: {1}",
new Object[]{file, fac.getClass().getSimpleName(), fragment});
}
return fac;
}

// second, try to look up one word in magics
fragment = getWords(opening, 1);
fac = magics.get(fragment);
if (Objects.isNull(fac)) {
fragment = getWords(opening, 1);
fac = magics.get(fragment);
}
if (fac != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "''{0}'': chosen by magic {2}: {1}",
new Object[]{file, fac.getClass().getSimpleName(), fragment});
}
return fac;
return Optional.of(fac);
}

// try to match initial substrings (DESC strlen)
for (Map.Entry<String, AnalyzerFactory> entry :
magics.entrySet()) {
String magic = entry.getKey();
if (opening.startsWith(magic)) {
fac = entry.getValue();
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"''{0}'': chosen by magic(substr) {2}: {1}",
new Object[]{file, fac.getClass().getSimpleName(), magic});
}
return fac;
}
}
return magics.entrySet()
.stream()
.filter(entry -> opening.startsWith(entry.getKey()))
.map(entry -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"''{0}'': chosen by magic(substr) {2}: {1}",
new Object[]{file,
entry.getValue().getClass().getSimpleName(), entry.getKey()}
);
}
return entry.getValue();
})
.findFirst();

return null;
}

/**
Expand Down Expand Up @@ -1110,7 +1120,7 @@ private static String readOpening(InputStream in, byte[] sig)
in.mark(MARK_READ_LIMIT);

String encoding = IOUtils.findBOMEncoding(sig);
if (encoding == null) {
if (Objects.isNull(encoding)) {
// SRCROOT is read with UTF-8 as a default.
encoding = StandardCharsets.UTF_8.name();
} else {
ginoaugustine marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -1122,53 +1132,43 @@ private static String readOpening(InputStream in, byte[] sig)
}

int nRead = 0;
boolean sawNonWhitespace = false;
boolean lastWhitespace = false;
boolean postHashbang = false;
boolean ignoreWhiteSpace = true;
boolean breakOnNewLine = false;
int r;

StringBuilder opening = new StringBuilder();
BufferedReader readr = new BufferedReader(new InputStreamReader(in, encoding), OPENING_MAX_CHARS);

while ((r = readr.read()) != -1) {
if (++nRead > OPENING_MAX_CHARS) {
ginoaugustine marked this conversation as resolved.
Show resolved Hide resolved
break;
}
char c = (char) r;
boolean isWhitespace = Character.isWhitespace(c);
if (!sawNonWhitespace) {
if (isWhitespace) {
continue;
}
sawNonWhitespace = true;
}
if (c == '\n') {
if (isNewLineOrMaxReadLimit(c, ++nRead, breakOnNewLine)) {
ginoaugustine marked this conversation as resolved.
Show resolved Hide resolved
break;
}

boolean isWhitespace = Character.isWhitespace(c);
if (isWhitespace) {
// Track `lastWhitespace' to condense stretches of whitespace,
// and use ' ' regardless of actual whitespace character to
// accord with magic string definitions.
if (!lastWhitespace && !postHashbang) {
if (!ignoreWhiteSpace) {
opening.append(' ');
ignoreWhiteSpace = true;
}
} else {
opening.append(c);
postHashbang = false;
}
lastWhitespace = isWhitespace;
// If the opening starts with "#!", then track so that any
// trailing whitespace after the hashbang is ignored.
ignoreWhiteSpace = opening.length() == 2 && opening.charAt(0) == '#' && opening.charAt(1) == '!';
breakOnNewLine = true;

// If the opening starts with "#!", then track so that any
// trailing whitespace after the hashbang is ignored.
if (opening.length() == 2 && opening.charAt(0) == '#' && opening.charAt(1) == '!') {
postHashbang = true;
}

}

in.reset();
return opening.toString();
}

private static boolean isNewLineOrMaxReadLimit(char c, int readcount, boolean breakOnNewLine) {
return readcount > OPENING_MAX_CHARS || (breakOnNewLine && c == '\n');
}

private static void addCustomizationKey(String k) {
CUSTOMIZATION_KEYS.add(k);
Object[] keys = CUSTOMIZATION_KEYS.toArray();
Expand All @@ -1195,4 +1195,6 @@ private static boolean factoriesDifferent(AnalyzerFactory a, AnalyzerFactory b)
}
return aName == null || !aName.equals(bName);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ public int exitValue() {

@Override
public void destroy() {
//Empty Method
}
};

Expand Down
Loading
Loading