1 11 package org.eclipse.ui.internal.ide.model; 12 13 import java.io.File ; 14 import java.io.StringReader ; 15 16 import javax.xml.parsers.*; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.QualifiedName; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.ui.IResourceActionFilter; 25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 26 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.XMLReader ; 31 import org.xml.sax.ext.LexicalHandler ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 66 67 public class PropertyParser extends DefaultHandler implements LexicalHandler { 68 69 private static SAXParser parser; 71 private static SAXParserFactory parserFactory; 72 73 private IResource parseResource = null; 75 int x = initializeParser(); 76 77 private int initializeParser() { 78 try { 79 parserFactory = SAXParserFactory.newInstance(); 80 parserFactory.setNamespaceAware(true); 81 try { 82 parserFactory.setFeature("http://xml.org/sax/features/string-interning", true); parserFactory.setValidating(false); 84 } catch (SAXException e) { 85 IDEWorkbenchPlugin.log("Problem initializing parser", new Status(IStatus.ERROR,IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.ERROR, "Problem initializing parser", e)); } 87 parser = parserFactory.newSAXParser(); 88 XMLReader reader = parser.getXMLReader(); 89 reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); } catch (Exception e) { 91 IDEWorkbenchPlugin.log("Problem initializing parser", new Status(IStatus.ERROR,IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.ERROR, "Problem initializing parser", e)); } 93 return 1; 94 } 95 96 102 synchronized public void parseResource(IResource resource) throws Exception { 103 if (resource == null) 104 return; 105 parseResource = resource; 106 IPath location = resource.getLocation(); 108 if (location == null) 109 return; 110 File file = location.toFile(); 111 if (file.length() == 0L) { 112 long modTime = parseResource.getModificationStamp(); 118 QualifiedName modKey = new QualifiedName(IDEWorkbenchPlugin.IDE_WORKBENCH, WorkbenchResource.XML_LAST_MOD); 119 try { 120 parseResource.setPersistentProperty(modKey, new Long (modTime).toString()); 121 } catch (CoreException c) { 122 IDEWorkbenchPlugin.log("Problem parsing element", c.getStatus()); } 124 return; 125 } 126 try { 127 parser.parse(file, this); 128 } catch (SAXException s) { 129 if (!s.getMessage().equals("PropertyParser stop")) { IDEWorkbenchPlugin.log("Problem parsing file", new Status(IStatus.ERROR,IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.ERROR, "Problem parsing file", s)); } 137 } 138 } 139 140 145 146 public void startElement(String uri, String elementName, String qName, Attributes attributes) 147 throws SAXException { 148 149 161 long modTime = parseResource.getModificationStamp(); 162 QualifiedName modKey = new QualifiedName(IDEWorkbenchPlugin.IDE_WORKBENCH, WorkbenchResource.XML_LAST_MOD); 163 try { 164 parseResource.setPersistentProperty(modKey, new Long (modTime).toString()); 165 } catch (CoreException c) { 166 IDEWorkbenchPlugin.log("Problem parsing element", c.getStatus()); } 168 QualifiedName key; 170 String propertyName = IResourceActionFilter.XML_FIRST_TAG; 171 key = new QualifiedName(IDEWorkbenchPlugin.IDE_WORKBENCH, propertyName); 172 try { 173 parseResource.setPersistentProperty(key, elementName); 174 } catch (CoreException c) { 175 IDEWorkbenchPlugin.log("Problem parsing element", c.getStatus()); } 177 throw new SAXException ("PropertyParser stop"); } 182 183 184 187 public void comment(char[] ch, int start, int length) throws SAXException { 188 } 190 191 194 public void endCDATA() throws SAXException { 195 } 197 198 201 public void endDTD() throws SAXException { 202 } 204 205 208 public void endEntity(String name) throws SAXException { 209 } 211 212 215 public void startCDATA() throws SAXException { 216 } 218 219 223 public void startDTD(String name, String publicId, String systemId) 224 throws SAXException { 225 226 234 if (systemId == null) 235 return; 236 237 QualifiedName qname = new QualifiedName(IDEWorkbenchPlugin.IDE_WORKBENCH, IResourceActionFilter.XML_DTD_NAME); 238 try { 239 parseResource.setPersistentProperty(qname, systemId); 240 } catch (CoreException c) { 241 IDEWorkbenchPlugin.log("Problem parsing dtd element", c.getStatus()); } 243 } 244 245 248 public void startEntity(String name) throws SAXException { 249 } 251 252 259 public InputSource resolveEntity(String publicId, String systemId) 260 throws SAXException { 261 return new InputSource (new StringReader ("")); } 263 } 264 | Popular Tags |