KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > SVGOMDocument


1 /*
2
3    Copyright 2000-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.svg;
19
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.MissingResourceException JavaDoc;
25
26 import org.apache.batik.css.engine.CSSEngine;
27 import org.apache.batik.dom.AbstractStylableDocument;
28 import org.apache.batik.dom.GenericAttr;
29 import org.apache.batik.dom.GenericAttrNS;
30 import org.apache.batik.dom.GenericCDATASection;
31 import org.apache.batik.dom.GenericComment;
32 import org.apache.batik.dom.GenericDocumentFragment;
33 import org.apache.batik.dom.GenericDocumentType;
34 import org.apache.batik.dom.GenericElement;
35 import org.apache.batik.dom.GenericEntityReference;
36 import org.apache.batik.dom.GenericProcessingInstruction;
37 import org.apache.batik.dom.GenericText;
38 import org.apache.batik.dom.StyleSheetFactory;
39 import org.apache.batik.dom.util.XMLSupport;
40 import org.apache.batik.i18n.Localizable;
41 import org.apache.batik.i18n.LocalizableSupport;
42 import org.apache.batik.util.SVGConstants;
43
44 import org.w3c.dom.Attr JavaDoc;
45 import org.w3c.dom.CDATASection JavaDoc;
46 import org.w3c.dom.Comment JavaDoc;
47 import org.w3c.dom.DOMException JavaDoc;
48 import org.w3c.dom.DOMImplementation JavaDoc;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.DocumentFragment JavaDoc;
51 import org.w3c.dom.DocumentType JavaDoc;
52 import org.w3c.dom.Element JavaDoc;
53 import org.w3c.dom.EntityReference JavaDoc;
54 import org.w3c.dom.Node JavaDoc;
55 import org.w3c.dom.ProcessingInstruction JavaDoc;
56 import org.w3c.dom.Text JavaDoc;
57 import org.w3c.dom.svg.SVGDocument;
58 import org.w3c.dom.svg.SVGLangSpace;
59 import org.w3c.dom.svg.SVGSVGElement;
60
61 /**
62  * This class implements {@link SVGDocument}.
63  *
64  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
65  * @version $Id: SVGOMDocument.java,v 1.58 2005/03/27 08:58:32 cam Exp $
66  */

67 public class SVGOMDocument
68     extends AbstractStylableDocument
69     implements SVGDocument,
70                SVGConstants {
71     
72     /**
73      * The error messages bundle class name.
74      */

75     protected final static String JavaDoc RESOURCES =
76         "org.apache.batik.dom.svg.resources.Messages";
77
78     /**
79      * The localizable support for the error messages.
80      */

81     protected transient LocalizableSupport localizableSupport =
82         new LocalizableSupport(RESOURCES, getClass().getClassLoader());
83
84     /**
85      * The string representing the referrer.
86      */

87     protected String JavaDoc referrer = "";
88
89     /**
90      * The url of the document.
91      */

92     protected URL JavaDoc url;
93
94     /**
95      * Is this document immutable?
96      */

97     protected transient boolean readonly;
98
99     /**
100      * Creates a new uninitialized document.
101      */

102     protected SVGOMDocument() {
103     }
104
105     /**
106      * Creates a new document.
107      */

108     public SVGOMDocument(DocumentType dt, DOMImplementation JavaDoc impl) {
109         super(dt, impl);
110     }
111
112     /**
113      * Implements {@link Localizable#setLocale(Locale)}.
114      */

115     public void setLocale(Locale JavaDoc l) {
116         super.setLocale(l);
117         localizableSupport.setLocale(l);
118     }
119
120     /**
121      * Implements {@link Localizable#formatMessage(String,Object[])}.
122      */

123     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args)
124         throws MissingResourceException JavaDoc {
125         try {
126             return super.formatMessage(key, args);
127         } catch (MissingResourceException JavaDoc e) {
128             return localizableSupport.formatMessage(key, args);
129         }
130     }
131
132     /**
133      * <b>DOM</b>: Implements {@link SVGDocument#getTitle()}.
134      */

