KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > dom > SVGBuilder


1 /*
2  * Copyright 1999-2004 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 package org.apache.cocoon.xml.dom;
17
18 import org.apache.avalon.framework.logger.LogEnabled;
19 import org.apache.avalon.framework.logger.Logger;
20 import org.apache.avalon.excalibur.pool.Recyclable;
21
22 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
23 import org.apache.batik.dom.svg.SVGDOMImplementation;
24 import org.apache.batik.dom.svg.SVGOMDocument;
25
26 import org.apache.cocoon.xml.XMLConsumer;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.Locator JavaDoc;
31
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34
35 /**
36  * The <code>SVGBuilder</code> is a utility class that will generate a
37  * SVG-DOM Document from SAX events using Batik's SVGDocumentFactory.
38  *
39  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
40  * @version CVS $Id: SVGBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class SVGBuilder extends SAXSVGDocumentFactory implements XMLConsumer, LogEnabled, Recyclable {
43     protected Logger log;
44
45     protected Locator JavaDoc locator;
46
47     private static final String JavaDoc SAX_PARSER
48         = "org.apache.xerces.parsers.SAXParser";
49
50     /**
51      * Construct a new instance of this TreeGenerator.
52      */

53     protected SVGBuilder() {
54         super(SAX_PARSER);
55     }
56
57     /**
58      * Provide component with a logger.
59      *
60      * @param logger the logger
61      */

62     public void enableLogging(Logger logger) {
63         if (this.log == null) {
64             this.log = logger;
65         }
66     }
67
68     protected Logger getLogger() {
69         return this.log;
70     }
71
72     /**
73      * Return the newly built Document.
74      */

75     public Document getDocument() {
76         return super.document;
77     }
78
79     /**
80      * Receive notification of the beginning of a document.
81      *
82      * @exception SAXException If this method was not called appropriately.
83      */

84     public void startDocument() throws SAXException JavaDoc {
85         try {
86             // Create SVG Document
87
String JavaDoc namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
88             super.document = implementation.createDocument(namespaceURI, "svg", null);
89             super.startDocument();
90             // Add svg, and SVG_NAMESPACE to SAXDocumentFactory namespace handling.
91
// This ties 'svg' prefix used above to the svg namespace uri.
92
namespaces.put("svg", SVGDOMImplementation.SVG_NAMESPACE_URI);
93         } catch (SAXException JavaDoc se) {
94             throw se;
95         } catch (Exception JavaDoc ex){
96             if (getLogger().isDebugEnabled()) {
97                 getLogger().debug("Got exception in startDocument, rethrowing", ex);
98             }
99             throw new SAXException JavaDoc("Exception in startDocument", ex);
100         }
101     }
102
103     public void setDocumentLocator(Locator JavaDoc locator) {
104         this.locator = locator;
105         super.setDocumentLocator(locator);
106     }
107
108     /**
109      * Receive notification of the end of a document.
110      *
111      * @exception SAXException If this method was not called appropriately.
112      */

113     public void endDocument() throws SAXException JavaDoc {
114         try {
115             super.endDocument();
116
117             // FIXME: Hack.
118
URL JavaDoc baseURL = null;
119             try {
120                 if (this.locator != null) {
121                     baseURL = new URL JavaDoc(this.locator.getSystemId());
122                 } else {
123                     baseURL = new URL JavaDoc("http://localhost/");
124                     getLogger().warn("setDocumentLocator was not called, will use http://localhost/ as base URI");
125                 }
126                 ((SVGOMDocument)super.document).setURLObject(baseURL);
127             } catch (MalformedURLException JavaDoc e) {
128                 getLogger().warn("Unable to set document base URI to " + baseURL + ", will default to http://localhost/", e);
129                 ((SVGOMDocument)super.document).setURLObject(new URL JavaDoc("http://localhost/"));
130             }
131             notify(super.document);
132         } catch (SAXException JavaDoc se) {
133             throw se;
134         } catch (Exception JavaDoc ex){
135             if (getLogger().isDebugEnabled()) {
136                 getLogger().debug("Got exception in endDocument, rethrowing", ex);
137             }
138             throw new SAXException JavaDoc("Exception in endDocument", ex);
139         }
140     }
141
142     /**
143      * Receive notification of a successfully completed DOM tree generation.
144      */

145     protected void notify(Document doc) throws SAXException JavaDoc {
146     }
147
148     public void recycle() {
149         locator = null;
150     }
151 }
152
Popular Tags