Ignore:
Timestamp:
2018-06-08T22:43:20+02:00 (18 months ago)
Author:
Don-vip
Message:

add new XmlUtils class with more "safe factories" methods

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r13869 r13901  
    12671267    private static String getImgUrlFromWikiInfoPage(final String base, final String fn) {
    12681268        try {
    1269             final XMLReader parser = Utils.newSafeSAXParser().getXMLReader();
     1269            final XMLReader parser = XmlUtils.newSafeSAXParser().getXMLReader();
    12701270            parser.setContentHandler(new DefaultHandler() {
    12711271                @Override
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r13838 r13901  
    6767import javax.script.ScriptEngine;
    6868import javax.script.ScriptEngineManager;
    69 import javax.xml.XMLConstants;
    7069import javax.xml.parsers.DocumentBuilder;
    71 import javax.xml.parsers.DocumentBuilderFactory;
    7270import javax.xml.parsers.ParserConfigurationException;
    7371import javax.xml.parsers.SAXParser;
    74 import javax.xml.parsers.SAXParserFactory;
    7572import javax.xml.validation.SchemaFactory;
    76 import javax.xml.validation.SchemaFactoryConfigurationError;
    7773
    7874import org.openstreetmap.josm.spi.preferences.Config;
     
    13391335     * Returns the W3C XML Schema factory implementation. Robust method dealing with ContextClassLoader problems.
    13401336     * @return the W3C XML Schema factory implementation
     1337     * @deprecated Use {@link XmlUtils#newXmlSchemaFactory}
    13411338     * @since 13715
    13421339     */
     1340    @Deprecated
    13431341    public static SchemaFactory newXmlSchemaFactory() {
    1344         try {
    1345             return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    1346         } catch (SchemaFactoryConfigurationError e) {
    1347             Logging.debug(e);
    1348             // Can happen with icedtea-web. Use workaround from https://issues.apache.org/jira/browse/GERONIMO-6185
    1349             Thread currentThread = Thread.currentThread();
    1350             ClassLoader old = currentThread.getContextClassLoader();
    1351             currentThread.setContextClassLoader(null);
    1352             try {
    1353                 return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    1354             } finally {
    1355                 currentThread.setContextClassLoader(old);
    1356             }
    1357         }
     1342        return XmlUtils.newXmlSchemaFactory();
    13581343    }
    13591344
     
    13621347     * @return a new secure DOM builder, supporting XML namespaces
    13631348     * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
     1349     * @deprecated Use {@link XmlUtils#newSafeDOMBuilder}
    13641350     * @since 10404
    13651351     */
     1352    @Deprecated
    13661353    public static DocumentBuilder newSafeDOMBuilder() throws ParserConfigurationException {
    1367         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    1368         builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    1369         builderFactory.setNamespaceAware(true);
    1370         builderFactory.setValidating(false);
    1371         return builderFactory.newDocumentBuilder();
     1354        return XmlUtils.newSafeDOMBuilder();
    13721355    }
    13731356
     
    13811364     * @throws IOException if any IO errors occur.
    13821365     * @throws SAXException for SAX errors.
     1366     * @deprecated Use {@link XmlUtils#parseSafeDOM}
    13831367     * @since 10404
    13841368     */
     1369    @Deprecated
    13851370    public static Document parseSafeDOM(InputStream is) throws ParserConfigurationException, IOException, SAXException {
    1386         long start = System.currentTimeMillis();
    1387         Logging.debug("Starting DOM parsing of {0}", is);
    1388         Document result = newSafeDOMBuilder().parse(is);
    1389         if (Logging.isDebugEnabled()) {
    1390             Logging.debug("DOM parsing done in {0}", getDurationString(System.currentTimeMillis() - start));
    1391         }
    1392         return result;
     1371        return XmlUtils.parseSafeDOM(is);
    13931372    }
    13941373
     
    13981377     * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
    13991378     * @throws SAXException for SAX errors.
     1379     * @deprecated Use {@link XmlUtils#newSafeSAXParser}
    14001380     * @since 8287
    14011381     */
     1382    @Deprecated
    14021383    public static SAXParser newSafeSAXParser() throws ParserConfigurationException, SAXException {
    1403         SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    1404         parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    1405         parserFactory.setNamespaceAware(true);
    1406         return parserFactory.newSAXParser();
     1384        return XmlUtils.newSafeSAXParser();
    14071385    }
    14081386
     
    14161394     * @throws SAXException for SAX errors.
    14171395     * @throws IOException if any IO errors occur.
     1396     * @deprecated Use {@link XmlUtils#parseSafeSAX}
    14181397     * @since 8347
    14191398     */
     1399    @Deprecated
    14201400    public static void parseSafeSAX(InputSource is, DefaultHandler dh) throws ParserConfigurationException, SAXException, IOException {
    1421         long start = System.currentTimeMillis();
    1422         Logging.debug("Starting SAX parsing of {0} using {1}", is, dh);
    1423         newSafeSAXParser().parse(is, dh);
    1424         if (Logging.isDebugEnabled()) {
    1425             Logging.debug("SAX parsing done in {0}", getDurationString(System.currentTimeMillis() - start));
    1426         }
     1401        XmlUtils.parseSafeSAX(is, dh);
    14271402    }
    14281403
  • trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r13715 r13901  
    245245    private Iterable<Object> start(final Reader in, final ContentHandler contentHandler) throws SAXException, IOException {
    246246        try {
    247             XMLReader reader = Utils.newSafeSAXParser().getXMLReader();
     247            XMLReader reader = XmlUtils.newSafeSAXParser().getXMLReader();
    248248            reader.setContentHandler(contentHandler);
    249249            try {
     
    285285     */
    286286    public Iterable<Object> startWithValidation(final Reader in, String namespace, String schemaSource) throws SAXException {
    287         SchemaFactory factory = Utils.newXmlSchemaFactory();
     287        SchemaFactory factory = XmlUtils.newXmlSchemaFactory();
    288288        try (CachedFile cf = new CachedFile(schemaSource); InputStream mis = cf.getInputStream()) {
    289289            Schema schema = factory.newSchema(new StreamSource(mis));
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java

    r12869 r13901  
    2222import org.openstreetmap.josm.tools.OpenBrowser;
    2323import org.openstreetmap.josm.tools.Utils;
     24import org.openstreetmap.josm.tools.XmlUtils;
    2425import org.w3c.dom.Document;
    2526import org.xml.sax.SAXException;
     
    127128
    128129            try (InputStream in = connection.getContent()) {
    129                 return retrieveDebugToken(Utils.parseSafeDOM(in));
     130                return retrieveDebugToken(XmlUtils.parseSafeDOM(in));
    130131            }
    131132        } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException t) {
Note: See TracChangeset for help on using the changeset viewer.