provided by: 
Originally published at Internet.comJava Programming Notes # 2214 * Preface * General * Viewing tip * Figures * Listings * Supplementary material * General background information * Preview * Discussion and sample code * The Svg07 program * The Svg08 program * Run the program * Summary * What's next? * Complete program listings * Copyright * Resources * About the author -----------------------------------
Preface
General
This is the second lesson in a series designed to teach you how to write servlets to produce SVG code that will be rendered in graphic form by an SVG-compatible browser.
An SVG graphics library
In the previous lesson titled "Java JAXP, Creating graphics using Java and SVG" (see Resources) I taught you how write your own SVG graphics library to eliminate, or at least alleviate the requirement to write raw XML code or raw JAXP DOM code, making it possible for you to produce SVG output simply by making typical Java method calls.
At the end of the previous lesson, I promised that this lesson would teach you how to take what you learned in the previous lesson and apply it to the generation of XHTML files containing in-line SVG/XML code. I also promised that this lesson would teach you how to apply that knowledge to the writing of servlets that produce XHTML output containing in-line SVG/XML code. Upon further consideration, however, I decided to delay that material until the lesson following this one.
Upgrading the SVG graphics library
In this lesson, I will provide two more methods for the SVG graphics library that make it even easier to write servlets to produce SVG output. The first method eliminates the frustration of dealing with the escape sequences required for the quotation marks that surround attribute values in XML. The second method makes it possible to create a general node in the DOM tree being used to represent an SVG graphic.
Viewing tip
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them. You may also find it useful to open a third browser window at the Resources section near the end of the document. That will make it easy for you to open those resources when they are mentioned in the text.
Figures
* Figure 1. Svg07 screen output. * Figure 2. Recommended usage of the makeElement method. * Figure 3. Graphic output from Svg08. * Figure 4. Recommended usage of the makeNode method.
Listings
* Listing 1. Beginning of the class named Svg07. * Listing 2. The beginning of the makeElement method. * Listing 3. Dealing with elements that have no attributes. * Listing 4. Processing the attributes. * Listing 5. Decision: empty element or non-empty element. * Listing 6. Remaining code in the main method. * Listing 7. Begin creating the DOM tree. * Listing 8. Create the root node named svg. * Listing 9. Beginning of the makeNode method. * Listing 10. Append new node to its parent. * Listing 11. Nodes that have no attributes. * Listing 12. Processing the attribute names and values. * Listing 13. Show outline of the canvas. * Listing 14. Create a group container named g. * Listing 15. Draw a blue line. * Listing 16. The makeLine method. * Listing 17. Draw the black ellipse. * Listing 18. The makeEllipse method. * Listing 19. Draw the red ellipse. * Listing 20. Draw the green rotated ellipse. * Listing 21. Transform the DOM and write the output file. * Listing 22. The program named Svg07. * Listing 23. The program named Svg08.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find a consolidated index at www.DickBaldwin.com.
General background information
According to the W3C
"SVG is a language for describing two-dimensional graphics in XML. SVG allows for three types of graphic objects: vector graphic shapes (e.g., paths consisting of straight lines and curves), images and text. Graphical objects can be grouped, styled, transformed and composited into previously rendered objects. The feature set includes nested transformations, clipping paths, alpha masks, filter effects and template objects."
Not easy reading
The Scalable Vector Graphics (SVG) 1.1 Specification is extensive. Unfortunately, a person needs a fairly strong background in XML to be able to understand much of the information in the specification, thereby reducing the accessibility of that information to Java programmers who lack such a background in XML.
Will present in a more understandable form
While I don't claim to have expertise in XML, I do know enough about the topic that I can usually make sense of the material provided in the SVG specification. One of my objectives for this series is to extract much of the important information from the specification and to present it in such a way as to make it accessible to Java programmers who lack a strong background in XML and who don't have the time available to gain that background knowledge.
Author: Richard G. Baldwin
Read article at Internet.com site