xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ilian Alekov <iale...@tlogica.com>
Subject crimson 1.1 parser problem!
Date Wed, 27 Jun 2001 14:13:12 GMT
Hello!
I need to use the crimson parser in order to parse a SVG document from a
file, so I can produce a buffered image then.
Unfortunately, I have diffiulties.
I get a java.lang.SecurityException when executing my program.

I will apriciate any help!

Best regards,
Ilian Alekov.

my code:
package package3;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//import org.xml.sax.Parser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import java.io.*;
import java.net.URL;

import org.apache.batik.bridge.UserAgent;

import org.w3c.dom.svg.*;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import java.awt.geom.Dimension2D;
import java.awt.geom.AffineTransform;

import org.apache.batik.gvt.event.EventDispatcher;

import org.apache.batik.bridge.*;

import org.apache.batik.gvt.GraphicsNodeRenderContext;
import org.apache.batik.gvt.renderer.StaticRendererFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.renderer.*;


//import oracle.xml.parser.v2.*;
import org.apache.crimson.parser.*;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.GenericDOMImplementation;




//import org.w3c.dom.Document;
//import org.w3c.dom.Element;


/**
 * A Swing-based top level window class.
 * <P>
 * @author Ilian Alekov
 */
public class MyFrame extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
//  JPanel jPanel1 = new JPanel();
  MyCanvas myCanvas = new MyCanvas();
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenu1 = new JMenu();
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenuItem jMenuItem2 = new JMenuItem();
  URL myURL;
  private Graphics2D graphics = null;
  /**
   * Constructs a new instance.
   */
  public MyFrame() {
    super();
    try  {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    jMenu1.setText("File");
    jMenuItem1.setText("Open");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jMenuItem1_actionPerformed(e);
      }
    });
    jMenuItem2.setText("Exit");
    this.getContentPane().add(myCanvas, BorderLayout.CENTER);
    jMenuBar1.add(jMenu1);
    jMenu1.add(jMenuItem1);
    jMenu1.add(jMenuItem2);
    this.setJMenuBar(jMenuBar1);
  }

protected class MUserAgent implements UserAgent {
        AffineTransform currentTransform = null;
        /**
         * Creates a new SVGUserAgent.
         */
        protected MUserAgent(AffineTransform at) {
            currentTransform = at;
        }

        /**
         * Displays an error message.
         */
        public void displayError(String message) {
            System.err.println(message);
        }

        /**
         * Displays an error resulting from the specified Exception.
         */
        public void displayError(Exception ex) {
            ex.printStackTrace(System.err);
        }

        /**
         * Displays a message in the User Agent interface.
         * The given message is typically displayed in a status bar.
         */
        public void displayMessage(String message) {
            System.out.println(message);
        }

        /**
         * Returns a customized the pixel to mm factor.
         */
        public float getPixelToMM() {
            return 0.264583333333333333333f; // 72 dpi
        }

        /**
         * Returns the language settings.
         */
        public String getLanguages() {
            return "en";//userLanguages;
        }

        /**
         * Returns the user stylesheet uri.
         * @return null if no user style sheet was specified.
         */
        public String getUserStyleSheetURI() {
            return null;//userStyleSheetURI;
        }

        /**
         * Returns the class name of the XML parser.
         */
        public String getXMLParserClassName() {
            String parserClassName =
System.getProperty("org.xml.sax.parser");
            if (parserClassName == null) {
                parserClassName = "org.apache.xerces.parsers.SAXParser";
            }
            return
parserClassName;//application.getXMLParserClassName();
        }

        /**
         * Opens a link in a new component.
         * @param doc The current document.
         * @param uri The document URI.
         */
        public void openLink(SVGAElement elt) {
            //application.openLink(uri);
        }

        public Point getClientAreaLocationOnScreen() {
            return new Point(0, 0);
        }

        public void setSVGCursor(java.awt.Cursor cursor) {
        }

        public AffineTransform getTransform() {
            return currentTransform;
        }

        public Dimension2D getViewportSize() {
            return new Dimension(100, 100);
        }

        public EventDispatcher getEventDispatcher() {
            return null;
        }

        public boolean supportExtension(String str) {
            return false;
        }

        public boolean hasFeature(String str) {
            return false;
        }

        public void registerExtension(BridgeExtension be) {
        }
    }

    protected void renderSVGDocument(Document doc, int x, int y) {
        UserAgent userAgent = new MUserAgent(new AffineTransform());

        GVTBuilder builder = new GVTBuilder();
        GraphicsNodeRenderContext rc = (new
StaticRendererFactory()).getRenderContext();
        BridgeContext ctx = new BridgeContext(userAgent, rc);
        GraphicsNode root;
        graphics.translate(200, 100);
        graphics.setRenderingHints(rc.getRenderingHints());
        root = builder.build(ctx, doc);
        root.paint(graphics, rc);
        graphics.translate(-200, -100);
    }

  void jMenuItem1_actionPerformed(ActionEvent e) {
  BufferedImage myBImage = new
BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
   JFileChooser fc = new JFileChooser("c:\\.");

   int choice = fc.showOpenDialog(myCanvas);
   if (choice == JFileChooser.APPROVE_OPTION)
   {
      File f = fc.getSelectedFile();
      try {
            myURL = f.toURL();
          }catch (Exception ex){ System.out.print(ex);}

      System.out.print("I'll try to parse it " + myURL.toString());
      try {

        // Step 1: create a DocumentBuilderFactory
        DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
        // Step 2: create a DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // Step 3: parse the input file to get a Document object
        Document doc = db.parse(f);


        graphics = (Graphics2D)myBImage.getGraphics();
      //  doc =parser.getDocument();
        renderSVGDocument(doc,50,50);
        myCanvas.setImage(myBImage);
       }
       catch(Exception ex) {
       System.out.print(ex);
        }
     
    }

  }
}
Mime
View raw message