選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractXMLWritingIFDocumentHandler.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.intermediate;
  19. import javax.xml.transform.OutputKeys;
  20. import javax.xml.transform.Result;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerConfigurationException;
  23. import javax.xml.transform.sax.SAXResult;
  24. import javax.xml.transform.sax.SAXTransformerFactory;
  25. import javax.xml.transform.sax.TransformerHandler;
  26. import org.xml.sax.ContentHandler;
  27. import org.apache.fop.util.GenerationHelperContentHandler;
  28. /**
  29. * Abstract base class for XML-writing {@link IFDocumentHandler} implementations.
  30. */
  31. public abstract class AbstractXMLWritingIFDocumentHandler extends AbstractIFDocumentHandler {
  32. /**
  33. * Default SAXTransformerFactory that can be used by subclasses.
  34. */
  35. protected SAXTransformerFactory tFactory
  36. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  37. /** Main SAX ContentHandler to receive the generated SAX events. */
  38. protected GenerationHelperContentHandler handler;
  39. protected AbstractXMLWritingIFDocumentHandler(IFContext context) {
  40. super(context);
  41. }
  42. /** {@inheritDoc} */
  43. public void setResult(Result result) throws IFException {
  44. if (result instanceof SAXResult) {
  45. SAXResult saxResult = (SAXResult)result;
  46. this.handler = new GenerationHelperContentHandler(
  47. saxResult.getHandler(), getMainNamespace(), getContext());
  48. } else {
  49. this.handler = new GenerationHelperContentHandler(
  50. createContentHandler(result), getMainNamespace(), getContext());
  51. }
  52. }
  53. /**
  54. * Returns the main namespace used for generated XML content.
  55. * @return the main namespace
  56. */
  57. protected abstract String getMainNamespace();
  58. /**
  59. * Creates a ContentHandler for the given JAXP Result instance.
  60. * @param result the JAXP Result instance
  61. * @return the requested SAX ContentHandler
  62. * @throws IFException if an error occurs setting up the output
  63. */
  64. protected ContentHandler createContentHandler(Result result) throws IFException {
  65. try {
  66. TransformerHandler tHandler = tFactory.newTransformerHandler();
  67. Transformer transformer = tHandler.getTransformer();
  68. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  69. transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  70. tHandler.setResult(result);
  71. return tHandler;
  72. } catch (TransformerConfigurationException tce) {
  73. throw new IFException(
  74. "Error while setting up the serializer for XML output", tce);
  75. }
  76. }
  77. }