Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ElementMappingRegistry.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.FOUserAgent;
  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 userAgent the user agent
  49. */
  50. public ElementMappingRegistry(FOUserAgent userAgent) {
  51. // Add standard element mappings
  52. setupDefaultMappings();
  53. // add additional ElementMappings defined within FOUserAgent
  54. List addlEMs = userAgent.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. addElementMapping("org.apache.fop.fo.FOElementMapping");
  66. addElementMapping("org.apache.fop.fo.extensions.svg.SVGElementMapping");
  67. addElementMapping("org.apache.fop.fo.extensions.svg.BatikExtensionElementMapping");
  68. addElementMapping("org.apache.fop.fo.extensions.ExtensionElementMapping");
  69. addElementMapping("org.apache.fop.render.ps.extensions.PSExtensionElementMapping");
  70. // add mappings from available services
  71. Iterator providers = Service.providers(ElementMapping.class);
  72. if (providers != null) {
  73. while (providers.hasNext()) {
  74. String str = (String)providers.next();
  75. try {
  76. addElementMapping(str);
  77. } catch (IllegalArgumentException e) {
  78. log.warn("Error while adding element mapping", e);
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Add the element mapping with the given class name.
  85. * @param mappingClassName the class name representing the element mapping.
  86. * @throws IllegalArgumentException if there was not such element mapping.
  87. */
  88. public void addElementMapping(String mappingClassName)
  89. throws IllegalArgumentException {
  90. try {
  91. ElementMapping mapping
  92. = (ElementMapping)Class.forName(mappingClassName).newInstance();
  93. addElementMapping(mapping);
  94. } catch (ClassNotFoundException e) {
  95. throw new IllegalArgumentException("Could not find "
  96. + mappingClassName);
  97. } catch (InstantiationException e) {
  98. throw new IllegalArgumentException("Could not instantiate "
  99. + mappingClassName);
  100. } catch (IllegalAccessException e) {
  101. throw new IllegalArgumentException("Could not access "
  102. + mappingClassName);
  103. } catch (ClassCastException e) {
  104. throw new IllegalArgumentException(mappingClassName
  105. + " is not an ElementMapping");
  106. }
  107. }
  108. private void addElementMapping(ElementMapping mapping) {
  109. this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable());
  110. this.namespaces.put(mapping.getNamespaceURI().intern(), mapping);
  111. }
  112. /**
  113. * Finds the Maker used to create node objects of a particular type
  114. * @param namespaceURI URI for the namespace of the element
  115. * @param localName name of the Element
  116. * @param locator the Locator instance for context information
  117. * @return the ElementMapping.Maker that can create an FO object for this element
  118. * @throws FOPException if a Maker could not be found for a bound namespace.
  119. */
  120. public Maker findFOMaker(String namespaceURI, String localName, Locator locator)
  121. throws FOPException {
  122. Map table = (Map)fobjTable.get(namespaceURI);
  123. Maker fobjMaker = null;
  124. if (table != null) {
  125. fobjMaker = (ElementMapping.Maker)table.get(localName);
  126. // try default
  127. if (fobjMaker == null) {
  128. fobjMaker = (ElementMapping.Maker)table.get(ElementMapping.DEFAULT);
  129. }
  130. }
  131. if (fobjMaker == null) {
  132. if (namespaces.containsKey(namespaceURI.intern())) {
  133. throw new FOPException(FONode.errorText(locator)
  134. + "No element mapping definition found for "
  135. + FONode.getNodeString(namespaceURI, localName), locator);
  136. } else {
  137. log.warn("Unknown formatting object " + namespaceURI + "^" + localName);
  138. fobjMaker = new UnknownXMLObj.Maker(namespaceURI);
  139. }
  140. }
  141. return fobjMaker;
  142. }
  143. /**
  144. * Tries to determine the DOMImplementation that is used to handled a particular namespace.
  145. * The method may return null for namespaces that don't result in a DOM. It is mostly used
  146. * in namespaces occurring in foreign objects.
  147. * @param namespaceURI the namespace URI
  148. * @return the handling DOMImplementation, or null if not applicable
  149. */
  150. public DOMImplementation getDOMImplementationForNamespace(String namespaceURI) {
  151. ElementMapping mapping = (ElementMapping)this.namespaces.get(namespaceURI);
  152. return mapping.getDOMImplementation();
  153. }
  154. }