KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.xml.parsers.ParserConfigurationException JavaDoc;
20 import javax.xml.parsers.SAXParser JavaDoc;
21 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.SAXNotRecognizedException JavaDoc;
37 import org.xml.sax.SAXNotSupportedException JavaDoc;
38 import org.xml.sax.XMLReader JavaDoc;
39 import org.xml.sax.helpers.DefaultHandler JavaDoc;
40
41 /**
42  * Refactoring session reader for XML-based refactoring sessions.
43  *
44  * @since 3.2
45  */

46 public final class RefactoringSessionReader extends DefaultHandler JavaDoc {
47
48     /** The comment of the refactoring session, or <code>null</code> */
49     private String JavaDoc fComment= null;
50
51     /** Should project information be included? */
52     private final boolean fProjects;
53
54     /**
55      * The current list of refactoring descriptors, or <code>null</code>
56      * (element type: <code>RefactoringDescriptor</code>)
57      */

58     private List JavaDoc fRefactoringDescriptors= null;
59
60     /** Has a session been found during parsing? */
61     private boolean fSessionFound= false;
62
63     /** The current version of the refactoring script, or <code>null</code> */
64     private String JavaDoc fVersion= null;
65
66     /**
67      * Creates a new refactoring session reader.
68      *
69      * @param projects
70      * <code>true</code> to include project information,
71      * <code>false</code> otherwise
72      */

73     public RefactoringSessionReader(final boolean projects) {
74         fProjects= projects;
75     }
76
77     /**
78      * Creates a new parser from the specified factory.
79      *
80      * @param factory
81      * the parser factoring to use
82      * @return the created parser
83      * @throws ParserConfigurationException
84      * if no parser is available with the given configuration
85      * @throws SAXException
86      * if an error occurs while creating the parser
87      */

88     private SAXParser JavaDoc createParser(final SAXParserFactory JavaDoc factory) throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
89
90         final SAXParser JavaDoc parser= factory.newSAXParser();
91         final XMLReader JavaDoc reader= parser.getXMLReader();
92
93         try {
94
95             reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$
96
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
97

98         } catch (SAXNotRecognizedException JavaDoc exception) {
99             // Do nothing
100
} catch (SAXNotSupportedException JavaDoc exception) {
101             // Do nothing
102
}
103         return parser;
104     }
105
106     /**
107      * Reads a refactoring history descriptor from the specified input object.
108      *
109      * @param source
110      * the input source
111      * @return a corresponding refactoring history descriptor, or
112      * <code>null</code>
113      * @throws CoreException
114      * if an error occurs while reading form the input source
115      */

116     public RefactoringSessionDescriptor readSession(final InputSource JavaDoc source) throws CoreException {
117         fSessionFound= false;
118         try {
119             source.setSystemId("/"); //$NON-NLS-1$
120
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)) //$NON-NLS-1$
125
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 JavaDoc exception) {
131             throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null));
132         } catch (ParserConfigurationException JavaDoc exception) {
133             throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_IO_ERROR, exception.getLocalizedMessage(), null));
134         } catch (SAXException JavaDoc 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     /**
145      * {@inheritDoc}
146      */

147     public void startElement(final String JavaDoc uri, final String JavaDoc localName, final String JavaDoc qualifiedName, final Attributes JavaDoc attributes) throws SAXException JavaDoc {
148         if (IRefactoringSerializationConstants.ELEMENT_REFACTORING.equals(qualifiedName)) {
149             final int length= attributes.getLength();
150             final Map JavaDoc map= new HashMap JavaDoc(length);
151             String JavaDoc id= ""; //$NON-NLS-1$
152
String JavaDoc stamp= ""; //$NON-NLS-1$
153
String JavaDoc description= ""; //$NON-NLS-1$
154
String JavaDoc comment= null;
155             String JavaDoc flags= "0"; //$NON-NLS-1$
156
String JavaDoc project= null;
157             for (int index= 0; index < length; index++) {
158                 final String JavaDoc name= attributes.getQName(index);
159                 final String JavaDoc 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)) //$NON-NLS-1$
170
comment= value;
171                 } else if (fProjects && IRefactoringSerializationConstants.ATTRIBUTE_PROJECT.equals(name))
172                     project= value;
173                 else if (!"".equals(name) && !"".equals(value)) //$NON-NLS-1$//$NON-NLS-2$
174
map.put(name, value);
175             }
176             int flag= 0;
177             try {
178                 flag= Integer.parseInt(flags);
179             } catch (NumberFormatException JavaDoc exception) {
180                 // Do nothing
181
}
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 JavaDoc exception) {
188                     // Do nothing
189
}
190                 if (fRefactoringDescriptors == null)
191                     fRefactoringDescriptors= new ArrayList JavaDoc();
192                 fRefactoringDescriptors.add(descriptor);
193             }
194         } else if (IRefactoringSerializationConstants.ELEMENT_SESSION.equals(qualifiedName)) {
195             fSessionFound= true;
196             final String JavaDoc version= attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_VERSION);
197             if (version != null && !"".equals(version)) //$NON-NLS-1$
198
fVersion= version;
199             fComment= attributes.getValue(IRefactoringSerializationConstants.ATTRIBUTE_COMMENT);
200         }
201     }
202 }
203
Popular Tags