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:
018: /* $Id: SVGElementMapping.java 453919 2006-10-07 14:26:37Z jeremias $ */
019:
020: package org.apache.fop.fo.extensions.svg;
021:
022: import java.util.HashMap;
023: import javax.xml.parsers.SAXParserFactory;
024:
025: import org.apache.fop.fo.FONode;
026: import org.apache.fop.fo.ElementMapping;
027:
028: import org.apache.batik.util.XMLResourceDescriptor;
029: import org.apache.batik.dom.svg.SVGDOMImplementation;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.w3c.dom.DOMImplementation;
033:
034: /**
035: * Setup the SVG element mapping.
036: * This adds the svg element mappings used to create the objects
037: * that create the SVG Document.
038: */
039: public class SVGElementMapping extends ElementMapping {
040:
041: /** the SVG namespace */
042: public static final String URI = SVGDOMImplementation.SVG_NAMESPACE_URI;
043:
044: /** logging instance */
045: protected Log log = LogFactory.getLog(SVGElementMapping.class);
046:
047: private boolean batikAvailable = true;
048:
049: /** Main constructor. */
050: public SVGElementMapping() {
051: namespaceURI = URI;
052: }
053:
054: /** @see org.apache.fop.fo.ElementMapping#getDOMImplementation() */
055: public DOMImplementation getDOMImplementation() {
056: return SVGDOMImplementation.getDOMImplementation();
057: }
058:
059: /**
060: * Returns the fully qualified classname of an XML parser for
061: * Batik classes that apparently need it (error messages, perhaps)
062: * @return an XML parser classname
063: */
064: private String getAParserClassName() {
065: try {
066: SAXParserFactory factory = SAXParserFactory.newInstance();
067: return factory.newSAXParser().getXMLReader().getClass()
068: .getName();
069: } catch (Exception e) {
070: return null;
071: }
072: }
073:
074: /** @see org.apache.fop.fo.ElementMapping#initialize() */
075: protected void initialize() {
076: if (foObjs == null && batikAvailable) {
077: // this sets the parser that will be used
078: // by default (SVGBrokenLinkProvider)
079: // normally the user agent value is used
080: try {
081: XMLResourceDescriptor
082: .setXMLParserClassName(getAParserClassName());
083:
084: foObjs = new HashMap();
085: foObjs.put("svg", new SE());
086: foObjs.put(DEFAULT, new SVGMaker());
087: } catch (Throwable t) {
088: log
089: .error(
090: "Error while initializing the Batik SVG extensions",
091: t);
092: // if the classes are not available
093: // the DISPLAY is not checked
094: batikAvailable = false;
095: }
096: }
097: }
098:
099: /** @see org.apache.fop.fo.ElementMapping#getStandardPrefix() */
100: public String getStandardPrefix() {
101: return "svg";
102: }
103:
104: static class SVGMaker extends ElementMapping.Maker {
105: public FONode make(FONode parent) {
106: return new SVGObj(parent);
107: }
108: }
109:
110: static class SE extends ElementMapping.Maker {
111: public FONode make(FONode parent) {
112: return new SVGElement(parent);
113: }
114: }
115:
116: }
|