1 11 package org.eclipse.ltk.internal.core.refactoring; 12 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import javax.xml.parsers.ParserConfigurationException ; 20 import javax.xml.parsers.SAXParser ; 21 import javax.xml.parsers.SAXParserFactory ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Status; 26 27 import org.eclipse.ltk.core.refactoring.IRefactoringCoreStatusCodes; 28 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 29 import org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor; 30 31 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringContributionManager; 32 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.SAXNotRecognizedException ; 37 import org.xml.sax.SAXNotSupportedException ; 38 import org.xml.sax.XMLReader ; 39 import org.xml.sax.helpers.DefaultHandler ; 40 41 46 public final class RefactoringSessionReader extends DefaultHandler { 47 48 49 private String fComment= null; 50 51 52 private final boolean fProjects; 53 54 58 private List fRefactoringDescriptors= null; 59 60 61 private boolean fSessionFound= false; 62 63 64 private String fVersion= null; 65 66 73 public RefactoringSessionReader(final boolean projects) { 74 fProjects= projects; 75 } 76 77 88 private SAXParser createParser(final SAXParserFactory factory) throws ParserConfigurationException , SAXException { 89 90 final SAXParser parser= factory.newSAXParser(); 91 final XMLReader reader= parser.getXMLReader(); 92 93 try { 94 95 reader.setFeature("http://xml.org/sax/features/validation", false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 98 } catch (SAXNotRecognizedException exception) { 99 } catch (SAXNotSupportedException exception) { 101 } 103 return parser; 104 } 105 106 116 public RefactoringSessionDescriptor readSession(final InputSource source) throws CoreException { 117 fSessionFound= false; 118 try { 119 source.setSystemId("/"); createParser(SAXParserFactory.newInstance()).parse(source, this); 121 if (!fSessionFound) 122 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, RefactoringCoreMessages.RefactoringSessionReader_no_session, null)); 123 if (fRefactoringDescriptors != null) { 124 if (fVersion == null || "".equals(fVersion)) throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.MISSING_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_missing_version_information, null)); 126 if (!IRefactoringSerializationConstants.CURRENT_VERSION.equals(fVersion)) 127 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.UNSUPPORTED_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_unsupported_version_information, null)); 128 return new RefactoringSessionDescriptor((RefactoringDescriptor[]) fRefactoringDescriptors.toArray(new RefactoringDescriptor[fRefactoringDescriptors.size()]), fVersion, fComment); 129 } 130 } catch (IOException exception) { 131 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 132 } catch (ParserConfigurationException exception) { 133 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 134 } catch (SAXException exception) { 135 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 136 } finally { 137 fRefactoringDescriptors= null; 138 fVersion= null; 139 fComment= null; 140 } 141 return null; 142 } 143 144 147 public void startElement(final String uri, final String localName, final String qualifiedName, final Attributes attributes) throws SAXException { 148 if (IRefactoringSerializationConstants.ELEMENT_REFACTORING.equals(qualifiedName)) { 149 final int length= attributes.getLength(); 150 final Map map= new HashMap (length); 151 String id= ""; String stamp= ""; String description= ""; String comment= null; 155 String flags= "0"; String project= null; 157 for (int index= 0; index < length; index++) { 158 final String name= attributes.getQName(index); 159 final String value= attributes.getValue(index); 160 if (IRefactoringSerializationConstants.ATTRIBUTE_ID.equals(name)) 161 id= value; 162 else if (IRefactoringSerializationConstants.ATTRIBUTE_STAMP.equals(name)) 163 stamp= value; 164 else if (IRefactoringSerializationConstants.ATTRIBUTE_DESCRIPTION.equals(name)) 165 description= value; 166 else if (IRefactoringSerializationConstants.ATTRIBUTE_FLAGS.equals(name)) 167 flags= value; 168 else if (IRefactoringSerializationConstants.ATTRIBUTE_COMMENT.equals(name)) { 169 if (!"".equals(value)) comment= value; 171 } else if (fProjects && IRefactoringSerializationConstants.ATTRIBUTE_PROJECT.equals(name)) 172 project= value; 173 else if (!"".equals(name) && !"".equals(value)) map.put(name, value); 175 } 176 int flag= 0; 177 try { 178 flag= Integer.parseInt(flags); 179 } catch (NumberFormatException exception) { 180 } 182 183 final RefactoringDescriptor descriptor= RefactoringContributionManager.getInstance().createDescriptor(id, project, description, comment, map, flag); 184 if (descriptor != null) { 185 try { 186 descriptor.setTimeStamp(Long.valueOf(stamp).longValue()); 187 } catch (NumberFormatException exception) { 188 } 190 if (fRefactoringDescriptors == null) 191 fRefactoringDescriptors= new ArrayList (); 192 fRefactoringDescriptors.add(descriptor); 193 } 194 } else if (IRefactoringSerializationConstants.ELEMENT_SESSION.equals(qualifiedName)) { 195 fSessionFound= true; 196 final String version= attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_VERSION); 197 if (version != null && !"".equals(version)) fVersion= version; 199 fComment= attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT); 200 } 201 } 202 } 203 | Popular Tags |