001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: $ */
019:
020: package org.apache.fop.render;
021:
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.configuration.ConfigurationException;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.fop.apps.FOPException;
027: import org.apache.fop.apps.FOUserAgent;
028:
029: /**
030: * Configurator for XMLHandler objects.
031: */
032: public class XMLHandlerConfigurator extends
033: AbstractRendererConfigurator {
034:
035: /** logger instance */
036: protected static Log log = LogFactory
037: .getLog(XMLHandlerConfigurator.class);
038:
039: /**
040: * Default constructor
041: * @param userAgent the user agent
042: */
043: public XMLHandlerConfigurator(FOUserAgent userAgent) {
044: super (userAgent);
045: }
046:
047: /**
048: * Returns the configuration subtree for a specific renderer.
049: * @param cfg the renderer configuration
050: * @param namespace the namespace (i.e. the XMLHandler) for which the configuration should
051: * be returned
052: * @return the requested configuration subtree, null if there's no configuration
053: */
054: private Configuration getHandlerConfig(Configuration cfg,
055: String namespace) {
056: if (cfg == null || namespace == null) {
057: return null;
058: }
059: Configuration handlerConfig = null;
060:
061: Configuration[] children = cfg.getChildren("xml-handler");
062: for (int i = 0; i < children.length; ++i) {
063: try {
064: if (children[i].getAttribute("namespace").equals(
065: namespace)) {
066: handlerConfig = children[i];
067: break;
068: }
069: } catch (ConfigurationException e) {
070: // silently pass over configurations without namespace
071: }
072: }
073: if (log.isDebugEnabled()) {
074: log.debug((handlerConfig == null ? "No" : "")
075: + "XML handler configuration found for namespace "
076: + namespace);
077: }
078: return handlerConfig;
079: }
080:
081: /**
082: * Configures renderer context by setting the handler configuration on it.
083: * @param context the RendererContext (contains the user agent)
084: * @param ns the Namespace of the foreign object
085: * @throws FOPException if configuring the target objects fails
086: */
087: public void configure(RendererContext context, String ns)
088: throws FOPException {
089: //Optional XML handler configuration
090: Configuration cfg = getRendererConfig(context.getRenderer());
091: if (cfg != null) {
092: cfg = getHandlerConfig(cfg, ns);
093: if (cfg != null) {
094: context.setProperty(
095: RendererContextConstants.HANDLER_CONFIGURATION,
096: cfg);
097: }
098: }
099: }
100: }
|