|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.converter.config.internal; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.TreeMap; |
| 25 | +import java.util.function.Supplier; |
| 26 | +import org.apache.logging.converter.config.ConfigurationConverterException; |
| 27 | +import org.apache.logging.converter.config.spi.ConfigurationNode; |
| 28 | +import org.jspecify.annotations.Nullable; |
| 29 | + |
| 30 | +public final class ComponentUtils { |
| 31 | + |
| 32 | + public static ConfigurationNodeBuilder newNodeBuilder() { |
| 33 | + return new ConfigurationNodeBuilder(); |
| 34 | + } |
| 35 | + |
| 36 | + public static ConfigurationNode createThresholdFilter(String level) { |
| 37 | + return newNodeBuilder() |
| 38 | + .setPluginName("ThresholdFilter") |
| 39 | + .addAttribute("level", level) |
| 40 | + .build(); |
| 41 | + } |
| 42 | + |
| 43 | + public static ConfigurationNode createCompositeFilter(Iterable<? extends ConfigurationNode> filters) { |
| 44 | + ConfigurationNodeBuilder builder = newNodeBuilder().setPluginName("Filters"); |
| 45 | + filters.forEach(builder::addChild); |
| 46 | + return builder.build(); |
| 47 | + } |
| 48 | + |
| 49 | + private ComponentUtils() {} |
| 50 | + |
| 51 | + public static class ConfigurationNodeBuilder implements Supplier<ConfigurationNode> { |
| 52 | + |
| 53 | + private @Nullable String pluginName; |
| 54 | + private final Map<String, String> attributes = new TreeMap<>(); |
| 55 | + private final List<ConfigurationNode> children = new ArrayList<>(); |
| 56 | + |
| 57 | + protected ConfigurationNodeBuilder() {} |
| 58 | + |
| 59 | + public ConfigurationNodeBuilder setPluginName(String pluginName) { |
| 60 | + this.pluginName = pluginName; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public ConfigurationNodeBuilder addAttribute(String key, @Nullable String value) { |
| 65 | + if (value != null) { |
| 66 | + attributes.put(key, value); |
| 67 | + } |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public ConfigurationNodeBuilder addAttribute(String key, boolean value) { |
| 72 | + attributes.put(key, String.valueOf(value)); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public ConfigurationNodeBuilder addChild(ConfigurationNode child) { |
| 77 | + children.add(child); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public ConfigurationNode build() { |
| 82 | + if (pluginName == null) { |
| 83 | + throw new ConfigurationConverterException("No plugin name specified"); |
| 84 | + } |
| 85 | + return new ConfigurationNodeImpl(pluginName, attributes, children); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ConfigurationNode get() { |
| 90 | + return build(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static final class ConfigurationNodeImpl implements ConfigurationNode { |
| 95 | + |
| 96 | + private final String pluginName; |
| 97 | + private final Map<String, String> attributes; |
| 98 | + private final List<ConfigurationNode> children; |
| 99 | + |
| 100 | + private ConfigurationNodeImpl( |
| 101 | + final String pluginName, |
| 102 | + final Map<String, String> attributes, |
| 103 | + final Collection<ConfigurationNode> children) { |
| 104 | + this.pluginName = pluginName; |
| 105 | + this.attributes = Collections.unmodifiableMap(new TreeMap<>(attributes)); |
| 106 | + this.children = Collections.unmodifiableList(new ArrayList<>(children)); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String getPluginName() { |
| 111 | + return pluginName; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Map<String, String> getAttributes() { |
| 116 | + return attributes; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public List<? extends ConfigurationNode> getChildren() { |
| 121 | + return children; |
| 122 | + } |
| 123 | + |
| 124 | + private static void formatTo(ConfigurationNode node, StringBuilder builder, int indent) { |
| 125 | + String indentation = getIndentation(indent); |
| 126 | + builder.append(indentation).append("<").append(node.getPluginName()); |
| 127 | + for (final Map.Entry<String, String> entry : node.getAttributes().entrySet()) { |
| 128 | + builder.append(" ") |
| 129 | + .append(entry.getKey()) |
| 130 | + .append("=\"") |
| 131 | + .append(entry.getValue()) |
| 132 | + .append("\""); |
| 133 | + } |
| 134 | + builder.append(">\n"); |
| 135 | + for (ConfigurationNode child : node.getChildren()) { |
| 136 | + formatTo(child, builder, indent + 1); |
| 137 | + builder.append('\n'); |
| 138 | + } |
| 139 | + builder.append(indentation) |
| 140 | + .append("</") |
| 141 | + .append(node.getPluginName()) |
| 142 | + .append(">"); |
| 143 | + } |
| 144 | + |
| 145 | + private static String getIndentation(int indent) { |
| 146 | + return String.join("", Collections.nCopies(indent, " ")); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String toString() { |
| 151 | + StringBuilder builder = new StringBuilder(); |
| 152 | + formatTo(this, builder, 0); |
| 153 | + return builder.toString(); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments