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.

SVGDOMContentHandlerFactory.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.fo.extensions.svg;
  19. import javax.xml.transform.TransformerConfigurationException;
  20. import javax.xml.transform.dom.DOMResult;
  21. import javax.xml.transform.sax.SAXTransformerFactory;
  22. import javax.xml.transform.sax.TransformerHandler;
  23. import org.w3c.dom.DOMImplementation;
  24. import org.w3c.dom.Document;
  25. import org.xml.sax.Attributes;
  26. import org.xml.sax.ContentHandler;
  27. import org.xml.sax.SAXException;
  28. import org.apache.batik.dom.svg.SVGDOMImplementation;
  29. import org.apache.fop.util.ContentHandlerFactory;
  30. import org.apache.fop.util.DelegatingContentHandler;
  31. /**
  32. * ContentHandlerFactory which constructs ContentHandlers that build SVG DOM
  33. * Documents.
  34. */
  35. public class SVGDOMContentHandlerFactory implements ContentHandlerFactory {
  36. private static SAXTransformerFactory tFactory
  37. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  38. /**
  39. * Default Constructor.
  40. */
  41. public SVGDOMContentHandlerFactory() {
  42. // nop
  43. }
  44. /** {@inheritDoc} */
  45. public String[] getSupportedNamespaces() {
  46. return new String[] {SVGDOMImplementation.SVG_NAMESPACE_URI};
  47. }
  48. /** {@inheritDoc} */
  49. public ContentHandler createContentHandler() throws SAXException {
  50. return new Handler();
  51. }
  52. private static class Handler extends DelegatingContentHandler implements
  53. ContentHandlerFactory.ObjectSource {
  54. private Document doc;
  55. private ObjectBuiltListener obListener;
  56. public Handler() throws SAXException {
  57. super();
  58. }
  59. public Document getDocument() {
  60. return this.doc;
  61. }
  62. /** {@inheritDoc} */
  63. public Object getObject() {
  64. return getDocument();
  65. }
  66. /** {@inheritDoc} */
  67. public void setObjectBuiltListener(ObjectBuiltListener listener) {
  68. this.obListener = listener;
  69. }
  70. /** {@inheritDoc} */
  71. public void startDocument() throws SAXException {
  72. // Suppress startDocument() call if doc has not been set, yet. It
  73. // will be done later.
  74. if (doc != null) {
  75. super.startDocument();
  76. }
  77. }
  78. private DOMImplementation getDOMImplementation(String ver) {
  79. //TODO It would be great if Batik provided this method as static helper method.
  80. if (ver == null || ver.length() == 0
  81. || ver.equals("1.0") || ver.equals("1.1")) {
  82. return SVGDOMImplementation.getDOMImplementation();
  83. } else if (ver.equals("1.2")) {
  84. try {
  85. Class clazz = Class.forName(
  86. "org.apache.batik.dom.svg12.SVG12DOMImplementation");
  87. return (DOMImplementation)clazz.getMethod(
  88. "getDOMImplementation", (Class[])null).invoke(null, (Object[])null);
  89. } catch (Exception e) {
  90. return SVGDOMImplementation.getDOMImplementation();
  91. }
  92. }
  93. throw new RuntimeException("Unsupport SVG version '" + ver + "'");
  94. }
  95. /** {@inheritDoc} */
  96. public void startElement(String uri, String localName, String qName, Attributes atts)
  97. throws SAXException {
  98. if (doc == null) {
  99. TransformerHandler handler;
  100. try {
  101. handler = tFactory.newTransformerHandler();
  102. } catch (TransformerConfigurationException e) {
  103. throw new SAXException("Error creating a new TransformerHandler", e);
  104. }
  105. String version = atts.getValue("version");
  106. DOMImplementation domImplementation = getDOMImplementation(version);
  107. doc = domImplementation.createDocument(uri, qName, null);
  108. // It's easier to work with an empty document, so remove the
  109. // root element
  110. doc.removeChild(doc.getDocumentElement());
  111. handler.setResult(new DOMResult(doc));
  112. setDelegateContentHandler(handler);
  113. setDelegateLexicalHandler(handler);
  114. setDelegateDTDHandler(handler);
  115. handler.startDocument();
  116. }
  117. super.startElement(uri, localName, qName, atts);
  118. }
  119. /** {@inheritDoc} */
  120. public void endDocument() throws SAXException {
  121. super.endDocument();
  122. if (obListener != null) {
  123. obListener.notifyObjectBuilt(getObject());
  124. }
  125. }
  126. }
  127. }