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

mybatis / generator / 1666

24 May 2025 05:59PM UTC coverage: 88.307% (-0.02%) from 88.328%
1666

push

github

web-flow
Merge pull request #1312 from hazendaz/master

Use NIO classes to resolve modernizer issues

2518 of 3410 branches covered (73.84%)

0 of 44 new or added lines in 7 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

11192 of 12674 relevant lines covered (88.31%)

0.88 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-2025 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.mybatis.generator.codegen.XmlConstants;
36
import org.mybatis.generator.config.Configuration;
37
import org.mybatis.generator.exception.XMLParserException;
38
import org.w3c.dom.Document;
39
import org.w3c.dom.DocumentType;
40
import org.w3c.dom.Element;
41
import org.w3c.dom.Node;
42
import org.xml.sax.InputSource;
43
import org.xml.sax.SAXException;
44
import org.xml.sax.SAXParseException;
45

46
public class ConfigurationParser {
47

48
    private final List<String> warnings;
49
    private final List<String> parseErrors;
50
    private final Properties extraProperties;
51

52
    public ConfigurationParser(List<String> warnings) {
53
        this(null, warnings);
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
     * @param warnings any warnings are added to this array
75
     */
76
    public ConfigurationParser(Properties extraProperties, List<String> warnings) {
77
        super();
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,
86
            XMLParserException {
87

NEW
88
        try (BufferedReader fr = Files.newBufferedReader(inputFile.toPath())) {
×
NEW
89
            return parseConfiguration(fr);
×
90
        }
91
    }
92

93
    public Configuration parseConfiguration(Reader reader) throws IOException,
94
            XMLParserException {
95

96
        InputSource is = new InputSource(reader);
×
97

98
        return parseConfiguration(is);
×
99
    }
100

101
    public Configuration parseConfiguration(InputStream inputStream)
102
            throws IOException, XMLParserException {
103

104
        InputSource is = new InputSource(inputStream);
1✔
105

106
        return parseConfiguration(is);
1✔
107
    }
108

109
    private Configuration parseConfiguration(InputSource inputSource)
110
            throws IOException, XMLParserException {
111
        parseErrors.clear();
1✔
112
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1✔
113
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
1✔
114
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
1✔
115
        factory.setValidating(true);
1✔
116

117
        try {
118
            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
1✔
119
            DocumentBuilder builder = factory.newDocumentBuilder();
1✔
120
            builder.setEntityResolver(new ParserEntityResolver());
1✔
121

122
            ParserErrorHandler handler = new ParserErrorHandler(warnings,
1✔
123
                    parseErrors);
124
            builder.setErrorHandler(handler);
1✔
125

126
            Document document = null;
1✔
127
            try {
128
                document = builder.parse(inputSource);
1✔
129
            } catch (SAXParseException e) {
×
130
                throw new XMLParserException(parseErrors);
×
131
            } catch (SAXException e) {
×
132
                if (e.getException() == null) {
×
133
                    parseErrors.add(e.getMessage());
×
134
                } else {
135
                    parseErrors.add(e.getException().getMessage());
×
136
                }
137
            }
1✔
138

139
            if (document == null || !parseErrors.isEmpty()) {
1!
140
                throw new XMLParserException(parseErrors);
×
141
            }
142

143
            Configuration config;
144
            Element rootNode = document.getDocumentElement();
1✔
145
            DocumentType docType = document.getDoctype();
1✔
146
            if (rootNode.getNodeType() == Node.ELEMENT_NODE
1!
147
                    && docType.getPublicId().equals(
1!
148
                            XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID)) {
149
                config = parseMyBatisGeneratorConfiguration(rootNode);
1✔
150
            } else {
151
                throw new XMLParserException(getString("RuntimeError.5")); //$NON-NLS-1$
×
152
            }
153

154
            if (!parseErrors.isEmpty()) {
1!
155
                throw new XMLParserException(parseErrors);
×
156
            }
157

158
            return config;
1✔
159
        } catch (ParserConfigurationException e) {
×
160
            parseErrors.add(e.getMessage());
×
161
            throw new XMLParserException(parseErrors);
×
162
        }
163
    }
164

165
    private Configuration parseMyBatisGeneratorConfiguration(Element rootNode)
166
            throws XMLParserException {
167
        MyBatisGeneratorConfigurationParser parser = new MyBatisGeneratorConfigurationParser(
1✔
168
                extraProperties);
169
        return parser.parseConfiguration(rootNode);
1✔
170
    }
171
}
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

© 2025 Coveralls, Inc