KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > translators > XmlExportFormat


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.translators;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.Result JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.Transformer JavaDoc;
35 import javax.xml.transform.TransformerConfigurationException JavaDoc;
36 import javax.xml.transform.TransformerException JavaDoc;
37 import javax.xml.transform.TransformerFactory JavaDoc;
38 import javax.xml.transform.dom.DOMSource JavaDoc;
39 import javax.xml.transform.stream.StreamResult JavaDoc;
40
41 import org.netbeans.modules.tasklist.core.export.ExportImportFormat;
42 import org.netbeans.modules.tasklist.core.export.ExportImportProvider;
43 import org.netbeans.modules.tasklist.core.export.SaveFilePanel;
44 import org.netbeans.modules.tasklist.core.util.ExtensionFileFilter;
45 import org.netbeans.modules.tasklist.core.util.ObjectList;
46 import org.netbeans.modules.tasklist.core.util.SimpleWizardPanel;
47 import org.netbeans.modules.tasklist.usertasks.options.Settings;
48 import org.netbeans.modules.tasklist.usertasks.UserTaskViewRegistry;
49 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
50 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
51 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
52 import org.openide.WizardDescriptor;
53 import org.openide.util.NbBundle;
54 import org.w3c.dom.Document JavaDoc;
55 import org.w3c.dom.Element JavaDoc;
56 import org.xml.sax.SAXException JavaDoc;
57
58 /**
59  * Export to XML
60  */

61 public class XmlExportFormat implements ExportImportFormat {
62     protected final static String JavaDoc
63         CHOOSE_FILE_PANEL_PROP = "ChooseFilePanel"; // NOI18N
64

65     private static String JavaDoc LINE_SEPARATOR =
66         System.getProperty("line.separator"); // NOI18N
67
private static DateFormat JavaDoc TIME_FORMAT = new SimpleDateFormat JavaDoc(
68         "yyyy-MM-dd'T'HH:mm:ssZ"); // NOI18N
69

70     private static final String JavaDoc[] PRIORITIES = {
71         "high", // NOI18N
72
"medium-high", // NOI18N
73
"medium", // NOI18N
74
"medium-low", // NOI18N
75
"low" // NOI18N
76
};
77     
78     /**
79      * Creates a new instance of XmlExportFormat
80      */

81     public XmlExportFormat() {
82     }
83     
84     public String JavaDoc getName() {
85         return NbBundle.getMessage(
86             XmlExportFormat.class, "XML"); // NOI18N
87
}
88     
89     public org.openide.WizardDescriptor getWizard() {
90         SaveFilePanel chooseFilePanel = new SaveFilePanel();
91         SimpleWizardPanel chooseFileWP = new SimpleWizardPanel(chooseFilePanel);
92         chooseFilePanel.setWizardPanel(chooseFileWP);
93         chooseFilePanel.getFileChooser().addChoosableFileFilter(
94             new ExtensionFileFilter(
95                 NbBundle.getMessage(XmlExportFormat.class,
96                     "XmlFilter"), // NOI18N
97
new String JavaDoc[] {".xml"})); // NOI18N
98
chooseFilePanel.setFile(new File JavaDoc(
99                 Settings.getDefault().getLastUsedExportFolder(),
100                 "tasklist.xml")); // NOI18N
101

102         // create the wizard
103
WizardDescriptor.Iterator iterator =
104             new WizardDescriptor.ArrayIterator(new WizardDescriptor.Panel[] {
105                 chooseFileWP
106         });
107         WizardDescriptor d = new WizardDescriptor(iterator);
108         d.putProperty("WizardPanel_contentData", // NOI18N
109
new String JavaDoc[] {
110                 NbBundle.getMessage(
111                     XmlExportFormat.class, "ChooseDestination"), // NOI18N
112
}
113         ); // NOI18N
114
d.setTitle(NbBundle.getMessage(XmlExportFormat.class,
115             "ExportToXml")); // NOI18N
116
d.putProperty(CHOOSE_FILE_PANEL_PROP, chooseFilePanel);
117         d.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
118
d.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
119
d.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
120
d.setTitleFormat(new java.text.MessageFormat JavaDoc("{0}")); // NOI18N todo
121
return d;
122     }
123     
124     /**
125      * Creates a transformer
126      *
127      * @return created transformer
128      */

129     protected Transformer JavaDoc createTransformer() {
130         try {
131             return TransformerFactory.newInstance().newTransformer();
132         } catch (TransformerConfigurationException JavaDoc e) {
133             UTUtils.LOGGER.log(Level.WARNING, "", e);
134             return null;
135         }
136     }
137     
138     public void doExportImport(ExportImportProvider provider, WizardDescriptor wd) {
139         SaveFilePanel panel =
140             (SaveFilePanel) wd.getProperty(CHOOSE_FILE_PANEL_PROP);
141         try {
142             UserTaskList list = UserTaskViewRegistry.getInstance().
143                     getCurrent().getUserTaskList();
144             Document JavaDoc doc = createXml(list);
145             Transformer JavaDoc t = createTransformer();
146             Source JavaDoc source = new DOMSource JavaDoc(doc);
147             Result JavaDoc result = new StreamResult JavaDoc(panel.getFile());
148             t.transform(source, result);
149             Settings.getDefault().setLastUsedExportFolder(
150                     panel.getFile().getParentFile());
151         } catch (TransformerException JavaDoc e) {
152             UTUtils.LOGGER.log(Level.SEVERE, "", e);
153         } catch (ParserConfigurationException JavaDoc e) {
154             UTUtils.LOGGER.log(Level.SEVERE, "", e);
155         } catch (SAXException JavaDoc e) {
156             UTUtils.LOGGER.log(Level.SEVERE, "", e);
157         }
158     }
159
160     /**
161      * Creates xml for the specified task list
162      *
163      * @param list task list
164      * @return created XML
165      */

166     public Document JavaDoc createXml(UserTaskList list)
167     throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
168         DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
169         Document JavaDoc doc = db.newDocument();
170         Element JavaDoc tasks = doc.createElement("tasks"); // NOI18N
171
doc.appendChild(tasks);
172
173         Iterator JavaDoc it = list.getSubtasks().iterator();
174         while (it.hasNext()) {
175             task(tasks, (UserTask) it.next());
176         }
177         return doc;
178     }
179     
180     /**
181      * Process one task
182      *
183      * @param el parent node
184      * @param task a task to process
185      */

