You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by vh...@apache.org on 2002/07/11 18:42:46 UTC

cvs commit: xml-batik/resources/org/apache/batik/apps/svgbrowser/resources GUI.properties

vhardy      2002/07/11 09:42:46

  Modified:    sources/org/apache/batik/apps/svgbrowser
                        JSVGViewerFrame.java LocalHistory.java Main.java
               resources/org/apache/batik/apps/svgbrowser/resources
                        GUI.properties
  Added:       sources/org/apache/batik/apps/svgbrowser
                        SVGInputHandler.java SquiggleInputHandler.java
                        XMLInputHandler.java
               samples/extensions/xsl data.xml data.xsl nodata.xsl
  Log:
  Added support for XML/XSL documents in Squiggle. Squiggle can now load an XML document and apply any referenced XSL stylesheet. This also works for self-referencing stylesheets and literal result templates. There are two examples in samples/extensions/xsl: data.xml and data.xsl. To enable XSL/XML support in Squiggle, you need to uncomment the entry in resources/META-INF/services/org.apache.batik.apps.svgbrowser.SquiggleInputHandler. Note that the mechanism used to handle XML/XSL files is generic and can be built upon to add support for other types of files and, most interstingly, for file types for which there are converters to the SVG format
  
  Revision  Changes    Path
  1.83      +138 -17   xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java
  
  Index: JSVGViewerFrame.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java,v
  retrieving revision 1.82
  retrieving revision 1.83
  diff -u -r1.82 -r1.83
  --- JSVGViewerFrame.java	27 Jun 2002 01:37:16 -0000	1.82
  +++ JSVGViewerFrame.java	11 Jul 2002 16:42:45 -0000	1.83
  @@ -45,6 +45,8 @@
   import java.io.OutputStream;
   import java.io.Reader;
   
  +import java.net.MalformedURLException;
  +
   import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.HashMap;
  @@ -55,6 +57,7 @@
   import java.util.Map;
   import java.util.MissingResourceException;
   import java.util.ResourceBundle;
  +import java.util.Vector;
   
   import java.util.zip.GZIPInputStream;
   
  @@ -132,6 +135,7 @@
   import org.apache.batik.transcoder.print.PrintTranscoder;
   
   import org.apache.batik.util.ParsedURL;
  +import org.apache.batik.util.Service;
   import org.apache.batik.util.MimeTypeConstants;
   import org.apache.batik.util.gui.DOMViewer;
   import org.apache.batik.util.gui.JErrorPane;
  @@ -277,6 +281,16 @@
           = Resources.getString("JSVGViewerFrame.property.os.windows.prefix");
   
       /**
  +     * The input handlers
  +     */
  +    protected static Vector handlers;
  +
  +    /**
  +     * The default input handler
  +     */
  +    protected static SquiggleInputHandler defaultHandler = new SVGInputHandler();
  +
  +    /**
        * The resource bundle
        */
       protected static ResourceBundle bundle;
  @@ -517,7 +531,7 @@
               JMenuBar mb = mf.createJMenuBar("MenuBar");
               setJMenuBar(mb);
   
  -            localHistory = new LocalHistory(mb, svgCanvas);
  +            localHistory = new LocalHistory(mb, this);
   
               String uri[] = application.getVisitedURIs();
               for (int i=0; i<uri.length; i++) {
  @@ -693,7 +707,7 @@
                           }
                           locationBar.setText(st);
                           locationBar.addToHistory(st);
  -                        svgCanvas.loadSVGDocument(st);
  +                        showSVGDocument(st);
                       }
                   }
               }
  @@ -773,23 +787,112 @@
               fileChooser.setFileHidingEnabled(false);
               fileChooser.setFileSelectionMode
                   (JFileChooser.FILES_ONLY);
  -            fileChooser.addChoosableFileFilter(new SVGFileFilter());
   
  +            //
  +            // Add file filters from the handlers map
  +            //
  +            Iterator iter = getHandlers().iterator();
  +            while (iter.hasNext()) {
  +                SquiggleInputHandler handler 
  +                    = (SquiggleInputHandler)iter.next();
  +                fileChooser.addChoosableFileFilter
  +                    (new SquiggleInputHandlerFilter(handler));
  +            }
  +            
               int choice = fileChooser.showOpenDialog(JSVGViewerFrame.this);
               if (choice == JFileChooser.APPROVE_OPTION) {
                   File f = fileChooser.getSelectedFile();
  -
  -                try {
  -                    currentPath = f;
  -                    svgCanvas.loadSVGDocument(f.toURL().toString());
  -                } catch (IOException ex) {
  -                    userAgent.displayError(ex);
  +                
  +                currentPath = f;
  +                try { 
  +                    String furl = f.toURL().toString();
  +                    showSVGDocument(furl);
  +                } catch (MalformedURLException ex) {
  +                    if (userAgent != null) {
  +                        userAgent.displayError(ex);
  +                    }
                   }
               }
           }
       }
   
       /**
  +     * Shows the given document into the viewer frame
  +     */
  +    public void showSVGDocument(String uri){
  +        try {
  +            ParsedURL purl = new ParsedURL(uri);
  +            SquiggleInputHandler 
  +                handler = getInputHandler(purl);
  +            
  +            handler.handle(purl,
  +                           JSVGViewerFrame.this);
  +        } catch (Exception e) {
  +            if (userAgent != null) {
  +                userAgent.displayError(e);
  +            }
  +        }
  +
  +    }
  +
  +    /**
  +     * Returns the input handler for the given URI
  +     */
  +    public SquiggleInputHandler getInputHandler(ParsedURL purl) throws IOException {
  +        Iterator iter = getHandlers().iterator();
  +        SquiggleInputHandler handler = null;
  +
  +        while (iter.hasNext()) {
  +            SquiggleInputHandler curHandler = 
  +                (SquiggleInputHandler)iter.next();
  +            if (curHandler.accept(purl)) {
  +                handler = curHandler;
  +                break;
  +            }
  +        }
  +
  +        // No handler found, use the default one.
  +        if (handler == null) {
  +            handler = defaultHandler;
  +        }
  +
  +        return handler;
  +    }
  +
  +
  +    /**
  +     * Returns the list of input file handler. 
  +     */
  +    protected static Vector getHandlers() {
  +        if (handlers != null) {
  +            return handlers;
  +        }
  +
  +        handlers = new Vector();
  +        registerHandler(new SVGInputHandler());
  +        
  +        Iterator iter = Service.providers(SquiggleInputHandler.class);
  +        while (iter.hasNext()) {
  +            SquiggleInputHandler handler 
  +                = (SquiggleInputHandler)iter.next();
  +
  +            registerHandler(handler);
  +        }
  +
  +        return handlers;
  +    }
  +
  +    /**
  +     * Registers an input file handler by adding it to the handlers map.
  +     * @param handler the new input handler to register.
  +     */
  +    public static synchronized 
  +        void registerHandler(SquiggleInputHandler handler) {
  +        Vector handlers = getHandlers();
  +        handlers.addElement(handler);
  +    }
  +
  +    /**
        * To open a new document.
        */
       public class OpenLocationAction extends AbstractAction {
  @@ -842,7 +945,8 @@
                           if (t.length() != 0) {
                               s += "#" + t;
                           }
  -                        svgCanvas.loadSVGDocument(s);
  +
  +                        showSVGDocument(s);
                       }
                   }
               }
  @@ -1223,8 +1327,11 @@
                       try {
                           Document  doc = new PlainDocument();
   
  +                        ParsedURL purl = new ParsedURL(svgDocument.getURL());
                           InputStream is
  -                            = u.openStream(MimeTypeConstants.MIME_TYPES_SVG);
  +                            = u.openStream(getInputHandler(purl).
  +                                           getHandledMimeTypes());
  +                        // u.openStream(MimeTypeConstants.MIME_TYPES_SVG);
   
                           Reader in = XMLUtilities.createXMLDocumentReader(is);
                           int len;
  @@ -1595,6 +1702,7 @@
           svgCanvas.setCursor(WAIT_CURSOR);
       }
   
  +
       /**
        * Called when the loading of a document was completed.
        */
  @@ -1603,9 +1711,22 @@
               System.out.print("Document load completed in ");
               System.out.println((System.currentTimeMillis() - time) + " ms");
           }
  -        svgDocument = e.getSVGDocument();
  +
  +        setSVGDocument(e.getSVGDocument(),
  +                       e.getSVGDocument().getURL(),
  +                       e.getSVGDocument().getTitle());
  +    }
  +
  +    /**
  +     * Forces the viewer frame to show the input SVGDocument
  +     */
  +    public void setSVGDocument(SVGDocument svgDocument,
  +                               String svgDocumentURL,
  +                               String svgDocumentTitle) {
  +        this.svgDocument = svgDocument;
  +
           if (domViewer != null) {
  -            if(domViewer.isVisible()) {
  +            if(domViewer.isVisible() && svgDocument != null) {
                   domViewer.setDocument(svgDocument,
                                         (ViewCSS)svgDocument.getDocumentElement());
               } else {
  @@ -1615,7 +1736,7 @@
           }
           stopAction.update(false);
           svgCanvas.setCursor(DEFAULT_CURSOR);
  -        String s = svgDocument.getURL();
  +        String s = svgDocumentURL;
           String t = svgCanvas.getFragmentIdentifier();
           if (t != null) {
               s += "#" + t;
  @@ -1626,7 +1747,7 @@
               title = getTitle();
           }
   
  -        String dt = svgDocument.getTitle();
  +        String dt = svgDocumentTitle;
           if (dt.length() != 0) {
               setTitle(title + ":" + dt);
           } else {
  @@ -2173,7 +2294,7 @@
               if (newc) {
                   application.openLink(uri);
               } else {
  -                svgCanvas.loadSVGDocument(uri);
  +                showSVGDocument(uri);
               }
           }
   
  
  
  
  1.9       +10 -11    xml-batik/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java
  
  Index: LocalHistory.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- LocalHistory.java	14 Nov 2001 15:23:52 -0000	1.8
  +++ LocalHistory.java	11 Jul 2002 16:42:45 -0000	1.9
  @@ -30,11 +30,10 @@
    * @version $Id$
    */
   public class LocalHistory {
  -
       /**
  -     * The canvas to manage.
  +     * The frame to manage.
        */
  -    protected JSVGCanvas svgCanvas;
  +    protected JSVGViewerFrame svgFrame;    
   
       /**
        * The menu which contains the history.
  @@ -82,10 +81,10 @@
        * @param mb The menubar used to display the history. It must
        *        contains one '@@@' item used as marker to place the
        *        history items.
  -     * @param canvas The canvas to manage.
  +     * @param svgFrame The frame to manage.
        */
  -    public LocalHistory(JMenuBar mb, JSVGCanvas canvas) {
  -        svgCanvas = canvas;
  +    public LocalHistory(JMenuBar mb, JSVGViewerFrame svgFrame) {
  +        this.svgFrame = svgFrame;
   
           // Find the marker.
           int mc = mb.getMenuCount();
  @@ -116,7 +115,7 @@
           update();
           state = BACK_PENDING_STATE;
           currentURI -= 2;
  -        svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
  +        svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
       }
   
       /**
  @@ -133,7 +132,7 @@
       public void forward() {
           update();
           state = FORWARD_PENDING_STATE;
  -        svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
  +        svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
       }
   
       /**
  @@ -150,7 +149,7 @@
           update();
           state = RELOAD_PENDING_STATE;
           currentURI--;
  -        svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
  +        svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
       }
   
       /**
  @@ -230,7 +229,7 @@
   	public void actionPerformed(ActionEvent e) {
   	    String uri = e.getActionCommand();
               currentURI = getItemIndex((JMenuItem)e.getSource()) - 1;
  -	    svgCanvas.loadSVGDocument(uri);
  +	    svgFrame.showSVGDocument(uri);
   	}
           public int getItemIndex(JMenuItem item) {
               int ic = menu.getItemCount();
  
  
  
  1.41      +2 -3      xml-batik/sources/org/apache/batik/apps/svgbrowser/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/Main.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- Main.java	22 Jun 2002 10:09:52 -0000	1.40
  +++ Main.java	11 Jul 2002 16:42:45 -0000	1.41
  @@ -465,8 +465,7 @@
                       if (frame == null)
                           frame = createAndShowJSVGViewerFrame();
   
  -                    frame.getJSVGCanvas().loadSVGDocument
  -                        (uri);
  +                    frame.showSVGDocument(uri);
                       frame = null;
                   } else {
                       // Let the user know that we are
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/apps/svgbrowser/SVGInputHandler.java
  
  Index: SVGInputHandler.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.apps.svgbrowser;
  
  import javax.swing.filechooser.FileFilter;
  
  import java.io.File;
  
  import org.apache.batik.swing.JSVGCanvas;
  import org.apache.batik.util.ParsedURL;
  
  /**
   * This implementation of the <tt>SquiggleInputHandler</tt> class
   * simply displays an SVG file into the JSVGCanvas.
   *
   * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
   * @version $Id: SVGInputHandler.java,v 1.1 2002/07/11 16:42:45 vhardy Exp $
   */
  public class SVGInputHandler implements SquiggleInputHandler {
      public static final String[] SVG_MIME_TYPES = 
      { "image/svg+xml" };
  
      public static final String[] SVG_FILE_EXTENSIONS =
      { ".svg", ".svgz" };
  
      /**
       * Returns the list of mime types handled by this handler.
       */
      public String[] getHandledMimeTypes() {
          return SVG_MIME_TYPES;
      }
      
      /**
       * Returns the list of file extensions handled by this handler
       */
      public String[] getHandledExtensions() {
          return SVG_FILE_EXTENSIONS;
      }
  
      /**
       * Returns a description for this handler.
       */
      public String getDescription() {
          return "";
      }
  
      /**
       * Handles the given input for the given JSVGViewerFrame
       */
      public void handle(ParsedURL purl, JSVGViewerFrame svgViewerFrame) {
          svgViewerFrame.getJSVGCanvas().loadSVGDocument(purl.toString());
      }
  
      /**
       * Returns true if the input file can be handled.
       */
      public boolean accept(File f) {
          return f != null && f.isFile() && accept(f.getPath());
      }
  
      /**
       * Returns true if the input URI can be handled by the handler
       */
      public boolean accept(ParsedURL purl) {
          // <!> Note: this should be improved to rely on Mime Type 
          //     when the http protocol is used. This will use the 
          //     ParsedURL.getContentType method.
          if (purl == null) {
              return false;
          }
  
          String path = purl.getPath();
  
          return accept(path);
      }
  
      /**
       * Returns true if the resource at the given path can be handled
       */
      public boolean accept(String path) {
          for (int i=0; i<SVG_FILE_EXTENSIONS.length; i++) {
              if (path.endsWith(SVG_FILE_EXTENSIONS[i])) {
                  return true;
              }
          }
  
          return false;
      }
  }
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/apps/svgbrowser/SquiggleInputHandler.java
  
  Index: SquiggleInputHandler.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.apps.svgbrowser;
  
  import java.io.File;
  
  import org.apache.batik.util.ParsedURL;
  
  /**
   * This is the interface expected from classes which can handle specific 
   * types of input for the Squiggle SVG browser. The simplest implementation
   * will simply handle SVG documents. Other, more sophisticated implementations
   * will handle other types of documents and convert them into SVG before
   * displaying them in an SVG canvas.
   *
   * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
   * @version $Id: SquiggleInputHandler.java,v 1.1 2002/07/11 16:42:45 vhardy Exp $
   */
  public interface SquiggleInputHandler {
      /**
       * Returns the list of mime types handled by this handler.
       */
      String[] getHandledMimeTypes();
  
      /**
       * Returns the list of file extensions handled by this handler
       */
      String[] getHandledExtensions();
  
      /**
       * Returns a description for this handler
       */
      String getDescription();
  
      /**
       * Returns true if the input file can be handled by the handler
       */
      boolean accept(File f);
  
      /**
       * Returns true if the input URI can be handled by the handler
       * @param purl URL describing the candidate input
       */
      boolean accept(ParsedURL purl);
  
      /**
       * Handles the given input for the given JSVGViewerFrame
       */
      void handle(ParsedURL purl, JSVGViewerFrame svgFrame) throws Exception ;
  }
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/apps/svgbrowser/XMLInputHandler.java
  
  Index: XMLInputHandler.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.apps.svgbrowser;
  
  import javax.swing.filechooser.FileFilter;
  
  import org.apache.batik.swing.JSVGCanvas;
  import org.apache.batik.swing.svg.SVGFileFilter;
  import org.apache.batik.dom.svg.SVGDOMImplementation;
  import org.apache.batik.dom.svg.SVGOMDocument;
  import org.apache.batik.dom.StyleSheetProcessingInstruction;
  
  import java.io.File;
  import java.io.StringWriter;
  import java.io.StringReader;
  
  import java.net.URL;
  
  import org.w3c.dom.Attr;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.ProcessingInstruction;
  import org.w3c.dom.DOMImplementation;
  import org.w3c.dom.svg.SVGDocument;
  import org.w3c.dom.stylesheets.StyleSheet;
  
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.dom.DOMResult;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.apache.batik.dom.util.DOMUtilities;
  import org.apache.batik.dom.util.HashTable;
  import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
  import org.apache.batik.util.XMLResourceDescriptor;
  import org.apache.batik.util.ParsedURL;
  import org.apache.batik.util.SVGConstants;
  
  /**
   * This implementation of the <tt>SquiggleInputHandler</tt> class
   * handles XML files by looking for the first
   * &lt;?xml-stylesheet ... ?&gt; processing instruction referencing
   * an xsl document. In case there is one, the transform is applied to the 
   * input XML file and the handler checks that the result is an 
   * SVG document with an SVG root.
   *
   * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
   * @version $Id: XMLInputHandler.java,v 1.1 2002/07/11 16:42:45 vhardy Exp $
   */
  public class XMLInputHandler implements SquiggleInputHandler {
      public static final String[] XVG_MIME_TYPES = 
      { "image/xml+xsl+svg" };
  
      public static final String[] XVG_FILE_EXTENSIONS =
      { ".xml", ".xsl" };
  
      public static final String ERROR_NO_XML_STYLESHEET_PROCESSING_INSTRUCTION
          = "XMLInputHandler.error.no.xml.stylesheet.processing.instruction";
  
      public static final String ERROR_TRANSFORM_OUTPUT_NOT_SVG
          = "XMLInputHandler.error.transform.output.not.svg";
  
      public static final String ERROR_TRANSFORM_PRODUCED_NO_CONTENT
          = "XMLInputHandler.error.transform.produced.no.content";
  
      public static final String ERROR_TRANSFORM_OUTPUT_WRONG_NS
          = "XMLInputHandler.error.transform.output.wrong.ns";
  
      public static final String ERROR_RESULT_GENERATED_EXCEPTION 
          = "XMLInputHandler.error.result.generated.exception";
  
      public static final String XSL_PROCESSING_INSTRUCTION_TYPE
          = "text/xsl";
  
      public static final String PSEUDO_ATTRIBUTE_TYPE
          = "type";
  
      public static final String PSEUDO_ATTRIBUTE_HREF
          = "href";
  
      /**
       * Returns the list of mime types handled by this handler.
       */
      public String[] getHandledMimeTypes() {
          return XVG_MIME_TYPES;
      }
      
      /**
       * Returns the list of file extensions handled by this handler
       */
      public String[] getHandledExtensions() {
          return XVG_FILE_EXTENSIONS;
      }
  
      /**
       * Returns a description for this handler
       */
      public String getDescription() {
          return "";
      }
  
      /**
       * Returns true if the input file can be handled by the handler
       */
      public boolean accept(File f) {
          return f.isFile() && accept(f.getPath());
      }
  
      /**
       * Returns true if the input URI can be handled by the handler
       */
      public boolean accept(ParsedURL purl) {
          if (purl == null) {
              return false;
          }
  
          // <!> Note: this should be improved to rely on Mime Type 
          //     when the http protocol is used. This will use the 
          //     ParsedURL.getContentType method.
  
          String path = purl.getPath();        
          return accept(path);
      }
  
      /**
       * Return true if the resource with the given path can 
       * be handled.
       */
      public boolean accept(String path) {
          for (int i=0; i<XVG_FILE_EXTENSIONS.length; i++) {
              if (path.endsWith(XVG_FILE_EXTENSIONS[i])) {
                  return true;
              }
          }
  
          return false;
      }
  
      /**
       * Handles the given input for the given JSVGViewerFrame
       */
      public void handle(ParsedURL purl, JSVGViewerFrame svgViewerFrame) throws Exception {
          String uri = purl.toString();
  
          TransformerFactory tFactory 
              = TransformerFactory.newInstance();
          
          // First, load the input XML document into a generic DOM tree
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          dbf.setValidating(false);
          dbf.setNamespaceAware(true);
  
          DocumentBuilder db = dbf.newDocumentBuilder();
  
          Document inDoc = db.parse(uri);
         
          // Now, look for <?xml-stylesheet ...?> processing instructions
          String xslStyleSheetURI 
              = extractXSLProcessingInstruction(inDoc);
          
          if (xslStyleSheetURI == null) {
              // Assume that the input file is a literal result template
              xslStyleSheetURI = uri;
          }
  
          ParsedURL parsedXSLStyleSheetURI 
              = new ParsedURL(uri, xslStyleSheetURI);
  
          Transformer transformer
              = tFactory.newTransformer
              (new StreamSource(parsedXSLStyleSheetURI.toString()));
  
          // Now, apply the transformation to the input document.
          DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
          String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  
          //
          // <!> Due to issues with namespaces, the transform creates the 
          //     result in a stream which is parsed. This is sub-optimal
          //     but this was the only solution found to be able to 
          //     generate content in the proper namespaces.
          //
          // SVGOMDocument outDoc = (SVGOMDocument)impl.createDocument(svgNS, "svg", null);
          // outDoc.setURLObject(new URL(uri));
          // transformer.transform
          //     (new DOMSource(inDoc),
          //     new DOMResult(outDoc.getDocumentElement()));
          //
          StringWriter sw = new StringWriter();
          StreamResult result = new StreamResult(sw);
          transformer.transform(new DOMSource(inDoc),
                                result);
          sw.flush();
          sw.close();
  
          String parser = XMLResourceDescriptor.getXMLParserClassName();
          SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
          SVGDocument outDoc = null;
  
          try {
              outDoc = f.createDocument(uri, 
                                        new StringReader(sw.toString()));
          } catch (Exception e) {
              throw new IllegalArgumentException
                  (Resources.getString(ERROR_RESULT_GENERATED_EXCEPTION));
          }
  
          // Patch the result tree to go under the root node
          // checkAndPatch(outDoc);
          
          svgViewerFrame.getJSVGCanvas().setSVGDocument((SVGDocument)outDoc);
          svgViewerFrame.setSVGDocument(outDoc,
                                        uri,
                                        outDoc.getTitle());
      }
  
      /**
       * This method checks that the generated content is SVG.
       *
       * This method accounts for the fact that the root svg's first child
       * is the result of the transform. It moves all its children under the root
       * and sets the attributes
       */
      protected void checkAndPatch(Document doc) {
          Element root = doc.getDocumentElement();
          Node realRoot = root.getFirstChild();
          String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  
          if (realRoot == null) {
              throw new IllegalArgumentException
                  (Resources.getString(ERROR_TRANSFORM_PRODUCED_NO_CONTENT));
          }
  
          if (realRoot.getNodeType() != realRoot.ELEMENT_NODE
              || 
              !SVGConstants.SVG_SVG_TAG.equals(realRoot.getLocalName())) {
              throw new IllegalArgumentException
                  (Resources.getString(ERROR_TRANSFORM_OUTPUT_NOT_SVG));
          }
  
          if (!svgNS.equals(realRoot.getNamespaceURI())) {
              throw new IllegalArgumentException
                  (Resources.getString(ERROR_TRANSFORM_OUTPUT_WRONG_NS));
          }
  
          Node child = realRoot.getFirstChild();
          while ( child != null ) {
              root.appendChild(child);
              child = realRoot.getFirstChild();
          }
  
          NamedNodeMap attrs = realRoot.getAttributes();
          int n = attrs.getLength();
          for (int i=0; i<n; i++) {
              root.setAttributeNode((Attr)attrs.item(i));
          }
  
          root.removeChild(realRoot);
      }
  
      /**
       * Extracts the first XSL processing instruction from the input 
       * XML document. 
       */
      protected String extractXSLProcessingInstruction(Document doc) {
          Node child = doc.getFirstChild();
          while (child != null) {
              if (child.getNodeType() == child.PROCESSING_INSTRUCTION_NODE) {
                  ProcessingInstruction pi 
                      = (ProcessingInstruction)child;
                  
                  HashTable table = new HashTable();
                  DOMUtilities.parseStyleSheetPIData(pi.getData(),
                                                     table);
  
                  Object type = table.get(PSEUDO_ATTRIBUTE_TYPE);
                  if (XSL_PROCESSING_INSTRUCTION_TYPE.equals(type)) {
                      Object href = table.get(PSEUDO_ATTRIBUTE_HREF);
                      if (href != null) {
                          return href.toString();
                      } else {
                          return null;
                      }
                  }
              }
              child = child.getNextSibling();
          }
  
          return null;
      }
  
  }
  
  
  
  1.1                  xml-batik/samples/extensions/xsl/data.xml
  
  Index: data.xml
  ===================================================================
  <?xml version="1.0" encoding="utf-8"?>
  <!-- ========================================================================= -->
  <!-- Copyright (C) The Apache Software Foundation. All rights reserved.        -->
  <!--                                                                           -->
  <!-- This software is published under the terms of the Apache Software License -->
  <!-- version 1.1, a copy of which has been included with this distribution in  -->
  <!-- the LICENSE file.                                                         -->
  <!-- ========================================================================= -->
  
  <!-- ========================================================================= -->
  <!-- Courtesy of Max Froumentin. This document can be processed directly by    -->
  <!-- the Squiggle browser which will apply the stylesheet (if the              -->
  <!-- XMLInputHandler in  resources/META-INF/services/                          -->
  <!-- (the org.apache.batik.apps.svgbrowser.SquiggleInputHandler file) is       -->
  <!-- enabled.                                                                  -->
  <!--                                                                           -->
  <!-- @author vincent.hardy@eng.sun.com                                         -->
  <!-- @version $Id: data.xml,v 1.1 2002/07/11 16:42:45 vhardy Exp $                                                             -->
  <!-- ========================================================================= -->
  <?xml-stylesheet href="data.xsl" type="text/xsl"?>
  <data depth="30">
  </data>
  
  
  
  1.1                  xml-batik/samples/extensions/xsl/data.xsl
  
  Index: data.xsl
  ===================================================================
  <?xml version="1.0" encoding="utf-8"?>
  <!-- ========================================================================= -->
  <!-- Copyright (C) The Apache Software Foundation. All rights reserved.        -->
  <!--                                                                           -->
  <!-- This software is published under the terms of the Apache Software License -->
  <!-- version 1.1, a copy of which has been included with this distribution in  -->
  <!-- the LICENSE file.                                                         -->
  <!-- ========================================================================= -->
  
  <!-- ========================================================================= -->
  <!-- Courtesy of Max Froumentin. This document is referenced by data.xml       -->
  <!--                                                                           -->
  <!-- @author vincent.hardy@eng.sun.com                                         -->
  <!-- @version $Id: data.xsl,v 1.1 2002/07/11 16:42:45 vhardy Exp $                                                             -->
  <!-- ========================================================================= -->
  <?xml-stylesheet href="" type="text/xsl"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns="http://www.w3.org/2000/svg"
                  xmlns:xlink="http://www.w3.org/1999/xlink" 
                  version="1.0">
  
    <xsl:template match="/">
      <svg viewBox="-40 -40 80 80" width="1024" height="768" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="gradient" gradientUnits="userSpaceOnUse"
            x1="-100" y1="-100" x2="100" y2="100">
            <stop offset="0" stop-color="crimson"/>
            <stop offset="0.8" stop-color="gold"/>
            <stop offset="1" stop-color="yellow" />
          </linearGradient>
  
          <linearGradient id="strokeGradient" gradientUnits="userSpaceOnUse"
            x1="100" y1="100" x2="-100" y2="-100">
            <stop offset=".2" stop-color="gold"/>
            <stop offset="1"  stop-color="rgb(128,0,0)" />
          </linearGradient>
        </defs>
  
        <g id="spiral">
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="data[position()=1]/@depth"/>
          </xsl:call-template>
        </g>
      </svg>
    </xsl:template>
  
    <xsl:template name="draw-primitive">
      <xsl:param name="depth" select="0"/>
      <xsl:if test="$depth > 0">
        <g transform="scale(.8, .8) rotate(10)">
          <rect x="-100" y="-100" width="200" height="200" fill="url(#gradient)" stroke="none" stroke-width="5"/>
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="$depth - 1"/>
          </xsl:call-template>            
        </g>
      </xsl:if>
    </xsl:template>
  </xsl:stylesheet>
  
  
  
  1.1                  xml-batik/samples/extensions/xsl/nodata.xsl
  
  Index: nodata.xsl
  ===================================================================
  <?xml version="1.0" encoding="utf-8"?>
  <!-- ========================================================================= -->
  <!-- Copyright (C) The Apache Software Foundation. All rights reserved.        -->
  <!--                                                                           -->
  <!-- This software is published under the terms of the Apache Software License -->
  <!-- version 1.1, a copy of which has been included with this distribution in  -->
  <!-- the LICENSE file.                                                         -->
  <!-- ========================================================================= -->
  
  <!-- ========================================================================= -->
  <!-- Courtesy of Max Froumentin. This document can be processed directly by    -->
  <!-- the Squiggle browser. This file as an empty xsl stylesheet, which means   -->
  <!-- that it applies to itself (no data).                                      -->
  <!--                                                                           -->
  <!-- @author vincent.hardy@eng.sun.com                                         -->
  <!-- @version $Id: nodata.xsl,v 1.1 2002/07/11 16:42:45 vhardy Exp $                                                             -->
  <!-- ========================================================================= -->
  <?xml-stylesheet href="" type="text/xsl"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns="http://www.w3.org/2000/svg"
                  xmlns:xlink="http://www.w3.org/1999/xlink" 
                  version="1.0">
  
    <xsl:template match="/">
      <svg viewBox="-40 -40 80 80" width="1024" height="768" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="gradient" gradientUnits="userSpaceOnUse"
            x1="-100" y1="-100" x2="100" y2="100">
            <stop offset="0" stop-color="crimson"/>
            <stop offset="0.8" stop-color="gold"/>
            <stop offset="1" stop-color="yellow" />
          </linearGradient>
  
          <linearGradient id="strokeGradient" gradientUnits="userSpaceOnUse"
            x1="100" y1="100" x2="-100" y2="-100">
            <stop offset=".2" stop-color="gold"/>
            <stop offset="1"  stop-color="rgb(128,0,0)" />
          </linearGradient>
        </defs>
  
        <g id="spiral">
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="40"/>
          </xsl:call-template>
        </g>
      </svg>
    </xsl:template>
  
    <xsl:template name="draw-primitive">
      <xsl:param name="depth" select="0"/>
      <xsl:if test="$depth > 0">
        <g transform="scale(.8, .8) rotate(10)">
          <rect x="-100" y="-100" width="200" height="200" fill="url(#gradient)" stroke="none" stroke-width="5"/>
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="$depth - 1"/>
          </xsl:call-template>            
        </g>
      </xsl:if>
    </xsl:template>
  </xsl:stylesheet>
  
  
  
  1.54      +20 -1     xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/GUI.properties
  
  Index: GUI.properties
  ===================================================================
  RCS file: /home/cvs/xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/GUI.properties,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -r1.53 -r1.54
  --- GUI.properties	27 Jun 2002 01:37:16 -0000	1.53
  +++ GUI.properties	11 Jul 2002 16:42:45 -0000	1.54
  @@ -559,3 +559,22 @@
   Stefano Mazzochi, John Morisson, Andreas Neumann, Luan O'Carroll, \
   Sheng Pei, Neeme Praks, Henri Ruini, Nicolas Socheleau,\
    David Schweinsberg, Nicholas Talian.
  +
  +#
  +# XMLInputHandler messages
  +#
  +XMLInputHandler.error.no.xml.stylesheet.processing.instruction = The input XML file does \
  +not contain an <?xml-stylesheet?> processing instruction of type "text/xsl"
  +
  +XMLInputHandler.error.transform.output.not.svg = The result of the XSL transformation \
  +did not produce an SVG document with a root <svg> element. It cannot be handled by Squiggle.
  +
  +XMLInputHandler.error.transform.produced.no.content = The XSL transformation did \
  +no produce any content.
  +
  +XMLInputHandler.error.transform.output.wrong.ns = The result of the XSL transformation \
  +produced a document which is not in the SVG namespace (http://www.w3.org/2000/svg)
  +
  +XMLInputHandler.error.result.generated.exception = An error occured when processing the \
  +result of the transformation applied to your document. Check that the transformation does \
  +not produce an empty document and that it is a well formed SVG document.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org