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.util;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.apache.xmlgraphics.util.Service;
029:
030: /**
031: * This class holds references to various XML handlers used by FOP. It also
032: * supports automatic discovery of additional XML handlers available through
033: * the class path.
034: */
035: public class ContentHandlerFactoryRegistry {
036:
037: /** the logger */
038: private static Log log = LogFactory
039: .getLog(ContentHandlerFactoryRegistry.class);
040:
041: /** Map from namespace URIs to ContentHandlerFactories */
042: private Map factories = new java.util.HashMap();
043:
044: /**
045: * Default constructor.
046: */
047: public ContentHandlerFactoryRegistry() {
048: discover();
049: }
050:
051: /**
052: * Add an XML handler. The handler itself is inspected to find out what it supports.
053: * @param classname the fully qualified class name
054: */
055: public void addContentHandlerFactory(String classname) {
056: try {
057: ContentHandlerFactory factory = (ContentHandlerFactory) Class
058: .forName(classname).newInstance();
059: addContentHandlerFactory(factory);
060: } catch (ClassNotFoundException e) {
061: throw new IllegalArgumentException("Could not find "
062: + classname);
063: } catch (InstantiationException e) {
064: throw new IllegalArgumentException("Could not instantiate "
065: + classname);
066: } catch (IllegalAccessException e) {
067: throw new IllegalArgumentException("Could not access "
068: + classname);
069: } catch (ClassCastException e) {
070: throw new IllegalArgumentException(classname
071: + " is not an "
072: + ContentHandlerFactory.class.getName());
073: }
074: }
075:
076: /**
077: * Add an ContentHandlerFactory. The instance is inspected to find out what it supports.
078: * @param factory the ContentHandlerFactory instance
079: */
080: public void addContentHandlerFactory(ContentHandlerFactory factory) {
081: String[] ns = factory.getSupportedNamespaces();
082: for (int i = 0; i < ns.length; i++) {
083: factories.put(ns[i], factory);
084: }
085: }
086:
087: /**
088: * Retrieves a ContentHandlerFactory instance of a given namespace URI.
089: * @param namespaceURI the namespace to be handled.
090: * @return the ContentHandlerFactory or null, if no suitable instance is available.
091: */
092: public ContentHandlerFactory getFactory(String namespaceURI) {
093: ContentHandlerFactory factory = (ContentHandlerFactory) factories
094: .get(namespaceURI);
095: return factory;
096: }
097:
098: /**
099: * Discovers ContentHandlerFactory implementations through the classpath and dynamically
100: * registers them.
101: */
102: private void discover() {
103: // add mappings from available services
104: Iterator providers = Service
105: .providers(ContentHandlerFactory.class);
106: if (providers != null) {
107: while (providers.hasNext()) {
108: ContentHandlerFactory factory = (ContentHandlerFactory) providers
109: .next();
110: try {
111: if (log.isDebugEnabled()) {
112: log
113: .debug("Dynamically adding ContentHandlerFactory: "
114: + factory.getClass().getName());
115: }
116: addContentHandlerFactory(factory);
117: } catch (IllegalArgumentException e) {
118: log.error(
119: "Error while adding ContentHandlerFactory",
120: e);
121: }
122:
123: }
124: }
125: }
126: }
|