1. /*
  2. * Copyright 2001-2004 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. /*
  17. * $Id: TrAXFilter.java,v 1.8 2004/02/16 22:57:21 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.trax;
  20. import java.io.IOException;
  21. import javax.xml.parsers.FactoryConfigurationError;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import javax.xml.parsers.SAXParser;
  24. import javax.xml.parsers.SAXParserFactory;
  25. import javax.xml.transform.ErrorListener;
  26. import javax.xml.transform.Templates;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerConfigurationException;
  29. import javax.xml.transform.sax.SAXResult;
  30. import com.sun.org.apache.xml.internal.utils.XMLReaderManager;
  31. import org.xml.sax.ContentHandler;
  32. import org.xml.sax.InputSource;
  33. import org.xml.sax.SAXException;
  34. import org.xml.sax.XMLReader;
  35. import org.xml.sax.helpers.XMLFilterImpl;
  36. import org.xml.sax.helpers.XMLReaderFactory;
  37. /**
  38. * skeleton extension of XMLFilterImpl for now.
  39. * @author Santiago Pericas-Geertsen
  40. * @author G. Todd Miller
  41. */
  42. public class TrAXFilter extends XMLFilterImpl {
  43. private Templates _templates;
  44. private TransformerImpl _transformer;
  45. private TransformerHandlerImpl _transformerHandler;
  46. public TrAXFilter(Templates templates) throws
  47. TransformerConfigurationException
  48. {
  49. _templates = templates;
  50. _transformer = (TransformerImpl) templates.newTransformer();
  51. _transformerHandler = new TransformerHandlerImpl(_transformer);
  52. }
  53. public Transformer getTransformer() {
  54. return _transformer;
  55. }
  56. private void createParent() throws SAXException {
  57. XMLReader parent = null;
  58. try {
  59. SAXParserFactory pfactory = SAXParserFactory.newInstance();
  60. pfactory.setNamespaceAware(true);
  61. SAXParser saxparser = pfactory.newSAXParser();
  62. parent = saxparser.getXMLReader();
  63. }
  64. catch (ParserConfigurationException e) {
  65. throw new SAXException(e);
  66. }
  67. catch (FactoryConfigurationError e) {
  68. throw new SAXException(e.toString());
  69. }
  70. if (parent == null) {
  71. parent = XMLReaderFactory.createXMLReader();
  72. }
  73. // make this XMLReader the parent of this filter
  74. setParent(parent);
  75. }
  76. public void parse (InputSource input) throws SAXException, IOException
  77. {
  78. XMLReader managedReader = null;
  79. try {
  80. if (getParent() == null) {
  81. try {
  82. managedReader = XMLReaderManager.getInstance()
  83. .getXMLReader();
  84. setParent(managedReader);
  85. } catch (SAXException e) {
  86. throw new SAXException(e.toString());
  87. }
  88. }
  89. // call parse on the parent
  90. getParent().parse(input);
  91. } finally {
  92. if (managedReader != null) {
  93. XMLReaderManager.getInstance().releaseXMLReader(managedReader);
  94. }
  95. }
  96. }
  97. public void parse (String systemId) throws SAXException, IOException
  98. {
  99. parse(new InputSource(systemId));
  100. }
  101. public void setContentHandler (ContentHandler handler)
  102. {
  103. _transformerHandler.setResult(new SAXResult(handler));
  104. if (getParent() == null) {
  105. try {
  106. createParent();
  107. }
  108. catch (SAXException e) {
  109. return;
  110. }
  111. }
  112. getParent().setContentHandler(_transformerHandler);
  113. }
  114. public void setErrorListener (ErrorListener handler) { }
  115. }