You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SVGSVGHandler.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.svg;
  18. import org.apache.batik.dom.svg.SVGDOMImplementation;
  19. import org.apache.batik.dom.util.DOMUtilities;
  20. import org.apache.batik.dom.util.XMLSupport;
  21. import org.apache.fop.render.Renderer;
  22. import org.apache.fop.render.RendererContext;
  23. import org.apache.fop.render.XMLHandler;
  24. import org.w3c.dom.DOMImplementation;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.Node;
  27. import org.w3c.dom.svg.SVGDocument;
  28. import org.w3c.dom.svg.SVGElement;
  29. import org.w3c.dom.svg.SVGSVGElement;
  30. public class SVGSVGHandler implements XMLHandler, SVGRendererContextConstants {
  31. /** @see org.apache.fop.render.XMLHandler */
  32. public void handleXML(RendererContext context,
  33. org.w3c.dom.Document doc, String ns) throws Exception {
  34. if (getNamespace().equals(ns)) {
  35. if (!(doc instanceof SVGDocument)) {
  36. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  37. doc = DOMUtilities.deepCloneDocument(doc, impl);
  38. }
  39. SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
  40. SVGDocument targetDoc = (SVGDocument)context.getProperty(SVG_DOCUMENT);
  41. SVGElement currentPageG = (SVGElement)context.getProperty(SVG_PAGE_G);
  42. Element view = targetDoc.createElementNS(getNamespace(), "svg");
  43. Node newsvg = targetDoc.importNode(svg, true);
  44. //view.setAttributeNS(null, "viewBox", "0 0 ");
  45. int xpos = ((Integer)context.getProperty(XPOS)).intValue();
  46. int ypos = ((Integer)context.getProperty(YPOS)).intValue();
  47. view.setAttributeNS(null, "x", "" + xpos / 1000f);
  48. view.setAttributeNS(null, "y", "" + ypos / 1000f);
  49. // this fixes a problem where the xmlns is repeated sometimes
  50. Element ele = (Element) newsvg;
  51. ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
  52. getNamespace());
  53. if (ele.hasAttributeNS(null, "xmlns")) {
  54. ele.removeAttributeNS(null, "xmlns");
  55. }
  56. view.appendChild(newsvg);
  57. currentPageG.appendChild(view);
  58. }
  59. }
  60. /** @see org.apache.fop.render.XMLHandler#supportsRenderer(org.apache.fop.render.Renderer) */
  61. public boolean supportsRenderer(Renderer renderer) {
  62. return (renderer instanceof SVGRenderer);
  63. }
  64. /** @see org.apache.fop.render.XMLHandler#getNamespace() */
  65. public String getNamespace() {
  66. return SVGRenderer.SVG_NAMESPACE;
  67. }
  68. }