In comp.soft-sys.ptolemy, [EMAIL PROTECTED] (Roger I Martin) writes:
--------

    Hi,
    
    I'm new to Ptolemy and am custom building actors for x3d, mathml and
    femml operations.
    
    I've made a few actors and have more planned. However, before I go
    much further I would like your comments and guidance on fitting,
    organizing and preparing to propose additions to Ptolemy for working
    with x3d, svg, mathml and femml.  Also am interested in what others
    may already be implementing so my efforts are not redundant.
    
    Currently:
    
    X3DDOM
    Purpose: select an x3d document and load it into a DOM when triggered.
    Input: trigger
    Output: Object of org.w3c.dom.Document
    
    MATHMLDOM
    Purpose: select a mathml document and load it into a MathML 2.0
    DOM(http://www.w3.org/TR/MathML2/appendixe.html#dom-bindings.JavaBindings
    org.w3c.dom.mathml.MathMLDocument). There are two types of DOM
    implementation: nonvisual and lightweight components(views)
    Input: trigger
    Output: Object of org.w3c.dom.mathml.MathMLDocument
    
    X3DLoader
    Purpose: Build a Java3D scene graph from a org.w3c.dom.Document filled
    with x3d elements.
    Input: SceneGraph from other Java3D actors
    Input: Object of org.w3c.dom.Document
    Output: SceneGraph[ready for the ViewScreen(more libraries->graphics)]
    
    MathML2EditorTablet
    purpose: display and provide visual editing of mathml 2.0.
    Input: Object of org.w3c.dom.mathml.MathMLDocument
    Output: Object of org.w3c.dom.mathml.MathMLDocument(Especially after
    editing)
    
    Anticipate adding actors for mathml to Java source code xslt
    transforms(http://sourceforge.net/projects/mathml-x/ CVS currently
    broken) that take output from the MathML2EditorTablet or MATHMLDOM
    actors and generate source code that is the runtime "binding" or
    solver of equations defined by mathml documents. Also plan for
    equations in
    mathml to be used to govern x3d extrusion, x3d NURBS, femml
    boundaries, femml forces, etc.
    
    After the CVS for mathml-x at sourceforge is repaired and updated, I
    can put the source code for my current development for Ptolemy there
    or  http://sourceforge.net/projects/femml may be more appropriate.
    
    Thanks,
    Roger
--------
Looks promising.

I encourage you to follow the Ptolemy coding standards at
http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIlatest/ptII/doc/coding/style.htm

Note that you can either pass around ObjectTokens or you can derive
your own types and tokens.
To create a custom type, see the example at
http://ptolemy.eecs.berkeley.edu/ptolemyII/ptII3.0/ptII3.0.2/ptolemy/data/type/test/TestToken.java


BTW - Since the release of Ptolemy II 3.0.2 Yang Zhao has written an
XSLTTransformer actor.  This actor uses the data.expr.FileParameter
class which is not present in Ptolemy II 3.0.2, so the code below
will not work in Ptolemy II 3.0.2, but you get the general idea of the
actor

There is also ptolemy/util/XSLTUtilities.java, see
http://ptolemy.eecs.berkeley.edu/ptolemyII/ptII3.0/ptII3.0.2/ptolemy/util/XSLTUtilities.java

--start--
/* An actor that read an XSLT file and apply it to its input.

@Copyright (c) 2003 The Regents of the University of California.
All rights reserved.

Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in all
copies of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.

                                                PT_COPYRIGHT_VERSION 2
                                                COPYRIGHTENDKEY
@ProposedRating Red ([EMAIL PROTECTED])
@AcceptedRating Red ([EMAIL PROTECTED])
*/

package ptolemy.actor.lib.xslt;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;

import ptolemy.actor.lib.Transformer;
import ptolemy.data.StringToken;
import ptolemy.data.XMLToken;
import ptolemy.data.expr.FileParameter;
import ptolemy.data.type.BaseType;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;
import ptolemy.kernel.util.Workspace;

//////////////////////////////////////////////////////////////////////////
//// XSLTTransformer
/**
This actor reads an XSLT file and apply it to a dom tree. The file or
URL is specified using any form acceptable to the FileParameter class.

<p>Currently, this actor requires the 
<a href="http://saxon.sourceforge.net/";>Saxon</a> XSLT processor
so as to ensure reproducible results.  This restriction may
be relaxed in later versions of this actor. 

<p>FIXME: what should the type of the input/output ports be???.

@see ptolemy.actor.lib.javasound.AudioReader
@author  Yang Zhao, Chrisotpher Hylands Brooks
@version $Id: XSLTransformer.java,v 1.10 2003/09/11 21:36:18 ellen_zh Exp $
@since Ptolemy II 3.1
*/
public class XSLTransformer extends Transformer{

