Skip to content

Instantly share code, notes, and snippets.

@hageldave
Created June 20, 2019 22:50
Show Gist options
  • Save hageldave/d8d23aa81a855239b4e257c5ecc11206 to your computer and use it in GitHub Desktop.
Save hageldave/d8d23aa81a855239b4e257c5ecc11206 to your computer and use it in GitHub Desktop.
import static org.apache.batik.anim.dom.SVGDOMImplementation.SVG_NAMESPACE_URI;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class testmain {
public static void main(String[] args) {
DOMImplementation domImplementation = SVGDOMImplementation.getDOMImplementation();
Document document = domImplementation.createDocument(SVG_NAMESPACE_URI, "svg", null);
Element root = document.getDocumentElement();
root.setAttributeNS(null,"width",""+400);
root.setAttributeNS(null, "height", ""+300);
Element rect = document.createElementNS(SVG_NAMESPACE_URI, "rect");
rect.setAttributeNS(null, "x", "10");
rect.setAttributeNS(null, "y", "20");
rect.setAttributeNS(null, "width", "100");
rect.setAttributeNS(null, "height", "50");
rect.setAttributeNS(null, "fill", "red");
root.appendChild(rect);
TranscoderInput input = new TranscoderInput(document);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8")){
TranscoderOutput output = new TranscoderOutput(osw);
new SVGTranscoder().transcode(input, output);
} catch (IOException e) {
e.printStackTrace();
} catch (TranscoderException e) {
e.printStackTrace();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try(Scanner sc = new Scanner(bais)){
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment