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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fo;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FopFactory;
  25. import org.apache.fop.fo.ElementMapping.Maker;
  26. import org.apache.fop.util.Service;
  27. import org.w3c.dom.DOMImplementation;
  28. import org.xml.sax.Locator;
  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. // add additional ElementMappings defined within FOUserAgent
  54. List addlEMs = factory.getAdditionalElementMappings();
  55. if (addlEMs != null) {
  56. for (int i = 0; i < addlEMs.size(); i++) {
  57. addElementMapping((ElementMapping) addlEMs.get(i));
  58. }
  59. }
  60. }
  61. /**
  62. * Sets all the element and property list mappings to their default values.
  63. */
  64. private void setupDefaultMappings() {
  65. // add mappings from available services
  66. Iterator providers = Service.providers(ElementMapping.class);
  67. if (providers != null) {
  68. while (providers.hasNext()) {
  69. String str = (String)providers.next();
  70. try {
  71. addElementMapping(str);
  72. } catch (IllegalArgumentException e) {
  73. log.warn("Error while adding element mapping", e);
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Add the element mapping with the given class name.
  80. * @param mappingClassName the class name representing the element mapping.
  81. * @throws IllegalArgumentException if there was not such element mapping.
  82. */
  83. public void addElementMapping(String mappingClassName)
  84. throws IllegalArgumentException {
  85. try {
  86. ElementMapping mapping
  87. = (ElementMapping)Class.forName(mappingClassName).newInstance();
  88. addElementMapping(mapping);
  89. } catch (ClassNotFoundException e) {
  90. throw new IllegalArgumentException("Could not find "
  91. + mappingClassName);
  92. } catch (InstantiationException e) {
  93. throw new IllegalArgumentException("Could not instantiate "
  94. + mappingClassName);
  95. } catch (IllegalAccessException e) {
  96. throw new IllegalArgumentException("Could not access "
  97. + mappingClassName);
  98. } catch (ClassCastException e) {
  99. throw new IllegalArgumentException(mappingClassName
  100. + " is not an ElementMapping");
  101. }
  102. }
  103. private void addElementMapping(ElementMapping mapping) {
  104. this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable());
  105. this.namespaces.put(mapping.getNamespaceURI().intern(), mapping);
  106. }
  107. /**
  108. * Finds the Maker used to create node objects of a particular type
  109. * @param namespaceURI URI for the namespace of the element
  110. * @param localName name of the Element
  111. * @param locator the Locator instance for context information
  112. * @return the ElementMapping.Maker that can create an FO object for this element
  113. * @throws FOPException if a Maker could not be found for a bound namespace.
  114. */
  115. public Maker findFOMaker(String namespaceURI, String localName, Locator locator)
  116. throws FOPException {
  117. Map table = (Map)fobjTable.get(namespaceURI);
  118. Maker fobjMaker = null;
  119. if (table != null) {
  120. fobjMaker = (ElementMapping.Maker)table.get(localName);
  121. // try default
  122. if (fobjMaker == null) {
  123. fobjMaker = (ElementMapping.Maker)table.get(ElementMapping.DEFAULT);
  124. }
  125. }
  126. if (fobjMaker == null) {
  127. if (namespaces.containsKey(namespaceURI.intern())) {
  128. throw new FOPException(FONode.errorText(locator)
  129. + "No element mapping definition found for "
  130. + FONode.getNodeString(namespaceURI, localName), locator);
  131. } else {
  132. log.warn("Unknown formatting object " + namespaceURI + "^" + localName);
  133. fobjMaker = new UnknownXMLObj.Maker(namespaceURI);
  134. }
  135. }
  136. return fobjMaker;
  137. }
  138. /**
  139. * Tries to determine the DOMImplementation that is used to handled a particular namespace.
  140. * The method may return null for namespaces that don't result in a DOM. It is mostly used
  141. * in namespaces occurring in foreign objects.
  142. * @param namespaceURI the namespace URI
  143. * @return the handling DOMImplementation, or null if not applicable
  144. */
  145. public DOMImplementation getDOMImplementationForNamespace(String namespaceURI) {
  146. ElementMapping mapping = (ElementMapping)this.namespaces.get(namespaceURI);
  147. return mapping.getDOMImplementation();
  148. }
  149. }