1 18 package org.apache.batik.dom; 19 20 import java.net.URL ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 import java.util.Locale ; 26 import java.util.MissingResourceException ; 27 28 import org.apache.batik.css.engine.CSSContext; 29 import org.apache.batik.css.engine.CSSEngine; 30 import org.apache.batik.css.engine.value.ShorthandManager; 31 import org.apache.batik.css.engine.value.ValueManager; 32 import org.apache.batik.css.parser.ExtendedParser; 33 import org.apache.batik.css.parser.ExtendedParserWrapper; 34 import org.apache.batik.dom.AbstractDocument; 35 import org.apache.batik.dom.GenericElement; 36 import org.apache.batik.dom.GenericElementNS; 37 import org.apache.batik.dom.util.DOMUtilities; 38 import org.apache.batik.dom.util.DoublyIndexedTable; 39 import org.apache.batik.i18n.Localizable; 40 import org.apache.batik.i18n.LocalizableSupport; 41 import org.apache.batik.util.Service; 42 import org.apache.batik.util.XMLResourceDescriptor; 43 44 import org.w3c.css.sac.InputSource; 45 import org.w3c.css.sac.Parser; 46 import org.w3c.dom.css.DOMImplementationCSS; 47 import org.w3c.dom.css.ViewCSS; 48 import org.w3c.dom.Document ; 49 import org.w3c.dom.DOMException ; 50 import org.w3c.dom.DOMImplementation ; 51 import org.w3c.dom.Element ; 52 53 62 public abstract class ExtensibleDOMImplementation 63 extends AbstractDOMImplementation 64 implements DOMImplementationCSS, 65 StyleSheetFactory, 66 Localizable { 67 68 71 protected DoublyIndexedTable customFactories; 72 73 76 protected List customValueManagers; 77 78 81 protected List customShorthandManagers; 82 83 86 protected final static String RESOURCES = 87 "org.apache.batik.dom.resources.Messages"; 88 89 92 protected LocalizableSupport localizableSupport; 93 94 97 public ExtensibleDOMImplementation() { 98 initLocalizable(); 99 100 Iterator iter = getDomExtensions().iterator(); 101 102 while(iter.hasNext()) { 103 DomExtension de = (DomExtension)iter.next(); 104 de.registerTags(this); 105 } 106 } 107 108 110 113 public void setLocale(Locale l) { 114 localizableSupport.setLocale(l); 115 } 116 117 120 public Locale getLocale() { 121 return localizableSupport.getLocale(); 122 } 123 124 protected void initLocalizable() { 125 localizableSupport = 126 new LocalizableSupport(RESOURCES, getClass().getClassLoader()); 127 } 128 129 132 public String formatMessage(String key, Object [] args) 133 throws MissingResourceException { 134 return localizableSupport.formatMessage(key, args); 135 } 136 137 140 public void registerCustomElementFactory(String namespaceURI, 141 String localName, 142 ElementFactory factory) { 143 if (customFactories == null) { 144 customFactories = new DoublyIndexedTable(); 145 } 146 customFactories.put(namespaceURI, localName, factory); 147 } 148 149 152 public void registerCustomCSSValueManager(ValueManager vm) { 153 if (customValueManagers == null) { 154 customValueManagers = new LinkedList (); 155 } 156 customValueManagers.add(vm); 157 } 158 159 162 public void registerCustomCSSShorthandManager(ShorthandManager sm) { 163 if (customShorthandManagers == null) { 164 customShorthandManagers = new LinkedList (); 165 } 166 customShorthandManagers.add(sm); 167 } 168 169 172 public CSSEngine createCSSEngine(AbstractStylableDocument doc, 173 CSSContext ctx) { 174 String pn = XMLResourceDescriptor.getCSSParserClassName(); 175 Parser p; 176 try { 177 p = (Parser)Class.forName(pn).newInstance(); 178 } catch (ClassNotFoundException e) { 179 throw new DOMException (DOMException.INVALID_ACCESS_ERR, 180 formatMessage("css.parser.class", 181 new Object [] { pn })); 182 } catch (InstantiationException e) { 183 throw new DOMException (DOMException.INVALID_ACCESS_ERR, 184 formatMessage("css.parser.creation", 185 new Object [] { pn })); 186 } catch (IllegalAccessException e) { 187 throw new DOMException (DOMException.INVALID_ACCESS_ERR, 188 formatMessage("css.parser.access", 189 new Object [] { pn })); 190 } 191 ExtendedParser ep = ExtendedParserWrapper.wrap(p); 192 193 ValueManager[] vms; 194 if (customValueManagers == null) { 195 vms = new ValueManager[0]; 196 } else { 197 vms = new ValueManager[customValueManagers.size()]; 198 Iterator it = customValueManagers.iterator(); 199 int i = 0; 200 while (it.hasNext()) { 201 vms[i++] = (ValueManager)it.next(); 202 } 203 } 204 205 ShorthandManager[] sms; 206 if (customShorthandManagers == null) { 207 sms = new ShorthandManager[0]; 208 } else { 209 sms = new ShorthandManager[customShorthandManagers.size()]; 210 Iterator it = customShorthandManagers.iterator(); 211 int i = 0; 212 while (it.hasNext()) { 213 sms[i++] = (ShorthandManager)it.next(); 214 } 215 } 216 217 CSSEngine result = createCSSEngine(doc, ctx, ep, vms, sms); 218 doc.setCSSEngine(result); 219 return result; 220 } 221 222 public abstract CSSEngine createCSSEngine(AbstractStylableDocument doc, 223 CSSContext ctx, 224 ExtendedParser ep, 225 ValueManager [] vms, 226 ShorthandManager [] sms); 227 228 231 public abstract ViewCSS createViewCSS(AbstractStylableDocument doc); 232 233 237 public Element createElementNS(AbstractDocument document, 238 String namespaceURI, 239 String qualifiedName) { 240 if (namespaceURI == null) 241 return new GenericElement(qualifiedName.intern(), document); 242 243 if (customFactories != null) { 244 String name = DOMUtilities.getLocalName(qualifiedName); 245 ElementFactory cef; 246 cef = (ElementFactory)customFactories.get(namespaceURI, name); 247 if (cef != null) { 248 return cef.create(DOMUtilities.getPrefix(qualifiedName), 249 document); 250 } 251 } 252 return new GenericElementNS(namespaceURI.intern(), 253 qualifiedName.intern(), 254 document); 255 } 256 257 259 262 public interface ElementFactory { 263 266 Element create(String prefix, Document doc); 267 } 268 269 271 protected static List extensions = null; 272 273 protected synchronized static List getDomExtensions() { 274 if (extensions != null) 275 return extensions; 276 277 extensions = new LinkedList (); 278 279 Iterator iter = Service.providers(DomExtension.class); 280 281 while (iter.hasNext()) { 282 DomExtension de = (DomExtension)iter.next(); 283 float priority = de.getPriority(); 284 ListIterator li = extensions.listIterator(); 285 for (;;) { 286 if (!li.hasNext()) { 287 li.add(de); 288 break; 289 } 290 DomExtension lde = (DomExtension)li.next(); 291 if (lde.getPriority() > priority) { 292 li.previous(); 293 li.add(de); 294 break; 295 } 296 } 297 } 298 299 return extensions; 300 } 301 302 } 303 | Popular Tags |