public boolean optionalAndMissingTag(Element e, ConfigTag tag, boolean optional) { boolean isMissingElement = !hasChild(e, tag); if (!optional && isMissingElement) { throw bomb("Non optional tag '" + tag + "' is not in config file. Found: " + elementOutput(e)); } return optional && isMissingElement; } }
public String getAttribute(Element e, String attribute, String defaultValue) { if (!hasAttribute(e, attribute)) { return defaultValue; } return getAttribute(e, attribute); }
public Element getChild(Element e, ConfigTag tag) { Element child = child(e, tag); if (child == null) { throw bomb("Error finding child '" + tag + "' in config: " + configFile + elementOutput(e)); } return child; }
private boolean optionalAndMissingAttribute(Element e, ConfigAttribute attribute) { boolean optional = attribute.optional(); boolean isMissingAttribute = !configUtil.hasAttribute(e, attribute.value()); if(!optional && isMissingAttribute) { throw bomb("Non optional attribute '" + attribute.value() + "' is not in element: " + configUtil.elementOutput(e)); } return optional && isMissingAttribute; }
public Object getAttribute(Element e, ConfigAttribute attribute) { if (optionalAndMissingAttribute(e, attribute)) { return null; } return getAttribute(e, attribute.value()); }
@SuppressWarnings("unchecked") private Class<T> typeToGenerate(Element e) { if (isImplicitCollection(aClass, configCache)) { return aClass; } Class<T> type = (Class<T>) findConcreteType(e, aClass); if (type == null) { bombIfNull(type, format("Unable to determine type to generate. Type: %s Element: %s", aClass.getName(), configUtil.elementOutput(e))); } return type; }
private boolean atElement() { AttributeAwareConfigTag attributeAwareConfigTag = annotationFor(aClass, AttributeAwareConfigTag.class); if (attributeAwareConfigTag != null) { final String attribute = attributeAwareConfigTag.attribute(); bombIf(isBlank(attribute), format("Type '%s' has invalid configuration for @AttributeAwareConfigTag. It must have `attribute` with non blank value.", aClass.getName())); bombIf(!hasAttribute(attribute), format("Expected attribute `%s` to be present for %s.", attribute, configUtil.elementOutput(e))); return configUtil.atTag(e, attributeAwareConfigTag.value()); } ConfigTag configTag = annotationFor(aClass, ConfigTag.class); if (configTag == null) { return false; } String tag = configTag.value(); return configUtil.atTag(e, tag); }
public Object parse() { Class<?> type = findTypeOfField(); if (type == null) { return null; } ConfigTag tag = GoConfigClassLoader.configTag(type, configCache); if (configUtil.optionalAndMissingTag(e, tag, findSubTag(field).optional())) { return null; } return GoConfigClassLoader.classParser(configUtil.getChild(e, tag), type, configCache, new GoCipher(), registry, configReferenceElements).parse(); }
public Object parse(Object defaultValue) { ConfigAttribute attribute = findAttribute(field); validateAttributeName(attribute); Object val = configUtil.getAttribute(e, attribute); if (!attribute.allowNull() && val == null && defaultValue == null) { bomb("Field '" + field.getName() + "' is still set to null. Must give a default value."); } return val; }
public void validate(Element element, ConfigElementImplementationRegistry registry) throws Exception { XPathFactory xPathFactory = XPathFactory.instance(); List<String> tasks = ConfigUtil.allTasks(registry); for (String task : tasks) { List<Element> taskNodes = xPathFactory.compile("//" + task, Filters.element()).evaluate(element); for (Element taskNode : taskNodes) { List<Element> list = xPathFactory.compile("oncancel", Filters.element()).evaluate(taskNode); if (list.size() > 1) { throw new Exception("Task [" + task + "] should not contain more than 1 oncancel task"); } } } } }
private Object parseSubtag(Element e, Field field) { Class<?> type = field.getType(); ConfigTag tag = configTag(type); if (optionalAndMissingTag(e, field)) { return null; } Element subElement = configUtil.getChild(e, tag); return parseType(subElement, type); }
public boolean hasChild(Element e, ConfigTag tag) { return child(e, tag) != null; }
public boolean optionalAndMissingAttribute(Element e, ConfigAttribute attribute) { boolean optional = attribute.optional(); boolean isMissingAttribute = !hasAttribute(e, attribute.value()); if (!optional && isMissingAttribute) { throw bomb("Non optional attribute '" + attribute.value() + "' is not in element: " + elementOutput(e)); } return optional && isMissingAttribute; }
private Class<?> typeToGenerate(Element e, Class<?> type) { if (type.isInterface() && type.isAnnotationPresent(ConfigInterface.class)) { for (Class<?> implementation : registry.implementersOf(type)) { if (configTag(implementation).equals(e.getName())) { return implementation; } } } else { if (configTag(type).equals(e.getName())) { return type; } } throw bomb("Unable to determine type to generate.\n" + "Type: " + type.getName() + "\n" + "Element: " + configUtil.elementOutput(e)); }
private String parseAttribute(Element e, Field field) { ConfigAttribute attribute = ConfigCache.annotationFor(field, ConfigAttribute.class); if (optionalAndMissingAttribute(e, attribute)) { return null; } return configUtil.getAttribute(e, attribute.value()); }
@Test public void shouldGetAllTasks() { ConfigElementImplementationRegistry registry = new ConfigElementImplementationRegistry(new NoPluginsInstalled()); registry.registerImplementer(Task.class, AntTask.class, ExecTask.class, NantTask.class, RakeTask.class, FetchTask.class, FetchPluggableArtifactTask.class); List<String> tasks = ConfigUtil.allTasks(registry); assertThat(tasks.size(), is(5)); assertThat(tasks, hasItem("ant")); assertThat(tasks, hasItem("exec")); assertThat(tasks, hasItem("nant")); assertThat(tasks, hasItem("rake")); assertThat(tasks, hasItem("fetchartifact")); } }
private boolean optionalAndMissingTag(Element e, Field field) { ConfigTag tag = configTag(field.getType()); boolean optional = field.getAnnotation(ConfigSubtag.class).optional(); boolean isMissingElement = !configUtil.hasChild(e, tag); if(!optional && isMissingElement) { throw bomb("Non optional tag '" + tag + "' is not in config file. Found: " + configUtil.elementOutput(e)); } return optional && isMissingElement; }
public String getAttribute(Element e, String attribute) { Attribute attr = e.getAttribute(attribute); if(attr == null) { throw bomb("Error finding attribute '" + attribute + "' in config: " + configFile + elementOutput(e)); } return attr.getValue(); }