KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > ElementMappingRegistry


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.fo;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.w3c.dom.DOMImplementation JavaDoc;
26 import org.xml.sax.Locator JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.apache.xmlgraphics.util.Service;
32
33 import org.apache.fop.apps.FOPException;
34 import org.apache.fop.apps.FopFactory;
35 import org.apache.fop.fo.ElementMapping.Maker;
36
37 /**
38  * This class keeps track of all configured ElementMapping implementations which are responsible
39  * for properly handling all kinds of different XML namespaces.
40  */

41 public class ElementMappingRegistry {
42
43     /** logging instance */
44     protected Log log = LogFactory.getLog(ElementMappingRegistry.class);
45     
46     /**
47      * Table mapping element names to the makers of objects
48      * representing formatting objects.
49      */

50     protected Map JavaDoc fobjTable = new java.util.HashMap JavaDoc();
51
52     /**
53      * Map of mapped namespaces and their associated ElementMapping instances.
54      */

55     protected Map JavaDoc namespaces = new java.util.HashMap JavaDoc();
56
57     /**
58      * Main constructor. Adds all default element mapping as well as detects ElementMapping
59      * through the Service discovery.
60      * @param factory the Fop Factory
61      */

62     public ElementMappingRegistry(FopFactory factory) {
63         // Add standard element mappings
64
setupDefaultMappings();
65     }
66     
67     /**
68      * Sets all the element and property list mappings to their default values.
69      */

70     private void setupDefaultMappings() {
71         // add mappings from available services
72
Iterator JavaDoc providers = Service.providers(ElementMapping.class, false);
73         if (providers != null) {
74             while (providers.hasNext()) {
75                 String JavaDoc mapping = (String JavaDoc)providers.next();
76                 try {
77                     addElementMapping(mapping);
78                 } catch (IllegalArgumentException JavaDoc e) {
79                     log.warn("Error while adding element mapping", e);
80                 }
81
82             }
83         }
84     }
85
86     /**
87      * Add the element mapping with the given class name.
88      * @param mappingClassName the class name representing the element mapping.
89      * @throws IllegalArgumentException if there was not such element mapping.
90      */

91     public void addElementMapping(String JavaDoc mappingClassName)
92                 throws IllegalArgumentException JavaDoc {
93
94         try {
95             ElementMapping mapping
96                 = (ElementMapping)Class.forName(mappingClassName).newInstance();
97             addElementMapping(mapping);
98         } catch (ClassNotFoundException JavaDoc e) {
99             throw new IllegalArgumentException JavaDoc("Could not find "
100                                                + mappingClassName);
101         } catch (InstantiationException JavaDoc e) {
102             throw new IllegalArgumentException JavaDoc("Could not instantiate "
103                                                + mappingClassName);
104         } catch (IllegalAccessException JavaDoc e) {
105             throw new IllegalArgumentException JavaDoc("Could not access "
106                                                + mappingClassName);
107         } catch (ClassCastException JavaDoc e) {
108             throw new IllegalArgumentException JavaDoc(mappingClassName
109                                                + " is not an ElementMapping");
110         }
111     }
112
113     /**
114      * Add the element mapping.
115      * @param mapping the element mapping instance
116      */

117     public void addElementMapping(ElementMapping mapping) {
118         this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable());
119         this.namespaces.put(mapping.getNamespaceURI().intern(), mapping);
120     }
121
122     /**
123      * Finds the Maker used to create node objects of a particular type
124      * @param namespaceURI URI for the namespace of the element
125      * @param localName name of the Element
126      * @param locator the Locator instance for context information
127      * @return the ElementMapping.Maker that can create an FO object for this element
128      * @throws FOPException if a Maker could not be found for a bound namespace.
129      */

130     public Maker findFOMaker(String JavaDoc namespaceURI, String JavaDoc localName, Locator JavaDoc locator)
131                 throws FOPException {
132         Map JavaDoc table = (Map JavaDoc)fobjTable.get(namespaceURI);
133         Maker fobjMaker = null;
134         if (table != null) {
135             fobjMaker = (ElementMapping.Maker)table.get(localName);
136             // try default
137
if (fobjMaker == null) {
138                 fobjMaker = (ElementMapping.Maker)table.get(ElementMapping.DEFAULT);
139             }
140         }
141
142         if (fobjMaker == null) {
143             if (namespaces.containsKey(namespaceURI.intern())) {
144                   throw new FOPException(FONode.errorText(locator)
145                       + "No element mapping definition found for "
146                       + FONode.getNodeString(namespaceURI, localName), locator);
147             } else {
148                 log.warn("Unknown formatting object " + namespaceURI + "^" + localName);
149                 fobjMaker = new UnknownXMLObj.Maker(namespaceURI);
150             }
151         }
152         return fobjMaker;
153     }
154
155     /**
156      * Tries to determine the DOMImplementation that is used to handled a particular namespace.
157      * The method may return null for namespaces that don't result in a DOM. It is mostly used
158      * in namespaces occurring in foreign objects.
159      * @param namespaceURI the namespace URI
160      * @return the handling DOMImplementation, or null if not applicable
161      */

162     public DOMImplementation JavaDoc getDOMImplementationForNamespace(String JavaDoc namespaceURI) {
163         ElementMapping mapping = (ElementMapping)this.namespaces.get(namespaceURI);
164         if (mapping == null) {
165             return null;
166         } else {
167             return mapping.getDOMImplementation();
168         }
169     }
170     
171     /**
172      * Returns an ElementMapping class for a namespace URI if there is one.
173      * @param namespaceURI the namespace URI
174      * @return the requested ElementMapping or null, if no ElementMapping for the namespace is
175      * available.
176      */

177     public ElementMapping getElementMapping(String JavaDoc namespaceURI) {
178         return (ElementMapping)this.namespaces.get(namespaceURI);
179     }
180     
181     /**
182      * Indicates whether a namespace is known to FOP.
183      * @param namespaceURI the namespace URI
184      * @return true if the namespace is known.
185      */

186     public boolean isKnownNamespace(String JavaDoc namespaceURI) {
187         return this.namespaces.containsKey(namespaceURI);
188     }
189 }
190
Popular Tags