Search in sources :

Example 16 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class ConfigLoader method findAllResources.

private Map<String, Resource> findAllResources(String resourceString, boolean showLoadMessages, boolean areOptional, boolean loadingDefaultResources) {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(this.getClass().getClassLoader());
    Map<String, Resource> resources = new TreeMap<>();
    try {
        Resource[] resourceArray = resolver.getResources(resourceString);
        if (!loadingDefaultResources && showLoadMessages) {
            LOG.info("Loading {} rule files using expression: {}", resourceArray.length, resourceString);
        }
        for (Resource resource : resourceArray) {
            if (!keepTests && isTestRulesOnlyFile(resource.getFilename())) {
                if (showLoadMessages) {
                    LOG.info("- Skipping tests only file {} ({} bytes)", resource.getFilename(), resource.contentLength());
                }
                continue;
            }
            if (!loadingDefaultResources && showLoadMessages) {
                LOG.info("- Preparing {} ({} bytes)", resource.getFilename(), resource.contentLength());
            }
            resources.put(resource.getFilename(), resource);
        }
    } catch (IOException e) {
        if (areOptional) {
            LOG.error("The specified (optional) resource string is invalid: {}", resourceString);
            return Collections.emptyMap();
        } else {
            throw new InvalidParserConfigurationException("Error reading resources: " + e.getMessage(), e);
        }
    }
    return resources;
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Resource(org.springframework.core.io.Resource) YamlUtils.getValueAsString(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString) YamlUtils.getKeyAsString(nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) TreeMap(java.util.TreeMap)

Example 17 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class UserAgentAnnotationAnalyzer method initialize.

public void initialize(UserAgentAnnotationMapper<T> theMapper) {
    mapper = theMapper;
    if (mapper == null) {
        throw new InvalidParserConfigurationException("[Initialize] The mapper instance is null.");
    }
    Class<?>[] classOfTArray = GenericTypeResolver.resolveTypeArguments(mapper.getClass(), UserAgentAnnotationMapper.class);
    if (classOfTArray == null) {
        throw new InvalidParserConfigurationException("Couldn't find the used generic type of the UserAgentAnnotationMapper.");
    }
    Class<?> classOfT = classOfTArray[0];
    // Get all methods of the correct signature that have been annotated with YauaaField
    for (final Method method : mapper.getClass().getDeclaredMethods()) {
        final YauaaField field = method.getAnnotation(YauaaField.class);
        if (field != null) {
            final Class<?> returnType = method.getReturnType();
            final Class<?>[] parameters = method.getParameterTypes();
            if (returnType.getCanonicalName().equals("void") && parameters.length == 2 && parameters[0] == classOfT && parameters[1] == String.class) {
                if (!Modifier.isPublic(classOfT.getModifiers())) {
                    throw new InvalidParserConfigurationException("The class " + classOfT.getCanonicalName() + " is not public.");
                }
                if (!Modifier.isPublic(method.getModifiers())) {
                    throw new InvalidParserConfigurationException("Method annotated with YauaaField is not public: " + method.getName());
                }
                if (method.getDeclaringClass().isAnonymousClass()) {
                    String methodName = method.getReturnType().getName() + " " + method.getName() + "(" + parameters[0].getSimpleName() + " ," + parameters[1].getSimpleName() + ");";
                    LOG.warn("Trying to make anonymous {} {} accessible.", method.getDeclaringClass(), methodName);
                    method.setAccessible(true);
                }
                for (String fieldName : field.value()) {
                    List<Method> methods = fieldSetters.computeIfAbsent(fieldName, k -> new ArrayList<>());
                    methods.add(method);
                }
            } else {
                throw new InvalidParserConfigurationException("In class [" + method.getDeclaringClass() + "] the method [" + method.getName() + "] " + "has been annotated with YauaaField but it has the wrong method signature. " + "It must look like [ public void " + method.getName() + "(" + classOfT.getSimpleName() + " record, String value) ]");
            }
        }
    }
    if (fieldSetters.isEmpty()) {
        throw new InvalidParserConfigurationException("You MUST specify at least 1 field to extract.");
    }
    userAgentAnalyzer = UserAgentAnalyzer.newBuilder().hideMatcherLoadStats().withCache(cacheSize).withFields(fieldSetters.keySet()).dropTests().immediateInitialization().build();
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Method(java.lang.reflect.Method)

Example 18 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException 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)

Example 19 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class ConfigLoader method loadYamlLookup.

private void loadYamlLookup(MappingNode entry, String filename) {
    String name = null;
    Map<String, String> map = new HashMap<>();
    Set<String> merge = new LinkedHashSet<>();
    for (NodeTuple tuple : entry.getValue()) {
        switch(getKeyAsString(tuple, filename)) {
            case "name":
                name = getValueAsString(tuple, filename);
                break;
            case "merge":
                merge.addAll(getStringValues(getValueAsSequenceNode(tuple, filename), filename));
                break;
            case "map":
                List<NodeTuple> mappings = getValueAsMappingNode(tuple, filename).getValue();
                for (NodeTuple mapping : mappings) {
                    String key = getKeyAsString(mapping, filename);
                    String value = getValueAsString(mapping, filename);
                    if (map.containsKey(key)) {
                        throw new InvalidParserConfigurationException("In the lookup \"" + name + "\" the key \"" + key + "\" appears multiple times.");
                    }
                    map.put(key, value);
                }
                break;
            default:
                break;
        }
    }
    require(name != null && (!map.isEmpty() || !merge.isEmpty()), entry, filename, "Invalid lookup specified");
    if (!merge.isEmpty()) {
        analyzerConfig.putLookupMerges(name, merge);
    }
    analyzerConfig.putLookup(name, map);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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)

Example 20 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class AbstractUserAgentAnalyzerDirect method finalizeLoadingRules.

protected void finalizeLoadingRules() {
    logVersion();
    flattener = new UserAgentTreeFlattener(this);
    if (wantedFieldNames != null) {
        int wantedSize = wantedFieldNames.size();
        if (wantedFieldNames.contains(SET_ALL_FIELDS)) {
            wantedSize--;
        }
        LOG.info("Building all needed matchers for the requested {} fields.", wantedSize);
    } else {
        LOG.info("Building all matchers for all possible fields.");
    }
    Map<String, MatcherConfig> matcherConfigs = config.getMatcherConfigs();
    if (matcherConfigs.isEmpty()) {
        throw new InvalidParserConfigurationException("No matchers were loaded at all.");
    }
    allMatchers.clear();
    for (Map.Entry<String, MatcherConfig> matcherConfigEntry : matcherConfigs.entrySet()) {
        MatcherConfig matcherConfig = matcherConfigEntry.getValue();
        try {
            allMatchers.add(new Matcher(this, wantedFieldNames, matcherConfig));
        } catch (UselessMatcherException ume) {
        // skippedMatchers++;
        }
    }
    verifyWeAreNotAskingForImpossibleFields();
    if (!delayInitialization) {
        initializeMatchers();
    }
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Matcher(nl.basjes.parse.useragent.analyze.Matcher) UserAgentTreeFlattener(nl.basjes.parse.useragent.parse.UserAgentTreeFlattener) MatcherConfig(nl.basjes.parse.useragent.config.MatcherConfig) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UselessMatcherException(nl.basjes.parse.useragent.analyze.UselessMatcherException)

Aggregations

InvalidParserConfigurationException (nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException)35 Test (org.junit.jupiter.api.Test)24 YamlUtils.getKeyAsString (nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString)5 YamlUtils.getValueAsString (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString)5 YamlUtils.getExactlyOneNodeTuple (nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple)4 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Matcher (nl.basjes.parse.useragent.analyze.Matcher)2 ConfigLine (nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine)2 YamlUtils.getValueAsMappingNode (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsMappingNode)2 YamlUtils.getValueAsSequenceNode (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsSequenceNode)2 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)2 Node (org.yaml.snakeyaml.nodes.Node)2 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)2