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.2KB

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