| <?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: domTutorial.xml 201374 2004-08-18 07:17:26Z vhardy $ --> |
| <!-- ========================================================================= --> |
| |
| <!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 boostrap 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> |