186     private void task(Element JavaDoc el, UserTask task) throws SAXException JavaDoc {
187         Document JavaDoc doc = el.getOwnerDocument();
188         Element JavaDoc node = doc.createElement("task"); // NOI18N
189
el.appendChild(node);
190         
191         node.setAttribute("priority", // NOI18N
192
PRIORITIES[task.getPriority() - 1]);
193         
194         if (task.getCategory().length() != 0) {
195             node.setAttribute("category", // NOI18N
196
task.getCategory());
197         }
198         
199         node.setAttribute("progress", // NOI18N
200
String.valueOf(task.getPercentComplete()));
201         
202         if (task.isValuesComputed()) {
203             // for backward compatibility only
204
node.setAttribute("progress-computed", "yes"); // NOI18N
205
node.setAttribute("effort-computed", "yes"); // NOI18N
206
node.setAttribute("spent-time-computed", "yes"); // NOI18N
207

208             node.setAttribute("values-computed", "yes"); // NOI18N
209
}
210
211         node.setAttribute("effort", String.valueOf(task.getEffort())); // NOI18N
212

213         node.setAttribute("spent-time", // NOI18N
214
String.valueOf(task.getSpentTime()));
215         
216         if (task.getDueDate() != null) {
217             node.setAttribute("due", dateToString(task.getDueDate())); // NOI18N
218
}
219         
220         URL JavaDoc url = task.getUrl();
221         if (url != null) {
222             node.setAttribute("file", // NOI18N
223
url.toExternalForm());
224             node.setAttribute("line", // NOI18N
225
String.valueOf(task.getLineNumber() + 1));
226         }
227         
228         node.setAttribute("created", // NOI18N
229
dateToString(new Date JavaDoc(task.getCreatedDate())));
230
231         node.setAttribute("modified", // NOI18N
232
dateToString(new Date JavaDoc(task.getLastEditedDate())));
233         
234         if (task.getCompletedDate() != 0)
235             node.setAttribute("completed", // NOI18N
236
dateToString(new Date JavaDoc(task.getCompletedDate())));
237         
238         if (task.getOwner().length() != 0)
239             node.setAttribute("owner", task.getOwner()); // NOI18N
240

241         if (task.getStart() != -1)
242             node.setAttribute("start", // NOI18N
243
dateToString(new Date JavaDoc(task.getStart())));
244
245         Element JavaDoc summary = doc.createElement("summary"); // NOI18N
246
node.appendChild(summary);
247         summary.appendChild(doc.createTextNode(task.getSummary()));
248         
249         if (task.getDetails().length() > 0) {
250             Element JavaDoc details = doc.createElement("details"); // NOI18N
251
node.appendChild(details);
252             details.appendChild(doc.createTextNode(task.getDetails()));
253         }
254         
255         node.appendChild(doc.createTextNode(LINE_SEPARATOR));
256         
257         ObjectList wps = task.getWorkPeriods();
258         if (wps.size() > 0) {
259             Element JavaDoc workPeriods = doc.createElement("work-periods"); // NOI18N
260
node.appendChild(workPeriods);
261             for (int i = 0; i < wps.size(); i++) {
262                 UserTask.WorkPeriod wp = (UserTask.WorkPeriod) wps.get(i);
263                 Element JavaDoc period = doc.createElement("period"); // NOI18N
264
period.setAttribute("start", // NOI18N
265
dateToString(new Date JavaDoc(task.getCompletedDate())));
266                 period.setAttribute("duration", // NOI18N
267
Integer.toString(wp.getDuration()));
268                 workPeriods.appendChild(period);
269             }
270         }
271         
272         Iterator JavaDoc it = task.getSubtasks().iterator();
273         while (it.hasNext()) {
274             task(node, (UserTask) it.next());
275         }
276     }
277     
278     /**
279      * Converts a date to a string according to
280      * http://www.w3.org/TR/NOTE-datetime
281      *
282      * @param d a date
283      * @return string in format YYYY-MM-DDThh:mm:ssTZD
284      */

285     private String JavaDoc dateToString(Date JavaDoc d) {
286         String JavaDoc s = TIME_FORMAT.format(d);
287         return s.substring(0, s.length() - 2) + ":" + // NOI18N
288
s.substring(s.length() - 2, s.length());
289     }
290 }
291
Popular Tags