Search in sources :

Example 1 with ConfigLine

use of nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine in project yauaa by nielsbasjes.

the class ConfigLoader method loadYamlMatcher.

private void loadYamlMatcher(MappingNode entry, String filename) {
    int matcherSourceLineNumber = entry.getStartMark().getLine();
    String matcherSourceLocation = filename + ':' + matcherSourceLineNumber;
    // List of 'attribute', 'confidence', 'expression'
    List<ConfigLine> configLines = new ArrayList<>(16);
    List<String> options = null;
    for (NodeTuple nodeTuple : entry.getValue()) {
        String name = getKeyAsString(nodeTuple, matcherSourceLocation);
        switch(name) {
            case "options":
                options = YamlUtils.getStringValues(nodeTuple.getValueNode(), matcherSourceLocation);
                break;
            case "variable":
                for (String variableConfig : YamlUtils.getStringValues(nodeTuple.getValueNode(), matcherSourceLocation)) {
                    String[] configParts = variableConfig.split(":", 2);
                    if (configParts.length != 2) {
                        throw new InvalidParserConfigurationException("Invalid variable config line: " + variableConfig);
                    }
                    String variableName = configParts[0].trim();
                    String config = configParts[1].trim();
                    configLines.add(new ConfigLine(VARIABLE, variableName, null, config));
                }
                break;
            case "require":
                for (String requireConfig : YamlUtils.getStringValues(nodeTuple.getValueNode(), matcherSourceLocation)) {
                    requireConfig = requireConfig.trim();
                    if (requireConfig.startsWith("IsNull[")) {
                        // FIXME: Nasty String manipulation code: Cleanup
                        String failIfFoundConfig = requireConfig.replaceAll("^IsNull\\[", "").replaceAll("]$", "");
                        configLines.add(new ConfigLine(FAIL_IF_FOUND, null, null, failIfFoundConfig));
                    } else {
                        configLines.add(new ConfigLine(REQUIRE, null, null, requireConfig));
                    }
                }
                break;
            case "extract":
                for (String extractConfig : YamlUtils.getStringValues(nodeTuple.getValueNode(), matcherSourceLocation)) {
                    String[] configParts = extractConfig.split(":", 3);
                    if (configParts.length != 3) {
                        throw new InvalidParserConfigurationException("Invalid extract config line: " + extractConfig);
                    }
                    String attribute = configParts[0].trim();
                    Long confidence = Long.parseLong(configParts[1].trim());
                    String config = configParts[2].trim();
                    configLines.add(new ConfigLine(EXTRACT, attribute, confidence, config));
                }
                break;
            default:
        }
    }
    analyzerConfig.addMatcherConfigs(matcherSourceLocation, new MatcherConfig(filename, matcherSourceLineNumber, options, configLines));
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) ConfigLine(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine) ArrayList(java.util.ArrayList) YamlUtils.getValueAsString(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString) YamlUtils.getKeyAsString(nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString) YamlUtils.getExactlyOneNodeTuple(nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Aggregations

ArrayList (java.util.ArrayList)1 InvalidParserConfigurationException (nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException)1 ConfigLine (nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine)1 YamlUtils.getExactlyOneNodeTuple (nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple)1 YamlUtils.getKeyAsString (nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString)1 YamlUtils.getValueAsString (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1