use of com.cognifide.cq.cqsm.api.actions.ActionMapper in project APM by Cognifide.
the class ActionFactoryImpl method evaluate.
@Override
public ActionDescriptor evaluate(String command) throws ActionCreationException {
for (Object mapper : getMappers()) {
for (Method method : mapper.getClass().getDeclaredMethods()) {
if (!method.isAnnotationPresent(Mapping.class)) {
continue;
}
final Mapping annotation = method.getAnnotation(Mapping.class);
for (final String regex : annotation.value()) {
final Pattern pattern = Pattern.compile("^" + regex + "$");
final Matcher matcher = pattern.matcher(command);
if (matcher.matches()) {
final List<Object> args = new ArrayList<>();
final List<String> rawArgs = new ArrayList<>();
final Type[] parameterTypes = method.getGenericParameterTypes();
for (int i = 1; i <= matcher.groupCount(); i++) {
rawArgs.add(matcher.group(i));
if (mapper instanceof ActionMapper) {
args.add(((ActionMapper) mapper).mapParameter(matcher.group(i), parameterTypes[i - 1]));
} else {
args.add(matcher.group(i));
}
}
try {
return new ActionDescriptor(command, (Action) method.invoke(mapper, args.toArray()), rawArgs);
} catch (IllegalAccessException e) {
LOG.error("Cannot access action mapper method: {} while processing command: {}", e.getMessage(), command);
} catch (InvocationTargetException e) {
LOG.error("Cannot invoke action mapper method: {} while processing command: {}", e.getMessage(), command);
}
}
}
}
}
throw new ActionCreationException(String.format("Cannot find action for command: %s", command));
}
use of com.cognifide.cq.cqsm.api.actions.ActionMapper in project APM by Cognifide.
the class ActionFactoryImpl method refer.
@Override
public List<Map<String, Object>> refer() {
final List<Map<String, Object>> references = new ArrayList<>();
for (Object mapper : getMappers()) {
for (Method method : mapper.getClass().getDeclaredMethods()) {
if (!method.isAnnotationPresent(Mapping.class) || !(mapper instanceof ActionMapper)) {
continue;
}
final Mapping mapping = method.getAnnotation(Mapping.class);
final List<String> commands = ((ActionMapper) mapper).referMapping(mapping);
HashMap<String, Object> reference = new HashMap<>();
reference.put("commands", commands);
reference.put("pattern", mapping.value());
reference.put("args", mapping.args());
reference.put("reference", mapping.reference());
references.add(reference);
}
}
sortReferences(references);
return references;
}
Aggregations