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.

DOMBuilderContentHandlerFactory.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.util;
  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. /**
  29. * ContentHandlerFactory which constructs ContentHandlers that build DOM Documents.
  30. */
  31. public class DOMBuilderContentHandlerFactory implements ContentHandlerFactory {
  32. private static SAXTransformerFactory tFactory
  33. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  34. private String namespaceURI;
  35. private DOMImplementation domImplementation;
  36. /**
  37. * Main Constructor
  38. * @param namespaceURI the main namespace URI for the DOM to be parsed
  39. * @param domImplementation the DOMImplementation to use for build the DOM
  40. */
  41. public DOMBuilderContentHandlerFactory(String namespaceURI,
  42. DOMImplementation domImplementation) {
  43. this.namespaceURI = namespaceURI;
  44. this.domImplementation = domImplementation;
  45. }
  46. /** {@inheritDoc} */
  47. public String[] getSupportedNamespaces() {
  48. return new String[] {namespaceURI};
  49. }
  50. /** {@inheritDoc} */
  51. public ContentHandler createContentHandler() throws SAXException {
  52. return new Handler();
  53. }
  54. private class Handler extends DelegatingContentHandler
  55. implements ContentHandlerFactory.ObjectSource {
  56. private Document doc;
  57. private ObjectBuiltListener obListener;
  58. public Handler() throws SAXException {
  59. super();
  60. }
  61. public Document getDocument() {
  62. return this.doc;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public Object getObject() {
  68. return getDocument();
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public void setObjectBuiltListener(ObjectBuiltListener listener) {
  74. this.obListener = listener;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public void startDocument() throws SAXException {
  80. //Suppress startDocument() call if doc has not been set, yet. It will be done later.
  81. if (doc != null) {
  82. super.startDocument();
  83. }
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public void startElement(String uri, String localName, String qName, Attributes atts)
  89. throws SAXException {
  90. if (doc == null) {
  91. TransformerHandler handler;
  92. try {
  93. handler = tFactory.newTransformerHandler();
  94. } catch (TransformerConfigurationException e) {
  95. throw new SAXException("Error creating a new TransformerHandler", e);
  96. }
  97. doc = domImplementation.createDocument(namespaceURI, qName, null);
  98. //It's easier to work with an empty document, so remove the root element
  99. doc.removeChild(doc.getDocumentElement());
  100. handler.setResult(new DOMResult(doc));
  101. setDelegateContentHandler(handler);
  102. setDelegateLexicalHandler(handler);
  103. setDelegateDTDHandler(handler);
  104. handler.startDocument();
  105. }
  106. super.startElement(uri, localName, qName, atts);
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public void endDocument() throws SAXException {
  112. super.endDocument();
  113. if (obListener != null) {
  114. obListener.notifyObjectBuilt(getObject());
  115. }
  116. }
  117. }
  118. }