Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Simple Pipeline Examples


The Oracle XDK 10g XML Pipeline Processor provides a command-line utility that makes it easy to prototype and test pipeline applications. Oracle.xml.pipeline.controller.orapipe is a utility that can be launched from Java and has the following parameters:





–help Prints the help message





–version Prints the release version





–sequential Executes the pipeline in a sequential mode; the default is parallel





–validate Validates the XML pipeline control file with the pipeline schema; by default, does not validate





–log <logfile> Writes the errors/logs to the output file; by default, writes to pipe.log





–noinfo Doesn’t log any info items; default is on





–nowarning Doesn’t log any warnings; default is on





–force Executes pipeline even if target is up-to-date; by default, no force is specified





It also turns out that there is a very useful parameter that was not documented in the initial release that lets you pass in parameters into the XML pipeline control file:





–attr <name> <value> Sets the value of $name





We will use this utility and its parameters in the following simple examples.



SAX Parsing and Printing



This first example demonstrates how to use the XML Pipeline Processor to parse and print an input XML document. An example of an instance document that should validate against this schema is as follows:


<?xml version="1.0"?>
<book inStock="Yes">
<title>Compilers: Principles, Techniques, and Tools</title>
<author>Alfred V.Aho, Ravi Sethi, Jeffrey D. Ullman</author>
<ISBN>0-201-10088-6</ISBN>
<edition>Second</edition>
<publisher>Addison Wesley</publisher>
</book>


You could obviously write Java code to parse, validate, and write this document to a file. Instead, you could create an XML pipeline control file that calls generic classes to perform the same functions. The following file, pipeprocessing__1.xml, is an example:


<pipeline xmlns="http://www.w3.org/2002/02/xml-pipeline">
<param name="target" select="result1.xml"/>
<processdef name="saxparser.p"
definition="oracle.xml.pipeline.processes.SAXParserProcess"/>
<processdef name="saxprint.p"
definition="oracle.xml.sample.pipeline.SAXPrintProcess"/>
<process id="p1" type="saxparser.p" ignore-errors="true">
<input name="xmlsource" label="$source"/>
<output name="sax" label="saxevents"/>
</process>
<process id="p2" type="saxprint.p" ignore-errors="true">
<input name="pxmlsource" label="saxevents"/>
<param name="isPrint" select="true"/>
<output name="printxml" label="$result"/>
</process>
</pipeline>


Note that two named processes are called, saxparser.p and saxprint.p, and that these are associated with actual Java classes. This allows them to be used in the process sections identified by id attributes p1 and p2. These processes supply the parameters required by the XML Pipeline Processor and the called classes. In this case you see $source as the parameter for specifying the input filename and $result for the output filename. The following is the command line for executing this process after you have added the xml.jar in ORACLE_HOME/lib and the compiled oracle.xml.sample.pipeline.SAXPrintProcess class to your Java CLASSPATH:


java oracle.xml.pipeline.controller.orapipe -sequential –force
-attr source xml/book1.xml –attr result result1.xml
pipeline/pipeProcessing_1.xml


As noted, there are two classes that need to be created that implement the pipeline interfaces. The oracle.xml.pipeline.processes.SAXParserProcess method is a built-in class, and the following is a listing for the other, oracle.xml.sample.pipeline.SAXPrintProcess:


