org.jhotdraw.samples.odg.io
Class ODGInputFormat

java.lang.Object
  extended by org.jhotdraw.samples.odg.io.ODGInputFormat
All Implemented Interfaces:
InputFormat

public class ODGInputFormat
extends java.lang.Object
implements InputFormat

ODGInputFormat. This format is aimed to comply to the Open Document Version 1.1 Drawing format. http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1.pdf

Version:
$Id: ODGInputFormat.java 585 2009-11-24 21:53:05Z rawcoder $
Author:
Werner Randelshofer

Field Summary
private static boolean DEBUG
          Set this to true, to get debug output on System.out.
private  IXMLElement document
          Holds the document that is currently being read.
private  java.util.LinkedList<Figure> figures
          Holds the figures that are currently being read.
private  ODGStylesReader styles
           
 
Constructor Summary
ODGInputFormat()
          Creates a new instance.
 
Method Summary
private  ODGFigure createEnhancedGeometryEllipseFigure(java.awt.geom.Rectangle2D.Double bounds, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Ellipse figure.
private  ODGFigure createEnhancedGeometryRectangleFigure(java.awt.geom.Rectangle2D.Double bounds, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Rect figure.
private  CompositeFigure createGroupFigure()
          Creates a ODGGroupFigure.
private  ODGFigure createLineFigure(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Line figure.
private  ODGFigure createPathFigure(BezierPath[] paths, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Path figure.
private  ODGFigure createPolygonFigure(java.awt.geom.Point2D.Double[] points, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Polygon figure.
private  ODGFigure createPolylineFigure(java.awt.geom.Point2D.Double[] points, java.util.Map<AttributeKey,java.lang.Object> a)
          Creates a Polyline figure.
 javax.swing.filechooser.FileFilter getFileFilter()
          Return a FileFilter that can be used to identify files which can be read with this input format.
 javax.swing.JComponent getInputFormatAccessory()
          Return a JFileChooser accessory that can be used to customize the input format.
 boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
          Returns true, if this InputFormat can readFigures TransferData using the specified DataFlavor.
private  java.lang.Object nextEnhancedCoordinate(StreamPosTokenizer tt, java.lang.String str)
          Retrieves an enhanced coordinate from the specified tokenizer.
 void read(java.io.File file, Drawing drawing)
          Reads figures from a file and replaces the children of the drawing with them.
 void read(java.io.File file, Drawing drawing, boolean replace)
          Reads figures from a file and adds them to the specified drawing.
 void read(java.io.InputStream in, Drawing drawing, boolean replace)
          Reads figures from a file and adds them to the specified drawing.
 void read(java.awt.datatransfer.Transferable t, Drawing drawing, boolean replace)
          Reads figures from the specified Transferable and adds them to the specified drawing.
private  byte[] readAllBytes(java.io.InputStream in)
          Reads all bytes from the InputStreams until EOF is reached.
private  ODGFigure readCaptionElement(IXMLElement elem)
           
private  ODGFigure readCircleElement(IXMLElement elem)
           
private  void readCommonDrawingShapeAttributes(IXMLElement elem, java.util.HashMap<AttributeKey,java.lang.Object> a)
           
private  ODGFigure readCustomShapeElement(IXMLElement elem)
          A represents a shape that is capable of rendering complex figures.
private  void readDrawingElement(IXMLElement elem)
          Reads an ODG "office:drawing" element.
private  ODGFigure readElement(IXMLElement elem)
          Reads an ODG element.
private  ODGFigure readEllipseElement(IXMLElement elem)
           
private  ODGFigure readEnhancedGeometryElement(IXMLElement elem, java.util.Map<AttributeKey,java.lang.Object> a, java.awt.geom.Rectangle2D.Double figureBounds)
           
 void readFiguresFromDocumentContent(java.io.InputStream in, Drawing drawing, boolean replace)
          Reads figures from the content.xml file of an ODG open document drawing document.
private  ODGFigure readFrameElement(IXMLElement elem)
          Reads a <draw:frame> element from the specified XML element.
private  ODGFigure readGElement(IXMLElement elem)
           
private  ODGFigure readLineElement(IXMLElement elem)
          The <draw:line> element represents a line.
private  ODGFigure readMeasureElement(IXMLElement elem)
           
private  void readPageElement(IXMLElement elem)
          Reads an ODG "draw:page" element.
private  ODGFigure readPathElement(IXMLElement elem)
           
private  ODGFigure readPolygonElement(IXMLElement elem)
          The <draw:polygon> element represents a polygon.
private  ODGFigure readPolylineElement(IXMLElement elem)
          The <draw:polyline> element represents a polyline drawing shape.
private  ODGFigure readRectElement(IXMLElement elem)
           
private  ODGFigure readRegularPolygonElement(IXMLElement elem)
           
private  java.awt.geom.AffineTransform readViewBoxTransform(IXMLElement elem)
           
private  EnhancedPath toEnhancedPath(java.lang.String str)
          Returns a value as a EnhancedPath array.
private  double toLength(java.lang.String str, double percentFactor)
          Returns a value as a length.
private  double toNumber(java.lang.String str)
          Returns a value as a number.
private  BezierPath[] toPath(java.lang.String str)
          Returns a value as a BezierPath array.
static java.awt.geom.AffineTransform toTransform(java.lang.String str)
          Converts an ODG draw:transform attribute value into an AffineTransform.
private static double toUnitFactor(java.lang.String str)
           
static java.lang.String[] toWSOrCommaSeparatedArray(java.lang.String str)
          Returns a value as a String array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
Set this to true, to get debug output on System.out.

See Also:
Constant Field Values

figures

private java.util.LinkedList<Figure> figures
Holds the figures that are currently being read.


document

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


styles

private ODGStylesReader styles
Constructor Detail

ODGInputFormat

public ODGInputFormat()
Creates a new instance.

Method Detail

getFileFilter

public javax.swing.filechooser.FileFilter getFileFilter()
Description copied from interface: InputFormat
Return a FileFilter that can be used to identify files which can be read with this input format. Typically, each input format has its own recognizable file naming convention.

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

getInputFormatAccessory

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

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

read

public void read(java.io.File file,
                 Drawing drawing)
          throws java.io.IOException
Description copied from interface: InputFormat
Reads figures from a file and replaces the children of the drawing with them.

This is a convenience method for calling read(File,Drawing,true).

Specified by:
read in interface InputFormat
Parameters:
file - The file.
drawing - The drawing.
Throws:
java.io.IOException

read

public void read(java.io.File file,
                 Drawing drawing,
                 boolean replace)
          throws java.io.IOException
Description copied from interface: InputFormat
Reads figures from a file and adds them to the specified drawing.

Specified by:
read in interface InputFormat
Parameters:
file - The file.
drawing - The drawing.
replace - Set this to true, if the contents of the file replaces the contents of the drawing (for example, when loading a drawing from a file). Set this to false, to add the contents of the file to the drawing (for example, when the file has been dropped into the drawing view).
Throws:
java.io.IOException

isDataFlavorSupported

public boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
Description copied from interface: InputFormat
Returns true, if this InputFormat can readFigures TransferData using the specified DataFlavor.

Specified by:
isDataFlavorSupported in interface InputFormat
Parameters:
flavor - A DataFlavor.

read

public void read(java.awt.datatransfer.Transferable t,
                 Drawing drawing,
                 boolean replace)
          throws java.awt.datatransfer.UnsupportedFlavorException,
                 java.io.IOException
Description copied from interface: InputFormat
Reads figures from the specified Transferable and adds them to the specified drawing.

Specified by:
read in interface InputFormat
Parameters:
t - The Transferable.
drawing - The drawing.
replace - Set this to true, if the contents of the transferable replaces the contents of the drawing (for example, when loading a drawing from a transferable). Set this to false, to add the contents of the transferable to the drawing (for example, when the transferable has been dropped or pasted into the drawing view).
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

readAllBytes

private byte[] readAllBytes(java.io.InputStream in)
                     throws java.io.IOException
Reads all bytes from the InputStreams until EOF is reached.

Throws:
java.io.IOException

read

public void read(java.io.InputStream in,
                 Drawing drawing,
                 boolean replace)
          throws java.io.IOException
Description copied from interface: InputFormat
Reads figures from a file and adds them to the specified drawing.

Specified by:
read in interface InputFormat
Parameters:
in - The input stream.
drawing - The drawing.
replace - Set this to true, if the contents of the stream replaces the contents of the drawing (for example, when loading a drawing from a stream). Set this to false, to add the contents of the file to the drawing (for example, when the stream has been dropped into the drawing view).
Throws:
java.io.IOException

readFiguresFromDocumentContent

public void readFiguresFromDocumentContent(java.io.InputStream in,
                                           Drawing drawing,
                                           boolean replace)
                                    throws java.io.IOException
Reads figures from the content.xml file of an ODG open document drawing document.

Throws:
java.io.IOException

readDrawingElement

private void readDrawingElement(IXMLElement elem)
                         throws java.io.IOException
Reads an ODG "office:drawing" element.

Throws:
java.io.IOException

readPageElement

private void readPageElement(IXMLElement elem)
                      throws java.io.IOException
Reads an ODG "draw:page" element.

Throws:
java.io.IOException

readElement

private ODGFigure readElement(IXMLElement elem)
                       throws java.io.IOException
Reads an ODG element.

Throws:
java.io.IOException

readEllipseElement

private ODGFigure readEllipseElement(IXMLElement elem)
                              throws java.io.IOException
Throws:
java.io.IOException

readCircleElement

private ODGFigure readCircleElement(IXMLElement elem)
                             throws java.io.IOException
Throws:
java.io.IOException

readCustomShapeElement

private ODGFigure readCustomShapeElement(IXMLElement elem)
                                  throws java.io.IOException
A represents a shape that is capable of rendering complex figures. It is offering font work and extrusion functiona- lity. A custom shape may have a geometry that influences its shape. This geometry may be visualized in office application user interfaces, for instance by displaying interaction handles, that provide a simple way to modify the geometry.

Throws:
java.io.IOException

readEnhancedGeometryElement

private ODGFigure readEnhancedGeometryElement(IXMLElement elem,
                                              java.util.Map<AttributeKey,java.lang.Object> a,
                                              java.awt.geom.Rectangle2D.Double figureBounds)
                                       throws java.io.IOException
Throws:
java.io.IOException

createEnhancedGeometryEllipseFigure

private ODGFigure createEnhancedGeometryEllipseFigure(java.awt.geom.Rectangle2D.Double bounds,
                                                      java.util.Map<AttributeKey,java.lang.Object> a)
                                               throws java.io.IOException
Creates a Ellipse figure.

Throws:
java.io.IOException

createEnhancedGeometryRectangleFigure

private ODGFigure createEnhancedGeometryRectangleFigure(java.awt.geom.Rectangle2D.Double bounds,
                                                        java.util.Map<AttributeKey,java.lang.Object> a)
                                                 throws java.io.IOException
Creates a Rect figure.

Throws:
java.io.IOException

createLineFigure

private ODGFigure createLineFigure(java.awt.geom.Point2D.Double p1,
                                   java.awt.geom.Point2D.Double p2,
                                   java.util.Map<AttributeKey,java.lang.Object> a)
                            throws java.io.IOException
Creates a Line figure.

Throws:
java.io.IOException

createPolylineFigure

private ODGFigure createPolylineFigure(java.awt.geom.Point2D.Double[] points,
                                       java.util.Map<AttributeKey,java.lang.Object> a)
                                throws java.io.IOException
Creates a Polyline figure.

Throws:
java.io.IOException

createPolygonFigure

private ODGFigure createPolygonFigure(java.awt.geom.Point2D.Double[] points,
                                      java.util.Map<AttributeKey,java.lang.Object> a)
                               throws java.io.IOException
Creates a Polygon figure.

Throws:
java.io.IOException

createPathFigure

private ODGFigure createPathFigure(BezierPath[] paths,
                                   java.util.Map<AttributeKey,java.lang.Object> a)
                            throws java.io.IOException
Creates a Path figure.

Throws:
java.io.IOException

readFrameElement

private ODGFigure readFrameElement(IXMLElement elem)
                            throws java.io.IOException
Reads a <draw:frame> element from the specified XML element.

A frame is a rectangular container where that contains enhanced content like text boxes, images or objects. Frames are very similar to regular drawing shapes, but support some features that are not available for regular drawing shapes, like contours, image maps and hyperlinks. In particular, a frame allows to have multiple renditions of an object. That is, a frame may for instance contain an object as well as an image. In this case, the application may choose the content that it supports best. If the application supports the object type contained in the frame, it probably will render the object. If it does not support the object, it will render the image.

In general, an application must not render more than one of the content elements contained in a frame. The order of content elements dictates the document author's preference for rendering, with the first child being the most preferred. This means that applications should render the first child element that it supports. A frame must contain at least one content element. The inclusion of multiple content elements is optional. Application may preserve the content elements they don't render, but don't have to.

Within text documents, frames are also used to position content outside the default text flow of a document.

Frames can contain: • Text boxes • Objects represented either in the OpenDocument format or in a object specific binary format • Images • Applets • Plug-ins • Floating frames

Like the formatting properties of drawing shapes, frame formatting properties are stored in styles belonging to the graphic family. The way a frame is contained in a document also is the same as for drawing shapes.

Parameters:
elem - A <frame> element.
Throws:
java.io.IOException

createGroupFigure

private CompositeFigure createGroupFigure()
                                   throws java.io.IOException
Creates a ODGGroupFigure.

Throws:
java.io.IOException

readGElement

private ODGFigure readGElement(IXMLElement elem)
                        throws java.io.IOException
Throws:
java.io.IOException

readLineElement

private ODGFigure readLineElement(IXMLElement elem)
                           throws java.io.IOException
The <draw:line> element represents a line.

The attributes that may be associated with the <draw:line> element are: • Style, Layer, Z-Index, ID, Caption ID and Transformation – see section 9.2.15. • Text anchor, table background, draw end position– see section 9.2.16. • Start point • End point

The elements that may be contained in the <draw:line> element are: • Title (short accessible name) – see section 9.2.20. • Long description (in support of accessibility) – see section 9.2.20. • Event listeners – see section 9.2.21. • Glue points – see section 9.2.19. • Text – see section 9.2.17.

Throws:
java.io.IOException

readPathElement

private ODGFigure readPathElement(IXMLElement elem)
                           throws java.io.IOException
Throws:
java.io.IOException

readPolygonElement

private ODGFigure readPolygonElement(IXMLElement elem)
                              throws java.io.IOException
The <draw:polygon> element represents a polygon. A polygon is a closed put of straight lines. Some implementations may ignore the size attribute, and instead determine the size of a shape exclusively from the shape data (i.e., polygon vertices).

The attributes that may be associated with the <draw:polygon> element are: • Position, Size, View box, Style, Layer, Z-Index, ID, Caption ID and Transformation – see section 9.2.15 • Text anchor, table background, draw end position – see section 9.2.16 • Points – see section 9.2.3 The elements that may be contained in the <draw:polygon> element are: • Title (short accessible name) – see section 9.2.20. • Long description (in support of accessibility) – see section 9.2.20. • Event listeners – see section 9.2.21. • Glue points – see section 9.2.19. • Text – see section 9.2.17.

Throws:
java.io.IOException

readPolylineElement

private ODGFigure readPolylineElement(IXMLElement elem)
                               throws java.io.IOException
The <draw:polyline> element represents a polyline drawing shape. Some implementations may ignore the size attribute, and instead determine the size of a shape exclusively from the shape data (i.e., polygon vertices).

The attributes that may be associated with the <draw:polyline> element are: • Position, Size, View box, Style, Layer, Z-Index, ID, Caption ID and Transformation – see section 9.2.15 • Text anchor, table background, draw end position – see section 9.2.16 • Points The elements that may be contained in the <draw:polyline> element are: • Title (short accessible name) – see section 9.2.20. • Long description (in support of accessibility) – see section 9.2.20. • Event listeners – see section 9.2.21. • Glue points – see section 9.2.19. • Text – see section 9.2.17.

Throws:
java.io.IOException

readRectElement

private ODGFigure readRectElement(IXMLElement elem)
                           throws java.io.IOException
Throws:
java.io.IOException

readRegularPolygonElement

private ODGFigure readRegularPolygonElement(IXMLElement elem)
                                     throws java.io.IOException
Throws:
java.io.IOException

readMeasureElement

private ODGFigure readMeasureElement(IXMLElement elem)
                              throws java.io.IOException
Throws:
java.io.IOException

readCaptionElement

private ODGFigure readCaptionElement(IXMLElement elem)
                              throws java.io.IOException
Throws:
java.io.IOException

toWSOrCommaSeparatedArray

public static java.lang.String[] toWSOrCommaSeparatedArray(java.lang.String str)
                                                    throws java.io.IOException
Returns a value as a String array. The values are separated by whitespace or by commas with optional white space.

Throws:
java.io.IOException

toNumber

private double toNumber(java.lang.String str)
                 throws java.io.IOException
Returns a value as a number. http://www.w3.org/TR/SVGMobile12/types.html#DataTypeNumber

Throws:
java.io.IOException

toLength

private double toLength(java.lang.String str,
                        double percentFactor)
                 throws java.io.IOException
Returns a value as a length. http://www.w3.org/TR/SVGMobile12/types.html#DataTypeLength

Throws:
java.io.IOException

toUnitFactor

private static double toUnitFactor(java.lang.String str)
                            throws java.io.IOException
Throws:
java.io.IOException

toEnhancedPath

private EnhancedPath toEnhancedPath(java.lang.String str)
                             throws java.io.IOException
Returns a value as a EnhancedPath array. The draw:enhanced-path attribute specifies a path similar to the svg:d attribute of the element. Instructions such as moveto, lineto, arcto and other instructions together with its parameter are describing the geometry of a shape which can be filled and or stroked. Relative commands are not supported. The syntax of draw:enhanced-path attribute is as follows: • Instructions are expressed as one character (e.g., a moveto is expressed as an M). • A prefix notation is being used, that means that each command is followed by its parameter. • Superfluous white space and separators such as commas can be eliminated. (e.g., “M 10 10 L 20 20 L 30 20” can also be written: “M10 10L20 20L30 20” • If the command is repeated multiple times, only the first command is required. (e.g., “M 10 10 L 20 20 L 30 20” can also be expressed as followed “M 10 10 L 20 20 30 20” • Floats can be used, therefore the only allowable decimal point is a dot (“.”) The above mentioned rules are the same as specified for the element. A parameter can also have one of the following enhancements: • A “?” is used to mark the beginning of a formula name. The result of the element's draw:formula attribute is used as parameter value in this case. • If “$” is preceding a integer value, the value is indexing a draw:modifiers attribute. The corresponding modifier value is used as parameter value then.

Throws:
java.io.IOException

nextEnhancedCoordinate

private java.lang.Object nextEnhancedCoordinate(StreamPosTokenizer tt,
                                                java.lang.String str)
                                         throws java.io.IOException
Retrieves an enhanced coordinate from the specified tokenizer. An enhanced coordinate can be a double, or a '?' followed by a formula name, or a '$' followed by an index to a modifier.

Throws:
java.io.IOException

readCommonDrawingShapeAttributes

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

readViewBoxTransform

private java.awt.geom.AffineTransform readViewBoxTransform(IXMLElement elem)
                                                    throws java.io.IOException
Throws:
java.io.IOException

toTransform

public static java.awt.geom.AffineTransform toTransform(java.lang.String str)
                                                 throws java.io.IOException
Converts an ODG draw:transform attribute value into an AffineTransform.

The draw:transform attribute specifies a list of transformations that can be applied to a drawing shape. The value of this attribute is a list of transform definitions, which are applied to the drawing shape in the order in which they are listed. The transform definitions in the list must be separated by a white space and/or a comma. The types of transform definitions available include: • matrix( ), which specifies a transformation in the form of a transformation matrix of six values. matrix(a,b,c,d,e,f) is the equivalent of applying the transformation matrix [a b c d e f]. • translate( []), which specifies a translation by tx and ty. • scale( []), which specifies a scale operation by sx and sy. If is not provided, it is assumed to be equal to . • rotate(), which specifies a rotation by about the origin of the shapes coordinate system. • skewX(), which specifies a skew transformation along the X axis. • skewY(), which specifies a skew transformation along the Y axis.

Throws:
java.io.IOException

toPath

private BezierPath[] toPath(java.lang.String str)
                     throws java.io.IOException
Returns a value as a BezierPath array. as specified in http://www.w3.org/TR/SVGMobile12/shapes.html#PointsBNF Also supports elliptical arc commands 'a' and 'A' as specified in http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands

Throws:
java.io.IOException