• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
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.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.project;
18 
19 import com.android.ide.eclipse.adt.AndroidConstants;
20 
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.SAXParseException;
25 import org.xml.sax.helpers.DefaultHandler;
26 
27 /**
28  * XML error handler used by the parser to report errors/warnings.
29  */
30 public class XmlErrorHandler extends DefaultHandler {
31 
32     /** file being parsed */
33     private IFile mFile;
34 
35     /** link to the delta visitor, to set the xml error flag */
36     private XmlErrorListener mErrorListener;
37 
38     /**
39      * Classes which implement this interface provide a method that deals
40      * with XML errors.
41      */
42     public interface XmlErrorListener {
43         /**
44          * Sent when an XML error is detected.
45          */
errorFound()46         public void errorFound();
47     }
48 
49     public static class BasicXmlErrorListener implements XmlErrorListener {
50         public boolean mHasXmlError = false;
51 
errorFound()52         public void errorFound() {
53             mHasXmlError = true;
54         }
55     }
56 
XmlErrorHandler(IFile file, XmlErrorListener errorListener)57     public XmlErrorHandler(IFile file, XmlErrorListener errorListener) {
58         mFile = file;
59         mErrorListener = errorListener;
60     }
61 
62     /**
63      * Xml Error call back
64      * @param exception the parsing exception
65      * @throws SAXException
66      */
67     @Override
error(SAXParseException exception)68     public void error(SAXParseException exception) throws SAXException {
69         handleError(exception, exception.getLineNumber());
70     }
71 
72     /**
73      * Xml Fatal Error call back
74      * @param exception the parsing exception
75      * @throws SAXException
76      */
77     @Override
fatalError(SAXParseException exception)78     public void fatalError(SAXParseException exception) throws SAXException {
79         handleError(exception, exception.getLineNumber());
80     }
81 
82     /**
83      * Xml Warning call back
84      * @param exception the parsing exception
85      * @throws SAXException
86      */
87     @Override
warning(SAXParseException exception)88     public void warning(SAXParseException exception) throws SAXException {
89         if (mFile != null) {
90             BaseProjectHelper.addMarker(mFile,
91                     AndroidConstants.MARKER_XML,
92                     exception.getMessage(),
93                     exception.getLineNumber(),
94                     IMarker.SEVERITY_WARNING);
95         }
96     }
97 
getFile()98     protected final IFile getFile() {
99         return mFile;
100     }
101 
102     /**
103      * Handles a parsing error and an optional line number.
104      * @param exception
105      * @param lineNumber
106      */
handleError(Exception exception, int lineNumber)107     protected void handleError(Exception exception, int lineNumber) {
108         if (mErrorListener != null) {
109             mErrorListener.errorFound();
110         }
111 
112         String message = exception.getMessage();
113         if (message == null) {
114             message = "Unknown error " + exception.getClass().getCanonicalName();
115         }
116 
117         if (mFile != null) {
118             if (lineNumber != -1) {
119                 BaseProjectHelper.addMarker(mFile,
120                         AndroidConstants.MARKER_XML,
121                         message,
122                         lineNumber,
123                         IMarker.SEVERITY_ERROR);
124             } else {
125                 BaseProjectHelper.addMarker(mFile,
126                         AndroidConstants.MARKER_XML,
127                         message,
128                         IMarker.SEVERITY_ERROR);
129             }
130         }
131     }
132 }
133