Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

svg to svg saving problem

Thread view: 
milof83 - 18 Aug 2006 17:01 GMT
hi,
I am a beginner batik user and at the moment I am working on apiece of
code which would allow me to open svg file edit it and save it as an
svg file.

I have sucessfully loaded svg file (thanks to thomas.deweese) and
currently I'm facing a problem with saving it to an svg file.

Below is the sourcecode for the function which is supposed to do the
task.

public void editSVG (String inputFilename, String outputFilename)
throws Exception {

               String svgURI = new
File(inputFilename).toURL().toString();

               try{
               UserAgentAdapter ua = new UserAgentAdapter();
              DocumentLoader loader = new DocumentLoader(ua);
              Document doc = loader.loadDocument(svgURI);

               // get the root element (the svg element)
               Element svgRoot = doc.getDocumentElement();

               // set the width and height attribute on the root svg
element
               svgRoot.setAttributeNS(svgURI, "width", "400");
               svgRoot.setAttributeNS(svgURI, "height", "330");

               // create the rectangle
               Element rectangle = doc.createElementNS(svgNS, "rect");

               rectangle.setAttributeNS(null, "x", "200");
               rectangle.setAttributeNS(null, "y", "200");
               rectangle.setAttributeNS(null, "width", "100");
               rectangle.setAttributeNS(null, "height", "100");
               rectangle.setAttributeNS(null, "fill", "red");

               // attach the rectangle to the svg root element
               svgRoot.appendChild(rectangle);

               TranscoderInput input = new TranscoderInput(doc);
              OutputStream ostream = new
FileOutputStream(outputFilename);
              TranscoderOutput output = new TranscoderOutput(ostream);

              svgTrans.transcode(input, output);
              ostream.flush();
              ostream.close();

               System.out.println("SVG edited");
               } catch (Exception e){
                       System.out.println("Error: " + e);
               }
   }

The error message i'm recieving during the program runtime:

Exception in thread "main" java.lang.Error: Writer expected
       at
org.apache.batik.transcoder.svg2svg.SVGTranscoder.transcode(Unknown
Source)
       at SVGtoJPEG.editSVG(SVGtoJPEG.java:96)
       at SVGtoJPEG.main(SVGtoJPEG.java:112)
Roland de Ruiter - 18 Aug 2006 20:36 GMT
> hi,
> I am a beginner batik user and at the moment I am working on apiece of
[quoted text clipped - 9 lines]
> public void editSVG (String inputFilename, String outputFilename)
> throws Exception {

[...]

>                 TranscoderInput input = new TranscoderInput(doc);
>                OutputStream ostream = new
[quoted text clipped - 19 lines]
>         at SVGtoJPEG.editSVG(SVGtoJPEG.java:96)
>         at SVGtoJPEG.main(SVGtoJPEG.java:112)

Apparently SVGTranscoder wants a Writer for output instead of an
OutputStream. Try the following:

import java.io.Writer;
import java.io.FilterWriter;
...
TranscoderInput input = new TranscoderInput(doc); // still the same
Writer owriter = new FileWriter(outputFilename); // this is different
TranscoderOutput output = new TranscoderOutput(owriter); // almost same
svgTrans.transcode(input, output);                      // same
owriter.flush();                                       // almost same
owriter.close();                                      // almost same
...
Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.