org.jhotdraw.samples.svg.io
Class SVGOutputFormat

java.lang.Object
  extended by org.jhotdraw.samples.svg.io.SVGOutputFormat
All Implemented Interfaces:
OutputFormat
Direct Known Subclasses:
SVGZOutputFormat

public class SVGOutputFormat
extends java.lang.Object
implements OutputFormat

An output format for storing drawings as Scalable Vector Graphics SVG Tiny 1.2.

Version:
$Id: SVGOutputFormat.java 647 2010-01-24 22:52:59Z rawcoder $
Author:
Werner Randelshofer

Field Summary
private  IXMLElement defs
          This element holds all definitions of the SVG file.
private  IXMLElement document
          Holds the document that is currently being written.
private  java.util.HashMap<Gradient,java.lang.String> gradientToIDMap
          Maps gradients to ID's.
private  java.util.HashMap<IXMLElement,java.lang.String> identifiedElements
          In this hash map we store all elements to which we have assigned an id.
private static boolean isFloatPrecision
          Set this variable to true if values should be written with float precision instead with double precision.
private  boolean isPrettyPrint
          Set this to true for pretty printing.
private  int nextId
          This is a counter used to create the next unique identification.
private static java.util.HashMap<java.lang.Integer,java.lang.String> strokeLinecapMap
           
private static java.util.HashMap<java.lang.Integer,java.lang.String> strokeLinejoinMap
           
private  java.net.URL url
           
 
Constructor Summary
SVGOutputFormat()
          Creates a new instance.
 
