KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > ExtensibleDOMImplementation


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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  */

18 package org.apache.batik.dom;
19
20 import java.net.URL JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.MissingResourceException JavaDoc;
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 JavaDoc;
49 import org.w3c.dom.DOMException JavaDoc;
50 import org.w3c.dom.DOMImplementation JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52
53 /**
54  * This class implements the {@link org.w3c.dom.DOMImplementation} interface.
55  * It allows the user to extend the set of elements supported by a
56  * Document, directly or through the Service API (see
57  * {@link org.apache.batik.util.Service}).
58  *
59  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
60  * @version $Id: ExtensibleDOMImplementation.java,v 1.1 2004/11/18 01:46:56 deweese Exp $
61  */

62 public abstract class ExtensibleDOMImplementation
63     extends AbstractDOMImplementation
64     implements DOMImplementationCSS,
65                StyleSheetFactory,
66                Localizable {
67     
68     /**
69      * The custom elements factories.
70      */

71     protected DoublyIndexedTable customFactories;
72
73     /**
74      * The custom value managers.
75      */

76     protected List JavaDoc customValueManagers;
77
78     /**
79      * The custom shorthand value managers.
80      */

81     protected List JavaDoc customShorthandManagers;
82
83     /**
84      * The error messages bundle class name.
85      */

86     protected final static String JavaDoc RESOURCES =
87         "org.apache.batik.dom.resources.Messages";
88
89     /**
90      * The localizable support for the error messages.
91      */

92     protected LocalizableSupport localizableSupport;
93
94     /**
95      * Creates a new DOMImplementation.
96      */

97     public ExtensibleDOMImplementation() {
98         initLocalizable();
99
100         Iterator JavaDoc iter = getDomExtensions().iterator();
101
102         while(iter.hasNext()) {
103             DomExtension de = (DomExtension)iter.next();
104             de.registerTags(this);
105         }
106     }
107
108     // Localizable //////////////////////////////////////////////////////
109

110     /**
111      * Implements {@link Localizable#setLocale(Locale)}.
112      */

113     public void setLocale(Locale JavaDoc l) {
114     localizableSupport.setLocale(l);
115     }
116
117     /**
118      * Implements {@link Localizable#getLocale()}.
119      */

120     public Locale JavaDoc getLocale() {
121         return localizableSupport.getLocale();
122     }
123
124     protected void initLocalizable() {
125         localizableSupport =
126             new LocalizableSupport(RESOURCES, getClass().getClassLoader());
127     }
128
129     /**
130      * Implements {@link Localizable#formatMessage(String,Object[])}.
131      */

132     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args)
133         throws MissingResourceException JavaDoc {
134         return localizableSupport.formatMessage(key, args);
135     }
136
137     /**
138      * Allows the user to register a new element factory.
139      */

140     public void registerCustomElementFactory(String JavaDoc namespaceURI,
141                                              String JavaDoc localName,
142                                              ElementFactory factory) {
143         if (customFactories == null) {
144             customFactories = new DoublyIndexedTable();
145         }
146         customFactories.put(namespaceURI, localName, factory);
147     }
148
149     /**
150      * Allows the user to register a new CSS value manager.
151      */

152     public void registerCustomCSSValueManager(ValueManager vm) {
153         if (customValueManagers == null) {
154             customValueManagers = new LinkedList JavaDoc();
155         }
156         customValueManagers.add(vm);
157     }
158
159     /**
160      * Allows the user to register a new shorthand CSS value manager.
161      */

162     public void registerCustomCSSShorthandManager(ShorthandManager sm) {
163         if (customShorthandManagers == null) {
164             customShorthandManagers = new LinkedList JavaDoc();
165         }
166         customShorthandManagers.add(sm);
167     }
168
169     /**
170      * Creates new CSSEngine and attach it to the document.
171      */

172     public CSSEngine createCSSEngine(AbstractStylableDocument doc,
173                                      CSSContext ctx) {
174         String JavaDoc pn = XMLResourceDescriptor.getCSSParserClassName();
175         Parser p;
176         try {
177             p = (Parser)Class.forName(pn).newInstance();
178         } catch (ClassNotFoundException JavaDoc e) {
179             throw new DOMException JavaDoc(DOMException.INVALID_ACCESS_ERR,
180                                    formatMessage("css.parser.class",
181                                                  new Object JavaDoc[] { pn }));
182         } catch (InstantiationException JavaDoc e) {
183             throw new DOMException JavaDoc(DOMException.INVALID_ACCESS_ERR,
184                                    formatMessage("css.parser.creation",
185                                                  new Object JavaDoc[] { pn }));
186         } catch (IllegalAccessException JavaDoc e) {
187             throw new DOMException JavaDoc(DOMException.INVALID_ACCESS_ERR,
188                                    formatMessage("css.parser.access",
189                                                  new Object JavaDoc[] { 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 JavaDoc 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 JavaDoc 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     /**
229      * Creates a ViewCSS.
230      */

231     public abstract ViewCSS createViewCSS(AbstractStylableDocument doc);
232
233     /**
234      * Implements the behavior of Document.createElementNS() for this
235      * DOM implementation.
236      */

237     public Element createElementNS(AbstractDocument document,
238                                    String JavaDoc namespaceURI,
239                                    String JavaDoc qualifiedName) {
240         if (namespaceURI == null)
241             return new GenericElement(qualifiedName.intern(), document);
242
243         if (customFactories != null) {
244             String JavaDoc 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     // The element factories /////////////////////////////////////////////////
258

259     /**
260      * This interface represents a factory for elements.
261      */

262     public interface ElementFactory {
263         /**
264          * Creates an instance of the associated element type.
265          */

266         Element create(String JavaDoc prefix, Document doc);
267     }
268
269     // Service /////////////////////////////////////////////////////////
270

271     protected static List JavaDoc extensions = null;
272
273     protected synchronized static List JavaDoc getDomExtensions() {
274         if (extensions != null)
275             return extensions;
276
277         extensions = new LinkedList JavaDoc();
278
279         Iterator JavaDoc iter = Service.providers(DomExtension.class);
280
281         while (iter.hasNext()) {
282             DomExtension de = (DomExtension)iter.next();
283             float priority = de.getPriority();
284             ListIterator JavaDoc 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