KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > content > XMLRootElementContentDescriber


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime.content;
12
13 import java.io.*;
14 import java.util.Hashtable JavaDoc;
15 import javax.xml.parsers.ParserConfigurationException JavaDoc;
16 import org.eclipse.core.internal.content.*;
17 import org.eclipse.core.internal.runtime.RuntimeLog;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23 /**
24  * A content describer for detecting the name of the top-level element or the
25  * DTD system identifier in an XML file.
26  * <p>
27  * This executable extension supports two parameters:
28  * "dtd" and "element".
29  * At least one of them <strong>must</strong> be provided. If the
30  * <code>":-"</code> method is used, then the value is treated as
31  * "element".
32  * </p>
33  * <p>
34  * This class is not intended to be subclassed or instantiated by clients,
35  * only to be referenced by the "describer" configuration element in
36  * extensions to the <code>org.eclipse.core.runtime.contentTypes</code>
37  * extension point.
38  * </p>
39  *
40  * @since 3.0
41  */

42 public final class XMLRootElementContentDescriber extends XMLContentDescriber implements IExecutableExtension {
43     private static final String JavaDoc DTD_TO_FIND = "dtd"; //$NON-NLS-1$
44
private static final String JavaDoc ELEMENT_TO_FIND = "element"; //$NON-NLS-1$
45
/* (Intentionally not included in javadoc)
46      * The system identifier that we wish to find. This value will be
47      * initialized by the <code>setInitializationData</code> method. If no
48      * value is provided, then this means that we don't care what the system
49      * identifier will be.
50      */

51     private String JavaDoc dtdToFind = null;
52     /* (Intentionally not included in javadoc)
53      * The top-level element we are looking for. This value will be initialized
54      * by the <code>setInitializationData</code> method. If no value is
55      * provided, then this means that we don't care what the top-level element
56      * will be.
57      */

58     private String JavaDoc elementToFind = null;
59
60     /* (Intentionally not included in javadoc)
61      * Determines the validation status for the given contents.
62      *
63      * @param contents the contents to be evaluated
64      * @return one of the following:<ul>
65      * <li><code>VALID</code></li>,
66      * <li><code>INVALID</code></li>,
67      * <li><code>INDETERMINATE</code></li>
68      * </ul>
69      * @throws IOException
70      */

71     private int checkCriteria(InputSource JavaDoc contents) throws IOException {
72         XMLRootHandler xmlHandler = new XMLRootHandler(elementToFind != null);
73         try {
74             if (!xmlHandler.parseContents(contents))
75                 return INDETERMINATE;
76         } catch (SAXException JavaDoc e) {
77             // we may be handed any kind of contents... it is normal we fail to parse
78
return INDETERMINATE;
79         } catch (ParserConfigurationException JavaDoc e) {
80             // some bad thing happened - force this describer to be disabled
81
String JavaDoc message = ContentMessages.content_parserConfiguration;
82             RuntimeLog.log(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, e));
83             throw new RuntimeException JavaDoc(message);
84         }
85         // Check to see if we matched our criteria.
86
if ((elementToFind != null) && (!elementToFind.equals(xmlHandler.getRootName())))
87             return INDETERMINATE;
88         if ((dtdToFind != null) && (!dtdToFind.equals(xmlHandler.getDTD())))
89             return INDETERMINATE;
90         // We must be okay then.
91
return VALID;
92     }
93
94     /* (Intentionally not included in javadoc)
95      * @see IContentDescriber#describe(InputStream, IContentDescription)
96      */

97     public int describe(InputStream contents, IContentDescription description) throws IOException {
98         // call the basic XML describer to do basic recognition
99
if (super.describe(contents, description) == INVALID)
100             return INVALID;
101         // super.describe will have consumed some chars, need to rewind
102
contents.reset();
103         // Check to see if we matched our criteria.
104
return checkCriteria(new InputSource JavaDoc(contents));
105     }
106
107     /* (Intentionally not included in javadoc)
108      * @see IContentDescriber#describe(Reader, IContentDescription)
109      */

110     public int describe(Reader contents, IContentDescription description) throws IOException {
111         // call the basic XML describer to do basic recognition
112
if (super.describe(contents, description) == INVALID)
113             return INVALID;
114         // super.describe will have consumed some chars, need to rewind
115
contents.reset();
116         // Check to see if we matched our criteria.
117
return checkCriteria(new InputSource JavaDoc(contents));
118     }
119
120     /* (Intentionally not included in javadoc)
121      * @see IExecutableExtension#setInitializationData
122      */

123     public void setInitializationData(final IConfigurationElement config, final String JavaDoc propertyName, final Object JavaDoc data) throws CoreException {
124         if (data instanceof String JavaDoc)
125             elementToFind = (String JavaDoc) data;
126         else if (data instanceof Hashtable JavaDoc) {
127             Hashtable JavaDoc parameters = (Hashtable JavaDoc) data;
128             dtdToFind = (String JavaDoc) parameters.get(DTD_TO_FIND);
129             elementToFind = (String JavaDoc) parameters.get(ELEMENT_TO_FIND);
130         }
131         if (dtdToFind == null && elementToFind == null) {
132             String JavaDoc message = NLS.bind(ContentMessages.content_badInitializationData, XMLRootElementContentDescriber.class.getName());
133             throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null));
134         }
135     }
136 }
137
Popular Tags