Debian Codegraph @

documentation-sources/content/xdocs/using/dom-api.xml

raw
1 <?xml version="1.0"?>
2
3 <!--
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 -->
21
22 <!-- ========================================================================= -->
23 <!-- author [email protected] -->
24 <!-- version $Id$ -->
25 <!-- ========================================================================= -->
26
27 <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
28 <document>
29 <header>
30 <title>SVG DOM API</title>
31 </header>
32
33 <body>
34 <p>
35 The <a href="http://www.w3.org/dom/">Document Object Model</a> (DOM)
36 is an API for XML documents. It defines the logical structure of documents and
37 the way a document is accessed and manipulated. This page shows how to create
38 an SVG document using the DOM API.
39 </p>
40
41 <section id="gettingStarted">
42 <title>Getting started</title>
43 <p>
44 The DOM API defines an interface called
45 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/DOMImplementation.html">DOMImplementation</a>,
46 which represents the bootstrap of any DOM implementation. The role of
47 this class is to bootstrap a particular implementation of the DOM by
48 providing a method to create a
49 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</a>.
50 Then, the concrete <code>Document</code> represents an XML document and
51 also acts like a factory for the various DOM objects such as
52 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html">Element</a>,
53 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Attr.html">Attr</a>
54 and
55 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Text.html">Text</a>.
56 </p>
57
58 <p>
59 How to get an instance of the <code>DOMImplementation</code> interface depends
60 on the DOM implementation you are using. In Batik, the DOM implementation is
61 located in the package <code>org.apache.batik.dom.svg</code> and the class is
62 named
63 <a class="class" href="http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/dom/svg/SVGDOMImplementation.html">SVGDOMImplementation</a>.
64 The following example shows how to get a concrete
65 <code>DOMImplementation</code> object.
66 </p>
67
68 <source>import org.w3c.dom.DOMImplementation;
69 import org.apache.batik.dom.svg.SVGDOMImplementation;
70
71 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();</source>
72
73 <p>
74 Once you have an instance of a <code>DOMImplementation</code>, you are not
75 relying on Batik-specific code any more and ready to use the DOM API.
76 </p>
77
78 </section>
79
80 <section id="creating">
81 <title>Creating a Document</title>
82
83 <p>
84 Using the <code>DOMImplementation</code>, you are now able to create a
85 <code>Document</code>. The following example illustrates how to create an SVG
86 document. Note that the Batik’s DOM implementation can be
87 used to represent either an SVG document fragment or any kind of XML
88 document. Note that by choosing the namespace URI and the local name of
89 the root element of SVG, we are creating an SVG document.
90 </p>
91
92 <source>import org.apache.batik.dom.svg.SVGDOMImplementation;
93 import org.w3c.dom.Document;
94
95 // We are using a constant available on the SVGDOMImplementation,
96 // but we could have used "http://www.w3.org/2000/svg".
97 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
98 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
99 <strong>Document doc = impl.createDocument(svgNS, "svg", null);</strong>
100 </source>
101
102 <p>
103 As we have created an SVG <code>Document</code>, we can cast this document to an
104 <a class="class" href="../javadoc/org/w3c/dom/svg/SVGDocument.html">SVGDocument</a>
105 (defined in the <code>org.w3c.dom.svg</code> package) if needed.
106 </p>
107
108 </section>
109
110 <section id="buildsvgdoc">
111 <title>Building an SVG Document</title>
112
113 <p>
114 Finally, using the <code>Document</code> object, we are now able to
115 construct SVG content. Note that the document created before supports both
116 generic XML and SVG. Though the DOM implementation of Batik is an SVG DOM
117 implementation, the SVG-specific methods that rely on the document having
118 been rendered (particularly geometry related methods, such as
119 <a href="../javadoc/org/w3c/dom/svg/SVGLocatable.html#getBBox()">SVGLocatable.getBBox</a>)
120 cannot be used at this point.
121 </p>
122 <p>
123 The document can be built using DOM Level 2 Core methods. The following
124 example shows how to create a red rectangle located at (10, 20), with a
125 size of (100, 50) placed in a (400, 450) SVG canvas:
126 </p>
127
128 <source>
129 import org.apache.batik.dom.svg.SVGDOMImplementation;
130 import org.w3c.dom.Document;
131 import org.w3c.dom.Element;
132
133 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
134 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
135 Document doc = impl.createDocument(svgNS, "svg", null);
136
137 // Get the root element (the 'svg' element).
138 <strong>Element svgRoot = doc.getDocumentElement();</strong>
139
140 // Set the width and height attributes on the root 'svg' element.
141 <strong>svgRoot.setAttributeNS(null, "width", "400");</strong>
142 svgRoot.setAttributeNS(null, "height", "450");
143
144 // Create the rectangle.
145 <strong>Element rectangle = doc.createElementNS(svgNS, "rect");</strong>
146 rectangle.setAttributeNS(null, "x", "10");
147 rectangle.setAttributeNS(null, "y", "20");
148 rectangle.setAttributeNS(null, "width", "100");
149 rectangle.setAttributeNS(null, "height", "50");
150 rectangle.setAttributeNS(null, "fill", "red");
151
152 // Attach the rectangle to the root 'svg' element.
153 <strong>svgRoot.appendChild(rectangle);</strong>
154 </source>
155
156 <p>
157 The example given constructs a document equivalent to parsing the
158 following SVG file:
159 </p>
160
161 <source><![CDATA[<svg xmlns="http://www.w3.org/2000/svg" width="400" height="450">
162 <rect x="10" y="20" width="100" height="50" fill="red"/>
163 </svg>]]></source>
164
165 </section>
166
167 <section id="createdocfromsvgfile">
168 <title>Creating a Document from an SVG file</title>
169
170 <p>
171 With Batik, you can also create an SVG DOM tree from a URI, an
172 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html">InputStream</a>,
173 or a
174 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html">Reader</a>,
175 using the
176 <a class="class" href="../javadoc/org/apache/batik/dom/util/SAXDocumentFactory.html">SAXSVGDocumentFactory</a>.
177 The following example illustrates how to create an SVG document from a
178 URI using the <code>SAXSVGDocumentFactory</code> class.
179 </p>
180
181 <source>import java.io.IOException;
182
183 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
184 import org.apache.batik.util.XMLResourceDescriptor;
185
186 import org.w3c.dom.Document;
187
188 try {
189 String parser = XMLResourceDescriptor.getXMLParserClassName();
190 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
191 String uri = "http://www.example.org/diagram.svg";
192 <strong>Document doc = f.createDocument(uri);</strong>
193 } catch (IOException ex) {
194 // ...
195 }
196 </source>
197
198 <p>
199 As we have created an SVG <code>Document</code>, we can cast this document to an
200 <code>SVGDocument</code> (defined in the <code>org.w3c.dom.svg</code> package)
201 if needed.
202 </p>
203 </section>
204
205 <section id="rendering">
206 <title>Rendering an SVG Document</title>
207
208 <p>
209 Batik provides several ways to use an SVG DOM tree. Two modules can be
210 immediately used to render your SVG document.
211 </p>
212 <dl>
213 <dt>JSVGCanvas</dt>
214 <dd>
215 <p>
216 The
217 <a class="class" href="../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a>
218 is a Swing component that can display SVG document. A SVG document can
219 be specified using a URI or an SVG DOM tree (using the
220 <a class="class" href="../javadoc/org/apache/batik/swing/JSVGCanvas.html#setSVGDocument(Document)">setSVGDocument</a>
221 method). For futher information about the <code>JSVGCanvas</code>,
222 see the <a href="site:swing">Swing components module
223 documentation</a>.
224 </p>
225 </dd>
226 <dt>ImageTranscoder</dt>
227 <dd>
228 The
229 <a class="class" href="../javadoc/org/apache/batik/transcoder/image/ImageTranscoder.html">ImageTranscoder</a>
230 is a transcoder that can take a URI, an <code>InputStream</code> or an
231 SVG DOM tree and produces a raster image (such JPEG, PNG or TIFF).
232 By creating a
233 <a class="class" href="../javadoc/org/apache/batik/transcoder/TranscoderInput.html">TranscoderInput</a>
234 object with the SVG DOM tree, you will be able to transform your SVG
235 content to a raster image. For futher information, see the
236 <a href="site:transcoder">transcoder module documentation</a>.
237 </dd>
238 </dl>
239
240 </section>
241
242 </body>
243 </document>