Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ElementMappingRegistry.java 7.0KB

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