Class ConfigurationLoader


  • public final class ConfigurationLoader
    extends java.lang.Object
    Loads a configuration from a standard configuration XML file.
    • Field Detail

      • DTD_PUBLIC_ID_1_0

        private static final java.lang.String DTD_PUBLIC_ID_1_0
        The public ID for version 1_0 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_RESOURCE_NAME_1_0

        private static final java.lang.String DTD_RESOURCE_NAME_1_0
        The resource for version 1_0 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_PUBLIC_ID_1_1

        private static final java.lang.String DTD_PUBLIC_ID_1_1
        The public ID for version 1_1 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_RESOURCE_NAME_1_1

        private static final java.lang.String DTD_RESOURCE_NAME_1_1
        The resource for version 1_1 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_PUBLIC_ID_1_2

        private static final java.lang.String DTD_PUBLIC_ID_1_2
        The public ID for version 1_2 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_RESOURCE_NAME_1_2

        private static final java.lang.String DTD_RESOURCE_NAME_1_2
        The resource for version 1_2 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_PUBLIC_ID_1_3

        private static final java.lang.String DTD_PUBLIC_ID_1_3
        The public ID for version 1_3 of the configuration dtd.
        See Also:
        Constant Field Values
      • DTD_RESOURCE_NAME_1_3

        private static final java.lang.String DTD_RESOURCE_NAME_1_3
        The resource for version 1_3 of the configuration dtd.
        See Also:
        Constant Field Values
      • UNABLE_TO_PARSE_EXCEPTION_PREFIX

        private static final java.lang.String UNABLE_TO_PARSE_EXCEPTION_PREFIX
        Prefix for the exception when unable to parse resource.
        See Also:
        Constant Field Values
      • DOLLAR_SIGN

        private static final char DOLLAR_SIGN
        Dollar sign literal.
        See Also:
        Constant Field Values
      • overridePropsResolver

        private final PropertyResolver overridePropsResolver
        Property resolver.
      • configStack

        private final java.util.Deque<DefaultConfiguration> configStack
        The loaded configurations.
      • omitIgnoredModules

        private final boolean omitIgnoredModules
        Flags if modules with the severity 'ignore' should be omitted.
      • configuration

        private Configuration configuration
        The Configuration that is being built.
    • Constructor Detail

      • ConfigurationLoader

        private ConfigurationLoader​(PropertyResolver overrideProps,
                                    boolean omitIgnoredModules)
                             throws javax.xml.parsers.ParserConfigurationException,
                                    org.xml.sax.SAXException
        Creates a new ConfigurationLoader instance.
        Parameters:
        overrideProps - resolver for overriding properties
        omitIgnoredModules - true if ignored modules should be omitted
        Throws:
        javax.xml.parsers.ParserConfigurationException - if an error occurs
        org.xml.sax.SAXException - if an error occurs
    • Method Detail

      • createIdToResourceNameMap

        private static java.util.Map<java.lang.String,​java.lang.String> createIdToResourceNameMap()
        Creates mapping between local resources and dtd ids.
        Returns:
        map between local resources and dtd ids.
      • parseInputSource

        private void parseInputSource​(org.xml.sax.InputSource source)
                               throws java.io.IOException,
                                      org.xml.sax.SAXException
        Parses the specified input source loading the configuration information. The stream wrapped inside the source, if any, is NOT explicitly closed after parsing, it is the responsibility of the caller to close the stream.
        Parameters:
        source - the source that contains the configuration data
        Throws:
        java.io.IOException - if an error occurs
        org.xml.sax.SAXException - if an error occurs
      • loadConfiguration

        public static Configuration loadConfiguration​(java.lang.String config,
                                                      PropertyResolver overridePropsResolver)
                                               throws CheckstyleException
        Returns the module configurations in a specified file.
        Parameters:
        config - location of config file, can be either a URL or a filename
        overridePropsResolver - overriding properties
        Returns:
        the check configurations
        Throws:
        CheckstyleException - if an error occurs
      • loadConfiguration

        public static Configuration loadConfiguration​(java.lang.String config,
                                                      PropertyResolver overridePropsResolver,
                                                      boolean omitIgnoredModules)
                                               throws CheckstyleException
        Returns the module configurations in a specified file.
        Parameters:
        config - location of config file, can be either a URL or a filename
        overridePropsResolver - overriding properties
        omitIgnoredModules - true if modules with severity 'ignore' should be omitted, false otherwise
        Returns:
        the check configurations
        Throws:
        CheckstyleException - if an error occurs
      • loadConfiguration

        @Deprecated
        public static Configuration loadConfiguration​(java.io.InputStream configStream,
                                                      PropertyResolver overridePropsResolver,
                                                      boolean omitIgnoredModules)
                                               throws CheckstyleException
        Deprecated.
        As this method does not provide a valid system ID, preventing resolution of external entities, a version using an InputSource should be used instead
        Returns the module configurations from a specified input stream. Note that clients are required to close the given stream by themselves
        Parameters:
        configStream - the input stream to the Checkstyle configuration
        overridePropsResolver - overriding properties
        omitIgnoredModules - true if modules with severity 'ignore' should be omitted, false otherwise
        Returns:
        the check configurations
        Throws:
        CheckstyleException - if an error occurs
      • loadConfiguration

        public static Configuration loadConfiguration​(org.xml.sax.InputSource configSource,
                                                      PropertyResolver overridePropsResolver,
                                                      boolean omitIgnoredModules)
                                               throws CheckstyleException
        Returns the module configurations from a specified input source. Note that if the source does wrap an open byte or character stream, clients are required to close that stream by themselves
        Parameters:
        configSource - the input stream to the Checkstyle configuration
        overridePropsResolver - overriding properties
        omitIgnoredModules - true if modules with severity 'ignore' should be omitted, false otherwise
        Returns:
        the check configurations
        Throws:
        CheckstyleException - if an error occurs
      • replaceProperties

        private static java.lang.String replaceProperties​(java.lang.String value,
                                                          PropertyResolver props,
                                                          java.lang.String defaultValue)
                                                   throws CheckstyleException
        Replaces ${xxx} style constructions in the given value with the string value of the corresponding data types.

        Code copied from ant - http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java

        Parameters:
        value - The string to be scanned for property references. May be null, in which case this method returns immediately with no effect.
        props - Mapping (String to String) of property names to their values. Must not be null.
        defaultValue - default to use if one of the properties in value cannot be resolved from props.
        Returns:
        the original string with the properties replaced, or null if the original string is null.
        Throws:
        CheckstyleException - if the string contains an opening ${} without a closing {@code }
      • parsePropertyString

        private static void parsePropertyString​(java.lang.String value,
                                                java.util.List<java.lang.String> fragments,
                                                java.util.List<java.lang.String> propertyRefs)
                                         throws CheckstyleException
        Parses a string containing ${xxx} style property references into two lists. The first list is a collection of text fragments, while the other is a set of string property names. null entries in the first list indicate a property reference from the second list.

        Code copied from ant - http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java

        Parameters:
        value - Text to parse. Must not be null.
        fragments - List to add text fragments to. Must not be null.
        propertyRefs - List to add property names to. Must not be null.
        Throws:
        CheckstyleException - if the string contains an opening ${} without a closing {@code }