KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > svg > SVGSVGHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: SVGSVGHandler.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.svg;
21
22 import org.apache.batik.dom.svg.SVGDOMImplementation;
23 import org.apache.batik.dom.util.DOMUtilities;
24 import org.apache.batik.dom.util.XMLSupport;
25 import org.apache.fop.render.Renderer;
26 import org.apache.fop.render.RendererContext;
27 import org.apache.fop.render.XMLHandler;
28 import org.w3c.dom.DOMImplementation JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.svg.SVGDocument;
32 import org.w3c.dom.svg.SVGElement;
33 import org.w3c.dom.svg.SVGSVGElement;
34
35 public class SVGSVGHandler implements XMLHandler, SVGRendererContextConstants {
36
37     /** @see org.apache.fop.render.XMLHandler */
38     public void handleXML(RendererContext context,
39                 org.w3c.dom.Document JavaDoc doc, String JavaDoc ns) throws Exception JavaDoc {
40         if (getNamespace().equals(ns)) {
41             if (!(doc instanceof SVGDocument)) {
42                 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
43                 doc = DOMUtilities.deepCloneDocument(doc, impl);
44             }
45             SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
46             SVGDocument targetDoc = (SVGDocument)context.getProperty(SVG_DOCUMENT);
47             SVGElement currentPageG = (SVGElement)context.getProperty(SVG_PAGE_G);
48             Element JavaDoc view = targetDoc.createElementNS(getNamespace(), "svg");
49             Node JavaDoc newsvg = targetDoc.importNode(svg, true);
50             //view.setAttributeNS(null, "viewBox", "0 0 ");
51
int xpos = ((Integer JavaDoc)context.getProperty(XPOS)).intValue();
52             int ypos = ((Integer JavaDoc)context.getProperty(YPOS)).intValue();
53             view.setAttributeNS(null, "x", "" + xpos / 1000f);
54             view.setAttributeNS(null, "y", "" + ypos / 1000f);
55
56             // this fixes a problem where the xmlns is repeated sometimes
57
Element JavaDoc ele = (Element JavaDoc) newsvg;
58             ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
59                                getNamespace());
60             if (ele.hasAttributeNS(null, "xmlns")) {
61                 ele.removeAttributeNS(null, "xmlns");
62             }
63
64             view.appendChild(newsvg);
65             currentPageG.appendChild(view);
66         }
67     }
68
69
70     /** @see org.apache.fop.render.XMLHandler#supportsRenderer(org.apache.fop.render.Renderer) */
71     public boolean supportsRenderer(Renderer renderer) {
72         return (renderer instanceof SVGRenderer);
73     }
74
75     /** @see org.apache.fop.render.XMLHandler#getNamespace() */
76     public String JavaDoc getNamespace() {
77         return SVGRenderer.SVG_NAMESPACE;
78     }
79
80
81 }
82
Popular Tags