2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.project;
19 import com.android.ide.eclipse.adt.AndroidConstants;
20 import com.android.sdklib.xml.AndroidManifestParser.ManifestErrorHandler;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IMarker;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.xml.sax.Locator;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.SAXParseException;
29 import org.xml.sax.helpers.DefaultHandler;
32 * XML error handler used by the parser to report errors/warnings.
34 public class XmlErrorHandler extends DefaultHandler implements ManifestErrorHandler {
36 private final IJavaProject mJavaProject;
37 /** file being parsed */
38 private final IFile mFile;
39 /** link to the delta visitor, to set the xml error flag */
40 private final XmlErrorListener mErrorListener;
43 * Classes which implement this interface provide a method that deals
46 public interface XmlErrorListener {
48 * Sent when an XML error is detected.
50 public void errorFound();
53 public static class BasicXmlErrorListener implements XmlErrorListener {
54 public boolean mHasXmlError = false;
56 public void errorFound() {
61 public XmlErrorHandler(IJavaProject javaProject, IFile file, XmlErrorListener errorListener) {
62 mJavaProject = javaProject;
64 mErrorListener = errorListener;
67 public XmlErrorHandler(IFile file, XmlErrorListener errorListener) {
68 this(null, file, errorListener);
73 * @param exception the parsing exception
74 * @throws SAXException
77 public void error(SAXParseException exception) throws SAXException {
78 handleError(exception, exception.getLineNumber());
82 * Xml Fatal Error call back
83 * @param exception the parsing exception
84 * @throws SAXException
87 public void fatalError(SAXParseException exception) throws SAXException {
88 handleError(exception, exception.getLineNumber());
92 * Xml Warning call back
93 * @param exception the parsing exception
94 * @throws SAXException
97 public void warning(SAXParseException exception) throws SAXException {
99 BaseProjectHelper.markResource(mFile,
100 AndroidConstants.MARKER_XML,
101 exception.getMessage(),
102 exception.getLineNumber(),
103 IMarker.SEVERITY_WARNING);
107 protected final IFile getFile() {
112 * Handles a parsing error and an optional line number.
116 public void handleError(Exception exception, int lineNumber) {
117 if (mErrorListener != null) {
118 mErrorListener.errorFound();
121 String message = exception.getMessage();
122 if (message == null) {
123 message = "Unknown error " + exception.getClass().getCanonicalName();
127 BaseProjectHelper.markResource(mFile,
128 AndroidConstants.MARKER_XML,
131 IMarker.SEVERITY_ERROR);
136 * Checks that a class is valid and can be used in the Android Manifest.
138 * Errors are put as {@link IMarker} on the manifest file.
140 * @param className the fully qualified name of the class to test.
141 * @param superClassName the fully qualified name of the class it is supposed to extend.
142 * @param testVisibility if <code>true</code>, the method will check the visibility of
143 * the class or of its constructors.
145 public void checkClass(Locator locator, String className, String superClassName,
146 boolean testVisibility) {
147 if (mJavaProject == null) {
150 // we need to check the validity of the activity.
151 String result = BaseProjectHelper.testClassForManifest(mJavaProject,
152 className, superClassName, testVisibility);
153 if (result != BaseProjectHelper.TEST_CLASS_OK) {
154 // get the line number
155 int line = locator.getLineNumber();
158 IMarker marker = BaseProjectHelper.markResource(getFile(),
159 AndroidConstants.MARKER_ANDROID, result, line, IMarker.SEVERITY_ERROR);
161 // add custom attributes to be used by the manifest editor.
162 if (marker != null) {
164 marker.setAttribute(AndroidConstants.MARKER_ATTR_TYPE,
165 AndroidConstants.MARKER_ATTR_TYPE_ACTIVITY);
166 marker.setAttribute(AndroidConstants.MARKER_ATTR_CLASS, className);
167 } catch (CoreException e) {