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 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

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

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.tree.TreeNode;
import org.netbeans.modules.tasklist.core.ColumnProperty;
import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.core.TaskList;
import org.netbeans.modules.tasklist.core.TaskListView;
import org.openide.explorer.view.Visualizer;
import org.openide.filesystems.FileObject;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.xml.sax.SAXException;

/**
 * This class exports a given TaskListView to XML
 */
public class XMLTranslator extends org.netbeans.modules.tasklist.core.translators.AbstractTranslator {
    public String getDefaultExtension() {
        return "xml"; // NOI18N
    }
    
    public String getImportName() {
        return null;
    }
    
    public String getExportName() {
        return NbBundle.getMessage(XMLTranslator.class, "XML"); // NOI18N
    }
    
    public boolean supportsImport() {
        return false;
    }
    
    public boolean supportsExport() {
        return true;
    }
    
    protected String getExportDialogTitle() {
        return NbBundle.getMessage(XMLTranslator.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("tasklist"); // NOI18N


            TaskListView view = TaskListView.getCurrent();
            if (view.getList() == list) {
                // we use here visualizer because we want to export visible nodes
                TreeNode tn = Visualizer.findVisualizer(view.getEffectiveRoot());
                for (int i = 0; i < tn.getChildCount(); i++) {
                    exportNode(view, c, tn.getChildAt(i));
                }
            } else {
                // if we want to support export based on visibility (filters and expansion)
                // we must rework translator interface
                // other possibilty is to rewrite implemntation to export
                // all content unconditionally
                assert false : "Unimlemented for nonactivated tasklist views!";
            }

            c.ee();
            c.endDocument();
            writer.flush();
        } catch (SAXException e) {
            throw new IOException(e.getMessage());
        }
        return true;
    }
    
    protected void exportDone(FileObject file) {
    }
    
    protected void exportDone(File file) {
    }

    /**
     * Exports one node
     *
     * @param v a task list view
     * @param c output
     * @param tn a node
     */
    private void exportNode(TaskListView v, XMLCreator c, TreeNode tn) 
    throws SAXException {
        Node n = Visualizer.findNode(tn);
        c.e("item"); // NOI18N
        c.e("displayName"); // NOI18N
        c.ch(n.getDisplayName());
        c.ee();
        ColumnProperty[] columns = v.getVisibleColumns();
        for (int i = 1; i < columns.length; i++) {
            c.e(columns[i].getName());
            Node.Property p = TLUtils.getProperty(n, columns[i].getName());
            Object value;
            try {
                value = p == null ? "" : p.getValue(); // NOI18N
            } catch (IllegalAccessException e) {
                value = ""; // NOI18N
            } catch (InvocationTargetException e) {
                value = ""; // NOI18N
            }
            c.ch(value == null ? "" : value.toString()); // NOI18N
            c.ee();
        }
        
        for (int i = 0; i < tn.getChildCount(); i++) {
            exportNode(v, c, tn.getChildAt(i));
        }
        c.ee();
    }
}
... 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.