001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml.dom;
018:
019: import org.apache.avalon.framework.logger.LogEnabled;
020: import org.apache.avalon.framework.logger.Logger;
021: import org.apache.avalon.excalibur.pool.Recyclable;
022:
023: import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
024: import org.apache.batik.dom.svg.SVGDOMImplementation;
025: import org.apache.batik.dom.svg.SVGOMDocument;
026:
027: import org.apache.cocoon.xml.XMLConsumer;
028:
029: import org.w3c.dom.Document;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.Locator;
032:
033: import java.net.MalformedURLException;
034: import java.net.URL;
035:
036: /**
037: * The <code>SVGBuilder</code> is a utility class that will generate a
038: * SVG-DOM Document from SAX events using Batik's SVGDocumentFactory.
039: *
040: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
041: * @version CVS $Id: SVGBuilder.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class SVGBuilder extends SAXSVGDocumentFactory implements
044: XMLConsumer, LogEnabled, Recyclable {
045: protected Logger log;
046:
047: protected Locator locator;
048:
049: private static final String SAX_PARSER = "org.apache.xerces.parsers.SAXParser";
050:
051: /**
052: * Construct a new instance of this TreeGenerator.
053: */
054: protected SVGBuilder() {
055: super (SAX_PARSER);
056: }
057:
058: /**
059: * Provide component with a logger.
060: *
061: * @param logger the logger
062: */
063: public void enableLogging(Logger logger) {
064: if (this .log == null) {
065: this .log = logger;
066: }
067: }
068:
069: protected Logger getLogger() {
070: return this .log;
071: }
072:
073: /**
074: * Return the newly built Document.
075: */
076: public Document getDocument() {
077: return super .document;
078: }
079:
080: /**
081: * Receive notification of the beginning of a document.
082: *
083: * @exception SAXException If this method was not called appropriately.
084: */
085: public void startDocument() throws SAXException {
086: try {
087: // Create SVG Document
088: String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
089: super .document = implementation.createDocument(
090: namespaceURI, "svg", null);
091: super .startDocument();
092: // Add svg, and SVG_NAMESPACE to SAXDocumentFactory namespace handling.
093: // This ties 'svg' prefix used above to the svg namespace uri.
094: namespaces.put("svg",
095: SVGDOMImplementation.SVG_NAMESPACE_URI);
096: } catch (SAXException se) {
097: throw se;
098: } catch (Exception ex) {
099: if (getLogger().isDebugEnabled()) {
100: getLogger().debug(
101: "Got exception in startDocument, rethrowing",
102: ex);
103: }
104: throw new SAXException("Exception in startDocument", ex);
105: }
106: }
107:
108: public void setDocumentLocator(Locator locator) {
109: this .locator = locator;
110: super .setDocumentLocator(locator);
111: }
112:
113: /**
114: * Receive notification of the end of a document.
115: *
116: * @exception SAXException If this method was not called appropriately.
117: */
118: public void endDocument() throws SAXException {
119: try {
120: super .endDocument();
121:
122: // FIXME: Hack.
123: URL baseURL = null;
124: try {
125: if (this .locator != null) {
126: baseURL = new URL(this .locator.getSystemId());
127: } else {
128: baseURL = new URL("http://localhost/");
129: getLogger()
130: .warn(
131: "setDocumentLocator was not called, will use http://localhost/ as base URI");
132: }
133: ((SVGOMDocument) super .document).setURLObject(baseURL);
134: } catch (MalformedURLException e) {
135: getLogger()
136: .warn(
137: "Unable to set document base URI to "
138: + baseURL
139: + ", will default to http://localhost/",
140: e);
141: ((SVGOMDocument) super .document).setURLObject(new URL(
142: "http://localhost/"));
143: }
144: notify(super .document);
145: } catch (SAXException se) {
146: throw se;
147: } catch (Exception ex) {
148: if (getLogger().isDebugEnabled()) {
149: getLogger().debug(
150: "Got exception in endDocument, rethrowing", ex);
151: }
152: throw new SAXException("Exception in endDocument", ex);
153: }
154: }
155:
156: /**
157: * Receive notification of a successfully completed DOM tree generation.
158: */
159: protected void notify(Document doc) throws SAXException {
160: }
161:
162: public void recycle() {
163: locator = null;
164: }
165: }
|