KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > core > contentDescriber > AntBuildfileContentDescriber


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  * Philippe Ombredanne (pombredanne@nexb.com) - bug 125367
11  *******************************************************************************/

12 package org.eclipse.ant.internal.core.contentDescriber;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.Reader JavaDoc;
17
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19
20 import org.eclipse.core.internal.content.XMLContentDescriber;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExecutableExtension;
24 import org.eclipse.core.runtime.content.IContentDescription;
25 import org.xml.sax.InputSource JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 /**
29  * A content describer for Ant buildfiles.
30  * <p>
31  * If project top level element is found
32  * then if:
33  * target sub-elements are found returns VALID
34  * default attribute is found returns VALID
35  * some other likely Ant element is found (classpath, import, macrodef, path, property, taskdef, typedef) returns VALID
36  * else:
37  * returns INDETERMINATE
38  * else
39  * returns INDETERMINATE
40  * </p>
41  *
42  * @since 3.1
43  */

44 public final class AntBuildfileContentDescriber extends XMLContentDescriber implements IExecutableExtension {
45
46     /* (Intentionally not included in javadoc)
47      * Determines the validation status for the given contents.
48      *
49      * @param contents the contents to be evaluated
50      * @return one of the following:<ul>
51      * <li><code>VALID</code></li>,
52      * <li><code>INVALID</code></li>,
53      * <li><code>INDETERMINATE</code></li>
54      * </ul>
55      * @throws IOException
56      */

57     private int checkCriteria(InputSource JavaDoc contents) throws IOException JavaDoc {
58         AntHandler antHandler = new AntHandler();
59         try {
60             if (!antHandler.parseContents(contents)) {
61                 return INDETERMINATE;
62             }
63         } catch (SAXException JavaDoc e) {
64             // we may be handed any kind of contents... it is normal we fail to parse
65
return INDETERMINATE;
66         } catch (ParserConfigurationException JavaDoc e) {
67             // some bad thing happened - force this describer to be disabled
68
String JavaDoc message = "Internal Error: XML parser configuration error during content description for Ant buildfiles"; //$NON-NLS-1$
69
throw new RuntimeException JavaDoc(message);
70         }
71         // Check to see if we matched our criteria.
72
if (antHandler.hasRootProjectElement()) {
73             if (antHandler.hasProjectDefaultAttribute() || antHandler.hasTargetElement() || antHandler.hasAntElement()) {
74                 //project and default attribute or project and target element(s)
75
//or project and top level ant element(s) (classpath, import, macrodef, path, property, taskdef, typedef)
76
return VALID;
77             }
78             //only a top level project element...maybe an Ant buildfile
79
return INDETERMINATE;
80         }
81             
82         return INDETERMINATE;
83     }
84
85     /* (Intentionally not included in javadoc)
86      * @see IContentDescriber#describe(InputStream, IContentDescription)
87      */

88     public int describe(InputStream JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
89         // call the basic XML describer to do basic recognition
90
if (super.describe(contents, description) == INVALID) {
91             return INVALID;
92         }
93         // super.describe will have consumed some chars, need to rewind
94
contents.reset();
95         // Check to see if we matched our criteria.
96
return checkCriteria(new InputSource JavaDoc(contents));
97     }
98
99     /* (Intentionally not included in javadoc)
100      * @see IContentDescriber#describe(Reader, IContentDescription)
101      */

102     public int describe(Reader JavaDoc contents, IContentDescription description) throws IOException JavaDoc {
103         // call the basic XML describer to do basic recognition
104
if (super.describe(contents, description) == INVALID) {
105             return INVALID;
106         }
107         // super.describe will have consumed some chars, need to rewind
108
contents.reset();
109         // Check to see if we matched our criteria.
110
return checkCriteria(new InputSource JavaDoc(contents));
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
115      */

116     public void setInitializationData(IConfigurationElement config, String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
117     }
118 }
Popular Tags