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