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.

XMLHandlerConfigurator.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.apps.FOUserAgent;
  23. import org.apache.fop.configuration.Configuration;
  24. import org.apache.fop.configuration.ConfigurationException;
  25. /**
  26. * Configurator for XMLHandler objects.
  27. */
  28. public class XMLHandlerConfigurator extends AbstractRendererConfigurator {
  29. /** logger instance */
  30. protected static final Log log = LogFactory.getLog(XMLHandlerConfigurator.class);
  31. /**
  32. * Default constructor
  33. * @param userAgent the user agent
  34. */
  35. public XMLHandlerConfigurator(FOUserAgent userAgent) {
  36. super(userAgent);
  37. }
  38. /**
  39. * Returns the configuration subtree for a specific renderer.
  40. * @param cfg the renderer configuration
  41. * @param namespace the namespace (i.e. the XMLHandler) for which the configuration should
  42. * be returned
  43. * @return the requested configuration subtree, null if there's no configuration
  44. */
  45. private Configuration getHandlerConfig(Configuration cfg, String namespace) {
  46. if (cfg == null || namespace == null) {
  47. return null;
  48. }
  49. Configuration handlerConfig = null;
  50. Configuration[] children = cfg.getChildren("xml-handler");
  51. for (Configuration aChildren : children) {
  52. try {
  53. if (aChildren.getAttribute("namespace").equals(namespace)) {
  54. handlerConfig = aChildren;
  55. break;
  56. }
  57. } catch (ConfigurationException e) {
  58. // silently pass over configurations without namespace
  59. }
  60. }
  61. if (log.isDebugEnabled()) {
  62. log.debug((handlerConfig == null ? "No" : "")
  63. + "XML handler configuration found for namespace " + namespace);
  64. }
  65. return handlerConfig;
  66. }
  67. /**
  68. * Configures renderer context by setting the handler configuration on it.
  69. * @param context the RendererContext (contains the user agent)
  70. * @param ns the Namespace of the foreign object
  71. * @throws FOPException if configuring the target objects fails
  72. */
  73. public void configure(RendererContext context, String ns) throws FOPException {
  74. //Optional XML handler configuration
  75. Configuration cfg = userAgent.getRendererConfiguration(context.getRenderer().getMimeType());
  76. if (cfg != null) {
  77. cfg = getHandlerConfig(cfg, ns);
  78. if (cfg != null) {
  79. context.setProperty(RendererContextConstants.HANDLER_CONFIGURATION, cfg);
  80. }
  81. }
  82. }
  83. }