use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.
the class XmlConfiguration method constructHierarchy.
private void constructHierarchy(final Node node, final Element element) {
processAttributes(node, element);
final StringBuilder buffer = new StringBuilder();
final NodeList list = element.getChildNodes();
final List<Node> children = node.getChildren();
for (int i = 0; i < list.getLength(); i++) {
final org.w3c.dom.Node w3cNode = list.item(i);
if (w3cNode instanceof Element) {
final Element child = (Element) w3cNode;
final String name = getType(child);
final PluginType<?> type = pluginManager.getPluginType(name);
final Node childNode = new Node(node, name, type);
constructHierarchy(childNode, child);
if (type == null) {
final String value = childNode.getValue();
if (!childNode.hasChildren() && value != null) {
node.getAttributes().put(name, value);
} else {
status.add(new Status(name, element, ErrorType.CLASS_NOT_FOUND));
}
} else {
children.add(childNode);
}
} else if (w3cNode instanceof Text) {
final Text data = (Text) w3cNode;
buffer.append(data.getData());
}
}
final String text = buffer.toString().trim();
if (text.length() > 0 || (!node.hasChildren() && !node.isRoot())) {
node.setValue(text);
}
}
use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.
the class PluginElementVisitor method inject.
@Override
public void inject(final Object factory) {
final Optional<Class<?>> componentType = getComponentType(conversionType);
if (componentType.isPresent()) {
final Class<?> compType = componentType.get();
final List<Object> values = new ArrayList<>();
final Collection<Node> used = new ArrayList<>();
debugLog.append("={");
boolean first = true;
for (final Node child : node.getChildren()) {
final PluginType<?> type = child.getType();
if (name.equalsIgnoreCase(type.getElementName()) || compType.isAssignableFrom(type.getPluginClass())) {
if (!first) {
debugLog.append(", ");
}
first = false;
used.add(child);
final Object childObject = child.getObject();
if (childObject == null) {
LOGGER.warn("Skipping null object returned for element {} in node {}", child.getName(), node.getName());
} else if (childObject.getClass().isArray()) {
final Object[] children = (Object[]) childObject;
debugLog.append(Arrays.toString(children)).append('}');
node.getChildren().removeAll(used);
configurationBinder.bindObject(factory, children);
return;
} else {
debugLog.append(child.toString());
values.add(childObject);
}
}
}
debugLog.append('}');
if (!values.isEmpty() && !TypeUtil.isAssignable(compType, values.get(0).getClass())) {
LOGGER.error("Cannot assign element {} a list of {} as it is incompatible with {}", name, values.get(0).getClass(), compType);
return;
}
node.getChildren().removeAll(used);
// using List::toArray here would cause type mismatch later on
final Object[] vals = (Object[]) Array.newInstance(compType, values.size());
for (int i = 0; i < vals.length; i++) {
vals[i] = values.get(i);
}
configurationBinder.bindObject(factory, vals);
} else {
final Optional<Node> matchingChild = node.getChildren().stream().filter(this::isRequestedNode).findAny();
if (matchingChild.isPresent()) {
final Node child = matchingChild.get();
debugLog.append(child.getName()).append('(').append(child.toString()).append(')');
node.getChildren().remove(child);
configurationBinder.bindObject(factory, child.getObject());
} else {
debugLog.append(name).append("=null");
configurationBinder.bindObject(factory, null);
}
}
}
use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.
the class PluginBuilder method verifyNodeChildrenUsed.
private void verifyNodeChildrenUsed() {
final List<Node> children = node.getChildren();
if (!(pluginType.isDeferChildren() || children.isEmpty())) {
for (final Node child : children) {
final String nodeType = node.getType().getElementName();
final String start = nodeType.equals(node.getName()) ? node.getName() : nodeType + ' ' + node.getName();
LOGGER.error("{} has no parameter that matches element {}", start, child.getName());
}
}
}
use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.
the class JsonConfiguration method setup.
@Override
public void setup() {
final Iterator<Map.Entry<String, JsonNode>> iter = root.fields();
final List<Node> children = rootNode.getChildren();
while (iter.hasNext()) {
final Map.Entry<String, JsonNode> entry = iter.next();
final JsonNode n = entry.getValue();
if (n.isObject()) {
LOGGER.debug("Processing node for object {}", entry.getKey());
children.add(constructNode(entry.getKey(), rootNode, n));
} else if (n.isArray()) {
LOGGER.error("Arrays are not supported at the root configuration.");
}
}
LOGGER.debug("Completed parsing configuration");
if (status.size() > 0) {
for (final Status s : status) {
LOGGER.error("Error processing element {}: {}", s.name, s.errorType);
}
}
}
use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.
the class ValidHostValidatorTest method setUp.
@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
final PluginManager manager = new PluginManager("Test");
manager.collectPlugins();
plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
assertNotNull(plugin, "Rebuild this module to ensure annotation processing has been done.");
node = new Node(null, "HostAndPort", plugin);
}
Aggregations