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.0KB

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