Skip to content

Commit 694a7b8

Browse files
committed
devonfw#759: changed structure for XmlNamespace() method
1 parent 1801808 commit 694a7b8

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java

+61-33
Original file line numberDiff line numberDiff line change
@@ -170,44 +170,18 @@ private void handleReplacementPatternsFiles() {
170170

171171
private void checkForXmlNamespace() {
172172
this.context.info("Scanning XML files...");
173-
Path settingsDirectory = context.getIdeHome().resolve("settings");
173+
Path settingsDirectory = context.getSettingsPath();
174174
AtomicBoolean missingNamespaceFound = new AtomicBoolean(false);
175175

176176
try {
177-
Files.walk(settingsDirectory)
178-
.filter(path -> Files.isDirectory(path) && path.getFileName().toString().equals("workspace"))
179-
.forEach(workspaceDir -> {
180-
try {
181-
Files.walk(workspaceDir)
182-
.filter(file -> Files.isRegularFile(file) && file.toString().endsWith(".xml"))
183-
.forEach(file -> {
184-
try (BufferedReader reader = Files.newBufferedReader(file)) {
185-
String line;
186-
int linesRead = 0;
187-
boolean namespaceFound = false;
188-
189-
while ((line = reader.readLine()) != null && linesRead < 3) {
190-
linesRead++;
191-
if (line.contains("\"https://github.com/devonfw/IDEasy/merge\"")) {
192-
namespaceFound = true;
193-
break;
194-
}
195-
}
196-
197-
if (!namespaceFound) {
198-
this.context.warning("The XML file " + file + " does not contain the required 'xmlns:merge' attribute.");
199-
missingNamespaceFound.set(true);
200-
}
177+
List<Path> workspaceDirs = findWorkspaceDirectories(settingsDirectory);
201178

202-
} catch (IOException e) {
203-
this.context.error("Error reading the file: " + file, e);
204-
}
205-
});
206-
} catch (IOException e) {
207-
this.context.error("Error processing the workspace: " + workspaceDir, e);
208-
}
209-
});
179+
for (Path workspaceDir : workspaceDirs) {
180+
missingNamespaceFound.set(
181+
checkXmlFilesForNamespace(workspaceDir, missingNamespaceFound.get()));
182+
}
210183

184+
// Output the result
211185
if (missingNamespaceFound.get()) {
212186
this.context.warning("For further information, please visit https://github.com/devonfw/IDEasy/blob/main/documentation/configurator.adoc#xml-merger");
213187
} else {
@@ -219,6 +193,60 @@ private void checkForXmlNamespace() {
219193
}
220194
}
221195

196+
private List<Path> findWorkspaceDirectories(Path settingsDirectory) throws IOException {
197+
List<Path> workspaceDirs = new ArrayList<>();
198+
Files.walk(settingsDirectory)
199+
.filter(path -> Files.isDirectory(path) && path.getFileName().toString().equals("workspace"))
200+
.forEach(workspaceDirs::add);
201+
return workspaceDirs;
202+
}
203+
204+
private boolean checkXmlFilesForNamespace(Path workspaceDir, boolean missingNamespaceFound) {
205+
try {
206+
List<Path> xmlFiles = findXmlFilesInDirectory(workspaceDir);
207+
208+
for (Path xmlFile : xmlFiles) {
209+
missingNamespaceFound = checkXmlNamespaceInFile(xmlFile, missingNamespaceFound);
210+
}
211+
} catch (IOException e) {
212+
this.context.error("Error processing workspace directory: " + workspaceDir, e);
213+
}
214+
return missingNamespaceFound;
215+
}
216+
217+
private List<Path> findXmlFilesInDirectory(Path directory) throws IOException {
218+
List<Path> xmlFiles = new ArrayList<>();
219+
Files.walk(directory)
220+
.filter(file -> Files.isRegularFile(file) && file.toString().endsWith(".xml"))
221+
.forEach(xmlFiles::add);
222+
return xmlFiles;
223+
}
224+
225+
private boolean checkXmlNamespaceInFile(Path xmlFile, boolean missingNamespaceFound) {
226+
try (BufferedReader reader = Files.newBufferedReader(xmlFile)) {
227+
String line;
228+
int linesRead = 0;
229+
boolean namespaceFound = false;
230+
231+
while ((line = reader.readLine()) != null && linesRead < 3) {
232+
linesRead++;
233+
if (line.contains("\"https://github.com/devonfw/IDEasy/merge\"")) {
234+
namespaceFound = true;
235+
break;
236+
}
237+
}
238+
239+
if (!namespaceFound) {
240+
this.context.warning("The XML file " + xmlFile + " does not contain the required 'xmlns:merge' attribute.");
241+
missingNamespaceFound = true;
242+
}
243+
244+
} catch (IOException e) {
245+
this.context.error("Error reading the file: " + xmlFile, e);
246+
}
247+
return missingNamespaceFound;
248+
}
249+
222250

223251
private void createCustomToolsJson(String variable) {
224252
try (FileWriter writer = new FileWriter(context.getIdeHome().resolve("settings/custom-tools.json").toString())) {

0 commit comments

Comments
 (0)