You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ElementMappingRegistry.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* $Id$ */
  18. package org.apache.fop.fo;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import org.w3c.dom.DOMImplementation;
  23. import org.xml.sax.Locator;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.util.Service;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.FopFactory;
  29. import org.apache.fop.fo.ElementMapping.Maker;
  30. /**
  31. * This class keeps track of all configured ElementMapping implementations which are responsible
  32. * for properly handling all kinds of different XML namespaces.
  33. */
  34. public class ElementMappingRegistry {
  35. /** logging instance */
  36. private static final Log LOG = LogFactory.getLog(ElementMappingRegistry.class);
  37. /**
  38. * Table mapping element names to the makers of objects
  39. * representing formatting objects.
  40. */
  41. protected Map<String, Map<String, Maker>> fobjTable
  42. = new java.util.HashMap<String, Map<String, Maker>>();
  43. /**
  44. * Map of mapped namespaces and their associated ElementMapping instances.
  45. */
  46. protected Map<String, ElementMapping> namespaces
  47. = new java.util.HashMap<String, ElementMapping>();
  48. /**
  49. * Main constructor. Adds all default element mapping as well as detects ElementMapping
  50. * through the Service discovery.
  51. * @param factory the Fop Factory
  52. */
  53. public ElementMappingRegistry(FopFactory factory) {
  54. // Add standard element mappings
  55. setupDefaultMappings();
  56. }
  57. /**
  58. * Sets all the element and property list mappings to their default values.
  59. */
  60. private void setupDefaultMappings() {
  61. // add mappings from available services
  62. Iterator<String> providers = Service.providerNames(ElementMapping.class);
  63. if (providers != null) {
  64. while (providers.hasNext()) {
  65. String mapping = providers.next();
  66. try {
  67. addElementMapping(mapping);
  68. } catch (IllegalArgumentException e) {
  69. LOG.warn("Error while adding element mapping", e);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * Add the element mapping with the given class name.
  76. * @param mappingClassName the class name representing the element mapping.
  77. * @throws IllegalArgumentException if there was not such element mapping.
  78. */
  79. public void addElementMapping(String mappingClassName)
  80. throws IllegalArgumentException {
  81. try {
  82. ElementMapping mapping
  83. = (ElementMapping)Class.forName(mappingClassName).getDeclaredConstructor().newInstance();
  84. addElementMapping(mapping);
  85. } catch (ClassNotFoundException e) {
  86. throw new IllegalArgumentException("Could not find "
  87. + mappingClassName);
  88. } catch (InstantiationException e) {
  89. throw new IllegalArgumentException("Could not instantiate "
  90. + mappingClassName);
  91. } catch (IllegalAccessException e) {
  92. throw new IllegalArgumentException("Could not access "
  93. + mappingClassName);
  94. } catch (ClassCastException e) {
  95. throw new IllegalArgumentException(mappingClassName
  96. + " is not an ElementMapping");
  97. } catch (NoSuchMethodException e) {
  98. throw new IllegalArgumentException(e);
  99. } catch (InvocationTargetException e) {
  100. throw new IllegalArgumentException(e);
  101. }
  102. }
  103. /**
  104. * Add the element mapping.
  105. * @param mapping the element mapping instance
  106. */
  107. public void addElementMapping(ElementMapping mapping) {
  108. this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable());
  109. this.namespaces.put(mapping.getNamespaceURI().intern(), mapping);
  110. }
  111. /**
  112. * Finds the Maker used to create node objects of a particular type
  113. * @param namespaceURI URI for the namespace of the element
  114. * @param localName name of the Element
  115. * @param locator the Locator instance for context information
  116. * @return the ElementMapping.Maker that can create an FO object for this element
  117. * @throws FOPException if a Maker could not be found for a bound namespace.
  118. */
  119. public Maker findFOMaker(String namespaceURI, String localName, Locator locator)
  120. throws FOPException {
  121. Map<String, Maker> table = fobjTable.get(namespaceURI);
  122. Maker fobjMaker = null;
  123. if (table != null) {
  124. fobjMaker = table.get(localName);
  125. // try default
  126. if (fobjMaker == null) {
  127. fobjMaker = table.get(ElementMapping.DEFAULT);
  128. }
  129. }
  130. if (fobjMaker == null) {
  131. if (namespaces.containsKey(namespaceURI.intern())) {
  132. throw new FOPException(FONode.errorText(locator)
  133. + "No element mapping definition found for "
  134. + FONode.getNodeString(namespaceURI, localName), locator);
  135. } else {
  136. fobjMaker = new UnknownXMLObj.Maker(namespaceURI);
  137. }
  138. }
  139. return fobjMaker;
  140. }
  141. /**
  142. * Tries to determine the DOMImplementation that is used to handled a particular namespace.
  143. * The method may return null for namespaces that don't result in a DOM. It is mostly used
  144. * in namespaces occurring in foreign objects.
  145. * @param namespaceURI the namespace URI
  146. * @return the handling DOMImplementation, or null if not applicable
  147. */
  148. public DOMImplementation getDOMImplementationForNamespace(String namespaceURI) {
  149. ElementMapping mapping = this.namespaces.get(namespaceURI);
  150. if (mapping == null) {
  151. return null;
  152. } else {
  153. return mapping.getDOMImplementation();
  154. }
  155. }
  156. /**
  157. * Returns an ElementMapping class for a namespace URI if there is one.
  158. * @param namespaceURI the namespace URI
  159. * @return the requested ElementMapping or null, if no ElementMapping for the namespace is
  160. * available.
  161. */
  162. public ElementMapping getElementMapping(String namespaceURI) {
  163. return this.namespaces.get(namespaceURI);
  164. }
  165. /**
  166. * Indicates whether a namespace is known to FOP.
  167. * @param namespaceURI the namespace URI
  168. * @return true if the namespace is known.
  169. */
  170. public boolean isKnownNamespace(String namespaceURI) {
  171. return this.namespaces.containsKey(namespaceURI);
  172. }
  173. }