Method Summary
protected  IXMLElement createCircle(IXMLElement doc, double cx, double cy, double r, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createEllipse(IXMLElement doc, double cx, double cy, double rx, double ry, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createG(IXMLElement doc, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createImage(IXMLElement doc, double x, double y, double w, double h, byte[] imageData, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createLine(IXMLElement doc, double x1, double y1, double x2, double y2, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createLinearGradient(IXMLElement doc, double x1, double y1, double x2, double y2, double[] stopOffsets, java.awt.Color[] stopColors, double[] stopOpacities, boolean isRelativeToFigureBounds, java.awt.geom.AffineTransform transform)
           
protected  IXMLElement createPath(IXMLElement doc, BezierPath[] beziers, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createPolygon(IXMLElement doc, java.awt.geom.Point2D.Double[] points, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createPolyline(IXMLElement doc, java.awt.geom.Point2D.Double[] points, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createRadialGradient(IXMLElement doc, double cx, double cy, double fx, double fy, double r, double[] stopOffsets, java.awt.Color[] stopColors, double[] stopOpacities, boolean isRelativeToFigureBounds, java.awt.geom.AffineTransform transform)
           
protected  IXMLElement createRect(IXMLElement doc, double x, double y, double width, double height, double rx, double ry, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createText(IXMLElement doc, java.awt.geom.Point2D.Double[] coordinates, double[] rotate, javax.swing.text.StyledDocument text, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
protected  IXMLElement createTextArea(IXMLElement doc, double x, double y, double w, double h, javax.swing.text.StyledDocument text, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
 java.awt.datatransfer.Transferable createTransferable(Drawing drawing, java.util.List<Figure> figures, double scaleFactor)
          Creates a Transferable for the specified list of Figures.
 java.lang.String getFileExtension()
          Returns the file extension for the output format.
 javax.swing.filechooser.FileFilter getFileFilter()
          Return a FileFilter that can be used to identify files which can be stored with this output format.
 java.lang.String getId(IXMLElement element)
          Gets a unique ID for the specified element.
 javax.swing.JComponent getOutputFormatAccessory()
          Return a JFileChooser accessory that can be used to customize the output format.
private  void initStorageContext(IXMLElement root)
           
 boolean isPrettyPrint()
           
 void setPrettyPrint(boolean newValue)
           
static java.lang.String toColor(java.awt.Color color)
           
static java.lang.String toNumber(double number)
          Returns a double array as a number attribute value.
static java.lang.String toPath(BezierPath[] paths)
          Returns a value as a SVG Path attribute.
static java.lang.String toPoints(java.awt.geom.Point2D.Double[] points)
          Returns a Point2D.Double array as a Points attribute value.
static java.lang.String toTransform(java.awt.geom.AffineTransform t)
           
 void write(java.io.File file, Drawing drawing)
           
 void write(java.io.OutputStream out, Drawing drawing)
          Writes a Drawing into an output stream.
 void write(java.io.OutputStream out, Drawing drawing, java.util.List<Figure> figures)
          All other write methods delegate their work to here.
 void write(java.net.URI uri, Drawing drawing)
          Writes a Drawing into an URI.
protected  void writeAttribute(IXMLElement elem, java.lang.String name, double value, double defaultValue)
           
protected  void writeAttribute(IXMLElement elem, java.lang.String name, java.lang.String namespace, double value, double defaultValue)
           
protected  void writeAttribute(IXMLElement elem, java.lang.String name, java.lang.String value, java.lang.String defaultValue)
           
protected  void writeAttribute(IXMLElement elem, java.lang.String name, java.lang.String namespace, java.lang.String value, java.lang.String defaultValue)
           
protected  void writeCircleElement(IXMLElement parent, SVGEllipseFigure f)
           
protected  void writeElement(IXMLElement parent, Figure f)
           
protected  void writeEllipseElement(IXMLElement parent, SVGEllipseFigure f)
           
private  void writeFontAttributes(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> a)
           
protected  void writeGElement(IXMLElement parent, SVGGroupFigure f)
           
protected  void writeImageElement(IXMLElement parent, SVGImageFigure f)
           
protected  void writeLineElement(IXMLElement parent, SVGPathFigure f)
           
protected  void writeOpacityAttribute(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> m)
           
protected  void writePathElement(IXMLElement parent, SVGPathFigure f)
           
protected  void writePolygonElement(IXMLElement parent, SVGPathFigure f)
           
protected  void writePolylineElement(IXMLElement parent, SVGPathFigure f)
           
protected  void writeRectElement(IXMLElement parent, SVGRectFigure f)
           
protected  void writeShapeAttributes(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> m)
           
protected  void writeTextAreaElement(IXMLElement parent, SVGTextAreaFigure f)
           
protected  void writeTextElement(IXMLElement parent, SVGTextFigure f)
           
protected  void writeTransformAttribute(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> a)
           
private  void writeViewportAttributes(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

url

private java.net.URL url

nextId

private int nextId
This is a counter used to create the next unique identification.


identifiedElements

private java.util.HashMap<IXMLElement,java.lang.String> identifiedElements
In this hash map we store all elements to which we have assigned an id.


defs

private IXMLElement defs
This element holds all definitions of the SVG file.


document

private IXMLElement document
Holds the document that is currently being written.


gradientToIDMap

private java.util.HashMap<Gradient,java.lang.String> gradientToIDMap
Maps gradients to ID's. We use this, so that we need to store the same gradient only once.


isPrettyPrint

private boolean isPrettyPrint
Set this to true for pretty printing.


strokeLinejoinMap

private static final java.util.HashMap<java.lang.Integer,java.lang.String> strokeLinejoinMap

strokeLinecapMap

private static final java.util.HashMap<java.lang.Integer,java.lang.String> strokeLinecapMap

isFloatPrecision

private static final boolean isFloatPrecision
Set this variable to true if values should be written with float precision instead with double precision. Float precision is less accurate then double precision, but it uses less storage space.

See Also:
Constant Field Values
Constructor Detail

SVGOutputFormat

public SVGOutputFormat()
Creates a new instance.

Method Detail

getFileFilter

public javax.swing.filechooser.FileFilter getFileFilter()
Description copied from interface: OutputFormat
Return a FileFilter that can be used to identify files which can be stored with this output format. Typically, each output format has its own recognizable file extension.

Specified by:
getFileFilter in interface OutputFormat
Returns:
FileFilter to be used with a javax.swing.JFileChooser

getOutputFormatAccessory

public javax.swing.JComponent getOutputFormatAccessory()
Description copied from interface: OutputFormat
Return a JFileChooser accessory that can be used to customize the output format.

Specified by:
getOutputFormatAccessory in interface OutputFormat
Returns:
A JFileChooser accessory to be used with a javax.swing.JFileChooser Returns null, if no accessory is provided for this format.

setPrettyPrint

public void setPrettyPrint(boolean newValue)

isPrettyPrint

public boolean isPrettyPrint()

writeElement

protected void writeElement(IXMLElement parent,
                            Figure f)
                     throws java.io.IOException
Throws:
java.io.IOException

writeCircleElement

protected void writeCircleElement(IXMLElement parent,
                                  SVGEllipseFigure f)
                           throws java.io.IOException
Throws:
java.io.IOException

createCircle

protected IXMLElement createCircle(IXMLElement doc,
                                   double cx,
                                   double cy,
                                   double r,
                                   java.util.Map<AttributeKey,java.lang.Object> attributes)
                            throws java.io.IOException
Throws:
java.io.IOException

createG

protected IXMLElement createG(IXMLElement doc,
                              java.util.Map<AttributeKey,java.lang.Object> attributes)
                       throws java.io.IOException
Throws:
java.io.IOException

createLinearGradient

protected IXMLElement createLinearGradient(IXMLElement doc,
                                           double x1,
                                           double y1,
                                           double x2,
                                           double y2,
                                           double[] stopOffsets,
                                           java.awt.Color[] stopColors,
                                           double[] stopOpacities,
                                           boolean isRelativeToFigureBounds,
                                           java.awt.geom.AffineTransform transform)
                                    throws java.io.IOException
Throws:
java.io.IOException

createRadialGradient

protected IXMLElement createRadialGradient(IXMLElement doc,
                                           double cx,
                                           double cy,
                                           double fx,
                                           double fy,
                                           double r,
                                           double[] stopOffsets,
                                           java.awt.Color[] stopColors,
                                           double[] stopOpacities,
                                           boolean isRelativeToFigureBounds,
                                           java.awt.geom.AffineTransform transform)
                                    throws java.io.IOException
Throws:
java.io.IOException

writeEllipseElement

protected void writeEllipseElement(IXMLElement parent,
                                   SVGEllipseFigure f)
                            throws java.io.IOException
Throws:
java.io.IOException

createEllipse

protected IXMLElement createEllipse(IXMLElement doc,
                                    double cx,
                                    double cy,
                                    double rx,
                                    double ry,
                                    java.util.Map<AttributeKey,java.lang.Object> attributes)
                             throws java.io.IOException
Throws:
java.io.IOException

writeGElement

protected void writeGElement(IXMLElement parent,
                             SVGGroupFigure f)
                      throws java.io.IOException
Throws:
java.io.IOException

writeImageElement

protected void writeImageElement(IXMLElement parent,
                                 SVGImageFigure f)
                          throws java.io.IOException
Throws:
java.io.IOException

createImage

protected IXMLElement createImage(IXMLElement doc,
                                  double x,
                                  double y,
                                  double w,
                                  double h,
                                  byte[] imageData,
                                  java.util.Map<AttributeKey,java.lang.Object> attributes)
                           throws java.io.IOException
Throws:
java.io.IOException

writePathElement

protected void writePathElement(IXMLElement parent,
                                SVGPathFigure f)
                         throws java.io.IOException
Throws:
java.io.IOException

createPath

protected IXMLElement createPath(IXMLElement doc,
                                 BezierPath[] beziers,
                                 java.util.Map<AttributeKey,java.lang.Object> attributes)
                          throws java.io.IOException
Throws:
java.io.IOException

writePolygonElement

protected void writePolygonElement(IXMLElement parent,
                                   SVGPathFigure f)
                            throws java.io.IOException
Throws:
java.io.IOException

createPolygon

protected IXMLElement createPolygon(IXMLElement doc,
                                    java.awt.geom.Point2D.Double[] points,
                                    java.util.Map<AttributeKey,java.lang.Object> attributes)
                             throws java.io.IOException
Throws:
java.io.IOException

writePolylineElement

protected void writePolylineElement(IXMLElement parent,
                                    SVGPathFigure f)
                             throws java.io.IOException
Throws:
java.io.IOException

createPolyline

protected IXMLElement createPolyline(IXMLElement doc,
                                     java.awt.geom.Point2D.Double[] points,
                                     java.util.Map<AttributeKey,java.lang.Object> attributes)
                              throws java.io.IOException
Throws:
java.io.IOException

writeLineElement

protected void writeLineElement(IXMLElement parent,
                                SVGPathFigure f)
                         throws java.io.IOException
Throws:
java.io.IOException

createLine

protected IXMLElement createLine(IXMLElement doc,
                                 double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 java.util.Map<AttributeKey,java.lang.Object> attributes)
                          throws java.io.IOException
Throws:
java.io.IOException

writeRectElement

protected void writeRectElement(IXMLElement parent,
                                SVGRectFigure f)
                         throws java.io.IOException
Throws:
java.io.IOException

createRect

protected IXMLElement createRect(IXMLElement doc,
                                 double x,
                                 double y,
                                 double width,
                                 double height,
                                 double rx,
                                 double ry,
                                 java.util.Map<AttributeKey,java.lang.Object> attributes)
                          throws java.io.IOException
Throws:
java.io.IOException

writeTextElement

protected void writeTextElement(IXMLElement parent,
                                SVGTextFigure f)
                         throws java.io.IOException
Throws:
java.io.IOException

createText

protected IXMLElement createText(IXMLElement doc,
                                 java.awt.geom.Point2D.Double[] coordinates,
                                 double[] rotate,
                                 javax.swing.text.StyledDocument text,
                                 java.util.Map<AttributeKey,java.lang.Object> attributes)
                          throws java.io.IOException
Throws:
java.io.IOException

writeTextAreaElement

protected void writeTextAreaElement(IXMLElement parent,
                                    SVGTextAreaFigure f)
                             throws java.io.IOException
Throws:
java.io.IOException

createTextArea

protected IXMLElement createTextArea(IXMLElement doc,
                                     double x,
                                     double y,
                                     double w,
                                     double h,
                                     javax.swing.text.StyledDocument text,
                                     java.util.Map<AttributeKey,java.lang.Object> attributes)
                              throws java.io.IOException
Throws:
java.io.IOException

writeShapeAttributes

protected void writeShapeAttributes(IXMLElement elem,
                                    java.util.Map<AttributeKey,java.lang.Object> m)
                             throws java.io.IOException
Throws:
java.io.IOException

writeOpacityAttribute

protected void writeOpacityAttribute(IXMLElement elem,
                                     java.util.Map<AttributeKey,java.lang.Object> m)
                              throws java.io.IOException
Throws:
java.io.IOException

writeTransformAttribute

protected void writeTransformAttribute(IXMLElement elem,
                                       java.util.Map<AttributeKey,java.lang.Object> a)
                                throws java.io.IOException
Throws:
java.io.IOException

writeFontAttributes

private void writeFontAttributes(IXMLElement elem,
                                 java.util.Map<AttributeKey,java.lang.Object> a)
                          throws java.io.IOException
Throws:
java.io.IOException

writeViewportAttributes

private void writeViewportAttributes(IXMLElement elem,
                                     java.util.Map<AttributeKey,java.lang.Object> a)
                              throws java.io.IOException
Throws:
java.io.IOException

writeAttribute

protected void writeAttribute(IXMLElement elem,
                              java.lang.String name,
                              java.lang.String value,
                              java.lang.String defaultValue)

writeAttribute

protected void writeAttribute(IXMLElement elem,
                              java.lang.String name,
                              java.lang.String namespace,
                              java.lang.String value,
                              java.lang.String defaultValue)

writeAttribute

protected void writeAttribute(IXMLElement elem,
                              java.lang.String name,
                              double value,
                              double defaultValue)

writeAttribute

protected void writeAttribute(IXMLElement elem,
                              java.lang.String name,
                              java.lang.String namespace,
                              double value,
                              double defaultValue)

toPath

public static java.lang.String toPath(BezierPath[] paths)
Returns a value as a SVG Path attribute. as specified in http://www.w3.org/TR/SVGMobile12/paths.html#PathDataBNF


toNumber

public static java.lang.String toNumber(double number)
Returns a double array as a number attribute value.


toPoints

public static java.lang.String toPoints(java.awt.geom.Point2D.Double[] points)
                                 throws java.io.IOException
Returns a Point2D.Double array as a Points attribute value. as specified in http://www.w3.org/TR/SVGMobile12/shapes.html#PointsBNF

Throws:
java.io.IOException

toTransform

public static java.lang.String toTransform(java.awt.geom.AffineTransform t)
                                    throws java.io.IOException
Throws:
java.io.IOException

toColor

public static java.lang.String toColor(java.awt.Color color)

getFileExtension

public java.lang.String getFileExtension()
Description copied from interface: OutputFormat
Returns the file extension for the output format. The file extension should be appended to a file name when storing a Drawing with the specified file format.

Specified by:
getFileExtension in interface OutputFormat

write

public void write(java.net.URI uri,
                  Drawing drawing)
           throws java.io.IOException
Description copied from interface: OutputFormat
Writes a Drawing into an URI.

Specified by:
write in interface OutputFormat
Parameters:
uri - The uri.
drawing - The drawing.
Throws:
java.io.IOException

write

public void write(java.io.File file,
                  Drawing drawing)
           throws java.io.IOException
Throws:
java.io.IOException

write

public void write(java.io.OutputStream out,
                  Drawing drawing)
           throws java.io.IOException
Description copied from interface: OutputFormat
Writes a Drawing into an output stream.

Specified by:
write in interface OutputFormat
Parameters:
out - The output stream.
drawing - The drawing.
Throws:
java.io.IOException

write

public void write(java.io.OutputStream out,
                  Drawing drawing,
                  java.util.List<Figure> figures)
           throws java.io.IOException
All other write methods delegate their work to here.

Throws:
java.io.IOException

initStorageContext

private void initStorageContext(IXMLElement root)

getId

public java.lang.String getId(IXMLElement element)
Gets a unique ID for the specified element.


createTransferable

public java.awt.datatransfer.Transferable createTransferable(Drawing drawing,
                                                             java.util.List<Figure> figures,
                                                             double scaleFactor)
                                                      throws java.io.IOException
Description copied from interface: OutputFormat
Creates a Transferable for the specified list of Figures.

Specified by:
createTransferable in interface OutputFormat
Parameters:
drawing - The drawing.
figures - A list of figures of the drawing.
scaleFactor - The factor to be used, when the Transferable creates an image with a fixed size from the figures.
Returns:
The Transferable.
Throws:
java.io.IOException