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

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