1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp; 13 14 import java.io.BufferedInputStream ; 15 import java.io.IOException ; 16 17 import javax.xml.parsers.FactoryConfigurationError ; 18 import javax.xml.parsers.ParserConfigurationException ; 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 ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.helpers.DefaultHandler ; 32 33 37 public class CompCSFileValidator implements ISelectionStatusValidator { 38 39 42 public CompCSFileValidator() { 43 } 45 46 49 public IStatus validate(Object [] selection) { 50 51 if (selection.length == 0) { 53 return errorStatus(""); } 55 if ((selection[0] instanceof IFile) == false) { 57 return errorStatus(""); } 59 IFile file = (IFile)selection[0]; 60 if (isSimpleCSFile(file) == false) { 62 return errorStatus(PDEUIMessages.CompCSFileValidator_errorInvalidSimpleCS); 63 } 64 return okStatus(""); 67 } 68 69 72 private boolean isSimpleCSFile(IFile file) { 73 74 SimpleCSContentTypeHandler handler = new SimpleCSContentTypeHandler(); 75 try { 76 SAXParserWrapper parser = new SAXParserWrapper(); 77 parser.parse(new BufferedInputStream (file.getContents()), handler); 78 } catch (ParserConfigurationException e) { 79 return false; 80 } catch (AbortParseException e) { 81 return handler.isSimpleCS(); 82 } catch (SAXException e) { 83 return false; 84 } catch (FactoryConfigurationError e) { 85 return false; 86 } catch (IOException e) { 87 return false; 88 } catch (CoreException e) { 89 return false; 90 } 91 return handler.isSimpleCS(); 92 } 93 94 98 private static class AbortParseException extends SAXException { 99 102 private static final long serialVersionUID = 1L; 103 104 107 public AbortParseException() { 108 super("Parsing operation forcibly aborted to save on performance time."); } 110 } 111 112 116 private static class SimpleCSContentTypeHandler extends DefaultHandler { 117 118 private boolean fIsSimpleCS; 119 120 123 public SimpleCSContentTypeHandler() { 124 fIsSimpleCS = false; 125 } 126 127 130 public void startElement(String uri, String localName, String qName, 131 Attributes attributes) throws SAXException { 132 if (qName.equals(ISimpleCSConstants.ELEMENT_CHEATSHEET)) { 133 fIsSimpleCS = true; 134 } 135 throw new AbortParseException(); 138 } 139 140 143 public boolean isSimpleCS() { 144 return fIsSimpleCS; 145 } 146 } 147 148 152 private IStatus errorStatus(String message) { 153 return new Status( 154 IStatus.ERROR, 155 PDEPlugin.getPluginId(), 156 IStatus.ERROR, 157 message, 158 null); 159 } 160 161 165 private IStatus okStatus(String message) { 166 return new Status( 167 IStatus.OK, 168 PDEPlugin.getPluginId(), 169 IStatus.OK, 170 message, 171 null); 172 } 173 174 } 175 | Popular Tags |