Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > svg to svg saving problem

Reply
Thread Tools

svg to svg saving problem

 
 
milof83
Guest
Posts: n/a
 
      08-18-2006
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)

 
Reply With Quote
 
 
 
 
Roland de Ruiter
Guest
Posts: n/a
 
      08-18-2006
On 18-8-2006 18:01, milof83 wrote:
> 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 {
>

[...]
>
> 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)
>

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
....
--
Regards,

Roland
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: svg-chart 1.1 SVG Charting Library Helmut Jarausch Python 0 05-20-2008 11:05 AM
[SVG] probleme dessin d'images SVG avec Batik Mardagg Java 0 05-12-2006 02:13 PM
[SVG] problem with SVG image drawing with Batik Library Mardagg Java 0 05-12-2006 02:12 PM
Saving DataTable to session vs saving a Custom object. John Kandell ASP .Net 4 12-10-2004 05:08 AM
[SVG] using image-tag to integrate SVG in SVG Arne Nordmann XML 0 06-28-2004 01:16 PM



Advertisments