KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > RefactoringSessionTransformer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 package org.eclipse.ltk.internal.core.refactoring;
12
13 import com.ibm.icu.text.Collator;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
34 import org.w3c.dom.DOMException JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.NamedNodeMap JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39 /**
40  * Transformer for XML-based refactoring histories.
41  *
42  * @since 3.2
43  */

44 public final class RefactoringSessionTransformer {
45
46     /** Comparator for attributes */
47     private static final class AttributeComparator implements Comparator JavaDoc {
48
49         /**
50          * {@inheritDoc}
51          */

52         public int compare(final Object JavaDoc first, final Object JavaDoc second) {
53             final Attr JavaDoc predecessor= (Attr JavaDoc) first;
54             final Attr JavaDoc successor= (Attr JavaDoc) second;
55             return Collator.getInstance().compare(predecessor.getName(), successor.getName());
56         }
57     }
58
59     /** The current document, or <code>null</code> */
60     private Document JavaDoc fDocument= null;
61
62     /** Should project information be included? */
63     private final boolean fProjects;
64
65     /** The current refactoring node, or <code>null</code> */
66     private Node JavaDoc fRefactoring= null;
67
68     /** The current refactoring arguments, or <code>null</code> */
69     private List JavaDoc fRefactoringArguments= null;
70
71     /** The current session node, or <code>null</code> */
72     private Node JavaDoc fSession= null;
73
74     /** The current session arguments, or <code>null</code> */
75     private List JavaDoc fSessionArguments= null;
76
77     /**
78      * Creates a new refactoring session transformer.
79      *
80      * @param projects
81      * <code>true</code> to include project information,
82      * <code>false</code> otherwise
83      */

84     public RefactoringSessionTransformer(final boolean projects) {
85         fProjects= projects;
86     }
87
88     /**
89      * Adds the attributes specified in the list to the node, in ascending order
90      * of their names.
91      *
92      * @param node
93      * the node
94      * @param list
95      * the list of attributes
96      */

97     private void addArguments(final Node JavaDoc node, final List JavaDoc list) {
98         final NamedNodeMap JavaDoc map= node.getAttributes();
99         if (map != null) {
100             Collections.sort(list, new AttributeComparator());
101             for (final Iterator JavaDoc iterator= list.iterator(); iterator.hasNext();) {
102                 final Attr JavaDoc attribute= (Attr JavaDoc) iterator.next();
103                 map.setNamedItem(attribute);
104             }
105         }
106     }
107
108     /**
109      * Begins the transformation of a refactoring specified by the given
110      * arguments.
111      * <p>
112      * Calls to
113      * {@link RefactoringSessionTransformer#beginRefactoring(String, long, String, String, String, int)}
114      * must be balanced with calls to
115      * {@link RefactoringSessionTransformer#endRefactoring()}. If the
116      * transformer is already processing a refactoring, nothing happens.
117      * </p>
118      *
119      * @param id
120      * the unique identifier of the refactoring
121      * @param stamp
122      * the time stamp of the refactoring, or <code>-1</code>
123      * @param project
124      * the non-empty name of the project this refactoring is
125      * associated with, or <code>null</code>
126      * @param description
127      * a human-readable description of the refactoring
128      * @param comment
129      * the comment associated with the refactoring, or
130      * <code>null</code>
131      * @param flags
132      * the flags associated with refactoring
133      * @throws CoreException
134      * if an error occurs while creating a new refactoring
135      */

136     public void beginRefactoring(final String JavaDoc id, long stamp, final String JavaDoc project, final String JavaDoc description, final String JavaDoc 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 JavaDoc exception) {
144             throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null));
145         } catch (FactoryConfigurationError JavaDoc 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 JavaDoc(16);
151                 fRefactoring= fDocument.createElement(IRefactoringSerializationConstants.ELEMENT_REFACTORING);
152                 Attr JavaDoc 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 JavaDoc(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)) { //$NON-NLS-1$
169
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 JavaDoc exception) {
183                 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null));
184             }
185         }
186     }
187
188     /**
189      * Begins the transformation of a refactoring session.
190      * <p>
191      * Calls to
192      * {@link RefactoringSessionTransformer#beginSession(String, String)} must
193      * be balanced with calls to
194      * {@link RefactoringSessionTransformer#endSession()}. If the transformer
195      * is already processing a session, nothing happens.
196      * </p>
197      *
198      * @param comment
199      * the comment associated with the refactoring session, or
200      * <code>null</code>
201      * @param version
202      * the non-empty version tag
203      * @throws CoreException
204      * if an error occurs while creating a new session
205      */

206     public void beginSession(final String JavaDoc comment, final String JavaDoc 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 JavaDoc(2);
212                 Attr JavaDoc attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_VERSION);
213                 attribute.setValue(version);
214                 fSessionArguments.add(attribute);
215                 if (comment != null && !"".equals(comment)) { //$NON-NLS-1$
216
attribute= fDocument.createAttribute(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT);
217                     attribute.setValue(comment);
218                     fSessionArguments.add(attribute);
219                 }
220                 fDocument.appendChild(fSession);
221             } catch (DOMException JavaDoc exception) {
222                 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null));
223             } catch (ParserConfigurationException JavaDoc exception) {
224                 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null));
225             }
226         }
227     }
228
229     /**
230      * Creates a refactoring argument with the specified name and value.
231      * <p>
232      * If no refactoring is currently processed, this call has no effect.
233      * </p>
234      *
235      * @param name
236      * the non-empty name of the argument
237      * @param value
238      * the non-empty value of the argument
239      *
240      * @throws CoreException
241      * if an error occurs while creating a new argument
242      */

243     public void createArgument(final String JavaDoc name, final String JavaDoc value) throws CoreException {
244         Assert.isNotNull(name);
245         Assert.isTrue(!"".equals(name)); //$NON-NLS-1$
246
Assert.isNotNull(value);
247         Assert.isTrue(!"".equals(value)); //$NON-NLS-1$
248
if (fDocument != null && fRefactoringArguments != null && value != null) {
249             try {
250                 final Attr JavaDoc attribute= fDocument.createAttribute(name);
251                 attribute.setValue(value);
252                 fRefactoringArguments.add(attribute);
253             } catch (DOMException JavaDoc exception) {
254                 throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, exception.getLocalizedMessage(), null));
255             }
256         }
257     }
258
259     /**
260      * Ends the transformation of the current refactoring.
261      * <p>
262      * If no refactoring is currently processed, this call has no effect.
263      * </p>
264      */

265     public void endRefactoring() {
266         if (fRefactoring != null && fRefactoringArguments != null)
267             addArguments(fRefactoring, fRefactoringArguments);
268         fRefactoringArguments= null;
269         fRefactoring= null;
270     }
271
272     /**
273      * Ends the transformation of the current refactoring session.
274      * <p>
275      * If no refactoring session is currently processed, this call has no
276      * effect.
277      * </p>
278      */

279     public void endSession() {
280         if (fSession != null && fSessionArguments != null)
281             addArguments(fSession, fSessionArguments);
282         fSessionArguments= null;
283         fSession= null;
284     }
285
286     /**
287      * Returns the result of the transformation process.
288      * <p>
289      * This method must only be called once during the life time of a
290      * transformer.
291      * </p>
292      *
293      * @return the object representing the refactoring session, or
294      * <code>null</code> if no session has been transformed
295      */

296     public Document JavaDoc getResult() {
297         final Document JavaDoc document= fDocument;
298         fDocument= null;
299         return document;
300     }
301 }
302
Popular Tags