1 19 20 package org.netbeans.modules.tasklist.usertasks.translators; 21 22 import java.io.File ; 23 import java.net.URL ; 24 import java.text.DateFormat ; 25 import java.text.SimpleDateFormat ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import java.util.logging.Level ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.Transformer ; 35 import javax.xml.transform.TransformerConfigurationException ; 36 import javax.xml.transform.TransformerException ; 37 import javax.xml.transform.TransformerFactory ; 38 import javax.xml.transform.dom.DOMSource ; 39 import javax.xml.transform.stream.StreamResult ; 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 ; 55 import org.w3c.dom.Element ; 56 import org.xml.sax.SAXException ; 57 58 61 public class XmlExportFormat implements ExportImportFormat { 62 protected final static String 63 CHOOSE_FILE_PANEL_PROP = "ChooseFilePanel"; 65 private static String LINE_SEPARATOR = 66 System.getProperty("line.separator"); private static DateFormat TIME_FORMAT = new SimpleDateFormat ( 68 "yyyy-MM-dd'T'HH:mm:ssZ"); 70 private static final String [] PRIORITIES = { 71 "high", "medium-high", "medium", "medium-low", "low" }; 77 78 81 public XmlExportFormat() { 82 } 83 84 public String getName() { 85 return NbBundle.getMessage( 86 XmlExportFormat.class, "XML"); } 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"), new String [] {".xml"})); chooseFilePanel.setFile(new File ( 99 Settings.getDefault().getLastUsedExportFolder(), 100 "tasklist.xml")); 102 WizardDescriptor.Iterator iterator = 104 new WizardDescriptor.ArrayIterator(new WizardDescriptor.Panel[] { 105 chooseFileWP 106 }); 107 WizardDescriptor d = new WizardDescriptor(iterator); 108 d.putProperty("WizardPanel_contentData", new String [] { 110 NbBundle.getMessage( 111 XmlExportFormat.class, "ChooseDestination"), } 113 ); d.setTitle(NbBundle.getMessage(XmlExportFormat.class, 115 "ExportToXml")); d.putProperty(CHOOSE_FILE_PANEL_PROP, chooseFilePanel); 117 d.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); d.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); d.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); d.setTitleFormat(new java.text.MessageFormat ("{0}")); return d; 122 } 123 124 129 protected Transformer createTransformer() { 130 try { 131 return TransformerFactory.newInstance().newTransformer(); 132 } catch (TransformerConfigurationException 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 doc = createXml(list); 145 Transformer t = createTransformer(); 146 Source source = new DOMSource (doc); 147 Result result = new StreamResult (panel.getFile()); 148 t.transform(source, result); 149 Settings.getDefault().setLastUsedExportFolder( 150 panel.getFile().getParentFile()); 151 } catch (TransformerException e) { 152 UTUtils.LOGGER.log(Level.SEVERE, "", e); 153 } catch (ParserConfigurationException e) { 154 UTUtils.LOGGER.log(Level.SEVERE, "", e); 155 } catch (SAXException e) { 156 UTUtils.LOGGER.log(Level.SEVERE, "", e); 157 } 158 } 159 160 166 public Document createXml(UserTaskList list) 167 throws ParserConfigurationException , SAXException { 168 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 169 Document doc = db.newDocument(); 170 Element tasks = doc.createElement("tasks"); doc.appendChild(tasks); 172 173 Iterator it = list.getSubtasks().iterator(); 174 while (it.hasNext()) { 175 task(tasks, (UserTask) it.next()); 176 } 177 return doc; 178 } 179 180 186 private void task(Element el, UserTask task) throws SAXException { 187 Document doc = el.getOwnerDocument(); 188 Element node = doc.createElement("task"); el.appendChild(node); 190 191 node.setAttribute("priority", PRIORITIES[task.getPriority() - 1]); 193 194 if (task.getCategory().length() != 0) { 195 node.setAttribute("category", task.getCategory()); 197 } 198 199 node.setAttribute("progress", String.valueOf(task.getPercentComplete())); 201 202 if (task.isValuesComputed()) { 203 node.setAttribute("progress-computed", "yes"); node.setAttribute("effort-computed", "yes"); node.setAttribute("spent-time-computed", "yes"); 208 node.setAttribute("values-computed", "yes"); } 210 211 node.setAttribute("effort", String.valueOf(task.getEffort())); 213 node.setAttribute("spent-time", String.valueOf(task.getSpentTime())); 215 216 if (task.getDueDate() != null) { 217 node.setAttribute("due", dateToString(task.getDueDate())); } 219 220 URL url = task.getUrl(); 221 if (url != null) { 222 node.setAttribute("file", url.toExternalForm()); 224 node.setAttribute("line", String.valueOf(task.getLineNumber() + 1)); 226 } 227 228 node.setAttribute("created", dateToString(new Date (task.getCreatedDate()))); 230 231 node.setAttribute("modified", dateToString(new Date (task.getLastEditedDate()))); 233 234 if (task.getCompletedDate() != 0) 235 node.setAttribute("completed", dateToString(new Date (task.getCompletedDate()))); 237 238 if (task.getOwner().length() != 0) 239 node.setAttribute("owner", task.getOwner()); 241 if (task.getStart() != -1) 242 node.setAttribute("start", dateToString(new Date (task.getStart()))); 244 245 Element summary = doc.createElement("summary"); node.appendChild(summary); 247 summary.appendChild(doc.createTextNode(task.getSummary())); 248 249 if (task.getDetails().length() > 0) { 250 Element details = doc.createElement("details"); 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 workPeriods = doc.createElement("work-periods"); node.appendChild(workPeriods); 261 for (int i = 0; i < wps.size(); i++) { 262 UserTask.WorkPeriod wp = (UserTask.WorkPeriod) wps.get(i); 263 Element period = doc.createElement("period"); period.setAttribute("start", dateToString(new Date (task.getCompletedDate()))); 266 period.setAttribute("duration", Integer.toString(wp.getDuration())); 268 workPeriods.appendChild(period); 269 } 270 } 271 272 Iterator it = task.getSubtasks().iterator(); 273 while (it.hasNext()) { 274 task(node, (UserTask) it.next()); 275 } 276 } 277 278 285 private String dateToString(Date d) { 286 String s = TIME_FORMAT.format(d); 287 return s.substring(0, s.length() - 2) + ":" + s.substring(s.length() - 2, s.length()); 289 } 290 } 291 | Popular Tags |