<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ Copyright The WildFly Authors
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:jboss:domain:logging:1.1"
            xmlns="urn:jboss:domain:logging:1.1"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified"
            version="1.1">

    <!-- The logging subsystem root element -->
    <xs:element name="subsystem" type="subsystem"/>

    <xs:complexType name="subsystem">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                The configuration of the logging subsystem.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="logger" type="loggerType"/>
            <xs:element name="root-logger" type="rootLoggerType"/>
            <xs:element name="console-handler" type="consoleHandlerType"/>
            <xs:element name="file-handler" type="fileHandlerType"/>
            <xs:element name="periodic-rotating-file-handler" type="periodicFileHandlerType"/>
            <xs:element name="size-rotating-file-handler" type="sizeFileHandlerType"/>
            <xs:element name="async-handler" type="asyncHandlerType"/>
            <xs:element name="custom-handler" type="customHandlerType" />
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="propertiesType">
        <xs:annotation>
            <xs:documentation>
                A collection of free-form properties.
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="property">
                <xs:complexType>
                    <xs:attribute name="name" type="xs:string" use="required"/>
                    <xs:attribute name="value" type="xs:string" use="optional"/>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="refType">
        <xs:annotation>
            <xs:documentation>
                A named reference to another object.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="handlersType">
        <xs:annotation>
            <xs:documentation>
                A collection of handlers to apply to the enclosing object.
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="handler" type="refType"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="rootLoggerType">
        <xs:annotation>
            <xs:documentation>
                Defines the root logger for this log context.
            </xs:documentation>
        </xs:annotation>
        <xs:all minOccurs="1" maxOccurs="1">
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="handlers" type="handlersType" minOccurs="0"/>
        </xs:all>
    </xs:complexType>

    <xs:complexType name="loggerType">
        <xs:annotation>
            <xs:documentation>
                Defines a logger category.
            </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
            <xs:extension base="rootLoggerType">
                <xs:attribute name="use-parent-handlers" type="xs:boolean" use="optional" default="true"/>
                <xs:attribute name="category" type="xs:string" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="consoleHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a handler which writes to the console.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="encoding" type="valueType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="formatter" type="formatterType" minOccurs="0"/>
            <xs:element name="target" minOccurs="0">
                <xs:complexType>
                    <xs:attribute name="name" use="required">
                        <xs:simpleType>
                            <xs:restriction base="xs:token">
                                <xs:enumeration value="System.out"/>
                                <xs:enumeration value="System.err"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                </xs:complexType>
            </xs:element>
        </xs:all>
        <xs:attribute name="autoflush" type="xs:boolean" use="optional" default="true"/>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="fileHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a handler which writes to a file.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="encoding" type="valueType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="formatter" type="formatterType" minOccurs="0"/>
            <xs:element name="file" type="pathType" minOccurs="1"/>
            <xs:element name="append" type="booleanValueType" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="autoflush" type="xs:boolean" use="optional" default="true"/>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="periodicFileHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a handler which writes to a file, rotating the log after a time period derived from the given
                suffix string, which should be in a format understood by java.text.SimpleDateFormat.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="encoding" type="valueType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="formatter" type="formatterType" minOccurs="0"/>
            <xs:element name="file" type="pathType"/>
            <xs:element name="suffix" type="valueType"/>
            <xs:element name="append" type="booleanValueType" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="autoflush" type="xs:boolean" use="optional" default="true"/>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

        <xs:complexType name="sizeFileHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a handler which writes to a file, rotating the log after the size of the file grows beyond a
                certain point and keeping a fixed number of backups.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="encoding" type="valueType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="formatter" type="formatterType" minOccurs="0"/>
            <xs:element name="file" type="pathType"/>
            <xs:element name="rotate-size" type="sizeType" minOccurs="0"/>
            <xs:element name="max-backup-index" type="positiveIntType" minOccurs="0"/>
            <xs:element name="append" type="booleanValueType" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="autoflush" type="xs:boolean" use="optional" default="true"/>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="asyncHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a handler which writes to the sub-handlers in an asynchronous thread.  Used for handlers which
                introduce a substantial amount of lag.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="queue-length" type="queueLengthType" minOccurs="1" maxOccurs="1"/>
            <xs:element name="overflow-action" type="overflowActionType" minOccurs="0"/>
            <xs:element name="subhandlers" type="handlersType"/>
        </xs:all>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="customHandlerType">
        <xs:annotation>
            <xs:documentation>
                Defines a custom handler.
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="level" type="refType" minOccurs="0"/>
            <xs:element name="encoding" type="valueType" minOccurs="0"/>
            <xs:element name="filter" type="filterType" minOccurs="0"/>
            <xs:element name="formatter" type="formatterType" minOccurs="0"/>
            <xs:element name="properties" type="propertiesType" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="module" type="xs:string" use="required"/>
        <xs:attribute name="class" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="queueLengthType">
        <xs:attribute name="value" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:positiveInteger">
                    <xs:minExclusive value="1"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="overflowActionType">
        <xs:attribute name="value" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:token">
                    <xs:enumeration value="block"/>
                    <xs:enumeration value="discard"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="positiveIntType">
        <xs:attribute name="value" use="required" type="xs:positiveInteger"/>
    </xs:complexType>

    <xs:complexType name="booleanValueType">
        <xs:attribute name="value" use="required" type="xs:boolean"/>
    </xs:complexType>

    <xs:complexType name="valueType">
        <xs:attribute name="value" use="required" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="pathType">
        <xs:attribute name="relative-to" use="optional" type="xs:string"/>
        <xs:attribute name="path" use="required" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="sizeType">
        <xs:attribute name="value">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <!-- XSD doesn't allow ^ or $ so ^[0-9]+[bkmgtp]?$ is invalid -->
                    <xs:pattern value="[0-9]+[bkmgtp]"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="filterType">
        <xs:annotation>
            <xs:documentation>
                Defines a simple filter type.
            </xs:documentation>
        </xs:annotation>
        <xs:group ref="simpleFilterGroup"/>
    </xs:complexType>

    <xs:complexType name="multiFilterType">
        <xs:annotation>
            <xs:documentation>
                Defines a composite filter type.  The "any" filter will return true of any of its constituent filters
                returns true; the "all" filter will return false if any of its constituent filters returns false.  Both
                composite filter types are short-circuiting, meaning that if the result can be determined with an earlier
                filter, later filters are not run.
            </xs:documentation>
        </xs:annotation>
        <xs:group ref="simpleFilterGroup" maxOccurs="unbounded"/>
    </xs:complexType>

    <xs:group name="simpleFilterGroup">
        <xs:choice>
            <xs:element name="all" type="multiFilterType"/>
            <xs:element name="any" type="multiFilterType"/>
            <xs:element name="accept"/>
            <xs:element name="deny"/>
            <xs:element name="not" type="filterType"/>
            <xs:element name="match" type="regexFilterType"/>
            <xs:element name="replace" type="replaceFilterType"/>
            <xs:element name="level" type="levelFilterType"/>
            <xs:element name="level-range" type="levelRangeFilterType"/>
            <xs:element name="change-level" type="levelChangeFilterType"/>
        </xs:choice>
    </xs:group>

    <xs:complexType name="regexFilterType">
        <xs:annotation>
            <xs:documentation>
                A regular expression-based filter.  The filter returns true if the pattern matches.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="pattern" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="replaceFilterType">
        <xs:annotation>
            <xs:documentation>
                A regular expression substitution filter.  This filter modifies the log message and always returns true.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="pattern" type="xs:string" use="required"/>
        <xs:attribute name="replacement" type="xs:string" use="required"/>
        <xs:attribute name="replace-all" type="xs:boolean" default="true"/>
    </xs:complexType>

    <xs:complexType name="levelFilterType">
        <xs:annotation>
            <xs:documentation>
                A level filter.  This filter returns true if the log message level matches the parameter.  It is a
                numerical match; two differently-named levels with the same numeric value will be considered equal.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="levelRangeFilterType">
        <xs:annotation>
            <xs:documentation>
                A level range filter.  This filter returns true if the log message level matches the range specified
                by the parameters.  It is a
                numerical match; two differently-named levels with the same numeric value will be considered equal.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="min-level" type="xs:string" use="required"/>
        <xs:attribute name="min-inclusive" type="xs:boolean" use="optional" default="true"/>
        <xs:attribute name="max-level" type="xs:string" use="required"/>
        <xs:attribute name="max-inclusive" type="xs:boolean" use="optional" default="true"/>
    </xs:complexType>

    <xs:complexType name="levelChangeFilterType">
        <xs:annotation>
            <xs:documentation>
                A level change filter.  This filter modifies the log message and always returns true.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="new-level" type="xs:string" use="required"/>
    </xs:complexType>

    <!-- Formatters -->

    <xs:complexType name="formatterType">
        <xs:annotation>
            <xs:documentation>
                Defines a formatter.
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element name="pattern-formatter" type="patternFormatterType" maxOccurs="1"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="patternFormatterType">
        <xs:annotation>
            <xs:documentation>
                Defines a pattern formatter.  See the documentation for org.jboss.logmanager.formatters.FormatStringParser
                for more information about the format string.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="pattern" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>