KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > XMLHandlerRegistry


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

36 public class XMLHandlerRegistry {
37
38     /** the logger */
39     private static Log log = LogFactory.getLog(XMLHandlerRegistry.class);
40     
41     /** Map containing XML handlers for various document types */
42     private Map JavaDoc handlers = new java.util.HashMap JavaDoc();
43     
44     /**
45      * Default constructor.
46      */

47     public XMLHandlerRegistry() {
48         discoverXMLHandlers();
49     }
50     
51     /**
52      * Add a default XML handler which is able to handle any namespace.
53      * @param handler XMLHandler to use
54      */

55     private void setDefaultXMLHandler(XMLHandler handler) {
56         addXMLHandler(XMLHandler.HANDLE_ALL, handler);
57     }
58     
59     /**
60      * Add an XML handler. The handler itself is inspected to find out what it supports.
61      * @param classname the fully qualified class name
62      */

63     public void addXMLHandler(String JavaDoc classname) {
64         try {
65             XMLHandler handlerInstance = (XMLHandler)Class.forName(classname).newInstance();
66             addXMLHandler(handlerInstance);
67         } catch (ClassNotFoundException JavaDoc e) {
68             throw new IllegalArgumentException JavaDoc("Could not find "
69                                                + classname);
70         } catch (InstantiationException JavaDoc e) {
71             throw new IllegalArgumentException JavaDoc("Could not instantiate "
72                                                + classname);
73         } catch (IllegalAccessException JavaDoc e) {
74             throw new IllegalArgumentException JavaDoc("Could not access "
75                                                + classname);
76         } catch (ClassCastException JavaDoc e) {
77             throw new IllegalArgumentException JavaDoc(classname
78                                                + " is not an "
79                                                + XMLHandler.class.getName());
80         }
81     }
82     
83     /**
84      * Add an XML handler. The handler itself is inspected to find out what it supports.
85      * @param handler the XMLHandler instance
86      */

87     public void addXMLHandler(XMLHandler handler) {
88         String JavaDoc ns = handler.getNamespace();
89         if (ns == null) {
90             setDefaultXMLHandler(handler);
91         } else {
92             addXMLHandler(ns, handler);
93         }
94     }
95     
96     /**
97      * Add an XML handler for the given MIME type and XML namespace.
98      * @param ns Namespace URI
99      * @param handler XMLHandler to use
100      */

101     private void addXMLHandler(String JavaDoc ns,
102                               XMLHandler handler) {
103         List JavaDoc lst = (List JavaDoc)handlers.get(ns);
104         if (lst == null) {
105             lst = new java.util.ArrayList JavaDoc();
106             handlers.put(ns, lst);
107         }
108         lst.add(handler);
109     }
110     
111     /**
112      * Returns an XMLHandler which handles an XML dialect of the given namespace and for
113      * a specified output format defined by its MIME type.
114      * @param renderer the Renderer for which to retrieve a Renderer
115      * @param ns the XML namespace associated with the XML to be rendered
116      * @return the XMLHandler responsible for handling the XML or null if none is available
117      */

118     public XMLHandler getXMLHandler(Renderer renderer, String JavaDoc ns) {
119         XMLHandler handler;
120
121         List JavaDoc lst = (List JavaDoc)handlers.get(ns);
122         handler = getXMLHandler(renderer, lst);
123         if (handler == null) {
124             lst = (List JavaDoc)handlers.get(XMLHandler.HANDLE_ALL);
125             handler = getXMLHandler(renderer, lst);
126         }
127         return handler;
128     }
129
130     private XMLHandler getXMLHandler(Renderer renderer, List JavaDoc lst) {
131         XMLHandler handler;
132         if (lst != null) {
133             for (int i = 0, c = lst.size(); i < c; i++) {
134                 //TODO Maybe add priorities later
135
handler = (XMLHandler)lst.get(i);
136                 if (handler.supportsRenderer(renderer)) {
137                     return handler;
138                 }
139             }
140         }
141         return null; //No handler found
142
}
143     
144     /**
145      * Discovers XMLHandler implementations through the classpath and dynamically
146      * registers them.
147      */

148     private void discoverXMLHandlers() {
149         // add mappings from available services
150
Iterator JavaDoc providers = Service.providers(XMLHandler.class);
151         if (providers != null) {
152             while (providers.hasNext()) {
153                 XMLHandler handler = (XMLHandler)providers.next();
154                 try {
155                     if (log.isDebugEnabled()) {
156                         log.debug("Dynamically adding XMLHandler: " + handler.getClass().getName());
157                     }
158                     addXMLHandler(handler);
159                 } catch (IllegalArgumentException JavaDoc e) {
160                     log.error("Error while adding XMLHandler", e);
161                 }
162
163             }
164         }
165     }
166 }
167
Popular Tags