Skip to content

Commit bcb423c

Browse files
authored
log4j-docgen: Support boxed and native Java types in XSD generation (#190)
Previously, `SchemaGenerator` did not handle configuration attributes with boxed types (e.g., `Integer`, `Boolean`), leading to their omission from the generated XSD schema. This update introduces: * Support for boxed Java types as configuration attributes. * Improved handling of other native Java types that map to XML built-in data types (e.g., `BigDecimal`, `URL`). These enhancements ensure that all relevant configuration attributes are accurately represented in the schema. Fixes: #135
1 parent 4fc3e3f commit bcb423c

File tree

4 files changed

+91
-29
lines changed

4 files changed

+91
-29
lines changed

log4j-docgen/src/main/java/org/apache/logging/log4j/docgen/generator/SchemaGenerator.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
*/
1717
package org.apache.logging.log4j.docgen.generator;
1818

19+
import static java.util.Map.entry;
1920
import static java.util.Objects.requireNonNull;
2021

2122
import java.io.IOException;
2223
import java.io.OutputStream;
24+
import java.math.BigDecimal;
25+
import java.math.BigInteger;
26+
import java.net.URI;
27+
import java.net.URL;
2328
import java.nio.file.Files;
2429
import java.nio.file.Path;
2530
import java.util.ArrayList;
2631
import java.util.Collection;
2732
import java.util.List;
33+
import java.util.Map;
2834
import java.util.Set;
2935
import java.util.stream.Collectors;
3036
import java.util.stream.Stream;
@@ -61,6 +67,27 @@ public final class SchemaGenerator {
6167

6268
private static final String CHARSET_NAME = "UTF-8";
6369

70+
private static final Map<String, String> XML_BUILTIN_TYPES = Map.ofEntries(
71+
entry(BigDecimal.class.getName(), "decimal"),
72+
entry(BigInteger.class.getName(), "integer"),
73+
entry(boolean.class.getName(), "boolean"),
74+
entry(Boolean.class.getName(), "boolean"),
75+
entry(byte.class.getName(), "byte"),
76+
entry(Byte.class.getName(), "byte"),
77+
entry(double.class.getName(), "double"),
78+
entry(Double.class.getName(), "double"),
79+
entry(float.class.getName(), "float"),
80+
entry(Float.class.getName(), "float"),
81+
entry(int.class.getName(), "int"),
82+
entry(Integer.class.getName(), "int"),
83+
entry(short.class.getName(), "short"),
84+
entry(Short.class.getName(), "short"),
85+
entry(String.class.getName(), "string"),
86+
entry(long.class.getName(), "long"),
87+
entry(Long.class.getName(), "long"),
88+
entry(URI.class.getName(), "anyURI"),
89+
entry(URL.class.getName(), "anyURI"));
90+
6491
private SchemaGenerator() {}
6592

6693
public static void generateSchema(final SchemaGeneratorArgs args) throws XMLStreamException {
@@ -137,19 +164,7 @@ private static void writeTypes(final TypeLookup lookup, final XMLStreamWriter wr
137164
}
138165

139166
private static boolean isBuiltinXmlType(final String className) {
140-
switch (className) {
141-
case "boolean":
142-
case "byte":
143-
case "double":
144-
case "float":
145-
case "int":
146-
case "short":
147-
case "long":
148-
case "java.lang.String":
149-
return true;
150-
default:
151-
return false;
152-
}
167+
return XML_BUILTIN_TYPES.containsKey(className);
153168
}
154169

155170
private static void writeScalarType(final ScalarType type, final XMLStreamWriter writer) throws XMLStreamException {
@@ -304,23 +319,12 @@ private static void writePluginAttribute(
304319

305320
@Nullable
306321
private static String getXmlType(final TypeLookup lookup, final String className) {
307-
switch (className) {
308-
case "boolean":
309-
case "byte":
310-
case "double":
311-
case "float":
312-
case "int":
313-
case "short":
314-
case "long":
315-
return className;
316-
case "java.lang.String":
317-
return "string";
322+
final String builtinType = XML_BUILTIN_TYPES.get(className);
323+
if (builtinType != null) {
324+
return builtinType;
318325
}
319326
final ArtifactSourcedType type = lookup.get(className);
320-
if (type != null) {
321-
return LOG4J_PREFIX + ":" + className;
322-
}
323-
return null;
327+
return type != null ? LOG4J_PREFIX + ":" + className : null;
324328
}
325329

326330
private static void writeMultiplicity(

log4j-docgen/src/test/resources/SchemaGeneratorTest/expected-plugins.xsd

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
~ This is a test schema used in `SchemaGeneratorTest`.
2020
~ Unlike this file the `SchemaGenerator` strips whitespace.
2121
-->
22-
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:log4j="https://logging.apache.org/xml/ns" elementFormDefault="qualified" targetNamespace="https://logging.apache.org/xml/ns" version="1.2.3">
22+
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:log4j="https://logging.apache.org/xml/ns"
23+
elementFormDefault="qualified" targetNamespace="https://logging.apache.org/xml/ns" version="1.2.3">
2324
<element type="log4j:org.apache.logging.log4j.core.config.Configuration" name="Configuration"/>
2425
<simpleType name="org.apache.logging.log4j.Level">
2526
<annotation>
@@ -500,4 +501,28 @@ A conversion pattern is composed of literal text and format control expressions
500501
</annotation>
501502
</attribute>
502503
</complexType>
504+
<complexType name="org.apache.logging.log4j.dummy.AllTypesPlugin">
505+
<annotation>
506+
<documentation>Dummy plugin to test all types of builtin XML attributes.</documentation>
507+
</annotation>
508+
<attribute name="BigInteger" type="integer"/>
509+
<attribute name="BigDecimal" type="decimal"/>
510+
<attribute name="boolean" type="boolean"/>
511+
<attribute name="Boolean" type="boolean"/>
512+
<attribute name="byte" type="byte"/>
513+
<attribute name="Byte" type="byte"/>
514+
<attribute name="double" type="double"/>
515+
<attribute name="Double" type="double"/>
516+
<attribute name="float" type="float"/>
517+
<attribute name="Float" type="float"/>
518+
<attribute name="int" type="int"/>
519+
<attribute name="Integer" type="int"/>
520+
<attribute name="long" type="long"/>
521+
<attribute name="Long" type="long"/>
522+
<attribute name="short" type="short"/>
523+
<attribute name="Short" type="short"/>
524+
<attribute name="String" type="string"/>
525+
<attribute name="URI" type="anyURI"/>
526+
<attribute name="URL" type="anyURI"/>
527+
</complexType>
503528
</schema>

log4j-docgen/src/test/resources/SchemaGeneratorTest/plugins.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ A conversion pattern is composed of literal text and format control expressions
267267
</attribute>
268268
</attributes>
269269
</plugin>
270+
271+
<plugin name="AllTypes" className="org.apache.logging.log4j.dummy.AllTypesPlugin">
272+
<description>Dummy plugin to test all types of builtin XML attributes.</description>
273+
<attributes>
274+
<attribute name="BigInteger" type="java.math.BigInteger"/>
275+
<attribute name="BigDecimal" type="java.math.BigDecimal"/>
276+
<attribute name="boolean" type="boolean"/>
277+
<attribute name="Boolean" type="java.lang.Boolean"/>
278+
<attribute name="byte" type="byte"/>
279+
<attribute name="Byte" type="java.lang.Byte"/>
280+
<attribute name="double" type="double"/>
281+
<attribute name="Double" type="java.lang.Double"/>
282+
<attribute name="float" type="float"/>
283+
<attribute name="Float" type="java.lang.Float"/>
284+
<attribute name="int" type="int"/>
285+
<attribute name="Integer" type="java.lang.Integer"/>
286+
<attribute name="long" type="long"/>
287+
<attribute name="Long" type="java.lang.Long"/>
288+
<attribute name="short" type="short"/>
289+
<attribute name="Short" type="java.lang.Short"/>
290+
<attribute name="String" type="java.lang.String"/>
291+
<attribute name="URI" type="java.net.URI"/>
292+
<attribute name="URL" type="java.net.URL"/>
293+
</attributes>
294+
</plugin>
270295
</plugins>
271296

272297
<abstractTypes>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="135" link="https://github.com/apache/logging-log4j-tools/issues/135"/>
7+
<description format="asciidoc">Fix support of boxed and native Java types in XSD generation.</description>
8+
</entry>

0 commit comments

Comments
 (0)