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.xsl.transform;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.OutputStream;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.awt.Dialog;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;

import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;

import org.openide.*;
import org.openide.awt.HtmlBrowser;
import org.openide.filesystems.*;
import org.openide.nodes.Node;
import org.openide.loaders.DataObject;
import org.openide.util.HelpCtx;
import org.openide.util.RequestProcessor;
import org.openide.util.Task;
import org.openide.util.actions.CookieAction;

import org.netbeans.api.xml.cookies.*;

import org.netbeans.modules.xml.core.XMLDataObject;
import org.netbeans.modules.xml.core.actions.CollectXMLAction;
import org.netbeans.modules.xml.core.actions.InputOutputReporter;

import org.netbeans.modules.xml.core.lib.GuiUtil;
import org.netbeans.modules.xml.core.lib.FileUtilities;

import org.netbeans.modules.xsl.XSLDataObject;
import org.netbeans.modules.xsl.ui.TransformPanel;
import org.netbeans.modules.xsl.settings.TransformHistory;
import org.netbeans.modules.xsl.actions.TransformAction;
import org.netbeans.modules.xsl.utils.TransformUtil;
import org.openide.loaders.DataObjectNotFoundException;

/**
 * Handle workflow of transformation action, gather UI info and
 * launch the processor.
 * 

* This class has very/needlessly complicated workflow. * * @author Libor Kramolis * @version 0.1 */ public class TransformPerformer { /** Represent transformation output window. */ private InputOutputReporter cookieObserver = null; private Node[] nodes; // instance freshness state private volatile boolean stalled = false; private volatile boolean active = true; public TransformPerformer(Node[] nodes) { this.nodes = nodes; } /** * Entry point called from transform action. * There is a fresh instance per call. */ public void perform() { if (stalled) throw new IllegalStateException(); try { if ( nodes.length == 2 ) { // automatically detect if one of selected nodes is transformation // in such case suppose that user want to it to transform second file DataObject do1 = (DataObject) nodes[0].getCookie(DataObject.class); boolean xslt1 = TransformUtil.isXSLTransformation(do1); DataObject do2 = (DataObject) nodes[1].getCookie(DataObject.class); boolean xslt2 = TransformUtil.isXSLTransformation(do2); if ( Util.THIS.isLoggable() ) /* then */ { Util.THIS.debug("TransformAction.performAction:"); Util.THIS.debug(" do1 [" + xslt1 + "] = " + do1); Util.THIS.debug(" do2 [" + xslt2 + "] = " + do2); } if ( xslt1 != xslt2 ) { TransformableCookie transformable; DataObject xmlDO; DataObject xslDO; if ( xslt1 ) { transformable = (TransformableCookie) nodes[1].getCookie(TransformableCookie.class); xmlDO = do2; xslDO = do1; } else { transformable = (TransformableCookie) nodes[0].getCookie(TransformableCookie.class); xmlDO = do1; xslDO = do2; } DoublePerformer performer = new DoublePerformer(transformable, xmlDO, xslDO); performer.perform(); } else { TransformableCookie transformable1 = (TransformableCookie) nodes[0].getCookie(TransformableCookie.class); SinglePerformer performer = new SinglePerformer(transformable1, do1, xslt1); performer.setLastInBatch(false); performer.perform(); TransformableCookie transformable2 = (TransformableCookie) nodes[1].getCookie(TransformableCookie.class); performer = new SinglePerformer(transformable2, do2, xslt2); performer.perform(); } } else { // nodes.length != 2 for ( int i = 0; i < nodes.length; i++ ) { DataObject dataObject = (DataObject) nodes[i].getCookie(DataObject.class); TransformableCookie transformable = null; boolean xslt = TransformUtil.isXSLTransformation(dataObject); if ( xslt == false ) { transformable = (TransformableCookie) nodes[i].getCookie(TransformableCookie.class); } SinglePerformer performer = new SinglePerformer(transformable, dataObject, xslt); performer.setLastInBatch(i == (nodes.length -1)); performer.perform(); } } } finally { stalled = true; } } /** * Is still running */ public boolean isActive() { return active; } /** * Always return an instance. Shareable by all children nested performers. */ private InputOutputReporter getCookieObserver() { if ( cookieObserver == null ) { String label = Util.THIS.getString("PROP_transformation_io_name"); cookieObserver = new InputOutputReporter(label); } return cookieObserver; } // // class AbstractPerformer // private abstract class AbstractPerformer extends WindowAdapter implements ActionListener { // if called on TransformableCookie node private TransformableCookie transformableCookie; // input XML source DataObject protected DataObject xmlDO; // it should be displayed by CookieObserver! } catch (Exception exc) { // IOException, ParserConfigurationException, SAXException // during prepareData(), previewOutput() and fileOutput() if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(exc); // NotifyDescriptor nd = new NotifyDescriptor.Message (exc.getLocalizedMessage(), NotifyDescriptor.WARNING_MESSAGE); // TopManager.getDefault().notify (nd); ErrorManager.getDefault().notify(ErrorManager.WARNING, exc); } finally { if (isLastInBatch()) { InputOutputReporter cookieObserver = getCookieObserver(); if ( cookieObserver != null ) { cookieObserver.message(Util.THIS.getString("MSG_transformation_2")); cookieObserver.moveToFront(true); } active = false; } } } }); } /** * If possible it finds "file:" URL if fileObject is on LocalFileSystem. * @return URL of fileObject. */ protected URL preferFileURL(FileObject fileObject) throws MalformedURLException, FileStateInvalidException { URL fileURL = null; File file = FileUtil.toFile(fileObject); if ( file != null ) { fileURL = file.toURL(); } else { fileURL = fileObject.getURL(); } return fileURL; } public final void setLastInBatch(boolean last) { this.last = last; } /** * Return if caller uses more perfomers and this one is the last one. */ public final boolean isLastInBatch() { return last; } } // class AbstractPerformer // // class SinglePerformer // private class SinglePerformer extends AbstractPerformer { private DataObject dataObject; private boolean xslt; public SinglePerformer(TransformableCookie transformable, DataObject dataObject, boolean xslt) { super(transformable); this.dataObject = dataObject; this.xslt = xslt; } /** * @throws FileStateInvalidException from baseFO.getURL(); */ protected void init() throws IOException { baseFO = dataObject.getPrimaryFile(); baseURL = preferFileURL(baseFO); if ( xslt ) { xmlDO = null; xmlStylesheetSource = null; xslDO = dataObject; } else { xmlDO = dataObject; xmlStylesheetSource = TransformUtil.getAssociatedStylesheet(baseURL); xslDO = null; } } protected void storeData() { updateHistory(dataObject, xslt); } } // class SinglePerformer // // class DoublePerformer // private class DoublePerformer extends AbstractPerformer { public DoublePerformer(TransformableCookie transformable, DataObject xmlDO, DataObject xslDO) { super(transformable); this.xmlDO = xmlDO; this.xslDO = xslDO; } /** * @throws FileStateInvalidException from baseFO.getURL(); */ protected void init() throws IOException { baseFO = xmlDO.getPrimaryFile(); baseURL = preferFileURL(baseFO); } protected void storeData() { updateHistory(xmlDO, false); updateHistory(xslDO, true); } } // class DoublePerformer }

... 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.