Skip to content

Commit

Permalink
Refactor XML parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Jul 25, 2023
1 parent 6816477 commit 5dc372a
Show file tree
Hide file tree
Showing 79 changed files with 618 additions and 618 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.ant.core.AntCorePlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
Expand Down Expand Up @@ -58,6 +59,7 @@ private int checkCriteria(InputSource contents) throws IOException {
}
catch (SAXException e) {
// we may be handed any kind of contents... it is normal we fail to parse
AntCorePlugin.log(e);
return INDETERMINATE;
}
catch (ParserConfigurationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.ant.internal.core.IAntCoreConstants;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
Expand Down Expand Up @@ -71,61 +67,17 @@ public StopParsingException() {
* successful to the point of finding the top-level element.
*/
private String fTopElementFound = null;
private SAXParserFactory fFactory;

private boolean fDefaultAttributeFound = false;
private boolean fTargetFound = false;
private boolean fAntElementFound = false;

private int fLevel = -1;

/**
* Creates a new SAX parser for use within this instance.
*
* @return The newly created parser.
* @throws ParserConfigurationException
* If a parser of the given configuration cannot be created.
* @throws SAXException
* If something in general goes wrong when creating the parser.
*/
private final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException, SAXException, SAXNotRecognizedException, SAXNotSupportedException {
// Initialize the parser.
final SAXParser parser = parserFactory.newSAXParser();
final XMLReader reader = parser.getXMLReader();
// disable DTD validation (bug 63625)
try {
// be sure validation is "off" or the feature to ignore DTD's will not apply
reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
}
catch (SAXNotRecognizedException e) {
// not a big deal if the parser does not recognize the features
}
catch (SAXNotSupportedException e) {
// not a big deal if the parser does not support the features
}
return parser;
}

private SAXParserFactory getFactory() {
synchronized (this) {
if (fFactory != null) {
return fFactory;
}
fFactory = SAXParserFactory.newInstance();
fFactory.setNamespaceAware(true);
}
return fFactory;
}

protected boolean parseContents(InputSource contents) throws IOException, ParserConfigurationException, SAXException {
// Parse the file into we have what we need (or an error occurs).
try {
fFactory = getFactory();
if (fFactory == null) {
return false;
}
final SAXParser parser = createParser(fFactory);
@SuppressWarnings("restriction")
final SAXParser parser = org.eclipse.core.internal.runtime.XmlProcessorFactory.createSAXParserNoExternal(true);
// to support external entities specified as relative URIs (see bug 63298)
contents.setSystemId("/"); //$NON-NLS-1$
parser.parse(contents, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ public static void linkBuildFailedMessage(String message, IProcess process) {
String fileName = null;
String lineNumber = ""; //$NON-NLS-1$
int fileStart = 0;
int index = message.indexOf("xml"); //$NON-NLS-1$
int index = message.indexOf(".xml"); //$NON-NLS-1$
if (index > 0) {
int numberStart = index + 4;
int numberStart = index + 5;
int numberEnd = message.indexOf(':', numberStart);
int fileEnd = index + 3;
int fileEnd = index + 4;
if (numberStart > 0 && fileEnd > 0) {
fileName = message.substring(fileStart, fileEnd).trim();
if (numberEnd > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*******************************************************************************
* Copyright (c) 2002, 2014 GEBIT Gesellschaft fuer EDV-Beratung
* und Informatik-Technologien mbH,
* und Informatik-Technologien mbH,
* Berlin, Duesseldorf, Frankfurt (Germany) and others.
*
* This program and the accompanying materials
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial implementation
* IBM Corporation - additional tests
Expand All @@ -22,7 +22,6 @@
import java.io.FileNotFoundException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.ant.core.AntCorePlugin;
Expand All @@ -35,6 +34,7 @@
import org.eclipse.ant.tests.ui.editor.performance.EditorTestHelper;
import org.eclipse.ant.tests.ui.editor.support.TestTextCompletionProcessor;
import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest;
import org.eclipse.core.internal.runtime.XmlProcessorFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.TextSelection;
Expand All @@ -47,14 +47,14 @@

/**
* Tests everything about code completion and code assistance.
*
*
*/
@SuppressWarnings("restriction")
public class CodeCompletionTest extends AbstractAntUITest {

/**
* Constructor for CodeCompletionTest.
*
*
* @param name
*/
public CodeCompletionTest(String name) {
Expand Down Expand Up @@ -692,7 +692,7 @@ public void testMixedElements() {
public void testFindChildElement() throws ParserConfigurationException {

// Create the test data
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilder docBuilder = XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
Document doc = docBuilder.newDocument();
Element parentElement = doc.createElement("parent"); //$NON-NLS-1$
Attr attribute = doc.createAttribute("att1"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.ant.internal.ui.AntUIPlugin;
import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants;
Expand Down Expand Up @@ -421,23 +419,6 @@ protected String getFileContentAsString(File aFile) throws FileNotFoundException
return getReaderContentAsString(bufferedReader);
}

/**
* @return a new SAX parser instrance
*/
protected SAXParser getSAXParser() {
SAXParser parser = null;
try {
parser = SAXParserFactory.newInstance().newSAXParser();
}
catch (ParserConfigurationException e) {
AntUIPlugin.log(e);
}
catch (SAXException e) {
AntUIPlugin.log(e);
}
return parser;
}

/**
* Parses the given input stream with the given parser using the given handler
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private Node getChildNodeNamedWithTypeFromNode(String aName, short aNodeType, No
private Document parseFile(String aFileName) {
Document tempDocument = null;

DocumentBuilderFactory tempFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory tempFactory = XmlProcessorFactory.createDocumentBuilderFactoryWithErrorOnDOCTYPE();
tempFactory.setIgnoringComments(true);
tempFactory.setIgnoringElementContentWhitespace(true);
tempFactory.setCoalescing(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.ant.internal.ui.dtd.schema.SchemaFactory;
import org.xml.sax.EntityResolver;
Expand Down Expand Up @@ -98,7 +97,9 @@ public ISchema parse(InputSource inputSource, EntityResolver entityResolver) thr
private XMLReader getXMLReader() throws ParseError {
SAXParser parser = null;
try {
parser = SAXParserFactory.newInstance().newSAXParser();
@SuppressWarnings("restriction")
SAXParser p = org.eclipse.core.internal.runtime.XmlProcessorFactory.createSAXParserNoExternal();
parser = p;
return parser.getXMLReader();
}
catch (ParserConfigurationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ public class Schema implements ISchema {

@Override
public IElement getElement(String qname) {
if (fErrorException != null) {
throw new RuntimeException(fErrorException);
}
return fElementMap.get(qname);
}

@Override
public IElement[] getElements() {
if (fErrorException != null) {
throw new RuntimeException(fErrorException);
}
return fElementMap.values().toArray(new IElement[fElementMap.size()]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ String getDescription(Element element) {
private Document parseFile(String aFileName) {
Document tempDocument = null;

DocumentBuilderFactory tempFactory = DocumentBuilderFactory.newInstance();
@SuppressWarnings("restriction")
DocumentBuilderFactory tempFactory = org.eclipse.core.internal.runtime.XmlProcessorFactory.createDocumentBuilderFactoryWithErrorOnDOCTYPE();
tempFactory.setIgnoringComments(true);
tempFactory.setIgnoringElementContentWhitespace(true);
tempFactory.setCoalescing(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.TreeMap;
import java.util.TreeSet;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
Expand Down Expand Up @@ -114,8 +114,9 @@ public BuildFileCreator(IJavaProject project, Shell shell) throws ParserConfigur
this.projectName = project.getProject().getName();
this.projectRoot = ExportUtil.getProjectRoot(project);
this.variable2valueMap = new LinkedHashMap<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
this.doc = dbf.newDocumentBuilder().newDocument();
@SuppressWarnings("restriction")
DocumentBuilder db = org.eclipse.core.internal.runtime.XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
this.doc = db.newDocument();
this.shell = shell;
}

Expand Down Expand Up @@ -303,7 +304,9 @@ public void createImports() {
// child
Document docCandidate;
try {
docCandidate = ExportUtil.parseXmlFile(file);
@SuppressWarnings("restriction")
Document doc1 = org.eclipse.core.internal.runtime.XmlProcessorFactory.parseWithErrorOnDOCTYPE(file);
docCandidate = doc1;
NodeList nodes = docCandidate.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++) {
Node node = nodes.item(j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.eclipse.ant.internal.ui.datatransfer;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -466,7 +467,9 @@ public String resolveLinkedResource(String s) {
*/
private IClasspathEntry convertVariableClasspathEntry(IRuntimeClasspathEntry entry) {
try {
Document doc = ExportUtil.parseXmlString(entry.getMemento());
ByteArrayInputStream is = new ByteArrayInputStream(entry.getMemento().getBytes());
@SuppressWarnings("restriction")
Document doc = org.eclipse.core.internal.runtime.XmlProcessorFactory.parseWithErrorOnDOCTYPE(is);
Element element = (Element) doc.getElementsByTagName("memento").item(0); //$NON-NLS-1$
String variableString = element.getAttribute("variableString"); //$NON-NLS-1$
ExportUtil.addVariable(variable2valueMap, variableString, ExportUtil.getProjectRoot(project));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.eclipse.ant.internal.ui.datatransfer;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
Expand All @@ -38,8 +37,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
Expand Down Expand Up @@ -80,7 +77,6 @@
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
* Collection of utility methods to help when exporting to an Ant build file.
Expand Down Expand Up @@ -462,7 +458,8 @@ public static String toString(Document doc) throws TransformerConfigurationExcep
StringWriter writer = new StringWriter();
Source source = new DOMSource(doc);
Result result = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
@SuppressWarnings("restriction")
TransformerFactory factory = org.eclipse.core.internal.runtime.XmlProcessorFactory.createTransformerFactoryWithErrorOnDOCTYPE();
// https://ant.apache.org/manual/Tasks/style.html
// Need this feature to set true for Java 9 to enable extension Functions in the presence of Security manager
factory.setFeature("http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions", Boolean.TRUE); //$NON-NLS-1$
Expand All @@ -485,26 +482,6 @@ public static String toString(Document doc) throws TransformerConfigurationExcep
return writer.toString();
}

/**
* Read XML file.
*/
public static Document parseXmlFile(File file) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
Document doc = factory.newDocumentBuilder().parse(file);
return doc;
}

/**
* Read XML string.
*/
public static Document parseXmlString(String s) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(s.getBytes()));
return doc;
}

/**
* Converts collection to a separated string.
*
Expand Down

0 comments on commit 5dc372a

Please sign in to comment.