Re: Wait for User Input

Hi Ari,

I've had a number of occasions to develop what are essentially pass-through
steps, i.e., steps that do something related to the pipeline but don't
alter the actual content flowing through. Below is a very quick shell of a
step that displays a JOptionPane that displays a dialog upon encountering
the step. This could obviously be improved by adding passed arguments,
actually altering processing based upon the chosen option, etc., but I
leave that as an exercise to the implementer since I don't know what you'd
plan to do with it.

Hopefully it's helpful.

Here's the declaration:

---------------------- OptionDialogStep.java ---------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <p:declare-step
        name="option-dialog"
        type="acme:option-dialog"
        version="1.0"
        xmlns:acme="http://acme.com/xproc/1.0/"
        xmlns:p="http://www.w3.org/ns/xproc">

       <p:input port="source"/>
       <p:output port="result"/>

    </p:declare-step>

This must be imported into an existing pipeline using:

    <p:import href="declare-option-dialog.xpl"/>

and then used as a step, e.g.,

    <acme:option-dialog name="option-dialog-step">
        <p:documentation>
            This is a step used to display an option dialog.
        </p:documentation>
    </acme:option-dialog>

And here's the Java code (which I release without license restriction):

---------------------- OptionDialogStep.java ---------------------

package com.acme.transform;

import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;

import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.runtime.XAtomicStep;
import javax.swing.JOptionPane;

/**
 *  An XProc step that displays a dialog box.
 *
 * @author Ichiro Furusato
 */
public class OptionDialogStep extends DefaultStep
{
    private ReadablePipe source = null;  // the source of this step
    private WritablePipe result = null;  // the result of this step


    public OptionDialogStep( XProcRuntime runtime, XAtomicStep step )
    {
        super(runtime, step);
    }


    @Override
    public final void setInput( String port, ReadablePipe pipe )
    {
        source = pipe;
    }


    @Override
    public final void setOutput( String port, WritablePipe pipe )
    {
        result = pipe;
    }


    @Override
    public void reset()
    {
        if ( source != null ) {
            source.resetReader();
        }
        if ( result != null ) {
            result.resetWriter();
        }
    }


    @Override
    public void run() throws SaxonApiException
    {
        try {
            Object message = "Dialog message here.";
            String title   = "Dialog Title";
            int option = JOptionPane.showConfirmDialog(null,
                    message, title,
JOptionPane.YES_NO_CANCEL_OPTION);
            switch ( option ) {
                case JOptionPane.YES_OPTION:
                    System.out.println("option: YES_OPTION");
                    break;
                case JOptionPane.NO_OPTION:
                    System.out.println("option: NO_OPTION");
                    break;
                case JOptionPane.CANCEL_OPTION:
                    System.out.println("option: CANCEL_OPTION");
                    break;
                default:
                    System.out.println("option: default");
                    break;
            }
            super.run();

            // get Saxon document from pipeline
            XdmNode doc = source.read();
            if ( doc == null ) {
                throw new IllegalStateException("null document.");
            }
            // pass through pipeline
            result.write(doc);

        } catch ( Exception e ) {
            throw new SaxonApiException(e.getClass().getName()
                    + " thrown by OptionDialogStep: "
                    + e.getLocalizedMessage(), e);
        }
    }


} // end OptionDialogStep

Received on Thursday, 16 May 2013 08:56:50 UTC