135     public String JavaDoc getTitle() {
136         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
137         boolean preserve = false;
138
139         for (Node JavaDoc n = getDocumentElement().getFirstChild();
140              n != null;
141              n = n.getNextSibling()) {
142             String JavaDoc ns = n.getNamespaceURI();
143             if (ns != null && ns.equals(SVG_NAMESPACE_URI)) {
144                 if (n.getLocalName().equals(SVG_TITLE_TAG)) {
145                     preserve = ((SVGLangSpace)n).getXMLspace().equals("preserve");
146                     for (n = n.getFirstChild();
147                          n != null;
148                          n = n.getNextSibling()) {
149                         if (n.getNodeType() == Node.TEXT_NODE) {
150                             sb.append(n.getNodeValue());
151                         }
152                     }
153                     break;
154                 }
155             }
156         }
157
158         String JavaDoc s = sb.toString();
159         return (preserve)
160             ? XMLSupport.preserveXMLSpace(s)
161             : XMLSupport.defaultXMLSpace(s);
162     }
163
164     /**
165      * <b>DOM</b>: Implements {@link SVGDocument#getReferrer()}.
166      */

167     public String JavaDoc getReferrer() {
168         return referrer;
169     }
170
171     /**
172      * Sets the referrer string.
173      */

174     public void setReferrer(String JavaDoc s) {
175         referrer = s;
176     }
177
178     /**
179      * <b>DOM</b>: Implements {@link SVGDocument#getDomain()}.
180      */

181     public String JavaDoc getDomain() {
182         return (url == null) ? null : url.getHost();
183     }
184
185     /**
186      * <b>DOM</b>: Implements {@link SVGDocument#getRootElement()}.
187      */

188     public SVGSVGElement getRootElement() {
189         return (SVGSVGElement)getDocumentElement();
190     }
191
192     /**
193      * <b>DOM</b>: Implements {@link SVGDocument#getURL()}
194      */

195     public String JavaDoc getURL() {
196         return (url == null) ? null : url.toString();
197     }
198
199     /**
200      * Returns the URI of the document.
201      */

202     public URL JavaDoc getURLObject() {
203         return url;
204     }
205
206     /**
207      * Sets the URI of the document.
208      */

209     public void setURLObject(URL JavaDoc url) {
210         this.url = url;
211     }
212
213     /**
214      * <b>DOM</b>: Implements {@link Document#createElement(String)}.
215      */

216     public Element createElement(String JavaDoc tagName) throws DOMException JavaDoc {
217         return new GenericElement(tagName.intern(), this);
218     }
219
220     /**
221      * <b>DOM</b>: Implements {@link Document#createDocumentFragment()}.
222      */

223     public DocumentFragment createDocumentFragment() {
224         return new GenericDocumentFragment(this);
225     }
226
227     /**
228      * <b>DOM</b>: Implements {@link Document#createTextNode(String)}.
229      */

230     public Text createTextNode(String JavaDoc data) {
231         return new GenericText(data, this);
232     }
233
234     /**
235      * <b>DOM</b>: Implements {@link Document#createComment(String)}.
236      */

237     public Comment createComment(String JavaDoc data) {
238         return new GenericComment(data, this);
239     }
240
241     /**
242      * <b>DOM</b>: Implements {@link Document#createCDATASection(String)}
243      */

244     public CDATASection createCDATASection(String JavaDoc data) throws DOMException JavaDoc {
245         return new GenericCDATASection(data, this);
246     }
247
248     /**
249      * <b>DOM</b>: Implements {@link
250      * Document#createProcessingInstruction(String,String)}.
251      * @return a SVGStyleSheetProcessingInstruction if target is
252      * "xml-stylesheet" or a GenericProcessingInstruction otherwise.
253      */

