Loading and saving SVG files with Rogue Wave JViews

In order to read and write SVG files, the JViews Framework library defines a new instance of IlvStreamFactory specialized for SVG.. You will see how to set the SVGStreamFactory on the IlvManager and how to use it.
To configure the SVG stream factory:
  1. Create the manager you will work on and the stream factory
    import ilog.views.*;
    import ilog.views.svg.*;
    
    IlvManager manager = new IlvManager();
    SVGStreamFactory factory = new SVGStreamFactory();
    manager.setStreamFactory(factory);
    
  2. Set the options on the stream factory appropriately to change the way Rogue Wave JViews loads and saves SVG files: how the file size should be reduced, whether the reader should ignore some data, and so on.
  3. After the stream factory is configured, it must be attached to the manager by using:
    manager.setStreamfactory(factory);
    
More options can be found in the Java API Reference Manual for the class, such as other compaction techniques (remove invisible graphic objects, and so forth) or the ability to choose between CSS or XML styling.
The following example shows a stream factory configuration:
// When reading SVG, the parsing of the CSS style will include
// the definitions contained in the user.css file.
factory.getReaderConfigurator().setUserStyleSheetURL(“user.css”);
// When writing SVG, the following compaction techniques will be used:
//   - style on graphic element will be factored
//   - an algorithm will be applied to polyline to remove some points
factory.getBuilderConfigurator().
    setCompactMode(SVGDocumentBuilderConfigurator.COMPACT_STYLE |
                   SVGDocumentBuilderConfigurator.COMPACT_POLY);
// With the option set to true on the reader configurator, when reading SVG, 
// elements that cannot be processed will be memorized
// and with the option set to true on the builder configurator, this will 
// allow regenerating them later.
factory.getReaderConfigurator().setFullDocumentOn(true);
factory.getBuilderConfigurator().setFullDocumentOn(true);
You can load an SVG file once a stream factory has been set up.
To load an SVG File:
  • Call the IlvManager method to load an SVG file instead of a regular Rogue Wave JViews file.
The following code is an example of loading an SVG file.
try {
  manager.read("mysvgfile.svg");
} catch (ilog.views.io.IlvReadFileException rfe) {
  System.err.println("The SVG file is badly formatted");
} catch (java.io.IOException ioe) {
  System.err.println("Cannot access the SVG file");
}
Once an IlvManager object has been filled dynamically or by reading an SVG or an IVL file, it is possible to save the objects to an SVG file.
To save to an SVG file:
  • Call the IlvManager method to save an SVG file instead of a regular Rogue Wave JViews file.
The following code is an example of writing to an SVG file:
try {
  manager.write("mysvgfilemodified.svg");
} catch (java.io.IOException ioe) {
  System.err.println("Cannot access the SVG file");
}