From 53c6e37016aa9737cb38582e37d2d5260556337f Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 5 Jul 2026 08:27:35 +0200 Subject: [PATCH 1/2] Overload methods in XMLUtil to be able to set FEATURE_SECURE_PROCESSING The parse methods will leave it at its default value since setting it to true by default would break compatibility. 'getDOMImplementation()' and 'normalize()' will now set it to true. note: It is a 3-state boolean with JDK (11+) default set to true. Setting it to true again however, enables additional checks since the JDK impl tracks if it has been set explicitly by the application. That is also why passing false as parameter to the new methods will leave it at its default value instead of turning it completely off. --- .../src/org/openide/xml/XMLUtil.java | 71 +++++++++++++++---- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/platform/openide.util/src/org/openide/xml/XMLUtil.java b/platform/openide.util/src/org/openide/xml/XMLUtil.java index 7d538bf97956..c49370052855 100644 --- a/platform/openide.util/src/org/openide/xml/XMLUtil.java +++ b/platform/openide.util/src/org/openide/xml/XMLUtil.java @@ -36,6 +36,7 @@ import java.util.logging.Logger; import java.util.Map; import java.util.Set; +import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; @@ -108,22 +109,35 @@ public static XMLReader createXMLReader(boolean validate) return createXMLReader(validate, false); } - private static SAXParserFactory[][] saxes = new SAXParserFactory[2][2]; + /** Create a simple parser, possibly validating. + * @param validate if true, a validating parser is returned + * @param namespaceAware if true, a namespace aware parser is returned + * @throws SAXException if a parser can not be created + * @return createXMLReader(validate, false) + */ + public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException { + return createXMLReader(validate, namespaceAware, false); + } + + // TODO LazyConstant candidate + private static final SAXParserFactory[][][] saxes = new SAXParserFactory[2][2][2]; + /** Creates a SAX parser. * *

