alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 2004-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.usertasks.translators;

import java.io.*;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.netbeans.modules.tasklist.core.Task;
import org.netbeans.modules.tasklist.core.TaskList;
import org.netbeans.modules.tasklist.core.translators.AbstractTranslator;
import org.netbeans.modules.tasklist.core.translators.XMLCreator;
import org.netbeans.modules.tasklist.core.translators.XMLSerializer;
import org.netbeans.modules.tasklist.usertasks.UserTask;
import org.openide.util.NbBundle;
import org.xml.sax.SAXException;

/**
 * This class exports a given UserTaskList to XML
 */
public class UserTaskListXMLTranslator extends AbstractTranslator {
    private static DateFormat TIME_FORMAT = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ssZ"); // NOI18N
    
    private static final String[] PRIORITIES =  {
        "high", // NOI18N
        "medium-high", // NOI18N
        "medium", // NOI18N
        "medium-low", // NOI18N
        "low" // NOI18N
    };
    
    public String getDefaultExtension() {
        return "xml"; // NOI18N
    }
    
    public String getImportName() {
        return null;
    }
    
    public String getExportName() {
        return NbBundle.getMessage(UserTaskListXMLTranslator.class, "XML"); // NOI18N
    }
    
    public boolean supportsImport() {
        return false;
    }
    
    public boolean supportsExport() {
        return true;
    }
    
    protected String getExportDialogTitle() {
        return NbBundle.getMessage(UserTaskListXMLTranslator.class, "ExportXML"); // NOI18N
    }
    
    protected String getImportDialogTitle() {
        return null;
    }
    
    public boolean writeList(TaskList list, OutputStream out,
    boolean interactive, File dir) throws IOException {
        Writer writer = new OutputStreamWriter(out, "utf8");  // NOI18N
        XMLCreator c = new XMLCreator(new XMLSerializer(writer));
        try {
            c.startDocument();
            c.e("tasks"); // NOI18N
            
            Iterator it = list.getTasks().iterator();
            while (it.hasNext()) {
                task(c, (UserTask) it.next());
            }
            
            c.ee();
            c.endDocument();
            writer.flush();
        } catch (SAXException e) {
            throw new IOException(e.getMessage());
        }
        return true;
    }
    
    /**
     * Process one task
     *
     * @param c output
     * @param task a task to process
     */
    private void task(XMLCreator c, UserTask task) throws SAXException {
        List attrs = new ArrayList();
        
        attrs.add("priority"); // NOI18N
        attrs.add(PRIORITIES[task.getPriority().intValue() - 1]);
        
        if (task.getCategory().length() != 0) {
            attrs.add("category"); // NOI18N
            attrs.add(task.getCategory());
        }
        
        attrs.add("progress"); // NOI18N
        attrs.add(String.valueOf(task.getPercentComplete()));
        
        if (task.isProgressComputed()) {
            attrs.add("progress-computed"); // NOI18N
            attrs.add("yes"); // NOI18N
        }
        
        attrs.add("effort"); // NOI18N
        attrs.add(String.valueOf(task.getEffort()));
        
        if (task.isEffortComputed()) {
            attrs.add("effort-computed"); // NOI18N
            attrs.add("yes"); // NOI18N
        }
        
        attrs.add("spent-time"); // NOI18N
        attrs.add(String.valueOf(task.getSpentTime()));
        
        if (task.isSpentTimeComputed()) {
            attrs.add("spent-time-computed"); // NOI18N
            attrs.add("yes"); // NOI18N
        }
        
        if (task.getDueDate() != null) {
            attrs.add("due"); // NOI18N
            attrs.add(dateToString(task.getDueDate()));
        }
        
        if (task.getFilename().length() != 0) {
            attrs.add("file"); // NOI18N
            File f = new File(task.getFilename());
            try {
                attrs.add(f.toURI().toURL().toExternalForm());
            } catch (MalformedURLException e) {
                // ignore
            }
            attrs.add("line"); // NOI18N
            attrs.add(String.valueOf(task.getLineNumber()));
        }
        
	attrs.add("created"); // NOI18N
        attrs.add(dateToString(new Date(task.getCreatedDate())));

	attrs.add("modified"); // NOI18N
        attrs.add(dateToString(new Date(task.getLastEditedDate())));

        c.e("task", attrs); // NOI18N
        c.e("summary");
        c.ch(task.getSummary());
        c.ee();
        
        if (task.getDetails().length() > 0) {
            c.e("details");
            c.ch(task.getDetails());
            c.ee();
        }
        
        Iterator it = task.subtasksIterator();
        while (it.hasNext()) {
            task(c, (UserTask) it.next());
        }
        c.ee();
    }
    
    /**
     * Converts a date to a string according to 
     * http://www.w3.org/TR/NOTE-datetime 
     *
     * @param d a date
     * @return string in format YYYY-MM-DDThh:mm:ssTZD
     */
    private String dateToString(Date d) {
        String s = TIME_FORMAT.format(d);
        return s.substring(0, s.length() - 2) + ":" + 
            s.substring(s.length() - 2, s.length());
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.