diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEXMLHelper.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEXMLHelper.java index ea50bc7c83f..3c50ab05174 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEXMLHelper.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEXMLHelper.java @@ -13,64 +13,8 @@ *******************************************************************************/ package org.eclipse.pde.internal.core.util; -import java.lang.ref.SoftReference; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.FactoryConfigurationError; -import javax.xml.parsers.ParserConfigurationException; - -/** - * PDEXMLHelper - * - */ public class PDEXMLHelper { - - protected static PDEXMLHelper fPinstance; - protected static DocumentBuilderFactory fDOMFactory; - protected static List> fDOMParserQueue; - protected static int fSAXPoolLimit; - protected static int fDOMPoolLimit; - protected static final int FMAXPOOLLIMIT = 1; - - protected PDEXMLHelper() throws FactoryConfigurationError { - fDOMFactory = XmlDocumentBuilderFactory.createDocumentBuilderFactoryWithErrorOnDOCTYPE(); - fDOMParserQueue = Collections.synchronizedList(new LinkedList<>()); - fSAXPoolLimit = FMAXPOOLLIMIT; - fDOMPoolLimit = FMAXPOOLLIMIT; - } - - public synchronized DocumentBuilder getDefaultDOMParser() throws ParserConfigurationException { - - DocumentBuilder parser = null; - if (fDOMParserQueue.isEmpty()) { - parser = fDOMFactory.newDocumentBuilder(); - } else { - SoftReference reference = fDOMParserQueue.remove(0); - if (reference.get() != null) { - parser = reference.get(); - } else { - parser = fDOMFactory.newDocumentBuilder(); - } - } - return parser; - } - - public static PDEXMLHelper Instance() throws FactoryConfigurationError { - if (fPinstance == null) { - fPinstance = new PDEXMLHelper(); - } - return fPinstance; - } - - public synchronized void recycleDOMParser(DocumentBuilder parser) { - if (fDOMParserQueue.size() < fDOMPoolLimit) { - SoftReference reference = new SoftReference<>(parser); - fDOMParserQueue.add(reference); - } + private PDEXMLHelper() { } public static String getWritableString(String source) { @@ -79,26 +23,14 @@ public static String getWritableString(String source) { } StringBuilder buf = new StringBuilder(); for (int i = 0; i < source.length(); i++) { - char c = source.charAt(i); - switch (c) { - case '&' : - buf.append("&"); //$NON-NLS-1$ - break; - case '<' : - buf.append("<"); //$NON-NLS-1$ - break; - case '>' : - buf.append(">"); //$NON-NLS-1$ - break; - case '\'' : - buf.append("'"); //$NON-NLS-1$ - break; - case '\"' : - buf.append("""); //$NON-NLS-1$ - break; - default : - buf.append(c); - break; + char character = source.charAt(i); + switch (character) { + case '&' -> buf.append("&"); //$NON-NLS-1$ + case '<' -> buf.append("<"); //$NON-NLS-1$ + case '>' -> buf.append(">"); //$NON-NLS-1$ + case '\'' -> buf.append("'"); //$NON-NLS-1$ + case '\"' -> buf.append("""); //$NON-NLS-1$ + default -> buf.append(character); } } return buf.toString(); @@ -111,55 +43,22 @@ public static String getWritableAttributeString(String source) { } // Trim the leading and trailing whitespace if any source = source.trim(); - // Translate source using a buffer StringBuilder buffer = new StringBuilder(); // Translate source character by character for (int i = 0; i < source.length(); i++) { char character = source.charAt(i); switch (character) { - case '&' : - buffer.append("&"); //$NON-NLS-1$ - break; - case '<' : - buffer.append("<"); //$NON-NLS-1$ - break; - case '>' : - buffer.append(">"); //$NON-NLS-1$ - break; - case '\'' : - buffer.append("'"); //$NON-NLS-1$ - break; - case '\"' : - buffer.append("""); //$NON-NLS-1$ - break; - case '\r' : - buffer.append(" "); //$NON-NLS-1$ - break; - case '\n' : - buffer.append(" "); //$NON-NLS-1$ - break; - default : - buffer.append(character); - break; + case '&' -> buffer.append("&"); //$NON-NLS-1$ + case '<' -> buffer.append("<"); //$NON-NLS-1$ + case '>' -> buffer.append(">"); //$NON-NLS-1$ + case '\'' -> buffer.append("'"); //$NON-NLS-1$ + case '\"' -> buffer.append("""); //$NON-NLS-1$ + case '\r' -> buffer.append(" "); //$NON-NLS-1$ + case '\n' -> buffer.append(" "); //$NON-NLS-1$ + default -> buffer.append(character); } } return buffer.toString(); } - public static int getSAXPoolLimit() { - return fSAXPoolLimit; - } - - public static void setSAXPoolLimit(int poolLimit) { - fSAXPoolLimit = poolLimit; - } - - public static int getDOMPoolLimit() { - return fDOMPoolLimit; - } - - public static void setDOMPoolLimit(int poolLimit) { - fDOMPoolLimit = poolLimit; - } - } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/DOMParserWrapper.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/DOMParserWrapper.java deleted file mode 100755 index 3b7a7739621..00000000000 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/DOMParserWrapper.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 IBM Corporation and others. - * - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ - -package org.eclipse.pde.ui.tests.util; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.FactoryConfigurationError; -import javax.xml.parsers.ParserConfigurationException; - -import org.eclipse.pde.internal.core.util.PDEXMLHelper; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -public class DOMParserWrapper implements AutoCloseable { - - protected DocumentBuilder fParser; - protected boolean isdisposed; - - public DOMParserWrapper() throws ParserConfigurationException, FactoryConfigurationError { - fParser = PDEXMLHelper.Instance().getDefaultDOMParser(); - isdisposed = false; - } - - // Explicit disposal - public void dispose() { - if (isdisposed == false) { - PDEXMLHelper.Instance().recycleDOMParser(fParser); - isdisposed = true; - } - } - - public Document parse(File f) throws SAXException, IOException { - return fParser.parse(f); - } - - public Document newDocument() { - return fParser.newDocument(); - } - - // NOTE: If other parser method calls are required, the corresponding - // wrapper method needs to be added here - - @Override - public void close() { - dispose(); - } - -} diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ALLXMLUtilTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ALLXMLUtilTests.java deleted file mode 100644 index b1af49a22fa..00000000000 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ALLXMLUtilTests.java +++ /dev/null @@ -1,24 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006, 2017 IBM Corporation and others. - * - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.pde.ui.tests.util.xml; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({ ParserWrapperTestCase.class }) -public class ALLXMLUtilTests { - -} diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ParserWrapperTestCase.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ParserWrapperTestCase.java deleted file mode 100644 index 9471bb5a7db..00000000000 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/xml/ParserWrapperTestCase.java +++ /dev/null @@ -1,163 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. - * - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ - -package org.eclipse.pde.ui.tests.util.xml; - -import static org.junit.Assert.assertFalse; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -import javax.xml.parsers.FactoryConfigurationError; -import javax.xml.parsers.ParserConfigurationException; - -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.pde.internal.core.XMLDefaultHandler; -import org.eclipse.pde.internal.core.util.SAXParserWrapper; -import org.eclipse.pde.ui.tests.PDETestsPlugin; -import org.eclipse.pde.ui.tests.util.DOMParserWrapper; -import org.junit.Before; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.xml.sax.SAXException; - -public class ParserWrapperTestCase { - - protected static final int FTHREADCOUNT = 5; - protected static final int FSAX = 0; - protected static final int FDOM = 1; - protected static File fXMLFile; - protected static final String FFILENAME = "/plugin.xml"; //$NON-NLS-1$ - - @Before - public void setUp() throws Exception { - PDETestsPlugin plugin = PDETestsPlugin.getDefault(); - if (plugin == null) - throw new Exception("ERROR: Macro plug-in uninitialized"); //$NON-NLS-1$ - Bundle bundle = plugin.getBundle(); - if (bundle == null) - throw new Exception("ERROR: Bundle uninitialized"); //$NON-NLS-1$ - URL url = bundle.getEntry(FFILENAME); - if (url == null) - throw new Exception("ERROR: URL not found: " + FFILENAME); //$NON-NLS-1$ - String path = FileLocator.resolve(url).getPath(); - if ("".equals(path)) //$NON-NLS-1$ - throw new Exception("ERROR: URL unresolved: " + FFILENAME); //$NON-NLS-1$ - fXMLFile = new File(path); - } - - @Test - public void testSAXParserWrapperConcurrency() throws Exception { - - ParserThread[] threads = new ParserThread[FTHREADCOUNT]; - - for (int x = 0; x < FTHREADCOUNT; x++) { - threads[x] = new ParserThread(FSAX, fXMLFile); - threads[x].start(); - } - - for (int x = 0; x < FTHREADCOUNT; x++) { - threads[x].join(); - assertFalse(threads[x].getError()); - } - - } - - @Test - public void testDOMParserWrapperConcurrency() throws Exception { - - ParserThread[] threads = new ParserThread[FTHREADCOUNT]; - - for (int x = 0; x < FTHREADCOUNT; x++) { - threads[x] = new ParserThread(FDOM, fXMLFile); - threads[x].start(); - } - - for (int x = 0; x < FTHREADCOUNT; x++) { - threads[x].join(); - assertFalse(threads[x].getError()); - } - - } - - public static class ParserThread extends Thread { - - protected final int FITERATIONS = 100; - protected File fParserXMLFile; - protected boolean fError; - protected int fParserType; - - public ParserThread(int parserType, File file) { - fError = false; - fParserType = parserType; - fParserXMLFile = file; - } - - @Override - public void run() { - - if (fParserType == ParserWrapperTestCase.FSAX) { - runSAX(); - } else { - runDOM(); - } - - } - - public void runSAX() { - - for (int x = 0; x < FITERATIONS; x++) { - - try { - XMLDefaultHandler handler = new XMLDefaultHandler(); - SAXParserWrapper.parse(fParserXMLFile, handler); - } catch (ParserConfigurationException | SAXException | FactoryConfigurationError | IOException e) { - e.printStackTrace(); - fError = true; - } - // If an error was encountered abort the thread - // Any type of exception experienced is bad - if (fError) - return; - - } - - } - - public void runDOM() { - - for (int x = 0; x < FITERATIONS; x++) { - - try (DOMParserWrapper parser = new DOMParserWrapper()) { - parser.parse(fParserXMLFile); - } catch (ParserConfigurationException | SAXException | FactoryConfigurationError | IOException e) { - e.printStackTrace(); - fError = true; - } - // If an error was encountered abort the thread - // Any type of exception experienced is bad - if (fError) - return; - - } - } - - public boolean getError() { - return fError; - } - - } - -}