KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > comp > CompCSFileValidator


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
25 import org.eclipse.pde.internal.core.util.SAXParserWrapper;
26 import org.eclipse.pde.internal.ui.PDEPlugin;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33 /**
34  * CompCSFileValidator
35  *
36  */

37 public class CompCSFileValidator implements ISelectionStatusValidator {
38
39     /**
40      *
41      */

42     public CompCSFileValidator() {
43         // NO-OP
44
}
45
46     /* (non-Javadoc)
47      * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[])
48      */

49     public IStatus validate(Object JavaDoc[] selection) {
50         
51         // Ensure something was selected
52
if (selection.length == 0) {
53             return errorStatus(""); //$NON-NLS-1$
54
}
55         // Ensure we have a file
56
if ((selection[0] instanceof IFile) == false) {
57             return errorStatus(""); //$NON-NLS-1$
58
}
59         IFile file = (IFile)selection[0];
60         // Ensure we have a simple cheat sheet file
61
if (isSimpleCSFile(file) == false) {
62             return errorStatus(PDEUIMessages.CompCSFileValidator_errorInvalidSimpleCS);
63         }
64         // If we got this far, we have a valid file
65
return okStatus(""); //$NON-NLS-1$
66

67     }
68
69     /**
70      * @param file
71      */

72     private boolean isSimpleCSFile(IFile file) {
73
74         SimpleCSContentTypeHandler handler = new SimpleCSContentTypeHandler();
75         try {
76             SAXParserWrapper parser = new SAXParserWrapper();
77             parser.parse(new BufferedInputStream JavaDoc(file.getContents()), handler);
78         } catch (ParserConfigurationException JavaDoc e) {
79             return false;
80         } catch (AbortParseException e) {
81             return handler.isSimpleCS();
82         } catch (SAXException JavaDoc e) {
83             return false;
84         } catch (FactoryConfigurationError JavaDoc e) {
85             return false;
86         } catch (IOException JavaDoc e) {
87             return false;
88         } catch (CoreException e) {
89             return false;
90         }
91         return handler.isSimpleCS();
92     }
93     
94     /**
95      * AbortParseException
96      *
97      */

98     private static class AbortParseException extends SAXException JavaDoc {
99         /**
100          *
101          */

102         private static final long serialVersionUID = 1L;
103
104         /**
105          *
106          */

107         public AbortParseException() {
108             super("Parsing operation forcibly aborted to save on performance time."); //$NON-NLS-1$
109
}
110     }
111     
112     /**
113      * SimpleCSContentTypeHandler
114      *
115      */

116     private static class SimpleCSContentTypeHandler extends DefaultHandler JavaDoc {
117         
118         private boolean fIsSimpleCS;
119         
120         /**
121          *
122          */

123         public SimpleCSContentTypeHandler() {
124             fIsSimpleCS = false;
125         }
126         
127         /* (non-Javadoc)
128          * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
129          */

130         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
131                 Attributes JavaDoc attributes) throws SAXException JavaDoc {
132             if (qName.equals(ISimpleCSConstants.ELEMENT_CHEATSHEET)) {
133                 fIsSimpleCS = true;
134             }
135             // Only care about the root node
136
// Abort parsing to save on performance
137
throw new AbortParseException();
138         }
139         
140         /**
141          * @return
142          */

143         public boolean isSimpleCS() {
144             return fIsSimpleCS;
145         }
146     }
147     
148     /**
149      * @param message
150      * @return
151      */

152     private IStatus errorStatus(String JavaDoc message) {
153         return new Status(
154                 IStatus.ERROR,
155                 PDEPlugin.getPluginId(),
156                 IStatus.ERROR,
157                 message,
158                 null);
159     }
160     
161     /**
162      * @param message
163      * @return
164      */

165     private IStatus okStatus(String JavaDoc message) {
166         return new Status(
167                 IStatus.OK,
168                 PDEPlugin.getPluginId(),
169                 IStatus.OK,
170                 message,
171                 null);
172     }
173
174 }
175
Popular Tags