package oracle.xml.sample.pipeline;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import oracle.xml.parser.v2.XMLSAXSerializer;
import oracle.xml.pipeline.controller.Process;
import oracle.xml.pipeline.controller.Input;
import oracle.xml.pipeline.controller.Output;
import oracle.xml.pipeline.controller.PipelineException;
import org.xml.sax.ContentHandler;
public class SAXPrintProcess extends Process {
StringWriter sw=new StringWriter();
PrintWriter out= new PrintWriter(sw);
public SAXPrintProcess() {
}
public void initialize() {
/* Set the supported xml infoset types
for the inputs and outputs */
Input i = getInput("pxmlsource");
i.supportType(SAXSOURCE);
i.supportType(DOCUMENT);
Output o = getOutput("printxml");
o.supportType(STREAMRESULT);
o.supportType(DOCUMENT);
}
public
ContentHandler SAXContentHandler()
throws PipelineException
{
printHandler = new XMLSAXSerializer(out);
return printHandler;
}
public void execute() throws PipelineException {
Object isPrint= getInParamValue("isPrint");
if(isPrint == null) isPrint="false";
if(isPrint.equals("true")) {
try {
OutputStream outstream = getOutputStream("printxml");
outstream.write(sw.toString().getBytes());
out.flush();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
XMLSAXSerializer printHandler;
}


The steps needed to implement a customized XML pipeline processor are to extend the oracle.xml.pipeline.controller.Process class and implement the initialize() and execute() function. In order to create SAX-based pipeline processors, you also need to implement the SAXContentHandler() to process SAX events.



Parsing and Checking for Differences



Another simple example and a task we will incorporate into the application is the comparison of two XML files by using the XMLDiff class from the Oracle XDK 10g. In this case, you will compare two XML schemas. The following book_local.xsd schema specifies the format for the book listing files:


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="book" type="bookType"/>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element ref="title"/>
<xsd:choice>
<xsd:element ref="author"/>
<xsd:element ref="editor"/>
</xsd:choice>
<xsd:element ref="ISBN"/>
<xsd:element ref="edition" minOccurs="1"/>
<xsd:element ref="publisher"/>
</xsd:sequence>
<xsd:attribute name="inStock" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Yes"/>
<xsd:enumeration value="No"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="nameType"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="edition" default="First">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="First"/>
<xsd:enumeration value="Second"/>
<xsd:enumeration value="Third"/>
<xsd:enumeration value="Fourth"/>
<xsd:enumeration value="Fifth"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="publisher" type="xsd:string"/>
<xsd:element name="editor" type="nameType"/>
<xsd:simpleType name="nameType">
<xsd:restriction base="xsd:string">
<xsd:minLength value="4"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>


A second schema, book_remote.xsd, differs only in the <edition/> element, changing from a mandatory occurrence to an optional one, as follows:


<xsd:element ref="edition" minOccurs="0"/>


The two schemas can be compared using a very simple pipeline control file, pipeProcessing_2.xml:


<pipeline xmlns="http://www.w3.org/2002/02/xml-pipeline">
<param name="target" select="result2.xsl"/>
<processdef name="xmldiff.p"
definition="oracle.xml.sample.pipeline.XMLDiffProcess"/>
<process id="p1" type="xmldiff.p" ignore-errors="true">
<input name="xmlsource1" label="$source1"/>
<input name="xmlsource2" label="$source2"/>
<outparam name="isDiff" label="isDiffStatus"/>
<output name="xsl" label="result2.xsl"/>
</process>
</pipeline>


This file specifies a single xmldiff.p process to be called and process two input files, source1 and source2, and print the output to result2.xsl if there is a difference. This file is executed with the following command line:


java oracle.xml.pipeline.controller.orapipe -sequential -force
-attr source1 xsd/book_local.xsd -attr source2 xsd/book_remote.xsd
pipeline/pipeProcessing_2.xml


In this case a difference is found, so you get a result2.xsl stylesheet detailing the expected change in the <edition> element:


<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:output xmlns:ora="http://www.oracle.com/XSL/Transform/java"
ora:omit-xml-encoding="yes"/>
<!--Select all nodes-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/xsd:schema[1]/xsd:complexType[1]/xsd:sequence[1]
/xsd:element[3]/@minOccurs">
<xsl:attribute name="minOccurs">0</xsl:attribute>
</xsl:template>
</xsl:stylesheet>


As with the previous example, you need to implement oracle.xml.sample.pipeline.XMLDiffProcess with the proper pipeline interfaces. The following is an example of such an implementation:


package oracle.xml.sample.pipeline;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import oracle.xml.differ.XMLDiff;
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.v2.XMLParser;
import oracle.xml.pipeline.controller.Input;
import oracle.xml.pipeline.controller.Output;
import oracle.xml.pipeline.controller.PipelineException;
import oracle.xml.pipeline.controller.Process;
import org.xml.sax.InputSource;
public class XMLDiffProcess extends Process {
public XMLDiffProcess() {
}
public void initialize() {
// Initialized inputs
Input input1 = getInput("xmlsource1");
input1.supportType(DOMSOURCE);
input1.supportType(DOCUMENT);
Input input2 = getInput("xmlsource2");
input2.supportType(DOMSOURCE);
input2.supportType(DOCUMENT);
// Initialized Output
Output output = getOutput("xsl");
output.supportType(STREAMRESULT);
output.supportType(DOCUMENT);
}
public void execute() throws PipelineException {
XMLDocument doc1=null, doc2=null;
boolean res=false;
DOMParser parser = new DOMParser();
/* get input stream source */
Source xmlSource1 = getInputSource("xmlsource1");
Source xmlSource2 = getInputSource("xmlsource2");
XMLDiff xmlDiff = new XMLDiff();
if(xmlSource1 instanceof StreamSource) {
StreamSource xmlstream = (StreamSource)xmlSource1;
Object instream;
try {
if((instream = xmlstream.getInputStream()) != null) {
parser.parse((InputStream)instream);
} else {
error(30005, FATAL_ERROR, new String[]{"xmlsource"});
}
doc1= parser.getDocument();
} catch(Exception e) {
e.printStackTrace();
}
} else {
error(30002, FATAL_ERROR, new String[]{"StreamSource"});
}
if(xmlSource2 instanceof StreamSource) {
StreamSource xmlstream = (StreamSource)xmlSource2;
Object instream;
try {
if((instream = xmlstream.getInputStream()) != null) {
parser.parse((InputStream)instream);
} else {
error(30005, FATAL_ERROR, new String[]{"xmlsource"});
}
doc2= parser.getDocument();
} catch(Exception e) {
e.printStackTrace();
}
} else {
error(30002, FATAL_ERROR, new String[]{"StreamSource"});
}
if(doc1!=null && doc2 !=null) {
xmlDiff.setInput1(doc1);
xmlDiff.setInput2(doc2);
res = xmlDiff.diff();
if(res == true) setOutParam("isDiff","true");
else setOutParam("isDiff","false");
} else setOutParam("isDiff","N/A");
if(res==true){
try {
XMLDocument doc = xmlDiff.generateXSLDoc();
OutputStream outstream = getOutputStream("xsl");
doc.print(outstream);
} catch(Exception e) {
e.printStackTrace();
}
}
return;
}
public void endExecute() throws PipelineException
{
System.out.println("End of XMLDiffProcess!!");
}
}


/ 218