KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > ContentHandlerFactoryRegistry


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
18 /* $Id$ */
19
20 package org.apache.fop.util;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.xmlgraphics.util.Service;
29
30 /**
31  * This class holds references to various XML handlers used by FOP. It also
32  * supports automatic discovery of additional XML handlers available through
33  * the class path.
34  */

35 public class ContentHandlerFactoryRegistry {
36
37     /** the logger */
38     private static Log log = LogFactory.getLog(ContentHandlerFactoryRegistry.class);
39     
40     /** Map from namespace URIs to ContentHandlerFactories */
41     private Map JavaDoc factories = new java.util.HashMap JavaDoc();
42     
43     /**
44      * Default constructor.
45      */

46     public ContentHandlerFactoryRegistry() {
47         discover();
48     }
49     
50     /**
51      * Add an XML handler. The handler itself is inspected to find out what it supports.
52      * @param classname the fully qualified class name
53      */

54     public void addContentHandlerFactory(String JavaDoc classname) {
55         try {
56             ContentHandlerFactory factory
57                 = (ContentHandlerFactory)Class.forName(classname).newInstance();
58             addContentHandlerFactory(factory);
59         } catch (ClassNotFoundException JavaDoc e) {
60             throw new IllegalArgumentException JavaDoc("Could not find "
61                                                + classname);
62         } catch (InstantiationException JavaDoc e) {
63             throw new IllegalArgumentException JavaDoc("Could not instantiate "
64                                                + classname);
65         } catch (IllegalAccessException JavaDoc e) {
66             throw new IllegalArgumentException JavaDoc("Could not access "
67                                                + classname);
68         } catch (ClassCastException JavaDoc e) {
69             throw new IllegalArgumentException JavaDoc(classname
70                                                + " is not an "
71                                                + ContentHandlerFactory.class.getName());
72         }
73     }
74     
75     /**
76      * Add an ContentHandlerFactory. The instance is inspected to find out what it supports.
77      * @param factory the ContentHandlerFactory instance
78      */

79     public void addContentHandlerFactory(ContentHandlerFactory factory) {
80         String JavaDoc[] ns = factory.getSupportedNamespaces();
81         for (int i = 0; i < ns.length; i++) {
82             factories.put(ns[i], factory);
83         }
84     }
85     
86     /**
87      * Retrieves a ContentHandlerFactory instance of a given namespace URI.
88      * @param namespaceURI the namespace to be handled.
89      * @return the ContentHandlerFactory or null, if no suitable instance is available.
90      */

91     public ContentHandlerFactory getFactory(String JavaDoc namespaceURI) {
92         ContentHandlerFactory factory = (ContentHandlerFactory)factories.get(namespaceURI);
93         return factory;
94     }
95     
96     /**
97      * Discovers ContentHandlerFactory implementations through the classpath and dynamically
98      * registers them.
99      */

100     private void discover() {
101         // add mappings from available services
102
Iterator JavaDoc providers = Service.providers(ContentHandlerFactory.class);
103         if (providers != null) {
104             while (providers.hasNext()) {
105                 ContentHandlerFactory factory = (ContentHandlerFactory)providers.next();
106                 try {
107                     if (log.isDebugEnabled()) {
108                         log.debug("Dynamically adding ContentHandlerFactory: "
109                                 + factory.getClass().getName());
110                     }
111                     addContentHandlerFactory(factory);
112                 } catch (IllegalArgumentException JavaDoc e) {
113                     log.error("Error while adding ContentHandlerFactory", e);
114                 }
115
116             }
117         }
118     }
119 }
120
Popular Tags