001: // The UMLet source code is distributed under the terms of the GPL; see license.txt
002: package com.umlet.control.io;
003:
004: import java.awt.*;
005: import java.io.*;
006: import java.util.*;
007:
008: import org.apache.batik.dom.*;
009: import org.apache.batik.svggen.*;
010: import org.w3c.dom.*;
011:
012: import com.umlet.control.*;
013: import com.umlet.element.base.Entity;
014:
015: public class GenSvg {
016: private static GenSvg _instance;
017:
018: public static GenSvg getInstance() {
019: if (_instance == null) {
020: _instance = new GenSvg();
021: }
022: return _instance;
023: }
024:
025: public void paint(Graphics2D g2d) {
026: /*g2d.setPaint(Color.red);
027: g2d.fill(new Rectangle(10, 10, 100, 100));
028: */
029: /*Actor a=new Actor();
030: a.setSize(80,120);
031: a.setState("Testactor");
032: a.setLocation(40,150);*/
033:
034: Vector v = Selector.getInstance().getAllEntitiesOnPanel();
035:
036: for (int i = v.size() - 1; i >= 0; i--) {
037: Entity e = (Entity) v.elementAt(i);
038: g2d.translate(e.getX(), e.getY());
039: e.paint(g2d);
040: g2d.translate(-e.getX(), -e.getY());
041: }
042:
043: //Frame.IS_CLIPPING=true;
044: //Frame.getInstance().getPanel().paint(g2d);
045: }
046:
047: public static void genSvg() {
048: try {
049: // Get a DOMImplementation
050: DOMImplementation domImpl = GenericDOMImplementation
051: .getDOMImplementation();
052:
053: // Create an instance of org.w3c.dom.Document
054: Document document = domImpl.createDocument(null, "svg",
055: null);
056:
057: // Create an instance of the SVG Generator
058: SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
059:
060: // Ask the test to render into the SVG Graphics2D implementation
061: GenSvg test = new GenSvg();
062: test.paint(svgGenerator);
063:
064: // Finally, stream out SVG to the standard output using UTF-8
065: // character to byte encoding
066: boolean useCSS = true; // we want to use CSS style attribute
067: Writer out = new OutputStreamWriter(System.out, "UTF-8");
068: svgGenerator.stream(out, useCSS);
069: } catch (IOException e) {
070: System.out.println("IO Exception.");
071: }
072: }
073:
074: public static void createAndOutputSvgToFile(String filename) {
075: try {
076: OutputStream ostream = new FileOutputStream(filename);
077: createSvgToStream(ostream);
078: } catch (IOException e) {
079: System.out.println("IO Exception.");
080: }
081: }
082:
083: public static void createSvgToStream(OutputStream ostream) {
084: try {
085: // Get a DOMImplementation
086: DOMImplementation domImpl = GenericDOMImplementation
087: .getDOMImplementation();
088:
089: // Create an instance of org.w3c.dom.Document
090: Document document = domImpl.createDocument(null, "svg",
091: null);
092:
093: // Create an instance of the SVG Generator
094: SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
095:
096: // Ask the test to render into the SVG Graphics2D implementation
097: GenSvg test = new GenSvg();
098: test.paint(svgGenerator);
099:
100: // Finally, stream out SVG to the standard output using UTF-8
101: // character to byte encoding
102: boolean useCSS = true; // we want to use CSS style attribute
103: //
104: Writer out = new OutputStreamWriter(ostream, "UTF-8");
105: //Writer out = new OutputStreamWriter( new FileOutputStream(filename), "UTF-8");
106: //BufferedWriter bw = new BufferedWriter(
107:
108: svgGenerator.stream(out, useCSS);
109: //OutputStream ostream = new FileOutputStream("c:\\t\\out.pdf");
110: /*TranscoderOutput output = new TranscoderOutput(ostream);
111: // save the image
112: t.transcode(input, output);
113: // flush and close the stream then exit
114: ostream.flush();
115: ostream.close();*/
116: } catch (Exception e) {
117: System.out.println("UMLet: Error: Exception in outputPdf: "
118: + e);
119: }
120: }
121: }
|