From 21660b7678a4317e417e236d2cd36d12ec102434 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Fri, 17 Jan 2003 10:27:12 +0000 Subject: [PATCH] Actually enable namespace support in ProjectHelper2 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273819 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/helper/ProjectHelper2.java | 2 +- .../org/apache/tools/ant/util/JAXPUtils.java | 56 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java index 524a3c54a..698dfab37 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java @@ -161,7 +161,7 @@ public class ProjectHelper2 extends ProjectHelper { /** * SAX 2 style parser used to parse the given file. */ - context.parser =JAXPUtils.getXMLReader(); + context.parser =JAXPUtils.getNamespaceXMLReader(); String uri = fu.toURI(context.buildFile.getAbsolutePath()); diff --git a/src/main/org/apache/tools/ant/util/JAXPUtils.java b/src/main/org/apache/tools/ant/util/JAXPUtils.java index 4d2f185fe..3a9b1d125 100644 --- a/src/main/org/apache/tools/ant/util/JAXPUtils.java +++ b/src/main/org/apache/tools/ant/util/JAXPUtils.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -90,6 +90,14 @@ public class JAXPUtils { */ private static SAXParserFactory parserFactory = null; + /** + * Parser Factory to create Namespace aware parsers. + * + * @since Ant 1.6 + */ + private static SAXParserFactory nsParserFactory = null; + + /** * Returns the parser factory to use. Only one parser factory is * ever created by this method and is then cached for future use. @@ -98,7 +106,7 @@ public class JAXPUtils { * * @since Ant 1.5 */ - public synchronized static SAXParserFactory getParserFactory() + public synchronized static SAXParserFactory getParserFactory() throws BuildException { if (parserFactory == null) { @@ -107,6 +115,24 @@ public class JAXPUtils { return parserFactory; } + /** + * Returns the parser factory to use to create namespace aware parsers. + * + * @return a SAXParserFactory to use which supports manufacture of + * namespace aware parsers + * + * @since Ant 1.6 + */ + public synchronized static SAXParserFactory getNSParserFactory() + throws BuildException { + + if (nsParserFactory == null) { + nsParserFactory = newParserFactory(); + nsParserFactory.setNamespaceAware(true); + } + return nsParserFactory; + } + /** * Returns a new parser factory instance. * @@ -118,7 +144,7 @@ public class JAXPUtils { return SAXParserFactory.newInstance(); } catch (FactoryConfigurationError e) { throw new BuildException("XML parser factory has not been " - + "configured correctly: " + + "configured correctly: " + e.getMessage(), e); } } @@ -133,7 +159,7 @@ public class JAXPUtils { */ public static Parser getParser() throws BuildException { try { - return newSAXParser().getParser(); + return newSAXParser(getParserFactory()).getParser(); } catch (SAXException e) { throw convertToBuildException(e); } @@ -149,7 +175,22 @@ public class JAXPUtils { */ public static XMLReader getXMLReader() throws BuildException { try { - return newSAXParser().getXMLReader(); + return newSAXParser(getParserFactory()).getXMLReader(); + } catch (SAXException e) { + throw convertToBuildException(e); + } + } + + /** + * Returns a newly created SAX 2 XMLReader, which is namespace aware + * + * @return a SAX 2 XMLReader. + * @see #getParserFactory + * @since Ant 1.6 + */ + public static XMLReader getNamespaceXMLReader() throws BuildException { + try { + return newSAXParser(getNSParserFactory()).getXMLReader(); } catch (SAXException e) { throw convertToBuildException(e); } @@ -174,9 +215,10 @@ public class JAXPUtils { * * @since Ant 1.5 */ - private static SAXParser newSAXParser() throws BuildException { + private static SAXParser newSAXParser(SAXParserFactory factory) + throws BuildException { try { - return getParserFactory().newSAXParser(); + return factory.newSAXParser(); } catch (ParserConfigurationException e) { throw new BuildException("Cannot create parser for the given " + "configuration: " + e.getMessage(), e);