254     public ProcessingInstruction createProcessingInstruction(String JavaDoc target,
255                                                              String JavaDoc data)
256         throws DOMException JavaDoc {
257         if ("xml-stylesheet".equals(target)) {
258             return new SVGStyleSheetProcessingInstruction
259                 (data, this, (StyleSheetFactory)getImplementation());
260         }
261         return new GenericProcessingInstruction(target, data, this);
262     }
263
264     /**
265      * <b>DOM</b>: Implements {@link Document#createAttribute(String)}.
266      */

267     public Attr createAttribute(String JavaDoc name) throws DOMException JavaDoc {
268         return new GenericAttr(name.intern(), this);
269     }
270
271     /**
272      * <b>DOM</b>: Implements {@link Document#createEntityReference(String)}.
273      */

274     public EntityReference createEntityReference(String JavaDoc name)
275         throws DOMException JavaDoc {
276         return new GenericEntityReference(name, this);
277     }
278
279     /**
280      * <b>DOM</b>: Implements {@link Document#createAttributeNS(String,String)}.
281      */

282     public Attr createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
283         throws DOMException JavaDoc {
284         if (namespaceURI == null) {
285             return new GenericAttr(qualifiedName.intern(), this);
286         } else {
287             return new GenericAttrNS(namespaceURI.intern(),
288                                      qualifiedName.intern(),
289                                      this);
290         }
291     }
292
293     /**
294      * <b>DOM</b>: Implements {@link Document#createElementNS(String,String)}.
295      */

296     public Element createElementNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
297         throws DOMException JavaDoc {
298         SVGDOMImplementation impl = (SVGDOMImplementation)implementation;
299         return impl.createElementNS(this, namespaceURI, qualifiedName);
300     }
301
302     /**
303      * Returns true if the given Attr node represents an 'id'
304      * for this document.
305      */

306     public boolean isId(Attr node) {
307         if (node.getNamespaceURI() != null) return false;
308         return SVG_ID_ATTRIBUTE.equals(node.getNodeName());
309     }
310
311     // AbstractDocument ///////////////////////////////////////////////
312

313     /**
314      * Tests whether this node is readonly.
315      */

316     public boolean isReadonly() {
317         return readonly;
318     }
319
320     /**
321      * Sets this node readonly attribute.
322      */

323     public void setReadonly(boolean v) {
324         readonly = v;
325     }
326
327     /**
328      * Returns a new uninitialized instance of this object's class.
329      */

330     protected Node JavaDoc newNode() {
331         return new SVGOMDocument();
332     }
333
334     /**
335      * Copy the fields of the current node into the given node.
336      * @param n a node of the type of this.
337      */

338     protected Node JavaDoc copyInto(Node JavaDoc n) {
339     super.copyInto(n);
340     SVGOMDocument sd = (SVGOMDocument)n;
341         sd.localizableSupport = new LocalizableSupport
342             (RESOURCES, getClass().getClassLoader());
343         sd.referrer = referrer;
344         sd.url = url;
345     return n;
346     }
347
348     /**
349      * Deeply copy the fields of the current node into the given node.
350      * @param n a node of the type of this.
351      */

352     protected Node JavaDoc deepCopyInto(Node JavaDoc n) {
353     super.deepCopyInto(n);
354         SVGOMDocument sd = (SVGOMDocument)n;
355         sd.localizableSupport = new LocalizableSupport
356             (RESOURCES, getClass().getClassLoader());
357         sd.referrer = referrer;
358         sd.url = url;
359     return n;
360     }
361
362     // Serialization //////////////////////////////////////////////////////
363

364     /**
365      * Reads the object from the given stream.
366      */

367     private void readObject(ObjectInputStream JavaDoc s)
368         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
369         s.defaultReadObject();
370         
371         localizableSupport = new LocalizableSupport
372             (RESOURCES, getClass().getClassLoader());
373     }
374 }
375
Popular Tags