1 11 package org.eclipse.ltk.internal.core.refactoring; 12 13 import com.ibm.icu.text.Collator; 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.parsers.FactoryConfigurationError ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.eclipse.core.runtime.Assert; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Status; 29 30 import org.eclipse.ltk.core.refactoring.IRefactoringCoreStatusCodes; 31 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 32 33 import org.w3c.dom.Attr ; 34 import org.w3c.dom.DOMException ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.NamedNodeMap ; 37 import org.w3c.dom.Node ; 38 39 44 public final class RefactoringSessionTransformer { 45 46 47 private static final class AttributeComparator implements Comparator { 48 49 52 public int compare(final Object first, final Object second) { 53 final Attr predecessor= (Attr ) first; 54 final Attr successor= (Attr ) second; 55 return Collator.getInstance().compare(predecessor.getName(), successor.getName()); 56 } 57 } 58 59 60 private Document fDocument= null; 61 62 63 private final boolean fProjects; 64 65 66 private Node fRefactoring= null; 67 68 69 private List fRefactoringArguments= null; 70 71 72 private Node fSession= null; 73 74 75 private List fSessionArguments= null; 76 77 84 public RefactoringSessionTransformer(final boolean projects) { 85 fProjects= projects; 86 } 87 88 97 private void addArguments(final Node node, final List list) { 98 final NamedNodeMap map= node.getAttributes(); 99 if (map != null) { 100 Collections.sort(list, new AttributeComparator()); 101 for (final Iterator iterator= list.iterator(); iterator.hasNext();) { 102 final Attr attribute= (Attr ) iterator.next(); 103 map.setNamedItem(attribute); 104 } 105 } 106 } 107 108 136 public void beginRefactoring(final String id, long stamp, final String project, final String description, final String comment, final int flags) throws CoreException { 137 Assert.isNotNull(id); 138 Assert.isNotNull(description); 139 Assert.isTrue(flags >= RefactoringDescriptor.NONE); 140 try { 141 if (fDocument == null) 142 fDocument= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 143 } catch (ParserConfigurationException exception) { 144 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 145 } catch (FactoryConfigurationError exception) { 146 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 147 } 148 if (fRefactoring == null) { 149 try { 150 fRefactoringArguments= new ArrayList (16); 151 fRefactoring= fDocument.createElement(IRefactoringSerializationConstants.ELEMENT_REFACTORING); 152 Attr attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_ID); 153 attribute.setValue(id); 154 fRefactoringArguments.add(attribute); 155 if (stamp >= 0) { 156 attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_STAMP); 157 attribute.setValue(new Long (stamp).toString()); 158 fRefactoringArguments.add(attribute); 159 } 160 if (flags != RefactoringDescriptor.NONE) { 161 attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_FLAGS); 162 attribute.setValue(String.valueOf(flags)); 163 fRefactoringArguments.add(attribute); 164 } 165 attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_DESCRIPTION); 166 attribute.setValue(description); 167 fRefactoringArguments.add(attribute); 168 if (comment != null && !"".equals(comment)) { attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT); 170 attribute.setValue(comment); 171 fRefactoringArguments.add(attribute); 172 } 173 if (project != null && fProjects) { 174 attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_PROJECT); 175 attribute.setValue(project); 176 fRefactoringArguments.add(attribute); 177 } 178 if (fSession == null) 179 fDocument.appendChild(fRefactoring); 180 else 181 fSession.appendChild(fRefactoring); 182 } catch (DOMException exception) { 183 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null)); 184 } 185 } 186 } 187 188 206 public void beginSession(final String comment, final String version) throws CoreException { 207 if (fDocument == null) { 208 try { 209 fDocument= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 210 fSession= fDocument.createElement(IRefactoringSerializationConstants.ELEMENT_SESSION); 211 fSessionArguments= new ArrayList (2); 212 Attr attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_VERSION); 213 attribute.setValue(version); 214 fSessionArguments.add(attribute); 215 if (comment != null && !"".equals(comment)) { attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT); 217 attribute.setValue(comment); 218 fSessionArguments.add(attribute); 219 } 220 fDocument.appendChild(fSession); 221 } catch (DOMException exception) { 222 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null)); 223 } catch (ParserConfigurationException exception) { 224 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null)); 225 } 226 } 227 } 228 229 243 public void createArgument(final String name, final String value) throws CoreException { 244 Assert.isNotNull(name); 245 Assert.isTrue(!"".equals(name)); Assert.isNotNull(value); 247 Assert.isTrue(!"".equals(value)); if (fDocument != null && fRefactoringArguments != null && value != null) { 249 try { 250 final Attr attribute= fDocument.createAttribute(name); 251 attribute.setValue(value); 252 fRefactoringArguments.add(attribute); 253 } catch (DOMException exception) { 254 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null)); 255 } 256 } 257 } 258 259 265 public void endRefactoring() { 266 if (fRefactoring != null && fRefactoringArguments != null) 267 addArguments(fRefactoring, fRefactoringArguments); 268 fRefactoringArguments= null; 269 fRefactoring= null; 270 } 271 272 279 public void endSession() { 280 if (fSession != null && fSessionArguments != null) 281 addArguments(fSession, fSessionArguments); 282 fSessionArguments= null; 283 fSession= null; 284 } 285 286 296 public Document getResult() { 297 final Document document= fDocument; 298 fDocument= null; 299 return document; 300 } 301 } 302 | Popular Tags |