KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > extensions > svg > SVGElementMapping


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 /* $Id: SVGElementMapping.java 453919 2006-10-07 14:26:37Z jeremias $ */
19
20 package org.apache.fop.fo.extensions.svg;
21
22 import java.util.HashMap JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24
25 import org.apache.fop.fo.FONode;
26 import org.apache.fop.fo.ElementMapping;
27
28 import org.apache.batik.util.XMLResourceDescriptor;
29 import org.apache.batik.dom.svg.SVGDOMImplementation;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.w3c.dom.DOMImplementation JavaDoc;
33
34 /**
35  * Setup the SVG element mapping.
36  * This adds the svg element mappings used to create the objects
37  * that create the SVG Document.
38  */

39 public class SVGElementMapping extends ElementMapping {
40     
41     /** the SVG namespace */
42     public static final String JavaDoc URI = SVGDOMImplementation.SVG_NAMESPACE_URI;
43     
44     /** logging instance */
45     protected Log log = LogFactory.getLog(SVGElementMapping.class);
46     
47     private boolean batikAvailable = true;
48
49     /** Main constructor. */
50     public SVGElementMapping() {
51         namespaceURI = URI;
52     }
53
54     /** @see org.apache.fop.fo.ElementMapping#getDOMImplementation() */
55     public DOMImplementation getDOMImplementation() {
56         return SVGDOMImplementation.getDOMImplementation();
57     }
58
59     /**
60      * Returns the fully qualified classname of an XML parser for
61      * Batik classes that apparently need it (error messages, perhaps)
62      * @return an XML parser classname
63      */

64     private String JavaDoc getAParserClassName() {
65         try {
66             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
67             return factory.newSAXParser().getXMLReader().getClass().getName();
68         } catch (Exception JavaDoc e) {
69             return null;
70         }
71     }
72
73     /** @see org.apache.fop.fo.ElementMapping#initialize() */
74     protected void initialize() {
75         if (foObjs == null && batikAvailable) {
76             // this sets the parser that will be used
77
// by default (SVGBrokenLinkProvider)
78
// normally the user agent value is used
79
try {
80                 XMLResourceDescriptor.setXMLParserClassName(
81                   getAParserClassName());
82
83                 foObjs = new HashMap JavaDoc();
84                 foObjs.put("svg", new SE());
85                 foObjs.put(DEFAULT, new SVGMaker());
86             } catch (Throwable JavaDoc t) {
87                 log.error("Error while initializing the Batik SVG extensions", t);
88                 // if the classes are not available
89
// the DISPLAY is not checked
90
batikAvailable = false;
91             }
92         }
93     }
94
95     /** @see org.apache.fop.fo.ElementMapping#getStandardPrefix() */
96     public String JavaDoc getStandardPrefix() {
97         return "svg";
98     }
99
100     static class SVGMaker extends ElementMapping.Maker {
101         public FONode make(FONode parent) {
102             return new SVGObj(parent);
103         }
104     }
105
106     static class SE extends ElementMapping.Maker {
107         public FONode make(FONode parent) {
108             return new SVGElement(parent);
109         }
110     }
111
112 }
113
Popular Tags