See {@link #parse} for hints on setting an entity resolver. * * @param validate if true, a validating parser is returned * @param namespaceAware if true, a namespace aware parser is returned + * @param secureProcessing if true, {@link XMLConstants#FEATURE_SECURE_PROCESSING} will be set * * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type. * @throws SAXException if a parser fulfilling given parameters can not be created * * @return XMLReader configured according to passed parameters */ - public static synchronized XMLReader createXMLReader(boolean validate, boolean namespaceAware) + public static synchronized XMLReader createXMLReader(boolean validate, boolean namespaceAware, boolean secureProcessing) throws SAXException { - SAXParserFactory factory = saxes[validate ? 0 : 1][namespaceAware ? 0 : 1]; + SAXParserFactory factory = saxes[validate ? 0 : 1][namespaceAware ? 0 : 1][secureProcessing ? 0 : 1]; if (factory == null) { try { factory = SAXParserFactory.newInstance(); @@ -135,9 +149,17 @@ public static synchronized XMLReader createXMLReader(boolean validate, boolean n ); throw err; } + // keep default if false + if (secureProcessing) { + try { + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + } catch (ParserConfigurationException ex) { + throw new IllegalStateException("FEATURE_SECURE_PROCESSING required", ex); + } + } factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); - saxes[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory; + saxes[validate ? 0 : 1][namespaceAware ? 0 : 1][secureProcessing ? 0 : 1] = factory; } try { @@ -197,7 +219,7 @@ public static Document createDocument( private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public - DocumentBuilderFactory factory = getFactory(false, false); + DocumentBuilderFactory factory = getFactory(false, false, true); try { return factory.newDocumentBuilder().getDOMImplementation(); @@ -211,18 +233,38 @@ private static DOMImplementation getDOMImplementation() } } - private static DocumentBuilderFactory[][] doms = new DocumentBuilderFactory[2][2]; - private static synchronized DocumentBuilderFactory getFactory(boolean validate, boolean namespaceAware) { - DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1]; + // TODO LazyConstant candidate + private static final DocumentBuilderFactory[][][] doms = new DocumentBuilderFactory[2][2][2]; + + private static synchronized DocumentBuilderFactory getFactory(boolean validate, boolean namespaceAware, boolean secureProcessing) { + DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1][secureProcessing ? 0 : 1]; if (factory == null) { factory = DocumentBuilderFactory.newInstance(); + // keep default if false + if (secureProcessing) { + try { + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + } catch (ParserConfigurationException ex) { + throw new IllegalStateException("FEATURE_SECURE_PROCESSING required", ex); + } + } factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); - doms[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory; + doms[validate ? 0 : 1][namespaceAware ? 0 : 1][secureProcessing ? 0 : 1] = factory; } return factory; } + /** + * Calls {@link #parse(org.xml.sax.InputSource, boolean, boolean, boolean, org.xml.sax.ErrorHandler, org.xml.sax.EntityResolver) } + * with secureProcessing set to false. + * @see #parse(org.xml.sax.InputSource, boolean, boolean, boolean, org.xml.sax.ErrorHandler, org.xml.sax.EntityResolver) + */ + public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) + throws IOException, SAXException { + return parse(input, validate, namespaceAware, false, errorHandler, entityResolver); + } + /** * Parses an XML document into a DOM tree. * @@ -308,6 +350,7 @@ private static synchronized DocumentBuilderFactory getFactory(boolean validate, * @param input a parser input (for URL users use: new InputSource(url.toString()) * @param validate if true validating parser is used * @param namespaceAware if true DOM is created by namespace aware parser + * @param secureProcessing if true, {@link XMLConstants#FEATURE_SECURE_PROCESSING} will be set * @param errorHandler a error handler to notify about exception (such as {@link #defaultErrorHandler}) or null * @param entityResolver SAX entity resolver (such as {@link EntityCatalog#getDefault}) or null * @@ -318,12 +361,12 @@ private static synchronized DocumentBuilderFactory getFactory(boolean validate, * @return document representing given input */ public static Document parse( - InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, + InputSource input, boolean validate, boolean namespaceAware, boolean secureProcessing, ErrorHandler errorHandler, EntityResolver entityResolver ) throws IOException, SAXException { DocumentBuilder builder = null; - DocumentBuilderFactory factory = getFactory(validate, namespaceAware); + DocumentBuilderFactory factory = getFactory(validate, namespaceAware, secureProcessing); try { builder = factory.newDocumentBuilder(); @@ -789,7 +832,7 @@ private static boolean checkContentCharacters(String chars) */ private static Document normalize(Document orig) throws IOException { DocumentBuilder builder = null; - DocumentBuilderFactory factory = getFactory(false, false); + DocumentBuilderFactory factory = getFactory(false, false, true); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { @@ -799,9 +842,9 @@ private static Document normalize(Document orig) throws IOException { DocumentType doctype = null; NodeList nl = orig.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { - if (nl.item(i) instanceof DocumentType) { + if (nl.item(i) instanceof DocumentType dt) { // We cannot import DocumentType's, so we need to manually copy it. - doctype = (DocumentType) nl.item(i); + doctype = dt; } } Document doc; From 546938e0cd546163e03b3db693ff83bdfb1db35d Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Mon, 6 Jul 2026 13:48:30 +0200 Subject: [PATCH 2/2] Move some of the usage to the new XMLUtil methods some dead code removal --- .../queries/GlobalSourceForBinaryImpl.java | 9 +- .../nbproject/project.properties | 2 +- .../ergonomics/fod/FeatureProjectFactory.java | 17 +-- .../gradle/GradleAuxiliaryConfigImpl.java | 8 +- .../support/ant/GeneratedFilesHelper.java | 8 +- .../java/j2semodule/J2SEModularProject.java | 20 --- .../modules/maven/grammar/POMDataObject.java | 4 +- .../maven/indexer/api/PluginIndexManager.java | 6 +- .../modules/maven/M2AuxilaryConfigImpl.java | 10 +- .../maven/api/archetype/Archetype.java | 2 +- .../maven/execute/navigator/GoalsPanel.java | 2 +- .../javafx2/project/JFXProjectUtils.java | 2 +- .../modules/maven/htmlui/MavenUtilities.java | 29 ++-- .../modules/options/keymap/XMLStorage.java | 140 ------------------ 14 files changed, 40 insertions(+), 219 deletions(-) diff --git a/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/GlobalSourceForBinaryImpl.java b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/GlobalSourceForBinaryImpl.java index 6504abdcd50f..86a6602b42fd 100644 --- a/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/GlobalSourceForBinaryImpl.java +++ b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/GlobalSourceForBinaryImpl.java @@ -380,13 +380,10 @@ private void doScanZippedNetBeansOrgSources() throws IOException { private String parseCNB(final ZipEntry projectXML) throws IOException { Document doc; - InputStream is = nbSrcZip.getInputStream(projectXML); - try { - doc = XMLUtil.parse(new InputSource(is), false, true, null, null); + try (InputStream is = nbSrcZip.getInputStream(projectXML)) { + doc = XMLUtil.parse(new InputSource(is), false, true, true, null, null); } catch (SAXException e) { - throw (IOException) new IOException(projectXML + ": " + e.toString()).initCause(e); // NOI18N - } finally { - is.close(); + throw new IOException(projectXML + ": " + e.toString(), e); // NOI18N } Element docel = doc.getDocumentElement(); Element type = XMLUtil.findElement(docel, "type", "http://www.netbeans.org/ns/project/1"); // NOI18N diff --git a/ergonomics/ide.ergonomics/nbproject/project.properties b/ergonomics/ide.ergonomics/nbproject/project.properties index d33b3f79a170..45ad76ec2639 100644 --- a/ergonomics/ide.ergonomics/nbproject/project.properties +++ b/ergonomics/ide.ergonomics/nbproject/project.properties @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -javac.source=1.8 +javac.release=21 javac.compilerargs=-Xlint -Xlint:-serial javadoc.arch=${basedir}/arch.xml diff --git a/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java b/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java index 3b32ea63a518..79f8582af97f 100644 --- a/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java +++ b/ergonomics/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.File; +import java.io.FileInputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; @@ -38,7 +39,6 @@ import javax.swing.Icon; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.xml.parsers.DocumentBuilder; import org.netbeans.api.autoupdate.UpdateElement; import org.netbeans.api.progress.ProgressHandle; import org.netbeans.api.project.Project; @@ -63,8 +63,6 @@ import org.openide.util.lookup.ProxyLookup; import org.openide.util.lookup.ServiceProvider; import org.w3c.dom.Document; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.netbeans.api.project.ui.OpenProjects; import org.netbeans.spi.project.ui.LogicalViewProvider; import org.openide.filesystems.FileUtil; @@ -72,6 +70,8 @@ import org.openide.nodes.FilterNode; import org.openide.util.NbBundle; import org.openide.util.RequestProcessor.Task; +import org.openide.xml.XMLUtil; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -116,20 +116,15 @@ Document dom(String relative) { } File f = FileUtil.toFile(fo); try { - DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - if (f != null) { - doc = b.parse(f); - } else { - InputStream is = fo.getInputStream(); - doc = b.parse(is); + InputStream is = f != null ? new FileInputStream(f) : fo.getInputStream(); + try (is) { + doc = XMLUtil.parse(new InputSource(is), false, false, true, null, null); } if (doms == null) { doms = new HashMap(); } doms.put(relative, doc); return doc; - } catch (ParserConfigurationException parserConfigurationException) { - LOG.log(Level.WARNING, "Cannot configure XML parser", parserConfigurationException); // NOI18N } catch (SAXException sAXException) { LOG.log(Level.INFO, "XML broken in " + f, sAXException); // NOI18N } catch (Exception any) { diff --git a/extide/gradle/src/org/netbeans/modules/gradle/GradleAuxiliaryConfigImpl.java b/extide/gradle/src/org/netbeans/modules/gradle/GradleAuxiliaryConfigImpl.java index 2eae23d7eee3..522a98766f9e 100644 --- a/extide/gradle/src/org/netbeans/modules/gradle/GradleAuxiliaryConfigImpl.java +++ b/extide/gradle/src/org/netbeans/modules/gradle/GradleAuxiliaryConfigImpl.java @@ -170,7 +170,7 @@ public ProjectProblemsProvider getProblemProvider() { private Document loadConfig(FileObject config) throws IOException, SAXException { synchronized (configIOLock) { - return XMLUtil.parse(new InputSource(config.toURL().toString()), false, true, null, null); + return XMLUtil.parse(new InputSource(config.toURL().toString()), false, true, true, null, null); } } @@ -281,7 +281,7 @@ public void run() { if (str != null) { Document doc; try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); return XMLUtil.findElement(doc.getDocumentElement(), elementName, namespace); } catch (SAXException ex) { LOG.log(Level.FINE, "cannot parse", ex); @@ -327,7 +327,7 @@ private void lazyAttachListener() { String str = (String) projectDirectory.getAttribute(AUX_CONFIG); if (str != null) { try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); } catch (SAXException ex) { LOG.log(Level.FINE, "cannot parse", ex); } catch (IOException ex) { @@ -396,7 +396,7 @@ private void lazyAttachListener() { String str = (String) projectDirectory.getAttribute(AUX_CONFIG); if (str != null) { try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); } catch (SAXException | IOException ex) { Exceptions.printStackTrace(ex); } diff --git a/ide/project.ant/src/org/netbeans/spi/project/support/ant/GeneratedFilesHelper.java b/ide/project.ant/src/org/netbeans/spi/project/support/ant/GeneratedFilesHelper.java index e132f235aa76..7a7049186a08 100644 --- a/ide/project.ant/src/org/netbeans/spi/project/support/ant/GeneratedFilesHelper.java +++ b/ide/project.ant/src/org/netbeans/spi/project/support/ant/GeneratedFilesHelper.java @@ -444,7 +444,7 @@ private byte[] applyBuildExtensions(byte[] resultData, AntBuildExtender ext) { ByteArrayInputStream in2 = new ByteArrayInputStream(resultData); InputSource is = new InputSource(in2); - Document doc = XMLUtil.parse(is, false, true, null, null); + Document doc = XMLUtil.parse(is, false, true, true, null, null); Element el = doc.getDocumentElement(); Node firstSubnode = el.getFirstChild(); //TODO check if first one is text and use it as indentation.. @@ -495,11 +495,7 @@ private byte[] applyBuildExtensions(byte[] resultData, AntBuildExtender ext) { XMLUtil.write(doc, out, "UTF-8"); //NOI18N return out.toByteArray(); } - catch (IOException ex) { - Exceptions.printStackTrace(ex); - return resultData; - } - catch (SAXException ex) { + catch (IOException | SAXException ex) { Exceptions.printStackTrace(ex); return resultData; } diff --git a/java/java.j2semodule/src/org/netbeans/modules/java/j2semodule/J2SEModularProject.java b/java/java.j2semodule/src/org/netbeans/modules/java/j2semodule/J2SEModularProject.java index e8ce529dc6e7..00945b0d1274 100644 --- a/java/java.j2semodule/src/org/netbeans/modules/java/j2semodule/J2SEModularProject.java +++ b/java/java.j2semodule/src/org/netbeans/modules/java/j2semodule/J2SEModularProject.java @@ -28,9 +28,6 @@ import java.util.concurrent.Callable; import java.util.logging.Logger; import javax.swing.Icon; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.annotations.common.NullAllowed; import org.netbeans.api.java.classpath.ClassPath; @@ -579,23 +576,6 @@ public Project getOwningProject() { } - private static final DocumentBuilder db; - static { - try { - db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new AssertionError(e); - } - } -// private static Document createNewDocument() { -// // #50198: for thread safety, use a separate document. -// // Using XMLUtil.createDocument is much too slow. -// synchronized (db) { -// return db.newDocument(); -// } -// } -// -// @NonNull private Runnable newStartMainUpdaterAction() { return () -> { diff --git a/java/maven.grammar/src/org/netbeans/modules/maven/grammar/POMDataObject.java b/java/maven.grammar/src/org/netbeans/modules/maven/grammar/POMDataObject.java index d3aa00ce454b..a2f015f64340 100644 --- a/java/maven.grammar/src/org/netbeans/modules/maven/grammar/POMDataObject.java +++ b/java/maven.grammar/src/org/netbeans/modules/maven/grammar/POMDataObject.java @@ -20,7 +20,6 @@ package org.netbeans.modules.maven.grammar; import java.io.IOException; -import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; import org.netbeans.api.annotations.common.StaticResource; @@ -42,7 +41,6 @@ import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; -import org.openide.filesystems.MIMEResolver; import org.openide.loaders.DataObjectExistsException; import org.openide.loaders.MultiDataObject; import org.openide.loaders.MultiFileLoader; @@ -187,7 +185,7 @@ static String annotateWithProjectName(FileObject primaryFile) { // #154508 if (primaryFile.getNameExt().equals("pom.xml")) { // NOI18N try { //TODO faster and less memory intensive to have just FileObject().asText()-> regexp? - Element artifactId = XMLUtil.findElement(XMLUtil.parse(new InputSource(primaryFile.toURL().toString()), false, false, XMLUtil.defaultErrorHandler(), null).getDocumentElement(), "artifactId", null); // NOI18N + Element artifactId = XMLUtil.findElement(XMLUtil.parse(new InputSource(primaryFile.toURL().toString()), false, false, true, XMLUtil.defaultErrorHandler(), null).getDocumentElement(), "artifactId", null); // NOI18N if (artifactId != null) { String text = XMLUtil.findText(artifactId); if (text != null) { diff --git a/java/maven.indexer/src/org/netbeans/modules/maven/indexer/api/PluginIndexManager.java b/java/maven.indexer/src/org/netbeans/modules/maven/indexer/api/PluginIndexManager.java index 643636547740..c06c90b6b48f 100644 --- a/java/maven.indexer/src/org/netbeans/modules/maven/indexer/api/PluginIndexManager.java +++ b/java/maven.indexer/src/org/netbeans/modules/maven/indexer/api/PluginIndexManager.java @@ -330,7 +330,7 @@ public static Map> getLifecyclePlugins(String packaging, @Nu return Collections.emptyMap(); } private static Map> parsePhases(String u, String packaging) throws Exception { - Document doc = XMLUtil.parse(new InputSource(u), false, false, XMLUtil.defaultErrorHandler(), null); + Document doc = XMLUtil.parse(new InputSource(u), false, false, true, XMLUtil.defaultErrorHandler(), null); for (Element componentsEl : XMLUtil.findSubElements(doc.getDocumentElement())) { for (Element componentEl : XMLUtil.findSubElements(componentsEl)) { if (XMLUtil.findText(XMLUtil.findElement(componentEl, "role", null)).trim().equals("org.apache.maven.lifecycle.mapping.LifecycleMapping") @@ -379,8 +379,8 @@ private static Map> parsePhases(String u, String packaging) return null; } LOG.log(Level.FINER, "parsing plugin.xml from {0}", jar); - try { - return XMLUtil.parse(new InputSource("jar:" + BaseUtilities.toURI(jar) + "!/META-INF/maven/plugin.xml"), false, false, XMLUtil.defaultErrorHandler(), null); + try { + return XMLUtil.parse(new InputSource("jar:" + BaseUtilities.toURI(jar) + "!/META-INF/maven/plugin.xml"), false, false, true, XMLUtil.defaultErrorHandler(), null); } catch (Exception x) { LOG.log(Level.FINE, "could not parse " + jar, x.toString()); return null; diff --git a/java/maven/src/org/netbeans/modules/maven/M2AuxilaryConfigImpl.java b/java/maven/src/org/netbeans/modules/maven/M2AuxilaryConfigImpl.java index ba2dead727d3..a77abddb4bb3 100644 --- a/java/maven/src/org/netbeans/modules/maven/M2AuxilaryConfigImpl.java +++ b/java/maven/src/org/netbeans/modules/maven/M2AuxilaryConfigImpl.java @@ -173,7 +173,7 @@ public ProjectProblemsProvider getProblemProvider() { private Document loadConfig(FileObject config) throws IOException, SAXException { synchronized (configIOLock) { - return XMLUtil.parse(new InputSource(config.toURL().toString()), false, true, null, null); + return XMLUtil.parse(new InputSource(config.toURL().toString()), false, true, true, null, null); } } @@ -283,7 +283,7 @@ public void run() { if (str != null) { Document doc; try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); return XMLUtil.findElement(doc.getDocumentElement(), elementName, namespace); } catch (SAXException ex) { LOG.log(Level.FINE, "cannot parse", ex); @@ -329,7 +329,7 @@ private void lazyAttachListener() { String str = (String) projectDirectory.getAttribute(AUX_CONFIG); if (str != null) { try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); } catch (SAXException ex) { LOG.log(Level.FINE, "cannot parse", ex); } catch (IOException ex) { @@ -398,7 +398,7 @@ private void lazyAttachListener() { String str = (String) projectDirectory.getAttribute(AUX_CONFIG); if (str != null) { try { - doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null); + doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, true, null, null); } catch (SAXException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { @@ -423,7 +423,7 @@ private void lazyAttachListener() { try { ByteArrayOutputStream wr = new ByteArrayOutputStream(); XMLUtil.write(doc, wr, "UTF-8"); //NOI18N - projectDirectory.setAttribute(AUX_CONFIG, wr.toString("UTF-8")); + projectDirectory.setAttribute(AUX_CONFIG, wr.toString(StandardCharsets.UTF_8)); } catch (IOException ex) { Exceptions.printStackTrace(ex); } diff --git a/java/maven/src/org/netbeans/modules/maven/api/archetype/Archetype.java b/java/maven/src/org/netbeans/modules/maven/api/archetype/Archetype.java index 712189137bc5..201540ca7037 100644 --- a/java/maven/src/org/netbeans/modules/maven/api/archetype/Archetype.java +++ b/java/maven/src/org/netbeans/modules/maven/api/archetype/Archetype.java @@ -283,7 +283,7 @@ public Map loadRequiredProperties() { if (entry != null) { InputStream in = jf.getInputStream(entry); try { - Document doc = XMLUtil.parse(new InputSource(in), false, false, XMLUtil.defaultErrorHandler(), null); + Document doc = XMLUtil.parse(new InputSource(in), false, false, true, XMLUtil.defaultErrorHandler(), null); NodeList nl = doc.getElementsByTagName("requiredProperty"); for (int i = 0; i < nl.getLength(); i++) { Element rP = (Element) nl.item(i); diff --git a/java/maven/src/org/netbeans/modules/maven/execute/navigator/GoalsPanel.java b/java/maven/src/org/netbeans/modules/maven/execute/navigator/GoalsPanel.java index a30a0c3481a3..c00653490698 100644 --- a/java/maven/src/org/netbeans/modules/maven/execute/navigator/GoalsPanel.java +++ b/java/maven/src/org/netbeans/modules/maven/execute/navigator/GoalsPanel.java @@ -671,7 +671,7 @@ public Image getOpenedIcon(int type) { } LOG.log(Level.FINER, "parsing plugin.xml from {0}", jar); try { - return XMLUtil.parse(new InputSource("jar:" + Utilities.toURI(jar) + "!/META-INF/maven/plugin.xml"), false, false, XMLUtil.defaultErrorHandler(), null); + return XMLUtil.parse(new InputSource("jar:" + Utilities.toURI(jar) + "!/META-INF/maven/plugin.xml"), false, false, true, XMLUtil.defaultErrorHandler(), null); } catch (Exception x) { LOG.log(Level.FINE, "could not parse " + jar, x.toString()); return null; diff --git a/javafx/javafx2.project/src/org/netbeans/modules/javafx2/project/JFXProjectUtils.java b/javafx/javafx2.project/src/org/netbeans/modules/javafx2/project/JFXProjectUtils.java index 45d2052e1334..e83eb01b70b2 100644 --- a/javafx/javafx2.project/src/org/netbeans/modules/javafx2/project/JFXProjectUtils.java +++ b/javafx/javafx2.project/src/org/netbeans/modules/javafx2/project/JFXProjectUtils.java @@ -900,7 +900,7 @@ private static boolean modifyBuildXml(Project proj) throws IOException { } Document xmlDoc = null; try { - xmlDoc = XMLUtil.parse(new InputSource(buildXmlFO.toURL().toExternalForm()), false, true, null, null); + xmlDoc = XMLUtil.parse(new InputSource(buildXmlFO.toURL().toExternalForm()), false, true, true, null, null); } catch (SAXException ex) { Exceptions.printStackTrace(ex); } diff --git a/javafx/maven.htmlui/src/org/netbeans/modules/maven/htmlui/MavenUtilities.java b/javafx/maven.htmlui/src/org/netbeans/modules/maven/htmlui/MavenUtilities.java index f7dc44ab0a62..4890fc20ee4c 100644 --- a/javafx/maven.htmlui/src/org/netbeans/modules/maven/htmlui/MavenUtilities.java +++ b/javafx/maven.htmlui/src/org/netbeans/modules/maven/htmlui/MavenUtilities.java @@ -21,8 +21,9 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.io.StringReader; -import java.nio.file.NoSuchFileException; +import java.nio.file.Files; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.parsers.DocumentBuilder; @@ -41,6 +42,7 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.netbeans.modules.maven.api.FileUtilities; +import org.openide.xml.XMLUtil; final class MavenUtilities { @@ -70,23 +72,16 @@ private String readProperty(final String tag) { if (!this.settings.isFile()) { return null; } - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder; - dBuilder = dbFactory.newDocumentBuilder(); - Document settingsDoc = dBuilder.parse(this.settings); - NodeList elementsByTagName = settingsDoc.getElementsByTagName(tag); - if (elementsByTagName.getLength() >0) return elementsByTagName.item(0).getTextContent(); - return null; - } catch (NoSuchFileException ex) { - LOG.log(Level.FINE, "Cannot find " + settings, ex); - return null; - } catch (IOException ex) { - LOG.log(Level.INFO, "Cannot read " + settings, ex); - return null; - } catch (ParserConfigurationException ex) { - LOG.log(Level.INFO, "Cannot read " + settings, ex); + + try (InputStream is = Files.newInputStream(settings.toPath())) { + Document settingsDoc = XMLUtil.parse(new InputSource(is), false, false, true, null, null); + NodeList elementsByTagName = settingsDoc.getElementsByTagName(tag); + if (elementsByTagName.getLength() > 0) { + return elementsByTagName.item(0).getTextContent(); + } + } return null; - } catch (SAXException ex) { + } catch (IOException | SAXException ex) { LOG.log(Level.INFO, "Cannot read " + settings, ex); return null; } diff --git a/platform/options.keymap/src/org/netbeans/modules/options/keymap/XMLStorage.java b/platform/options.keymap/src/org/netbeans/modules/options/keymap/XMLStorage.java index 0332a2cafdae..5951aa05719d 100644 --- a/platform/options.keymap/src/org/netbeans/modules/options/keymap/XMLStorage.java +++ b/platform/options.keymap/src/org/netbeans/modules/options/keymap/XMLStorage.java @@ -19,140 +19,10 @@ package org.netbeans.modules.options.keymap; -import java.awt.Color; -import java.awt.Font; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import org.openide.ErrorManager; -import org.openide.filesystems.FileLock; - -import org.openide.filesystems.FileObject; -import org.openide.util.RequestProcessor; -import org.openide.xml.XMLUtil; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; - public class XMLStorage { - - private static final Map colorToName = new HashMap (); - private static final Map nameToColor = new HashMap (); - private static final Map nameToFontStyle = new HashMap (); - private static final Map fontStyleToName = new HashMap (); - static { - colorToName.put (Color.black, "black"); - nameToColor.put ("black", Color.black); - colorToName.put (Color.blue, "blue"); - nameToColor.put ("blue", Color.blue); - colorToName.put (Color.cyan, "cyan"); - nameToColor.put ("cyan", Color.cyan); - colorToName.put (Color.darkGray, "darkGray"); - nameToColor.put ("darkGray", Color.darkGray); - colorToName.put (Color.gray, "gray"); - nameToColor.put ("gray", Color.gray); - colorToName.put (Color.green, "green"); - nameToColor.put ("green", Color.green); - colorToName.put (Color.lightGray, "lightGray"); - nameToColor.put ("lightGray", Color.lightGray); - colorToName.put (Color.magenta, "magenta"); - nameToColor.put ("magenta", Color.magenta); - colorToName.put (Color.orange, "orange"); - nameToColor.put ("orange", Color.orange); - colorToName.put (Color.pink, "pink"); - nameToColor.put ("pink", Color.pink); - colorToName.put (Color.red, "red"); - nameToColor.put ("red", Color.red); - colorToName.put (Color.white, "white"); - nameToColor.put ("white", Color.white); - colorToName.put (Color.yellow, "yellow"); - nameToColor.put ("yellow", Color.yellow); - - nameToFontStyle.put ("plain", Integer.valueOf (Font.PLAIN)); - fontStyleToName.put (Integer.valueOf (Font.PLAIN), "plain"); - nameToFontStyle.put ("bold", Integer.valueOf (Font.BOLD)); - fontStyleToName.put (Integer.valueOf (Font.BOLD), "bold"); - nameToFontStyle.put ("italic", Integer.valueOf (Font.ITALIC)); - fontStyleToName.put (Integer.valueOf (Font.ITALIC), "italic"); - nameToFontStyle.put ("bold+italic", Integer.valueOf (Font.BOLD + Font.ITALIC)); - fontStyleToName.put (Integer.valueOf (Font.BOLD + Font.ITALIC), "bold+italic"); - } - - static String colorToString (Color color) { - if (colorToName.containsKey (color)) - return (String) colorToName.get (color); - return Integer.toHexString (color.getRGB ()); - } - - static Color stringToColor (String color) { - if (nameToColor.containsKey (color)) - return (Color) nameToColor.get (color); - return new Color ((int) Long.parseLong (color, 16)); - } - - - // generics support methods ................................................ - - private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage"); - - static void save (final FileObject fo, final String content) { - requestProcessor.post (new Runnable () { - public void run () { - try { - FileLock lock = fo.lock (); - try { - OutputStream os = fo.getOutputStream (lock); - Writer writer = new OutputStreamWriter (os, StandardCharsets.UTF_8); - try { - writer.write (content); - } finally { - writer.close (); - } - } finally { - lock.releaseLock (); - } - } catch (IOException ex) { - ErrorManager.getDefault ().notify (ex); - } - } - }); - } - - static Object load (FileObject fo, Handler handler) { - try { - XMLReader reader = XMLUtil.createXMLReader (); - reader.setEntityResolver (handler); - reader.setContentHandler (handler); - InputStream is = fo.getInputStream (); - try { - reader.parse (new InputSource (is)); - } finally { - is.close (); - } - return handler.getResult (); - } catch (SAXException ex) { - System.out.println("File: " + fo); - ex.printStackTrace (); - return handler.getResult (); - } catch (IOException ex) { - System.out.println("File: " + fo); - ex.printStackTrace (); - return handler.getResult (); - } catch (Exception ex) { - System.out.println("File: " + fo); - ex.printStackTrace (); - return handler.getResult (); - } - } static StringBuffer generateHeader () { StringBuffer sb = new StringBuffer (); @@ -218,16 +88,6 @@ private static void generateAttributes ( } } - static class Handler extends DefaultHandler { - private Object result; - void setResult (Object result) { - this.result = result; - } - Object getResult () { - return result; - } - } - static class Attribs { private List names = new ArrayList (); private List values = new ArrayList ();