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.usertasks.translators;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.netbeans.modules.tasklist.core.*;
import org.netbeans.modules.tasklist.core.TaskList;
import org.netbeans.modules.tasklist.core.translators.AbstractTranslator;
import org.netbeans.modules.tasklist.usertasks.translators.UserTaskListXMLTranslator;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;
import org.openide.NotifyDescriptor;
import org.openide.awt.HtmlBrowser;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.util.NbBundle;

/**
 * Creates HTML using XSL transformation
 *
 * @author  Tim Lebedkov
 */
public class HTMLTranslator extends AbstractTranslator {
    private String res;
    
    /** 
     * Creates a new instance of HTMLTranslator 
     *
     * @param res name of the XSL resource relative to usertasks/translators
     */
    public HTMLTranslator(String res) {
        this.res = res;
    }
    
    public String getDefaultExtension() {
        return "html"; // NOI18N
    }
    
    public String getImportName() {
        return null;
    }
    
    public String getExportName() {
        return NbBundle.getMessage(HTMLTranslator.class, "HTML"); // NOI18N
    }
    
    public boolean supportsImport() {
        return false;
    }
    
    public boolean supportsExport() {
        return true;
    }
    
    protected String getExportDialogTitle() {
        return NbBundle.getMessage(HTMLTranslator.class, "ExportHTML"); // NOI18N
    }
    
    protected String getImportDialogTitle() {
        return null;
    }
    
    public boolean writeList(TaskList list, OutputStream out,
    boolean interactive, File dir) throws IOException {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        UserTaskListXMLTranslator t = new UserTaskListXMLTranslator();
        t.writeList(list, stream, interactive, dir);
        
        ByteArrayInputStream is = new ByteArrayInputStream(stream.toByteArray());
        TransformerFactory tf = TransformerFactory.newInstance();
        try {
            InputStream xsl = HTMLTranslator.class.
                getResourceAsStream(res);
            Transformer tr = tf.newTransformer(new StreamSource(xsl));
            tr.transform(new StreamSource(is), new StreamResult(out));
            return true;
        } catch (TransformerConfigurationException e) {
            ErrorManager.getDefault().notify(e);
            return false;
        } catch (TransformerException e) {
            ErrorManager.getDefault().notify(e);
            return false;
        }
    }
    
    protected void exportDone(FileObject file) {
        URL url = null;
        try {
            url = file.getURL();
        } catch (FileStateInvalidException e) {
            // Can't show URL
            ErrorManager.getDefault().notify(e);
            return;
        }

        // Show the result?
        NotifyDescriptor nd = new NotifyDescriptor.Confirmation (
                     NbBundle.getMessage(HTMLTranslator.class,
                                       "ViewExportedHTML"), // NOI18N
                     NotifyDescriptor.YES_NO_OPTION
        );
        Object result = DialogDisplayer.getDefault().notify(nd);
        if (NotifyDescriptor.YES_OPTION == result) {
            HtmlBrowser.URLDisplayer.getDefault().showURL(url);
        }
    }


   protected void exportDone(File file) {
        URL url;
        try {
            url = file.toURI().toURL();
        } catch (MalformedURLException e) {
            // Can't show URL
            ErrorManager.getDefault().notify(e);
            return;
        }

        // Show the result?
        NotifyDescriptor nd = new NotifyDescriptor.Confirmation (
                     NbBundle.getMessage(HTMLTranslator.class,
                                       "ViewExportedHTML"), // NOI18N
                     NotifyDescriptor.YES_NO_OPTION
        );
        Object result = DialogDisplayer.getDefault().notify(nd);
        if (NotifyDescriptor.YES_OPTION == result) {
            HtmlBrowser.URLDisplayer.getDefault().showURL(url);
        }
    }
}
... 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.