Skip to content

Commit

Permalink
NPE on xsd datatype autocompletion in binary mode
Browse files Browse the repository at this point in the history
Fixes eclipse#1189

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Mar 29, 2022
1 parent 4a176d5 commit e1b6afb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
package org.eclipse.lemminx.extensions.xsd;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
Expand All @@ -30,6 +33,8 @@

public class DataType {

private static final Logger LOGGER = Logger.getLogger(DataType.class.getName());

private static String lineSeparator = System.lineSeparator();

private static final Map<String, DataType> dataTypes;
Expand Down Expand Up @@ -103,14 +108,16 @@ public static String getDocumentation(DOMAttr attr) {
}

private static Map<String, DataType> loadDataTypes() {
String dataTypesFile = "/schemas/xsd/datatypes.xml";
try {
SAXParserFactory factory = DOMUtils.newSAXParserFactory();
SAXParser saxParser = factory.newSAXParser();
DataTypeHandler handler = new DataTypeHandler();
saxParser.parse(new InputSource(DataType.class.getResourceAsStream("/schemas/xsd/datatypes.xml")), handler);
saxParser.parse(new InputSource(DataType.class.getResourceAsStream(dataTypesFile)), handler);
return handler.getDataTypes();
} catch (Exception e) {
return null;
LOGGER.log(Level.SEVERE, "Error while loading data types with file '" + dataTypesFile + "'.", e);
return Collections.emptyMap();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.apache.xerces.jaxp.SAXParserFactoryImpl;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMParser;
Expand Down Expand Up @@ -152,7 +153,9 @@ public static DOMDocument loadDocument(String documentURI, URIResolverExtensionM
*/
public static SAXParserFactory newSAXParserFactory()
throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
// To have no problem with binary, we use the Xerces SAXParserFactory instead of calling SAXParserFactory.newInstance()
// which fails in binary mode because it uses Java reflection.
SAXParserFactory factory = new SAXParserFactoryImpl();
// to be more secure, completely disable DOCTYPE declaration:
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
return factory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{
"pattern": "\\Qschemas/xsd/datatypes.dtd\\E"
},
{
"pattern": "\\Qschemas/xsd/datatypes.xml\\E"
},
{
"pattern": "\\Qschemas/xsd/xml.xsd\\E"
},
Expand Down

0 comments on commit e1b6afb

Please sign in to comment.