    /** Construct an actor with the given container and name.
     *  @param container The container.
     *  @param name The name of this actor.
     *  @exception IllegalActionException If the actor cannot be contained
     *   by the proposed container.
     *  @exception NameDuplicationException If the container already has an
     *   actor with this name.
     */
    public XSLTransformer(CompositeEntity container, String name)
            throws IllegalActionException, NameDuplicationException {
        super(container, name);

        // Set the type of the input port.
        //input.setMultiport(true);
        input.setTypeEquals(BaseType.XMLTOKEN);

        // Set the type of the output port.
        //output.setMultiport(true);
        output.setTypeEquals(BaseType.STRING);

        fileOrURL = new FileParameter(this, "fileOrURL");
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    /** The file name or URL from which to read.  This is a string with
     *  any form accepted by FileParameter.
     *  @see FileParameter
     */
    public FileParameter fileOrURL;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    /** Clone the actor into the specified workspace. This calls the
     *  base class and then set the filename public member.
     *  @param workspace The workspace for the new object.
     *  @return A new actor.
     *  @exception CloneNotSupportedException If a derived class contains
     *   an attribute that cannot be cloned.
     */
    public Object clone(Workspace workspace)
            throws CloneNotSupportedException {
        XSLTransformer newObject = (XSLTransformer)super.clone(workspace);
        newObject.input.setTypeEquals(BaseType.XMLTOKEN);
        newObject.output.setTypeEquals(BaseType.STRING);
        return newObject;
    }

    /** Consume an XMLToken from the input and apply the XSL transform
     *  specified by the fileOrURL parameter.  If the fileOrURL parameter
     *  does not name a file, then the input is copied to the output
     *  without modification.
     *  @exception IllegalActionException If the parent class throws it
     */
    public void fire() throws IllegalActionException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        javax.xml.transform.Result result =
            new javax.xml.transform.stream.StreamResult(out);
        if (_debugging) {
            _debug("--- open an output stream for the result. \n");
        }

        if (_transformer != null ) {
            for (int i = 0; i < input.getWidth(); i++) {
                if (input.hasToken(i)) {
                    XMLToken in = (XMLToken)input.get(i);
                    Document doc = in.getDomTree();
                    try {
                        javax.xml.transform.Source xmlSource
                            = new javax.xml.transform.dom.DOMSource(doc);
                        _transformer.transform(xmlSource, result);
                        if (_debugging) {
                            _debug("--- transform the xmlSource: "
                                    + in.toString() + "\n");
                        }
                        if (out != null) {
                            if (_debugging) {
                                _debug("--- moml change request string: "
                                        + out.toString() + "\n");
                            }
                            StringToken outputToken =
                                new StringToken(out.toString());
                            output.broadcast(outputToken);
                            if (_debugging) {
                                _debug("--- change request string token "
                                       + "send out. \n");
                            }
                        }
                    } catch (TransformerException ex) {
                        throw new IllegalActionException(this, ex,
                                "Failed  to process '" + in + "'");
                    }
                    try {
                        out.flush();
                        out.close();
                    } catch (IOException ex) {
                        throw new IllegalActionException(this, ex,
                                "Failed  to close or flush '" + out + "'");
                    }
                }
            }
        } else {
            // If there is no transformer, then output the xml string.
            for (int i = 0; i < input.getWidth(); i++) {
                if (input.hasToken(i)) {
                    XMLToken in = (XMLToken)input.get(i);
                    output.broadcast(new StringToken(in.toString()));
                }
            }
        }
    }

    /** Open the XSL file named by the fileOrURL parameter and
     *  set up the transformer.   
     *  @exception IllegalActionException If the TransformFactory
     *  class name does not start with net.sf.saxon. 
     */
    public void initialize() throws IllegalActionException {
        _xsltSource = null;
        _transformer = null;
        try {
            BufferedReader reader;
            // Ignore if the fileOrURL is blank.
            if (fileOrURL.getExpression().trim().equals("")) {
                reader = null;
            } else {
                reader = fileOrURL.openForReading();
            }
            if (reader != null) {
                _xsltSource =
                    new javax.xml.transform.stream.StreamSource(reader);
            } else {
                _xsltSource = null;
            }
            if (_debugging) {
                _debug("processing xsltSource change in " + getFullName());
            }
            if (_xsltSource != null) {
                _transformerFactory =
                    javax.xml.transform.TransformerFactory.newInstance();
                if (!_transformerFactory.getClass()
                        .getName().startsWith("net.sf.saxon")) {

                    throw new IllegalActionException(this,
                            "The XSLTransformer actor works best\nwith "
                            + "saxon7.jar.\n"
                            + "The transformerFactory was '"
                            + _transformerFactory.getClass().getName()
                            + "'.\nIf saxon7.jar was in the classpath, then "
                            + "it should have\nstarted with "
                            + "\"net.sf.saxon\".\n"
                            + "If this actor does not use "
                            + "saxon, then the results will be "
                            + "different between\nruns that "
                            + "use saxon and runs that "
                            + "do not.\nDetails:\n"
                            + "This actor uses "
                            + "javax.xml.transform.TransformerFactory.\nThe "
                            + "concrete TransformerFactory class can be "
                            + "adjusted by\nsetting the "
                            + "javax.xml.transform.TransformerFactory "
                            + "property or by\nreading in a jar file that "
                            + "has the appropriate\nService Provider set.\n"
                            + "(For details about Jar Service Providers,\nsee "
                            + 
"http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html)\n"
                            + "The saxon7.jar file includes a\n" 
                            + 
"META-INF/services/javax.xml.transform.TransformerFactory "
                            + "\nfile that sets the TransformerFactory "
                            + "class name start with 'net.sf.saxon'."
                                                     );
                }
                _transformer = _transformerFactory.newTransformer(_xsltSource);
                if (_debugging) {
                    _debug("1 processing xsltSource change in "
                            + getFullName());
                }
            } else {
                _transformer = null;
            }
        } catch (java.lang.Exception ex) {
            throw new IllegalActionException(this, ex.getMessage());
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private members                   ////

    private javax.xml.transform.Source _xsltSource;

    private javax.xml.transform.TransformerFactory _transformerFactory;

    private javax.xml.transform.Transformer _transformer;
}
--end--

-Christopher

Christopher Hylands Brooks [EMAIL PROTECTED]  University of California
Programmer/Analyst Chess/Ptolemy/GSRC         US Mail: 558 Cory Hall #1770
ph: 510.643.9841 fax:510.642.2739             Berkeley, CA 94720-1770
home: (F-Tu) 707.665.0131 (W-F) 510.655.5480  (office: 400A Cory)


----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list.  Please send administrative
mail for this list to: [EMAIL PROTECTED]

Reply via email to