Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix violations of Sonar rule 2755 #619

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Contributors: Red Hat, Inc.
******************************************************************************/
package org.jboss.tools.rsp.internal.launching.java.util;
import javax.xml.XMLConstants;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class LaunchingSupportUtils {
private static DocumentBuilder getParser() throws CoreException {
if (fgXMLParser == null) {
try {
fgXMLParser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
fgXMLParser = createDocumentBuilderFactory().newDocumentBuilder();
fgXMLParser.setErrorHandler(new DefaultHandler());
} catch (ParserConfigurationException e) {
abort(LaunchingPlugin_34, e);
Expand Down Expand Up @@ -348,4 +349,11 @@ protected static void abort(String message, Throwable exception, int code) throw
throw new CoreException(new Status(IStatus.ERROR, IVMInstallChangedListener.LAUNCHING_ID_PLUGIN,
code, message, exception));
}

private static DocumentBuilderFactory createDocumentBuilderFactory() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
return factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static XMLMemento createReadRoot(InputStream in) {

Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = createDocumentBuilderFactory();
DocumentBuilder parser = factory.newDocumentBuilder();
document = parser.parse(new InputSource(in));
Node node = document.getFirstChild();
Expand Down Expand Up @@ -126,7 +126,7 @@ private static void logError(Exception t) {
public static XMLMemento createWriteRoot(String type) {
Document document;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
document = createDocumentBuilderFactory().newDocumentBuilder().newDocument();
Element element = document.createElement(type);
document.appendChild(element);
return new XMLMemento(document, element);
Expand Down Expand Up @@ -348,7 +348,7 @@ public void save(OutputStream os) throws IOException {
Result result = new StreamResult(os);
Source source = new DOMSource(factory);
try {
TransformerFactory factory = TransformerFactory.newInstance();
TransformerFactory factory = createTransformerFactory();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
Expand Down Expand Up @@ -436,4 +436,18 @@ public String getTextData() {
}
return ""; //$NON-NLS-1$
}

private static DocumentBuilderFactory createDocumentBuilderFactory() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
return factory;
}

private static TransformerFactory createTransformerFactory() {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
return factory;
}
}