[14627] | 1 | /*
|
---|
| 2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | *
|
---|
| 16 | */
|
---|
| 17 | package org.apache.tools.ant.util;
|
---|
| 18 |
|
---|
| 19 | import java.io.File;
|
---|
| 20 | import javax.xml.parsers.DocumentBuilder;
|
---|
| 21 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
| 22 | import javax.xml.parsers.FactoryConfigurationError;
|
---|
| 23 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 24 | import javax.xml.parsers.SAXParser;
|
---|
| 25 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 26 | import org.apache.tools.ant.BuildException;
|
---|
| 27 | import org.xml.sax.Parser;
|
---|
| 28 | import org.xml.sax.SAXException;
|
---|
| 29 | import org.xml.sax.XMLReader;
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Collection of helper methods that retrieve a ParserFactory or
|
---|
| 33 | * Parsers and Readers.
|
---|
| 34 | *
|
---|
| 35 | * <p>This class will create only a single factory instance.</p>
|
---|
| 36 | *
|
---|
| 37 | *
|
---|
| 38 | * @since Ant 1.5
|
---|
| 39 | */
|
---|
| 40 | public class JAXPUtils {
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Helper for systemId.
|
---|
| 44 | *
|
---|
| 45 | * @since Ant 1.6
|
---|
| 46 | */
|
---|
| 47 | private static final FileUtils fu = FileUtils.newFileUtils();
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Parser factory to use to create parsers.
|
---|
| 51 | * @see #getParserFactory
|
---|
| 52 | *
|
---|
| 53 | * @since Ant 1.5
|
---|
| 54 | */
|
---|
| 55 | private static SAXParserFactory parserFactory = null;
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Parser Factory to create Namespace aware parsers.
|
---|
| 59 | *
|
---|
| 60 | * @since Ant 1.6
|
---|
| 61 | */
|
---|
| 62 | private static SAXParserFactory nsParserFactory = null;
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Parser factory to use to create document builders.
|
---|
| 66 | *
|
---|
| 67 | * @since Ant 1.6
|
---|
| 68 | */
|
---|
| 69 | private static DocumentBuilderFactory builderFactory = null;
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Returns the parser factory to use. Only one parser factory is
|
---|
| 73 | * ever created by this method and is then cached for future use.
|
---|
| 74 | *
|
---|
| 75 | * @return a SAXParserFactory to use
|
---|
| 76 | *
|
---|
| 77 | * @since Ant 1.5
|
---|
| 78 | */
|
---|
| 79 | public static synchronized SAXParserFactory getParserFactory()
|
---|
| 80 | throws BuildException {
|
---|
| 81 |
|
---|
| 82 | if (parserFactory == null) {
|
---|
| 83 | parserFactory = newParserFactory();
|
---|
| 84 | }
|
---|
| 85 | return parserFactory;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Returns the parser factory to use to create namespace aware parsers.
|
---|
| 90 | *
|
---|
| 91 | * @return a SAXParserFactory to use which supports manufacture of
|
---|
| 92 | * namespace aware parsers
|
---|
| 93 | *
|
---|
| 94 | * @since Ant 1.6
|
---|
| 95 | */
|
---|
| 96 | public static synchronized SAXParserFactory getNSParserFactory()
|
---|
| 97 | throws BuildException {
|
---|
| 98 |
|
---|
| 99 | if (nsParserFactory == null) {
|
---|
| 100 | nsParserFactory = newParserFactory();
|
---|
| 101 | nsParserFactory.setNamespaceAware(true);
|
---|
| 102 | }
|
---|
| 103 | return nsParserFactory;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Returns a new parser factory instance.
|
---|
| 108 | *
|
---|
| 109 | * @since Ant 1.5
|
---|
| 110 | */
|
---|
| 111 | public static SAXParserFactory newParserFactory() throws BuildException {
|
---|
| 112 |
|
---|
| 113 | try {
|
---|
| 114 | return SAXParserFactory.newInstance();
|
---|
| 115 | } catch (FactoryConfigurationError e) {
|
---|
| 116 | throw new BuildException("XML parser factory has not been "
|
---|
| 117 | + "configured correctly: "
|
---|
| 118 | + e.getMessage(), e);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * Returns a newly created SAX 1 Parser, using the default parser
|
---|
| 124 | * factory.
|
---|
| 125 | *
|
---|
| 126 | * @return a SAX 1 Parser.
|
---|
| 127 | * @see #getParserFactory
|
---|
| 128 | * @since Ant 1.5
|
---|
| 129 | */
|
---|
| 130 | public static Parser getParser() throws BuildException {
|
---|
| 131 | try {
|
---|
| 132 | return newSAXParser(getParserFactory()).getParser();
|
---|
| 133 | } catch (SAXException e) {
|
---|
| 134 | throw convertToBuildException(e);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Returns a newly created SAX 2 XMLReader, using the default parser
|
---|
| 140 | * factory.
|
---|
| 141 | *
|
---|
| 142 | * @return a SAX 2 XMLReader.
|
---|
| 143 | * @see #getParserFactory
|
---|
| 144 | * @since Ant 1.5
|
---|
| 145 | */
|
---|
| 146 | public static XMLReader getXMLReader() throws BuildException {
|
---|
| 147 | try {
|
---|
| 148 | return newSAXParser(getParserFactory()).getXMLReader();
|
---|
| 149 | } catch (SAXException e) {
|
---|
| 150 | throw convertToBuildException(e);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Returns a newly created SAX 2 XMLReader, which is namespace aware
|
---|
| 156 | *
|
---|
| 157 | * @return a SAX 2 XMLReader.
|
---|
| 158 | * @see #getParserFactory
|
---|
| 159 | * @since Ant 1.6
|
---|
| 160 | */
|
---|
| 161 | public static XMLReader getNamespaceXMLReader() throws BuildException {
|
---|
| 162 | try {
|
---|
| 163 | return newSAXParser(getNSParserFactory()).getXMLReader();
|
---|
| 164 | } catch (SAXException e) {
|
---|
| 165 | throw convertToBuildException(e);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * This is a best attempt to provide a URL.toExternalForm() from
|
---|
| 171 | * a file URL. Some parsers like Crimson choke on uri that are made of
|
---|
| 172 | * backslashed paths (ie windows) as it is does not conform
|
---|
| 173 | * URI specifications.
|
---|
| 174 | * @param file the file to create the system id from.
|
---|
| 175 | * @return the systemid corresponding to the given file.
|
---|
| 176 | * @since Ant 1.5.2
|
---|
| 177 | */
|
---|
| 178 | public static String getSystemId(File file) {
|
---|
| 179 | return fu.toURI(file.getAbsolutePath());
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * Returns a newly created DocumentBuilder.
|
---|
| 184 | *
|
---|
| 185 | * @return a DocumentBuilder
|
---|
| 186 | * @since Ant 1.6
|
---|
| 187 | */
|
---|
| 188 | public static DocumentBuilder getDocumentBuilder() throws BuildException {
|
---|
| 189 | try {
|
---|
| 190 | return getDocumentBuilderFactory().newDocumentBuilder();
|
---|
| 191 | } catch (ParserConfigurationException e) {
|
---|
| 192 | throw new BuildException(e);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * @return a new SAXParser instance as helper for getParser and
|
---|
| 198 | * getXMLReader.
|
---|
| 199 | *
|
---|
| 200 | * @since Ant 1.5
|
---|
| 201 | */
|
---|
| 202 | private static SAXParser newSAXParser(SAXParserFactory factory)
|
---|
| 203 | throws BuildException {
|
---|
| 204 | try {
|
---|
| 205 | return factory.newSAXParser();
|
---|
| 206 | } catch (ParserConfigurationException e) {
|
---|
| 207 | throw new BuildException("Cannot create parser for the given "
|
---|
| 208 | + "configuration: " + e.getMessage(), e);
|
---|
| 209 | } catch (SAXException e) {
|
---|
| 210 | throw convertToBuildException(e);
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Translate a SAXException into a BuildException
|
---|
| 216 | *
|
---|
| 217 | * @since Ant 1.5
|
---|
| 218 | */
|
---|
| 219 | private static BuildException convertToBuildException(SAXException e) {
|
---|
| 220 | Exception nested = e.getException();
|
---|
| 221 | if (nested != null) {
|
---|
| 222 | return new BuildException(nested);
|
---|
| 223 | } else {
|
---|
| 224 | return new BuildException(e);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Obtains the default builder factory if not already.
|
---|
| 230 | *
|
---|
| 231 | * @since Ant 1.6
|
---|
| 232 | */
|
---|
| 233 | private static synchronized
|
---|
| 234 | DocumentBuilderFactory getDocumentBuilderFactory()
|
---|
| 235 | throws BuildException {
|
---|
| 236 | if (builderFactory == null) {
|
---|
| 237 | try {
|
---|
| 238 | builderFactory = DocumentBuilderFactory.newInstance();
|
---|
| 239 | } catch (FactoryConfigurationError e) {
|
---|
| 240 | throw new BuildException("Document builder factory has not "
|
---|
| 241 | + "been configured correctly: "
|
---|
| 242 | + e.getMessage(), e);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | return builderFactory;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | }
|
---|