Download Part 2 – SVG

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Multimedia & XML
T
HE Java API for XML Processing (JAXP) is for processing XML data using applications
written in the Java programming language. JAXP leverages the parser standards Simple
API for XML Parsing (SAX) and Document Object Model (DOM) so that you can
choose to parse your data as a stream of events or to build an object representation of it. The
SAX and DOM APIs are defined by the XML-DEV group and by the W3C, respectively. The
libraries that define those APIs are as follows:


javax.xml.parsers: The JAXP APIs, which provide a common interface for
different vendors’ SAX and DOM parsers
org.w3c.dom: Defines the Document class (a DOM) as well as classes for all the
components of a DOM
Simple API for XML (SAX)
The Simple API for XML (SAX) is the event-driven, serial-access mechanism that does
element-by-element processing. The API for this level reads and writes XML to a data
repository or the web. As shown in the figure below, to start the XML process, an instance of
the SAXParserFactory class is used to generate an instance of the parser. The parser wraps
a SAXReader object. When the parser’s parse()method is invoked, the reader invokes one
of several callback methods implemented in the application. Those methods are defined by
the
interfaces
ContentHandler,
ErrorHandler,
DTDHandler,
and
EntityResolver.
Document Object Model API
The DOM API is generally an easier API to use. It provides a familiar tree structure of objects.
You can use the DOM API to manipulate the hierarchy of application objects it encapsulates.
The DOM API is ideal for interactive applications because the entire object model is present in
memory, where it can be accessed and manipulated by the user. You use the
javax.xml.parsers.DocumentBuilderFactory class to get a DocumentBuilder
instance, and you use that instance to produce a Document object that conforms to the DOM
specification. The builder you get, in fact, is determined by the system property
- Page 1/8 -
javax.xml.parsers.DocumentBuilderFactory,
which
selects
the
factory
implementation that is used to produce the builder. You can also use the DocumentBuilder
newDocument() method to create an empty document that implements the
org.w3c.dom.Document interface. Alternatively, you can use one of the builder’s parse
methods to create a document from existing XML data.
Constructing the DOM requires reading the entire XML structure and holding the object tree
in memory, so it is much more CPU- and memory-intensive. For that reason, the SAX API
tends to be preferred for server-side applications and data filters that do not require an inmemory representation of the data.
For more details, you can see later the following web sites: http://www.saxproject.org/ and
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/
Part 1 – XML Parsing
In this section, we will scan and produce XML files. For that, you need to use my documents
here.
Step 1 - Sax to Parse
Now, launch NetBeans and create a new empty project called multimedia. Insert the document
Java_SAX.java and carnet.xml inside your project. You can use Netbeans to check the
well-forming of the xml document. After, identify and correct errors to compile and run
Java_SAX.java (you can use the carnet.xml file as an input). Try now to add your
comments to the java file and modify its output so it displays a result similar to the original
XML document as shown below.
- Page 2/8 -
Java_SAX.java:
package javaapplication1;
import java.io.File;
import javax.swing.JOptionPane;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Richard CHBEIR
*/
public class Scanner_SAX extends DefaultHandler
{
public static JFileChooser chooser_ouvrir = new JFileChooser();
public String s_addr_in = "";
public static void main(String[] args){}
public void run()
{
DefaultHandler handler = new Scanner_SAX();
SAXParserFactory factory = SAXParserFactory.newInstance();
if(chooser_ouvrir.showOpenDialog(null)== JFileChooser.APPROVE_OPTION)
{
String
s_addr_in=chooser_ouvrir.getCurrentDirectory()+"\\"+chooser_ouvrir.getSelectedFile().getName();
if ((new File(s_addr_in)).exists())
{
System.out.println(s_addr_in);
try{
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(s_addr_in), handler );
}
catch ( SAXParseException error) {}
catch (Throwable t) {t.printStackTrace (); }
}
}
else System.out.println("No XML File Found");
}
public void error(SAXParseException e)
throws SAXParseException { throw e; }
//-----------------------------------------------------------------------------------public void startDocument ()
throws SAXException {System.out.println("[???????]");}
//-----------------------------------------------------------------------------------public void endDocument ()
throws SAXException { System.out.println("[---------]");}
//-----------------------------------------------------------------------------------public void startElement (String namespaceURI, String simpleName, String qualifiedName,
Attributes attrs)
throws
SAXException
{
System.out.println("");
System.out.print("!!!!!!!!"+qualifiedName+namespaceURI);
}
//-----------------------------------------------------------------------------------public void endElement (String namespaceURI,String simpleName,String qualifiedName)
throws SAXException {System.out.println(":::::::::"+simpleName+qualifiedName+namespaceURI);
}
//-----------------------------------------------------------------------------------public void characters (char buf [], int offset, int len)
throws SAXException { }
}
- Page 3/8 -
carnet.xml:
Step 2 - DOM to Write
Add the Java_Dom.java file to your project now and execute it. What happens? See the
result in a browser. Provide to the user the possibility to give the path and the name of the
output file. Try now using a file extension .svg and see the result in a browser. What
happens?
Java_Dom.java:
import
import
import
import
import
import
import
import
java.util.*;
java.io.*;
java.awt.*;
org.w3c.dom.*;
javax.xml.parsers.*;
javax.xml.transform.*;
javax.xml.transform.dom.DOMSource;
javax.xml.transform.stream.StreamResult;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright Richard Chbeir (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Write_DOM
{
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
Document newDoc;
boolean finished = false;
Element rootPolicy;
public Write_DOM()
{
finished = false;
domFactory = DocumentBuilderFactory.newInstance();
try {
domBuilder = domFactory.newDocumentBuilder();
}
catch (ParserConfigurationException ex) {
- Page 4/8 -
}
newDoc = domBuilder.newDocument();
rootPolicy = newDoc.createElement("svg");
rootPolicy.setAttribute("xmlns","http://www.w3.org/2000/svg");
rootPolicy.setAttribute("width","100%");
rootPolicy.setAttribute("height","100%");
this.addSVGComponent();
}
public void addSVGComponent( )
{
Element graphique = newDoc.createElement("g");
graphique.setAttribute("style", "stroke:black; fill:none;");
Element objectGrph = newDoc.createElement("circle");
objectGrph.setAttribute("cx","70");
objectGrph.setAttribute("cy","100");
objectGrph.setAttribute("r","50");
objectGrph.setAttribute("style","fill:blue; stroke:red;");
graphique.appendChild(objectGrph);
rootPolicy.appendChild(graphique);
newDoc.appendChild(rootPolicy);
}
public void saveFile (String filePath)
{
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = null;
try
{ aTransformer = tranFactory.newTransformer();
}
catch (TransformerConfigurationException ex)
{
}
Source src = new DOMSource(newDoc);
Result dest = new StreamResult(new File(filePath));
try
{aTransformer.transform(src, dest); }
catch (TransformerException ex1)
{
}
finished = true;
}
public static void main (String args[])
{
Write_DOM parse = new Write_DOM();
parse.saveFile("testapp.xml");
}
}
Step 3 – Integrating XML Parsers
In this section, you will implement an interface (Jframe) similar to the screen shot below with
two buttons allowing parsing and writing XML files (using the Java_SAX and Java_DOM
classes).
:
- Page 5/8 -
Part 2 – SVG
In this section, we will start by reading the two SVG files: basicshapes.svg and cat.svg.
Cat.svg:
<?xml version="1.0" encoding="utf-8"?>
<svg width="240" height="270">
<title>Cat</title>
<desc>How to draw a cat with SVG</desc>
<circle cx="70" cy="95" r="50" style="stroke:black; fill:none;"/>
<circle cx="55" cy="80" r="5" stroke="black" fill="#339933"/>
<circle cx="80" cy="80" r="5" stroke="black" fill="#339933"/>
<g id="moustaches">
<line x1="75" y1="95" x2="135" y2="85" style="stroke:black;"/>
<line x1="75" y1="95" x2="135" y2="105" style="stroke:black;"/>
</g>
<use xlink:href="#moustaches" transform="scale(-1,1) translate(-140 0)"/>
<!--ears-->
<polyline points="108 62, 90 10, 70 45, 50, 10, 32, 62"
style="stroke:black; fill:none;"/>
<!—Smiley Cat-->
<polyline points="35 110, 45 120, 95 120, 105, 110"
style="stroke:black; fill:none;"/>
<!--noise-->
<path d="M 75 90 L 65 90 A 5 10 0 0 0 75 90"
style="stroke:black; fill:#ffcccc;"/>
<text x="50" y="165" style="font-family:sans-serif;font-size:14pt;
stroke:none;fill:black;">Cat</text>
</svg>
basicshapes.svg:
<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN" "svg.dtd">
<svg width="500" height="500" onmousemove="affiche()" >
<script xlink:href="script.js" type="text/ecmascript" />
<text x="5" y="20" style="font-size:22">Basic SVG shapes</text>
<text id="richie_text" x="5" y="500">Coordinates</text>
<g style="stroke:black; fill:none;">
<circle cx="70" cy="100" r="50" style="fill:blue; stroke:red;" />
<rect x="150" y="50" width="135" height="100" style="stroke:lime; fill:yellow; strokewidth:4;" />
<line x1="325" y1="150" x2="375" y2="50" />
<line x1="375" y1="50" x2="425" y2="150" />
<ellipse cx="100" cy="300" rx="72" ry="50" style="fill:gray;" />
<polygon points="250,250,297,284,279,340,220,340,202,284,250,250" />
</g>
<g>
<text
<text
<text
<text
<text
</g>
</svg>
x="50" y="175">Circle</text>
x="185" y="175">Rectangle</text>
x="355" y="175">Lines</text>
x="80" y="375">Ellipse</text>
x="225" y="375">Polygon</text>
Try to fill the following table
- Page 6/8 -
Basic Shapes
Tag
Possible Attributes
Description
<text>
<rect>
<circle>
<ellipse>
<line>
<polygon>
1. Create a function to be called basic_shapes that:
 Accepts SVG file as input
 Display the number of basic shapes identified in the file
2. Modify the basic_shapes function so that the basic shapes are given by the user in a XML
document similar to the following file:
basic.XML:
<?xml version="1.0"?>
<basic>
<shapes>
<text/>
<circle/>
<rect/>
<line/>
<ellipse/>
<polygon/>
</shapes>
</basic>
3. Create another function to be called basic_colors that:
 Accepts SVG file as input
 Display the number of basic colors identified in the file. The basic colors are given in
an XML document:
basic.XML:
<?xml version="1.0"?>
<basic>
<shapes>
<text/>
<circle/>
- Page 7/8 -
<rect/>
<line/>
<ellipse/>
<polygon/>
</shapes>
<colors>
<gray/>
<blue/>
<red/>
</colors>
</basic>
Part 3 – SVG Quad-tree
4. Create now another function Color_Quad_tree able to return the Quad-tree peano code of an
image. The homogeneous region contains only one basic color (given in an XML doc). The
scanning must be done as follows:
2
3
0
1
For instance, given the following SVG, we obtain the following peano code:
0
0
1 2
3
0
0
Peano Code
30
1
2
1 2
1 2
3
3
0
1 2
3
3
1 112 03 13
5. Create now another function Shape_Quad_tree able to return the Quad-tree peano code of an
image. The homogeneous region contains only one basic shape (given in an XML doc).
6. Create now another function Quad_tree able to return the Quad-tree peano code of an image.
The homogeneous region contains only one basic shape and color (given in an XML doc).
Email the whole project and folder in a zip file to [email protected].
- Page 8/8 -