• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 1942

12 Jan 2026 05:01PM UTC coverage: 88.75% (+0.4%) from 88.365%
1942

push

github

web-flow
Merge pull request #1412 from jeffgbutler/jspecify

Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/xml/ConfigurationParser.java
1
/*
2
 *    Copyright 2006-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.generator.config.xml;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import java.io.BufferedReader;
21
import java.io.File;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.Reader;
25
import java.nio.file.Files;
26
import java.util.ArrayList;
27
import java.util.List;
28
import java.util.Objects;
29
import java.util.Properties;
30
import javax.xml.XMLConstants;
31
import javax.xml.parsers.DocumentBuilder;
32
import javax.xml.parsers.DocumentBuilderFactory;
33
import javax.xml.parsers.ParserConfigurationException;
34

35
import org.jspecify.annotations.Nullable;
36
import org.mybatis.generator.codegen.XmlConstants;
37
import org.mybatis.generator.config.Configuration;
38
import org.mybatis.generator.exception.XMLParserException;
39
import org.w3c.dom.Document;
40
import org.w3c.dom.DocumentType;
41
import org.w3c.dom.Element;
42
import org.w3c.dom.Node;
43
import org.xml.sax.InputSource;
44
import org.xml.sax.SAXException;
45
import org.xml.sax.SAXParseException;
46

47
public class ConfigurationParser {
48

49
    private final List<String> warnings;
50
    private final List<String> parseErrors;
51
    private final @Nullable Properties extraProperties;
52

53
    public ConfigurationParser(List<String> warnings) {
54
        this(null, warnings);
1✔
55
    }
1✔
56

57
    /**
58
     * This constructor accepts a properties object which may be used to specify
59
     * an additional property set.  Typically, this property set will be Ant or Maven properties
60
     * specified in the build.xml file or the POM.
61
     *
62
     * <p>If there are name collisions between the different property sets, they will be
63
     * resolved in this order:
64
     *
65
     * <ol>
66
     *   <li>System properties take the highest precedence</li>
67
     *   <li>Properties specified in the &lt;properties&gt; configuration
68
     *       element are next</li>
69
     *   <li>Properties specified in this "extra" property set are
70
     *       the lowest precedence.</li>
71
     * </ol>
72
     *
73
     * @param extraProperties an (optional) set of properties used to resolve property
74
     *     references in the configuration file
75
     * @param warnings any warnings are added to this array
76
     */
77
    public ConfigurationParser(@Nullable Properties extraProperties, List<String> warnings) {
1✔
78
        this.extraProperties = extraProperties;
1✔
79

80
        this.warnings = Objects.requireNonNullElseGet(warnings, ArrayList::new);
1✔
81

82
        parseErrors = new ArrayList<>();
1✔
83
    }
1✔
84

85
    public Configuration parseConfiguration(File inputFile) throws IOException, XMLParserException {
UNCOV
86
        try (BufferedReader fr = Files.newBufferedReader(inputFile.toPath())) {
×
87
            return parseConfiguration(fr);
×
88
        }
89
    }
90

91
    public Configuration parseConfiguration(Reader reader) throws IOException, XMLParserException {
UNCOV
92
        InputSource is = new InputSource(reader);
×
UNCOV
93
        return parseConfiguration(is);
×
94
    }
95

96
    public Configuration parseConfiguration(InputStream inputStream) throws IOException, XMLParserException {
97
        InputSource is = new InputSource(inputStream);
1✔
98
        return parseConfiguration(is);
1✔
99
    }
100

101
    private Configuration parseConfiguration(InputSource inputSource) throws IOException, XMLParserException {
102
        parseErrors.clear();
1✔
103
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1✔
104
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
1✔
105
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
1✔
106
        factory.setValidating(true);
1✔
107

108
        try {
109
            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
1✔
110
            DocumentBuilder builder = factory.newDocumentBuilder();
1✔
111
            builder.setEntityResolver(new ParserEntityResolver());
1✔
112

113
            ParserErrorHandler handler = new ParserErrorHandler(warnings,
1✔
114
                    parseErrors);
115
            builder.setErrorHandler(handler);
1✔
116

117
            Document document = null;
1✔
118
            try {
119
                document = builder.parse(inputSource);
1✔
120
            } catch (SAXParseException e) {
×
121
                throw new XMLParserException(parseErrors);
×
122
            } catch (SAXException e) {
×
123
                if (e.getException() == null) {
×
124
                    parseErrors.add(e.getMessage());
×
125
                } else {
126
                    parseErrors.add(e.getException().getMessage());
×
127
                }
128
            }
1✔
129

130
            if (document == null || !parseErrors.isEmpty()) {
1!
131
                throw new XMLParserException(parseErrors);
×
132
            }
133

134
            Configuration config;
135
            Element rootNode = document.getDocumentElement();
1✔
136
            DocumentType docType = document.getDoctype();
1✔
137
            if (rootNode.getNodeType() == Node.ELEMENT_NODE
1!
138
                    && docType.getPublicId().equals(XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID)) {
1!
139
                config = parseMyBatisGeneratorConfiguration(rootNode);
1✔
140
            } else {
141
                throw new XMLParserException(getString("RuntimeError.5")); //$NON-NLS-1$
×
142
            }
143

144
            if (!parseErrors.isEmpty()) {
1!
145
                throw new XMLParserException(parseErrors);
×
146
            }
147

148
            return config;
1✔
149
        } catch (ParserConfigurationException e) {
×
150
            parseErrors.add(e.getMessage());
×
151
            throw new XMLParserException(parseErrors);
×
152
        }
153
    }
154

155
    private Configuration parseMyBatisGeneratorConfiguration(Element rootNode) throws XMLParserException {
156
        MyBatisGeneratorConfigurationParser parser = new MyBatisGeneratorConfigurationParser(extraProperties);
1✔
157
        return parser.parseConfiguration(rootNode);
1✔
158
    }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc