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

mybatis / generator / 1958

18 Jan 2026 03:42PM UTC coverage: 88.883% (-0.07%) from 88.948%
1958

push

github

web-flow
Merge pull request #1419 from jeffgbutler/api-refactoring

Modernize Some Public API Classes

2333 of 3154 branches covered (73.97%)

55 of 100 new or added lines in 10 files covered. (55.0%)

4 existing lines in 3 files now uncovered.

11585 of 13034 relevant lines covered (88.88%)

0.89 hits per line

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

67.31
/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.Collections;
28
import java.util.List;
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
    private final List<String> warnings = new ArrayList<>();
1✔
49
    private final List<String> parseErrors = new ArrayList<>();
1✔
50
    private final @Nullable Properties extraProperties;
51

52
    public ConfigurationParser() {
53
        this(null);
1✔
54
    }
1✔
55

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

79
    public List<String> getWarnings() {
NEW
80
        return Collections.unmodifiableList(warnings);
×
81
    }
82

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

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

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

99
    private Configuration parseConfiguration(InputSource inputSource) throws IOException, XMLParserException {
100
        parseErrors.clear();
1✔
101
        warnings.clear();
1✔
102

103
        try {
104
            Document document = basicParse(inputSource);
1✔
105

106
            if (document == null || !parseErrors.isEmpty()) {
1!
107
                throw new XMLParserException(parseErrors);
×
108
            }
109

110
            Configuration config;
111
            Element rootNode = document.getDocumentElement();
1✔
112
            DocumentType docType = document.getDoctype();
1✔
113
            if (rootNode.getNodeType() == Node.ELEMENT_NODE
1!
114
                    && docType.getPublicId().equals(XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID)) {
1!
115
                config = parseMyBatisGeneratorConfiguration(rootNode);
1✔
116
            } else {
117
                throw new XMLParserException(getString("RuntimeError.5")); //$NON-NLS-1$
×
118
            }
119

120
            if (!parseErrors.isEmpty()) {
1!
121
                throw new XMLParserException(parseErrors);
×
122
            }
123

124
            return config;
1✔
125
        } catch (ParserConfigurationException e) {
×
126
            parseErrors.add(e.getMessage());
×
127
            throw new XMLParserException(parseErrors);
×
128
        }
129
    }
130

131
    private @Nullable Document basicParse(InputSource inputSource) throws IOException, ParserConfigurationException,
132
            XMLParserException {
133
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1✔
134
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); //$NON-NLS-1$
1✔
135
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); //$NON-NLS-1$
1✔
136
        factory.setValidating(true);
1✔
137

138
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
1✔
139
        DocumentBuilder builder = factory.newDocumentBuilder();
1✔
140
        builder.setEntityResolver(new ParserEntityResolver());
1✔
141

142
        ParserErrorHandler handler = new ParserErrorHandler(warnings, parseErrors);
1✔
143
        builder.setErrorHandler(handler);
1✔
144

145
        Document document = null;
1✔
146
        try {
147
            document = builder.parse(inputSource);
1✔
148
        } catch (SAXParseException e) {
×
149
            throw new XMLParserException(parseErrors);
×
150
        } catch (SAXException e) {
×
151
            if (e.getException() == null) {
×
152
                parseErrors.add(e.getMessage());
×
153
            } else {
154
                parseErrors.add(e.getException().getMessage());
×
155
            }
156
        }
1✔
157

158
        return document;
1✔
159
    }
160

161
    private Configuration parseMyBatisGeneratorConfiguration(Element rootNode) throws XMLParserException {
162
        MyBatisGeneratorConfigurationParser parser = new MyBatisGeneratorConfigurationParser(extraProperties);
1✔
163
        return parser.parseConfiguration(rootNode);
1✔
164
    }
165
}
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