Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
*/
package org.apache.logging.log4j.docgen.generator;

import static java.util.Map.entry;
import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -61,6 +67,27 @@ public final class SchemaGenerator {

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

private static final Map<String, String> XML_BUILTIN_TYPES = Map.ofEntries(
entry(BigDecimal.class.getName(), "decimal"),
entry(BigInteger.class.getName(), "integer"),
entry(boolean.class.getName(), "boolean"),
entry(Boolean.class.getName(), "boolean"),
entry(byte.class.getName(), "byte"),
entry(Byte.class.getName(), "byte"),
entry(double.class.getName(), "double"),
entry(Double.class.getName(), "double"),
entry(float.class.getName(), "float"),
entry(Float.class.getName(), "float"),
entry(int.class.getName(), "int"),
entry(Integer.class.getName(), "int"),
entry(short.class.getName(), "short"),
entry(Short.class.getName(), "short"),
entry(String.class.getName(), "string"),
entry(long.class.getName(), "long"),
entry(Long.class.getName(), "long"),
entry(URI.class.getName(), "anyURI"),
entry(URL.class.getName(), "anyURI"));

private SchemaGenerator() {}

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

private static boolean isBuiltinXmlType(final String className) {
switch (className) {
case "boolean":
case "byte":
case "double":
case "float":
case "int":
case "short":
case "long":
case "java.lang.String":
return true;
default:
return false;
}
return XML_BUILTIN_TYPES.containsKey(className);
}

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

@Nullable
private static String getXmlType(final TypeLookup lookup, final String className) {
switch (className) {
case "boolean":
case "byte":
case "double":
case "float":
case "int":
case "short":
case "long":
return className;
case "java.lang.String":
return "string";
final String builtinType = XML_BUILTIN_TYPES.get(className);
if (builtinType != null) {
return builtinType;
}
final ArtifactSourcedType type = lookup.get(className);
if (type != null) {
return LOG4J_PREFIX + ":" + className;
}
return null;
return type != null ? LOG4J_PREFIX + ":" + className : null;
}

private static void writeMultiplicity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
~ This is a test schema used in `SchemaGeneratorTest`.
~ Unlike this file the `SchemaGenerator` strips whitespace.
-->
<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">
<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">
<element type="log4j:org.apache.logging.log4j.core.config.Configuration" name="Configuration"/>
<simpleType name="org.apache.logging.log4j.Level">
<annotation>
Expand Down Expand Up @@ -500,4 +501,28 @@ A conversion pattern is composed of literal text and format control expressions
</annotation>
</attribute>
</complexType>
<complexType name="org.apache.logging.log4j.dummy.AllTypesPlugin">
<annotation>
<documentation>Dummy plugin to test all types of builtin XML attributes.</documentation>
</annotation>
<attribute name="BigInteger" type="integer"/>
<attribute name="BigDecimal" type="decimal"/>
<attribute name="boolean" type="boolean"/>
<attribute name="Boolean" type="boolean"/>
<attribute name="byte" type="byte"/>
<attribute name="Byte" type="byte"/>
<attribute name="double" type="double"/>
<attribute name="Double" type="double"/>
<attribute name="float" type="float"/>
<attribute name="Float" type="float"/>
<attribute name="int" type="int"/>
<attribute name="Integer" type="int"/>
<attribute name="long" type="long"/>
<attribute name="Long" type="long"/>
<attribute name="short" type="short"/>
<attribute name="Short" type="short"/>
<attribute name="String" type="string"/>
<attribute name="URI" type="anyURI"/>
<attribute name="URL" type="anyURI"/>
</complexType>
</schema>
25 changes: 25 additions & 0 deletions log4j-docgen/src/test/resources/SchemaGeneratorTest/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,31 @@ A conversion pattern is composed of literal text and format control expressions
</attribute>
</attributes>
</plugin>

<plugin name="AllTypes" className="org.apache.logging.log4j.dummy.AllTypesPlugin">
<description>Dummy plugin to test all types of builtin XML attributes.</description>
<attributes>
<attribute name="BigInteger" type="java.math.BigInteger"/>
<attribute name="BigDecimal" type="java.math.BigDecimal"/>
<attribute name="boolean" type="boolean"/>
<attribute name="Boolean" type="java.lang.Boolean"/>
<attribute name="byte" type="byte"/>
<attribute name="Byte" type="java.lang.Byte"/>
<attribute name="double" type="double"/>
<attribute name="Double" type="java.lang.Double"/>
<attribute name="float" type="float"/>
<attribute name="Float" type="java.lang.Float"/>
<attribute name="int" type="int"/>
<attribute name="Integer" type="java.lang.Integer"/>
<attribute name="long" type="long"/>
<attribute name="Long" type="java.lang.Long"/>
<attribute name="short" type="short"/>
<attribute name="Short" type="java.lang.Short"/>
<attribute name="String" type="java.lang.String"/>
<attribute name="URI" type="java.net.URI"/>
<attribute name="URL" type="java.net.URL"/>
</attributes>
</plugin>
</plugins>

<abstractTypes>
Expand Down
8 changes: 8 additions & 0 deletions src/changelog/.0.x.x/135_native-types.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="135" link="https://github.com/apache/logging-log4j-tools/issues/135"/>
<description format="asciidoc">Fix support of boxed and native Java types in XSD generation.</description>
</entry>