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