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

openmrs / openmrs-core / 43293528472

12 Nov 2026 05:19PM UTC coverage: 66.969% (+0.2%) from 66.721%
43293528472

push

github

ibacher
Validate module ids and contain module paths (#6813)

38 of 42 new or added lines in 4 files covered. (90.48%)

391 existing lines in 8 files now uncovered.

26387 of 39402 relevant lines covered (66.97%)

0.67 hits per line

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

86.79
/api/src/main/java/org/openmrs/module/UpdateFileParser.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.module;
11

12
import java.io.StringReader;
13
import java.util.ArrayList;
14
import java.util.List;
15

16
import javax.xml.parsers.DocumentBuilder;
17
import javax.xml.parsers.DocumentBuilderFactory;
18

19
import org.openmrs.util.OpenmrsConstants;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22
import org.w3c.dom.Document;
23
import org.w3c.dom.Element;
24
import org.w3c.dom.NodeList;
25
import org.xml.sax.InputSource;
26

27
import static org.openmrs.util.XmlUtils.createDocumentBuilder;
28

29
/**
30
 * This class will parse an xml update.rdf file
31
 *
32
 * @version 1.0
33
 */
34
public class UpdateFileParser {
35
        
36
        private static final Logger log = LoggerFactory.getLogger(UpdateFileParser.class);
1 ✔
37
        
38
        private String content;
39
        
40
        // these properties store the 'best fit' (most recent update that will fit with the current code version)
41
        private String moduleId = null;
1 ✔
42
        
43
        private String currentVersion = null;
1 ✔
44
        
45
        private String downloadURL = null;
1 ✔
46
        
47
        /**
48
         * Default constructor
49
         *
50
         * @param s String to parse (Contents of update.rdf file)
51
         */
52
        public UpdateFileParser(String s) {
1 ✔
53
                this.content = s;
1 ✔
54
        }
1 ✔
55
        
56
        /**
57
         * Parse the contents of the update.rdf file.
58
         *
59
         * @throws ModuleException
60
         * <strong>Should</strong> set properties from xml file
61
         * <strong>Should</strong> set properties using the newest update
62
         * <strong>Should</strong> not set properties using updates ahead of current openmrs version
63
         */
64
        public void parse() throws ModuleException {
65
                StringReader stringReader = null;
1 ✔
66
                try {
67
                        Document updateDoc;
68
                        try {
69
                                stringReader = new StringReader(content);
1 ✔
70
                                InputSource inputSource = new InputSource(stringReader);
1 ✔
71
                                inputSource.setSystemId("./");
1 ✔
72
                                
73
                                DocumentBuilder db = createDocumentBuilder();
1 ✔
74
                                updateDoc = db.parse(inputSource);
1 ✔
75
                        }
UNCOV
76
                        catch (Exception e) {
×
UNCOV
77
                                log.warn("Unable to parse content");
×
UNCOV
78
                                throw new ModuleException("Error parsing update.rdf file: " + content, e);
×
79
                        }
1 ✔
80
                        
81
                        Element rootNode = updateDoc.getDocumentElement();
1 ✔
82
                        
83
                        String configVersion = rootNode.getAttribute("configVersion");
1 ✔
84
                        
85
                        if (!validConfigVersions().contains(configVersion)) {
1 ✔
UNCOV
86
                                throw new ModuleException("Invalid configVersion: '" + configVersion + "' found In content: " + content);
×
87
                        }
88
                        
89
                        if ("1.0".equals(configVersion)) {
1 ✔
90
                                // the only update in the xml file is the 'best fit'
91
                                this.moduleId = getElement(rootNode, configVersion, "moduleId");
1 ✔
92
                                this.currentVersion = getElement(rootNode, configVersion, "currentVersion");
1 ✔
93
                                this.downloadURL = getElement(rootNode, configVersion, "downloadURL");
1 ✔
94
                        } else if ("1.1".equals(configVersion)) {
1 ✔
95
                                
96
                                this.moduleId = rootNode.getAttribute("moduleId");
1 ✔
97
                                
98
                                NodeList nodes = rootNode.getElementsByTagName("update");
1 ✔
99
                                // default to the lowest version possible
100
                                this.currentVersion = "";
1 ✔
101
                                
102
                                // loop over all 'update' tags
103
                                for (int i = 0; i < nodes.getLength(); i++) {
1 ✔
104
                                        Element currentNode = (Element) nodes.item(i);
1 ✔
105
                                        String currentVersion = getElement(currentNode, configVersion, "currentVersion");
1 ✔
106
                                        // if the currently saved version is less than the current tag
107
                                        if (ModuleUtil.compareVersion(this.currentVersion, currentVersion) < 0) {
1 ✔
108
                                                String requireOpenMRSVersion = getElement(currentNode, configVersion, "requireOpenMRSVersion");
1 ✔
109
                                                // if the openmrs code version is compatible, this node is a winner
110
                                                if (requireOpenMRSVersion == null
1 ✔
111
                                                        || ModuleUtil.matchRequiredVersions(OpenmrsConstants.OPENMRS_VERSION_SHORT,
1 ✔
112
                                                            requireOpenMRSVersion)) {
113
                                                        this.currentVersion = currentVersion;
1 ✔
114
                                                        this.downloadURL = getElement(currentNode, configVersion, "downloadURL");
1 ✔
115
                                                }
116
                                        }
117
                                }
118
                        }
119
                }
UNCOV
120
                catch (ModuleException e) {
×
121
                        // rethrow the moduleException
UNCOV
122
                        throw e;
×
123
                }
124
                finally {
125
                        if (stringReader != null) {
1 ✔
126
                                stringReader.close();
1 ✔
127
                        }
128
                }
129
                
130
        }
1 ✔
131
        
132
        /**
133
         * Generic method to get a module tag
134
         *
135
         * @param element
136
         * @param version
137
         * @param tag
138
         * @return
139
         */
140
        private static String getElement(Element element, String version, String tag) {
141
                if (element.getElementsByTagName(tag).getLength() > 0) {
1 ✔
142
                        return element.getElementsByTagName(tag).item(0).getTextContent();
1 ✔
143
                }
UNCOV
144
                return "";
×
145
        }
146
        
147
        /**
148
         * List of the valid sqldiff versions
149
         *
150
         * @return
151
         */
152
        private static List<String> validConfigVersions() {
153
                List<String> versions = new ArrayList<>();
1 ✔
154
                versions.add("1.0");
1 ✔
155
                versions.add("1.1");
1 ✔
156
                return versions;
1 ✔
157
        }
158
        
159
        /**
160
         * @return the downloadURL
161
         */
162
        public String getDownloadURL() {
163
                return downloadURL;
1 ✔
164
        }
165
        
166
        /**
167
         * @return the moduleId
168
         */
169
        public String getModuleId() {
170
                return moduleId;
1 ✔
171
        }
172
        
173
        /**
174
         * @return the version
175
         */
176
        public String getCurrentVersion() {
177
                return currentVersion;
1 ✔
178
        }
179
        
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc