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.

SVGElementMapping.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 1999-2004,2006 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.fo.extensions.svg;
  18. import java.util.HashMap;
  19. import javax.xml.parsers.SAXParserFactory;
  20. import org.apache.fop.fo.FONode;
  21. import org.apache.fop.fo.ElementMapping;
  22. import org.apache.batik.util.XMLResourceDescriptor;
  23. import org.apache.batik.dom.svg.SVGDOMImplementation;
  24. import org.w3c.dom.DOMImplementation;
  25. /**
  26. * Setup the SVG element mapping.
  27. * This adds the svg element mappings used to create the objects
  28. * that create the SVG Document.
  29. */
  30. public class SVGElementMapping extends ElementMapping {
  31. /** the SVG namespace */
  32. public static final String URI = SVGDOMImplementation.SVG_NAMESPACE_URI;
  33. private boolean batikAvailable = true;
  34. /** Main constructor. */
  35. public SVGElementMapping() {
  36. namespaceURI = URI;
  37. }
  38. /** @see org.apache.fop.fo.ElementMapping#getDOMImplementation() */
  39. public DOMImplementation getDOMImplementation() {
  40. return SVGDOMImplementation.getDOMImplementation();
  41. }
  42. /**
  43. * Returns the fully qualified classname of an XML parser for
  44. * Batik classes that apparently need it (error messages, perhaps)
  45. * @return an XML parser classname
  46. */
  47. private String getAParserClassName() {
  48. try {
  49. SAXParserFactory factory = SAXParserFactory.newInstance();
  50. return factory.newSAXParser().getXMLReader().getClass().getName();
  51. } catch (Exception e) {
  52. return null;
  53. }
  54. }
  55. /** @see org.apache.fop.fo.ElementMapping#initialize() */
  56. protected void initialize() {
  57. if (foObjs == null && batikAvailable) {
  58. // this sets the parser that will be used
  59. // by default (SVGBrokenLinkProvider)
  60. // normally the user agent value is used
  61. try {
  62. XMLResourceDescriptor.setXMLParserClassName(
  63. getAParserClassName());
  64. foObjs = new HashMap();
  65. foObjs.put("svg", new SE());
  66. foObjs.put(DEFAULT, new SVGMaker());
  67. } catch (Throwable t) {
  68. // if the classes are not available
  69. // the DISPLAY is not checked
  70. batikAvailable = false;
  71. }
  72. }
  73. }
  74. static class SVGMaker extends ElementMapping.Maker {
  75. public FONode make(FONode parent) {
  76. return new SVGObj(parent);
  77. }
  78. }
  79. static class SE extends ElementMapping.Maker {
  80. public FONode make(FONode parent) {
  81. return new SVGElement(parent);
  82. }
  83. }
  84. }