nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | 19526ad | 2007-04-10 17:03:52 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2004, 2006 IBM Corporation and others. |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
amywu | 19526ad | 2007-04-10 17:03:52 +0000 | [diff] [blame^] | 7 | * |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
nitind | 4941845 | 2005-02-15 06:42:31 +0000 | [diff] [blame] | 11 | package org.eclipse.jst.jsp.core.internal.util; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 12 | |
| 13 | |
nitind | 0a4d3b5 | 2006-11-06 20:31:56 +0000 | [diff] [blame] | 14 | import java.io.BufferedInputStream; |
nitind | 6e3e71d | 2006-02-09 20:39:41 +0000 | [diff] [blame] | 15 | import java.io.ByteArrayInputStream; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 16 | import java.io.File; |
| 17 | import java.io.FileInputStream; |
| 18 | import java.io.FileNotFoundException; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 21 | import java.net.URL; |
| 22 | import java.net.URLConnection; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 23 | |
| 24 | import javax.xml.parsers.DocumentBuilder; |
| 25 | import javax.xml.parsers.DocumentBuilderFactory; |
| 26 | import javax.xml.parsers.FactoryConfigurationError; |
| 27 | import javax.xml.parsers.ParserConfigurationException; |
| 28 | |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 29 | import org.eclipse.jst.jsp.core.internal.Logger; |
david_williams | 2aecf08 | 2005-04-13 05:03:21 +0000 | [diff] [blame] | 30 | import org.eclipse.wst.sse.core.internal.util.JarUtilities; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 31 | import org.w3c.dom.DOMException; |
| 32 | import org.w3c.dom.DOMImplementation; |
| 33 | import org.w3c.dom.Document; |
| 34 | import org.w3c.dom.Element; |
| 35 | import org.w3c.dom.Node; |
| 36 | import org.w3c.dom.NodeList; |
| 37 | import org.xml.sax.EntityResolver; |
| 38 | import org.xml.sax.ErrorHandler; |
| 39 | import org.xml.sax.InputSource; |
| 40 | import org.xml.sax.SAXException; |
| 41 | import org.xml.sax.SAXParseException; |
| 42 | |
nitind | 0a4d3b5 | 2006-11-06 20:31:56 +0000 | [diff] [blame] | 43 | import com.ibm.icu.util.StringTokenizer; |
| 44 | |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 45 | /** |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 46 | * An XML Creator/Reader/Writer that 1) Ignores any DocumentType Nodes found |
| 47 | * within the document (as well as any entities) 2) Ignores any |
| 48 | * errors/exceptions from Xerces when loading a document 3) Can load Documents |
| 49 | * from within a .JAR file (***read-only***) |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | public class DocumentProvider { |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 53 | private Document document = null; |
| 54 | private ErrorHandler errorHandler = null; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 55 | private String fBaseReference; |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 56 | private String fileName = null; |
nitind | 6e3e71d | 2006-02-09 20:39:41 +0000 | [diff] [blame] | 57 | private boolean fValidating = false; |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 58 | private InputStream inputStream = null; |
| 59 | private String jarFileName = null; |
| 60 | private EntityResolver resolver = null; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 61 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 62 | private Node rootElement = null; |
| 63 | private String rootElementName = null; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 64 | |
| 65 | public DocumentProvider() { |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 66 | super(); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 67 | } |
| 68 | |
nitind | 63bacdc | 2005-08-11 03:28:18 +0000 | [diff] [blame] | 69 | private String _getFileName() { |
| 70 | if (inputStream != null) |
| 71 | return null; |
| 72 | else if (isJAR()) { |
| 73 | return getJarFileName(); |
| 74 | } |
| 75 | return getFileName(); |
| 76 | } |
| 77 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 78 | private Document _getParsedDocumentDOM2() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 79 | Document result = null; |
| 80 | |
| 81 | InputStream is = null; |
| 82 | try { |
| 83 | DocumentBuilder builder = getDocumentBuilder(); |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 84 | // DOMParser parser = new DOMParser(); |
| 85 | // parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", |
| 86 | // false);//$NON-NLS-1$ |
| 87 | // parser.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", |
| 88 | // false);//$NON-NLS-1$ |
| 89 | // parser.setErrorHandler(getNullErrorHandler()); |
| 90 | // parser.setEntityResolver(getNullEntityResolver()); |
| 91 | // is = getInputStream(); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 92 | builder.setEntityResolver(getEntityResolver()); |
| 93 | builder.setErrorHandler(getNullErrorHandler()); |
| 94 | is = getInputStream(); |
| 95 | if (is != null) |
| 96 | result = builder.parse(is, getBaseReference()); |
| 97 | } |
| 98 | catch (SAXException e) { |
| 99 | result = null; |
| 100 | // parsing exception, notify the user? |
nitind | 63bacdc | 2005-08-11 03:28:18 +0000 | [diff] [blame] | 101 | Logger.log(Logger.WARNING, "SAXException while reading descriptor " + _getFileName() + " " + e); //$NON-NLS-1$ //$NON-NLS-2$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 102 | } |
| 103 | catch (FileNotFoundException e) { |
| 104 | // NOT an "exceptional case"; do not Log |
| 105 | } |
| 106 | catch (IOException e) { |
nitind | 63bacdc | 2005-08-11 03:28:18 +0000 | [diff] [blame] | 107 | Logger.log(Logger.WARNING, "IOException while reading descriptor " + _getFileName() + " " + e); //$NON-NLS-1$ //$NON-NLS-2$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 108 | } |
| 109 | finally { |
| 110 | if (is != null) { |
| 111 | try { |
| 112 | is.close(); |
| 113 | } |
| 114 | catch (Exception e) { |
| 115 | // what can be done? |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return |
| 124 | */ |
| 125 | public String getBaseReference() { |
| 126 | return fBaseReference; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * |
| 131 | * @return Document |
| 132 | */ |
| 133 | public Document getDocument() { |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 134 | return getDocument(true); |
| 135 | } |
| 136 | |
| 137 | public Document getDocument(boolean createEmptyOnFailure) { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 138 | if (document == null) |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 139 | load(createEmptyOnFailure); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 140 | return document; |
| 141 | } |
| 142 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 143 | private DocumentBuilder getDocumentBuilder() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 144 | return CommonXML.getDocumentBuilder(isValidating()); |
| 145 | } |
| 146 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 147 | private DOMImplementation getDomImplementation() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 148 | DocumentBuilder builder = null; |
| 149 | try { |
| 150 | builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 151 | } |
| 152 | catch (ParserConfigurationException e1) { |
| 153 | Logger.logException(e1); |
| 154 | } |
| 155 | catch (FactoryConfigurationError e1) { |
| 156 | Logger.logException(e1); |
| 157 | } |
| 158 | DOMImplementation impl = builder.getDOMImplementation(); |
| 159 | return impl; |
| 160 | } |
| 161 | |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 162 | /************************************************************************* |
| 163 | * Takes a single string of the form "a/b/c" and ensures that that |
| 164 | * structure exists below the head element, down through 'c', and returns |
| 165 | * a <em>single</em> element 'c'. For multiple elements (such as |
| 166 | * multiple <macro> elements contained within a single |
| 167 | * <macros> element, full DOM access is required for searching and |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 168 | * child element manipulation. |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 169 | ************************************************************************/ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 170 | public Element getElement(String name) { |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 171 | Element result = null; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 172 | if (document == null) |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 173 | load(false); |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 174 | if (document != null) { |
| 175 | result = (Element) getNode(getRootElement(), name); |
| 176 | } |
| 177 | return result; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 181 | * Returns an EntityResolver that won't try to load and resolve ANY |
| 182 | * entities |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 183 | */ |
| 184 | private EntityResolver getEntityResolver() { |
| 185 | if (resolver == null) { |
| 186 | resolver = new EntityResolver() { |
| 187 | public InputSource resolveEntity(String publicID, String systemID) throws SAXException, IOException { |
| 188 | InputSource result = null; |
nitind | 6e3e71d | 2006-02-09 20:39:41 +0000 | [diff] [blame] | 189 | if (isValidating()) { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 190 | try { |
| 191 | URL spec = new URL("file://" + getBaseReference()); //$NON-NLS-1$ |
| 192 | URL url = new URL(spec, systemID); |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 193 | if (url.getProtocol().startsWith("file:")) { //$NON-NLS-1$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 194 | URLConnection connection = url.openConnection(); |
| 195 | result = new InputSource(systemID != null ? systemID : "/_" + toString()); //$NON-NLS-1$ |
| 196 | result.setPublicId(publicID); |
| 197 | result.setByteStream(connection.getInputStream()); |
| 198 | } |
| 199 | } |
| 200 | catch (Exception e) { |
| 201 | result = null; |
| 202 | } |
| 203 | } |
nitind | 6e3e71d | 2006-02-09 20:39:41 +0000 | [diff] [blame] | 204 | |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 205 | if (result == null) { |
nitind | 6e3e71d | 2006-02-09 20:39:41 +0000 | [diff] [blame] | 206 | result = new InputSource(new ByteArrayInputStream(new byte[0])); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 207 | result.setPublicId(publicID); |
| 208 | result.setSystemId(systemID != null ? systemID : "/_" + getClass().getName()); //$NON-NLS-1$ |
| 209 | } |
| 210 | return result; |
| 211 | } |
| 212 | }; |
| 213 | } |
| 214 | return resolver; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * |
| 219 | * @return java.lang.String |
| 220 | */ |
nitind | 21f317d | 2005-02-17 23:52:35 +0000 | [diff] [blame] | 221 | public String getFileName() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 222 | return fileName; |
| 223 | } |
| 224 | |
| 225 | /** |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 226 | * Returns and input stream to use as the source of the Document 1) from |
| 227 | * an InputStream set on this instance 2) from a JAR file with the given |
| 228 | * entry name 3) from a normal file |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 229 | * |
| 230 | * @return InputStream |
| 231 | */ |
| 232 | public InputStream getInputStream() throws FileNotFoundException { |
| 233 | if (inputStream != null) |
| 234 | return inputStream; |
| 235 | else if (isJAR()) { |
| 236 | return JarUtilities.getInputStream(getJarFileName(), getFileName()); |
| 237 | } |
| 238 | else { |
nitind | 0a4d3b5 | 2006-11-06 20:31:56 +0000 | [diff] [blame] | 239 | return new BufferedInputStream(new FileInputStream(getFileName())); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * |
| 245 | * @return java.lang.String |
| 246 | */ |
nitind | 21f317d | 2005-02-17 23:52:35 +0000 | [diff] [blame] | 247 | public String getJarFileName() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 248 | return jarFileName; |
| 249 | } |
| 250 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 251 | private Node getNamedChild(Node parent, String childName) { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 252 | if (parent == null) { |
| 253 | return null; |
| 254 | } |
| 255 | NodeList childList = parent.getChildNodes(); |
| 256 | for (int i = 0; i < childList.getLength(); i++) { |
| 257 | if (childList.item(i).getNodeName().equals(childName)) |
| 258 | return childList.item(i); |
| 259 | } |
| 260 | return null; |
| 261 | } |
| 262 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 263 | private Document getNewDocument() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 264 | Document result = null; |
| 265 | try { |
nitind | 7228c6e | 2005-08-03 21:36:32 +0000 | [diff] [blame] | 266 | result = getDomImplementation().createDocument("http://www.w3.org/XML/1998/namespace", getRootElementName(), null); //$NON-NLS-1$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 267 | NodeList children = result.getChildNodes(); |
| 268 | for (int i = 0; i < children.getLength(); i++) { |
| 269 | result.removeChild(children.item(i)); |
| 270 | } |
| 271 | // we're going through this effort to avoid a NS element |
nitind | 73e7641 | 2005-11-10 01:50:19 +0000 | [diff] [blame] | 272 | Element settings = result.createElementNS("http://www.w3.org/XML/1998/namespace", getRootElementName()); //$NON-NLS-1$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 273 | result.appendChild(settings); |
| 274 | return result; |
| 275 | } |
| 276 | catch (DOMException e) { |
| 277 | Logger.logException(e); |
| 278 | } |
| 279 | return null; |
| 280 | } |
| 281 | |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 282 | /************************************************************************* |
| 283 | * Takes a single string of the form "a/b/c" and ensures that that |
| 284 | * structure exists below the head element, down through 'c', and returns |
| 285 | * the element 'c'. |
| 286 | ************************************************************************/ |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 287 | private Node getNode(Node node, String name) { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 288 | StringTokenizer tokenizer = new StringTokenizer(name, "/"); //$NON-NLS-1$ |
| 289 | String token = null; |
| 290 | while (tokenizer.hasMoreTokens()) { |
| 291 | token = tokenizer.nextToken(); |
| 292 | if (getNamedChild(node, token) == null) |
| 293 | node.appendChild(document.createElement(token)); |
| 294 | node = getNamedChild(node, token); |
| 295 | } |
| 296 | return node; |
| 297 | } |
| 298 | |
| 299 | /** |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 300 | * Returns an ErrorHandler that will not stop the parser on reported |
| 301 | * errors |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 302 | */ |
| 303 | private ErrorHandler getNullErrorHandler() { |
| 304 | if (errorHandler == null) { |
| 305 | errorHandler = new ErrorHandler() { |
| 306 | public void error(SAXParseException exception) throws SAXException { |
david_williams | e3f4661 | 2005-04-07 01:12:35 +0000 | [diff] [blame] | 307 | Logger.log(Logger.WARNING, "SAXParseException with " + getJarFileName() + "/" + getFileName() + " (error) while reading descriptor: " + exception.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | public void fatalError(SAXParseException exception) throws SAXException { |
david_williams | e3f4661 | 2005-04-07 01:12:35 +0000 | [diff] [blame] | 311 | Logger.log(Logger.WARNING, "SAXParseException with " + getJarFileName() + "/" + getFileName() + " (fatalError) while reading descriptor: " + exception.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | public void warning(SAXParseException exception) throws SAXException { |
david_williams | e3f4661 | 2005-04-07 01:12:35 +0000 | [diff] [blame] | 315 | Logger.log(Logger.WARNING, "SAXParseException with " + getJarFileName() + "/" + getFileName() + " (warning) while reading descriptor: " + exception.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 316 | } |
| 317 | }; |
| 318 | } |
| 319 | return errorHandler; |
| 320 | } |
| 321 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 322 | private Document getParsedDocument() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 323 | Document result = null; |
| 324 | if (inputStream == null) { |
| 325 | File existenceTester = null; |
| 326 | if (isJAR()) |
| 327 | existenceTester = new File(getJarFileName()); |
| 328 | else |
| 329 | existenceTester = new File(getFileName()); |
| 330 | if (!existenceTester.exists()) |
| 331 | return null; |
| 332 | } |
| 333 | |
| 334 | result = _getParsedDocumentDOM2(); |
| 335 | |
| 336 | return result; |
| 337 | |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Returns the root Element of the current document |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 342 | * |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 343 | * @return org.w3c.dom.Element |
| 344 | */ |
| 345 | public Node getRootElement() { |
| 346 | return getRootElement(getDocument()); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns the/a root Element for the current document |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 351 | * |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 352 | * @return org.w3c.dom.Element |
| 353 | */ |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 354 | private Node getRootElement(Document doc) { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 355 | if (doc == null) |
| 356 | return null; |
| 357 | if (doc.getDocumentElement() != null) |
| 358 | return doc.getDocumentElement(); |
| 359 | try { |
| 360 | Element newRootElement = doc.createElement(getRootElementName()); |
| 361 | doc.appendChild(newRootElement); |
| 362 | return newRootElement; |
| 363 | } |
| 364 | catch (DOMException e) { |
| 365 | Logger.logException(e); |
| 366 | } |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * |
| 372 | * @return java.lang.String |
| 373 | */ |
| 374 | public java.lang.String getRootElementName() { |
| 375 | return rootElementName; |
| 376 | } |
| 377 | |
david_williams | 757ccfe | 2005-05-01 08:03:21 +0000 | [diff] [blame] | 378 | private boolean isJAR() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 379 | return getJarFileName() != null; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * @return |
| 384 | */ |
| 385 | public boolean isValidating() { |
| 386 | return fValidating; |
| 387 | } |
| 388 | |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 389 | void load(boolean createEmptyOnFailure) { |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 390 | // rootElementName and fileName are expected to be defined at this |
| 391 | // point |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 392 | document = getParsedDocument(); |
| 393 | if (document != null) { |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 394 | if (rootElementName != null) |
| 395 | rootElement = getRootElement(document); |
| 396 | else |
| 397 | rootElement = document.getDocumentElement(); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | if (document == null || rootElement == null) { |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 401 | if (createEmptyOnFailure) { |
| 402 | document = getNewDocument(); |
| 403 | if (document != null) { |
| 404 | NodeList children = document.getChildNodes(); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 405 | for (int i = 0; i < children.getLength(); i++) { |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 406 | if (children.item(i).getNodeType() == Node.ELEMENT_NODE && children.item(i).getNodeName().equals(getRootElementName())) |
david_williams | cc8918e | 2005-11-19 20:27:41 +0000 | [diff] [blame] | 407 | rootElement = children.item(i); |
nitind | 24835b9 | 2006-01-16 23:20:53 +0000 | [diff] [blame] | 408 | } |
| 409 | if (rootElement == null) { |
| 410 | for (int i = 0; i < children.getLength(); i++) { |
| 411 | if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { |
| 412 | rootElement = children.item(i); |
| 413 | break; |
| 414 | } |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 422 | /** |
| 423 | * @param string |
| 424 | */ |
| 425 | public void setBaseReference(String string) { |
| 426 | fBaseReference = string; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 431 | * @param newFileName |
| 432 | * java.lang.String |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 433 | */ |
| 434 | public void setFileName(java.lang.String newFileName) { |
| 435 | fileName = newFileName; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Sets the inputStream for which to provide a Document. |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 440 | * |
| 441 | * @param inputStream |
| 442 | * The inputStream to set |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 443 | */ |
nitind | 63bacdc | 2005-08-11 03:28:18 +0000 | [diff] [blame] | 444 | public void setInputStream(InputStream iStream) { |
| 445 | this.inputStream = iStream; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 450 | * @param newJarFileName |
| 451 | * java.lang.String |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 452 | */ |
| 453 | public void setJarFileName(java.lang.String newJarFileName) { |
| 454 | jarFileName = newJarFileName; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * |
nitind | 3a1c971 | 2004-12-07 06:47:36 +0000 | [diff] [blame] | 459 | * @param newRootElementName |
| 460 | * java.lang.String |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 461 | */ |
| 462 | public void setRootElementName(java.lang.String newRootElementName) { |
| 463 | rootElementName = newRootElementName; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * @param b |
| 468 | */ |
| 469 | public void setValidating(boolean b) { |
| 470 | fValidating = b; |
| 471 | } |
nitind | 0a4d3b5 | 2006-11-06 20:31:56 +0000 | [diff] [blame] | 472 | } |