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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  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);
  60. if (providers != null) {
  61. while (providers.hasNext()) {
  62. String str = (String)providers.next();
  63. try {
  64. addElementMapping(str);
  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 = (ElementMapping.Maker)table.get(localName);
  118. // try default
  119. if (fobjMaker == null) {
  120. fobjMaker = (ElementMapping.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. log.warn("Unknown formatting object " + namespaceURI + "^" + localName);
  130. fobjMaker = new UnknownXMLObj.Maker(namespaceURI);
  131. }
  132. }
  133. return fobjMaker;
  134. }
  135. /**
  136. * Tries to determine the DOMImplementation that is used to handled a particular namespace.
  137. * The method may return null for namespaces that don't result in a DOM. It is mostly used
  138. * in namespaces occurring in foreign objects.
  139. * @param namespaceURI the namespace URI
  140. * @return the handling DOMImplementation, or null if not applicable
  141. */
  142. public DOMImplementation getDOMImplementationForNamespace(String namespaceURI) {
  143. ElementMapping mapping = (ElementMapping)this.namespaces.get(namespaceURI);
  144. return mapping.getDOMImplementation();
  145. }
  146. }