|
Size: 2875
Comment: converted to 1.6 markup
|
← Revision 3 as of 2012-06-22 14:09:13 ⇥
Size: 2881
Comment: display java code with syntax highlighting
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 12: | Line 12: |
| {{{ | {{{#!java |
Changing CSS Stylesheet for multiple transcodings
I wanted to recolor a map in several ways, and transcode each into a png. There is little on the web about how to deal with CSS style sheets in SVG. Finally I found a message with a suggestion:
as amended by its reply:
The folowing outputs two .pngs with different colors for the United States:
1 public void testTranscode() {
2 try {
3 // make a Document with the base map
4 String parser = XMLResourceDescriptor.getXMLParserClassName();
5 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
6 String filename = "C:/maps/BlankMap-World6.svg";
7 String uri = "file:///" + filename;
8 Document doc = f.createDocument(uri, new FileReader(filename));
9
10 // prepare to modify and transcode the document
11 SVGDocument sdoc = (SVGDocument)doc;
12 Element svgRoot = sdoc.getDocumentElement();
13 PNGTranscoder t = new PNGTranscoder();
14 TranscoderInput input = new TranscoderInput(doc);
15
16 // find the existing stylesheet in the document
17 NodeList stylesList = doc.getElementsByTagName("style");
18 Node styleNode = stylesList.item(0);
19
20 // append another stylesheet after the existing one
21 SVGStyleElement style = (SVGStyleElement)
22 doc.createElementNS(SVG_NAMESPACE_URI, "style");
23 style.setAttributeNS(null,"type","text/css");
24 style.appendChild(doc.createCDATASection(".us {fill: blue;}"));
25 styleNode.getParentNode().appendChild(style);
26
27 // transcode the map
28 OutputStream ostream = new FileOutputStream("outblue.jpg");
29 TranscoderOutput output = new TranscoderOutput(ostream);
30 t.transcode(input, output);
31 ostream.close();
32
33 // replace the appended stylesheet with another
34 SVGStyleElement oldStyle = style;
35 style = (SVGStyleElement)
36 doc.createElementNS(SVG_NAMESPACE_URI, "style");
37 style.setAttributeNS(null,"type","text/css");
38 style.appendChild(doc.createCDATASection(".us {fill: green;}"));
39 styleNode.getParentNode().replaceChild(style, oldStyle);
40
41 // transcode the revised map
42 ostream = new FileOutputStream("outgreen.jpg");
43 output = new TranscoderOutput(ostream);
44 t.transcode(input, output);
45 ostream.close();
46 } catch (Exception ex) {
47 ex.printStackTrace();
48 }
49 }