1 17 18 19 20 package org.apache.fop.fo; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.w3c.dom.DOMImplementation ; 26 import org.xml.sax.Locator ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.apache.xmlgraphics.util.Service; 32 33 import org.apache.fop.apps.FOPException; 34 import org.apache.fop.apps.FopFactory; 35 import org.apache.fop.fo.ElementMapping.Maker; 36 37 41 public class ElementMappingRegistry { 42 43 44 protected Log log = LogFactory.getLog(ElementMappingRegistry.class); 45 46 50 protected Map fobjTable = new java.util.HashMap (); 51 52 55 protected Map namespaces = new java.util.HashMap (); 56 57 62 public ElementMappingRegistry(FopFactory factory) { 63 setupDefaultMappings(); 65 } 66 67 70 private void setupDefaultMappings() { 71 Iterator providers = Service.providers(ElementMapping.class, false); 73 if (providers != null) { 74 while (providers.hasNext()) { 75 String mapping = (String )providers.next(); 76 try { 77 addElementMapping(mapping); 78 } catch (IllegalArgumentException e) { 79 log.warn("Error while adding element mapping", e); 80 } 81 82 } 83 } 84 } 85 86 91 public void addElementMapping(String mappingClassName) 92 throws IllegalArgumentException { 93 94 try { 95 ElementMapping mapping 96 = (ElementMapping)Class.forName(mappingClassName).newInstance(); 97 addElementMapping(mapping); 98 } catch (ClassNotFoundException e) { 99 throw new IllegalArgumentException ("Could not find " 100 + mappingClassName); 101 } catch (InstantiationException e) { 102 throw new IllegalArgumentException ("Could not instantiate " 103 + mappingClassName); 104 } catch (IllegalAccessException e) { 105 throw new IllegalArgumentException ("Could not access " 106 + mappingClassName); 107 } catch (ClassCastException e) { 108 throw new IllegalArgumentException (mappingClassName 109 + " is not an ElementMapping"); 110 } 111 } 112 113 117 public void addElementMapping(ElementMapping mapping) { 118 this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable()); 119 this.namespaces.put(mapping.getNamespaceURI().intern(), mapping); 120 } 121 122 130 public Maker findFOMaker(String namespaceURI, String localName, Locator locator) 131 throws FOPException { 132 Map table = (Map )fobjTable.get(namespaceURI); 133 Maker fobjMaker = null; 134 if (table != null) { 135 fobjMaker = (ElementMapping.Maker)table.get(localName); 136 if (fobjMaker == null) { 138 fobjMaker = (ElementMapping.Maker)table.get(ElementMapping.DEFAULT); 139 } 140 } 141 142 if (fobjMaker == null) { 143 if (namespaces.containsKey(namespaceURI.intern())) { 144 throw new FOPException(FONode.errorText(locator) 145 + "No element mapping definition found for " 146 + FONode.getNodeString(namespaceURI, localName), locator); 147 } else { 148 log.warn("Unknown formatting object " + namespaceURI + "^" + localName); 149 fobjMaker = new UnknownXMLObj.Maker(namespaceURI); 150 } 151 } 152 return fobjMaker; 153 } 154 155 162 public DOMImplementation getDOMImplementationForNamespace(String namespaceURI) { 163 ElementMapping mapping = (ElementMapping)this.namespaces.get(namespaceURI); 164 if (mapping == null) { 165 return null; 166 } else { 167 return mapping.getDOMImplementation(); 168 } 169 } 170 171 177 public ElementMapping getElementMapping(String namespaceURI) { 178 return (ElementMapping)this.namespaces.get(namespaceURI); 179 } 180 181 186 public boolean isKnownNamespace(String namespaceURI) { 187 return this.namespaces.containsKey(namespaceURI); 188 } 189 } 190 | Popular Tags |