From a94282bd1e85f0180986d58a2d48efc4d890dd10 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Wed, 7 Aug 2024 09:34:48 +0530 Subject: [PATCH 1/3] using access extenal dtd property as not allowed in xmleventreader of lemminx Signed-off-by: Arun Venmany --- .../langserver/lemminx/util/XmlReader.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java index c12809b3..5e0ad64d 100644 --- a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java +++ b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java @@ -1,15 +1,15 @@ /******************************************************************************* -* Copyright (c) 2023, 2024 IBM Corporation and others. -* -* This program and the accompanying materials are made available under the -* terms of the Eclipse Public License v. 2.0 which is available at -* http://www.eclipse.org/legal/epl-2.0. -* -* SPDX-License-Identifier: EPL-2.0 -* -* Contributors: -* IBM Corporation - initial API and implementation -*******************************************************************************/ + * Copyright (c) 2023, 2024 IBM Corporation and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.tools.langserver.lemminx.util; @@ -25,19 +25,19 @@ import java.util.Set; import java.util.logging.Logger; +import javax.xml.XMLConstants; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Characters; import javax.xml.stream.events.XMLEvent; -import javax.xml.transform.stream.StreamSource; public class XmlReader { private static final Logger LOGGER = Logger.getLogger(XmlReader.class.getName()); public static boolean hasServerRoot(String filePath) { File file = null; - + try { file = new File(new URI(filePath).getPath()); return hasServerRoot(file); @@ -55,14 +55,14 @@ public static boolean hasServerRoot(File xmlFile) { if (!xmlFile.exists() || xmlFile.length() == 0) { return false; } - + try { XMLInputFactory factory = getXmlInputFactory(); XMLEventReader reader = null; try (FileInputStream fis = new FileInputStream(xmlFile)) { - reader = factory.createXMLEventReader(new StreamSource(fis)); + reader = factory.createXMLEventReader(fis); while (reader.hasNext()) { XMLEvent nextEvent = reader.nextEvent(); if (nextEvent.isStartElement()) { @@ -70,15 +70,15 @@ public static boolean hasServerRoot(File xmlFile) { } } } catch (XMLStreamException | FileNotFoundException e) { - LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath()); + LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath()); } finally { if (reader != null) { try { reader.close(); - } catch (Exception ignored) { + } catch (Exception ignored) { } } - } + } } catch (Exception e) { LOGGER.severe("Unable to access XML file "+ xmlFile.getAbsolutePath()); } @@ -87,12 +87,13 @@ public static boolean hasServerRoot(File xmlFile) { } private static XMLInputFactory getXmlInputFactory() { - XMLInputFactory factory = XMLInputFactory.newFactory(); + XMLInputFactory factory = XMLInputFactory.newInstance(); try { factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); + factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,""); } catch (Exception e) { LOGGER.warning("Could not set properties on XMLInputFactory."); } @@ -118,7 +119,7 @@ public static Map getElementValues(Path file, Set elemen XMLInputFactory factory = getXmlInputFactory(); XMLEventReader reader = null; try { - reader = factory.createXMLEventReader(new StreamSource(new FileInputStream(file.toFile()))); + reader = factory.createXMLEventReader(new FileInputStream(file.toFile())); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (!event.isStartElement()) { @@ -132,7 +133,7 @@ public static Map getElementValues(Path file, Set elemen returnValues.put(elementName, value.getData()); } } - } + } } catch (FileNotFoundException e) { LOGGER.severe("Unable to access file "+ file.toFile().getName()); } catch (XMLStreamException e) { @@ -141,7 +142,7 @@ public static Map getElementValues(Path file, Set elemen if (reader != null) { try { reader.close(); - } catch (Exception ignored) { + } catch (Exception ignored) { } } } From 103116d46c85d516aef70f2786c000e6d78107f6 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Tue, 6 Aug 2024 18:08:00 +0530 Subject: [PATCH 2/3] adding property to xmlreader of lemminx Signed-off-by: Arun Venmany --- .../langserver/lemminx/util/XmlReader.java | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java index 5e0ad64d..c730ab88 100644 --- a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java +++ b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java @@ -1,15 +1,15 @@ /******************************************************************************* - * Copyright (c) 2023, 2024 IBM Corporation and others. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ +* Copyright (c) 2023, 2024 IBM Corporation and others. +* +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License v. 2.0 which is available at +* http://www.eclipse.org/legal/epl-2.0. +* +* SPDX-License-Identifier: EPL-2.0 +* +* Contributors: +* IBM Corporation - initial API and implementation +*******************************************************************************/ package io.openliberty.tools.langserver.lemminx.util; @@ -25,7 +25,6 @@ import java.util.Set; import java.util.logging.Logger; -import javax.xml.XMLConstants; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; @@ -37,7 +36,7 @@ public class XmlReader { public static boolean hasServerRoot(String filePath) { File file = null; - + try { file = new File(new URI(filePath).getPath()); return hasServerRoot(file); @@ -55,7 +54,7 @@ public static boolean hasServerRoot(File xmlFile) { if (!xmlFile.exists() || xmlFile.length() == 0) { return false; } - + try { XMLInputFactory factory = getXmlInputFactory(); @@ -70,15 +69,15 @@ public static boolean hasServerRoot(File xmlFile) { } } } catch (XMLStreamException | FileNotFoundException e) { - LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath()); + LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath()); } finally { if (reader != null) { try { reader.close(); - } catch (Exception ignored) { + } catch (Exception ignored) { } } - } + } } catch (Exception e) { LOGGER.severe("Unable to access XML file "+ xmlFile.getAbsolutePath()); } @@ -93,7 +92,6 @@ private static XMLInputFactory getXmlInputFactory() { factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); - factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,""); } catch (Exception e) { LOGGER.warning("Could not set properties on XMLInputFactory."); } @@ -116,7 +114,7 @@ public static Map getElementValues(Path file, Set elemen } Map returnValues = new HashMap (); - XMLInputFactory factory = getXmlInputFactory(); + XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader reader = null; try { reader = factory.createXMLEventReader(new FileInputStream(file.toFile())); @@ -133,7 +131,7 @@ public static Map getElementValues(Path file, Set elemen returnValues.put(elementName, value.getData()); } } - } + } } catch (FileNotFoundException e) { LOGGER.severe("Unable to access file "+ file.toFile().getName()); } catch (XMLStreamException e) { @@ -142,7 +140,7 @@ public static Map getElementValues(Path file, Set elemen if (reader != null) { try { reader.close(); - } catch (Exception ignored) { + } catch (Exception ignored) { } } } From a42af181532d497583767a07ace4ef8c46f0a05e Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Wed, 7 Aug 2024 09:46:03 +0530 Subject: [PATCH 3/3] using access extenal dtd property as not allowed in xmleventreader of lemminx Signed-off-by: Arun Venmany --- .../openliberty/tools/langserver/lemminx/util/XmlReader.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java index c730ab88..138bcefe 100644 --- a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java +++ b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/util/XmlReader.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.logging.Logger; +import javax.xml.XMLConstants; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; @@ -92,6 +93,7 @@ private static XMLInputFactory getXmlInputFactory() { factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); + factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); } catch (Exception e) { LOGGER.warning("Could not set properties on XMLInputFactory."); } @@ -114,7 +116,7 @@ public static Map getElementValues(Path file, Set elemen } Map returnValues = new HashMap (); - XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLInputFactory factory = getXmlInputFactory(); XMLEventReader reader = null; try { reader = factory.createXMLEventReader(new FileInputStream(file.toFile()));