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

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

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

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

Mark V. Scardina, Ben ChangandJinyu Wang

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Building the Pipeline Application

Obviously, if you simply wanted to invoke one or two processes, you would not need to use a pipeline framework. That, however, is not the case for our application. Building upon the previous examples, our application needs to invoke additional processes, and thus your pipeline control file becomes more sophisticated, as shown here in pipeProcessing_3.xml:

<pipeline xmlns="http://www.w3.org/2002/02/xml-pipeline">
<param name="target" select="result3.xml"/>
<processdef name="saxparser.p"
definition="oracle.xml.pipeline.processes.SAXParserProcess"/>
<processdef name="XSDVal.p"
definition="oracle.xml.sample.pipeline.XSDValProcess"/>
<processdef name="xsdschemabuilder.p"
definition="oracle.xml.pipeline.processes.XSDSchemaBuilder"/>
<processdef name="conditionvalidation.p"
definition="oracle.xml.sample.pipeline.XSDConditionalValProcess"/>
<processdef name="xmldiff.p"
definition="oracle.xml.sample.pipeline.XMLDiffProcess"/>
<processdef name="saxprint.p"
definition="oracle.xml.sample.pipeline.SAXPrintProcess"/>
<!-- Parse and build the local schema -->
<process id="p1" type="xsdschemabuilder.p" ignore-errors="true">
<input name="schema" label="$Lschema"/>
<param name="baseURL" select="url"></param>
<outparam name="xmlschema" label="xmlschemaobj"/>
</process>
<!--Parse the source XML -->
<process id="p2" type="saxparser.p" ignore-errors="true">
<input name="xmlsource" label="$source"/>
<output name="sax" label="saxevents"/>
<param name="preserveWhitespace" select="false"></param>
</process>
<!--Check for validation errors and switch the output accordingly -->
<process id="p3" type="XSDVal.p" ignore-errors="true">
<input name="xml" label="saxevents"/>
<param name="sschemaObj" label="xmlschemaobj"></param>
<outparam name="isCheckXSDDiff" label="isCheckStatus"></outparam>
<output name="validatedxml" label="xsdsaxevents"/>
</process>
<!--Parse and build the remote schema -->
<process id="p4" type="xsdschemabuilder.p" ignore-errors="true">
<input name="schema" label="$Rschema"/>
<param name="baseURL" select="url"></param>
<outparam name="xmlschema" label="txmlschemaobj"/>
</process>
<!--Compare local and remote schemas and report result -->
<process id="p5" type="xmldiff.p" ignore-errors="true">
<input name="xmlsource1" label="$Lschema"/>
<input name="xmlsource2" label="Rschema"/>
<outparam name="isDiff" label="isDiffStatus"/>
<output name="xsl" label="result.xsl"/>
</process>
<!--Optionally validates input if remote schema is different -->
<process id="p6" type="conditionvalidation.p" ignore-errors="true">
<input name="csax" label="xsdsaxevents"/>
<param name="txmlschema" label="txmlschemaobj"/>
<param name="isCheck" label="isCheckStatus"/>
<param name="isXSDDiff" label="isDiffStatus"/>
<outparam name="isValid" label="isConditionalValid"/>
<output name="xmlout" label="valsaxevents"/>
</process>
<!-- Print validated document if successful -->
<process id="p7" type="saxprint.p" ignore-errors="true">
<input name="pxmlsource" label="valsaxevents"/>
<param name="isPrint" label="isConditionalValid"/>
<output name="printxml" label="result3.xml"/>
</process>
</pipeline>

In this application, no fewer than seven processes are pipelined, each process passing its results to the next. We have already reviewed four of these processes. Let’s review the remaining three.


The XSDSchemaBuilder Process


The XSDSchemaBuilder process is built into the XML Pipeline Processor and builds schema objects that are used for validating XML documents. In this case, it is used for both the local and remote schemas and returns the XML schema objects xmlschemaobj and txmlschemaobj respectively, which can then be passed into other processes for validation.


The XSDValProcess Process


The XSDValProcess class performs a validation of the source file input via SAX events and reports the result of a validation error analysis. The following is its code listing:

package oracle.xml.sample.pipeline;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import oracle.xml.comp.CXMLContext;
import oracle.xml.comp.CXMLStream;
import oracle.xml.parser.schema.XSDConstantValues;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.pipeline.controller.Input;
import oracle.xml.pipeline.controller.Output;
import oracle.xml.pipeline.controller.Process;
import oracle.xml.pipeline.controller.PipelineException;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
public class XSDValProcess extends Process {
StringWriter sw=new StringWriter();
PrintWriter out= new PrintWriter(sw);
boolean isCheckXSDDiff=false;
boolean isValid=true;
public XSDValProcess() {
}
public void initialize() {
/* Set the supported xml infoset types for the inputs and outputs */
Input i = getInput("xml");
i.supportType(SAXSOURCE);
i.supportType(DOCUMENT);
Output o = getOutput("validatedxml");
//o.supportType(DOMRESULT);
o.supportType(SAXRESULT);
o.supportType(STREAMRESULT);
o.supportType(DOCUMENT);
}
public ContentHandler SAXContentHandler() throws PipelineException {
try {
Output output = getOutput("validatedxml");
// if dependents are not available then return null
ContentHandler hdlr = getSAXContentHandler(output);
if (hdlr == null)
return null;
Object schemaObj = getInParamValue("sschemaObj");
if (schemaObj == null)
return null;
exXsdHandler = new ExXSDHandler(new PrintWriter(out), this);
exXsdHandler.addContentHandler(hdlr);
exXsdHandler.setXMLProperty(XSDConstantValues.VALIDATION_MODE,
XSDConstantValues.STRICT_VALIDATION);
Object obj = ExXSDHandler.setXMLProperty(XSDConstantValues.FIXED_SCHEMA,
schemaObj);
if(obj== null)
throw new PipelineException("The XML Schema Object is not correct.");
} catch(Exception ex) {
ex.printStackTrace();
}
return exXSDHandler;
}
public void execute() throws PipelineException {
super.execute();
if(isValid==true) {
setOutParam("isCheckXSDDiff","ignore");
} else {
//Set the output parameter
if(isCheckXSDDiff == true) setOutParam("isCheckXSDDiff","true");
else {
setOutParam("isCheckXSDDiff","false");
}
}
}
ExXSDHandler exXsdHandler;
}

After initializing the objects, the XSDValProcess takes the input schema object, schemaObj, and sets the type of validation and confirms its validity in SAXContentHandler(), returning exXsdHandler. This handler, along with the SAX stream, is passed to execute() to perform the validation. The SAX stream by way of exXsdHandler is output along with the validation result in the isCheckASDiff output parameter. The exXsdHandler performs an error analysis to determine whether the error is one that should trigger a check of the remote schema. Here is its code listing:

package oracle.xml.sample.pipeline;
import java.io.PrintWriter;
import java.util.Hashtable;
import oracle.xml.parser.schema.XSDException;
import oracle.xml.parser.schema.XSDValidator;
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
import oracle.xml.parser.v2.XMLError;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
public class ExXSDHandler extends XSDValidator {
private MyXMLError err;
Hashtable ht;
PrintWriter out;
XSDValProcess parentProcess;
int errorIndex=0;
public ExXSDHandler(PrintWriter out, XSDValProcess parentProcess) throws
XSDException,SAXException {
this.out= out;
this.parentProcess=parentProcess;
parentProcess.isValid=true;
err = new MyXMLError();
setError(err);
//Initialize hashtable for error numbers to track
ht =new Hashtable();
// XML-24536 Missing ''{0}'' Attribute
ht.put(new Integer(24536),"24536");
// XML-24534 "Element ''{0}'' not expected.
ht.put(new Integer(24534),"24534");
}
public void startDocument() throws SAXException {
super.startDocument();
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
super.startElement(namespaceURI,localName,qName,atts);
}
public void endElement(String namespaceURI, String localName,String qName)
throws SAXException {
super.endElement(namespaceURI, localName,qName);
// If validation errors exist, exit the process
int i = err.getNumMessages();
if(i>errorIndex) {
errorIndex=i;
parentProcess.isValid=false;
int j= err.getErrorCode(i-1);
if(ht.get(new Integer(j)) != null) {
parentProcess.isCheckXSDDiff=true;
}
}
}
public void endDocument() throws SAXException {
super.endDocument();
}
}

Note that this class creates a hash table of error numbers to be compared to the validation errors. The reason for this analysis is to minimize the round trips to compare the remote schema with the local one. The schema validator returns error messages and associated numbers from 24000 to 24999. These numbers are available in errid, which can be retrieved by extending the XMLError class as follows:

package oracle.xml.sample.pipeline;
import oracle.xml.parser.v2.XMLError;
import oracle.xml.parser.v2.XMLParseException;
public class MyXMLError extends XMLError {
public MyXMLError() {
}
public int getErrorCode(int index) {
return errid[index];
}
}





Note

The error codes and their messages were not included in the Oracle Database 10g release. However, their documentation is available on OTN in the Oracle XDK Development section of the XML Technology Center.



The XSDConditionalValProcess Process


The XML Pipeline Processor does not include the capability for conditional logic branching. The specification intentionally omits this capability so that it remains simple. However, sometimes you may need branching, as in this chapter’s application. The final process, XSDConditionalValProcess, adds conditional logic to the pipeline. The process takes an input XML document parsed into SAX events, the schemaObject, txmlschema, the conditional, isCheck, and an isXSDDiff input parameter. If isCheck is true, it validates the XML document against an XSD file. Otherwise, no action is taken. The following is its code listing:

package oracle.xml.sample.pipeline;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import oracle.xml.comp.CXMLContext;
import oracle.xml.comp.CXMLStream;
import oracle.xml.parser.schema.XSDConstantValues;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.pipeline.controller.Input;
import oracle.xml.pipeline.controller.Output;
import oracle.xml.pipeline.controller.Process;
import oracle.xml.pipeline.controller.PipelineException;
import org.xml.sax.ContentHandler;
public class XSDConditionalValProcess extends Process {
StringWriter sw=new StringWriter();
PrintWriter out= new PrintWriter(sw);
boolean isValid=true;
public XSDConditionalValProcess() {
}
public void initialize() {
/* Set the supported xml infoset types for the inputs and outputs */
Input i = getInput("csax");
i.supportType(SAXSOURCE);
i.supportType(DOCUMENT);
Output o = getOutput("xmlout");
o.supportType(SAXRESULT);
o.supportType(STREAMRESULT);
o.supportType(DOCUMENT);
}
public ContentHandler SAXContentHandler() throws PipelineException
{
try {
Object schemaObj = getInParamValue("txmlschema");
if (schemaObj == null)
return null;
Output output = getOutput("xmlout");
xsdHandler = new XSDValidationHandler(new PrintWriter(out), this);
xsdHandler.setXMLProperty(XSDConstantValues.VALIDATION_MODE,
XSDConstantValues.STRICT_VALIDATION);
Object obj = xsdHandler.setXMLProperty(XSDConstantValues.FIXED_SCHEMA,
schemaObj);
if(obj== null)
throw new PipelineException("The XML Schema Object is not correct.");
ContentHandler hdlr = getSAXContentHandler(output);
xsdHandler.addContentHandler(hdlr);
} catch(Exception ex) {
ex.printStackTrace();
}
return xsdHandler;
}
public void execute() throws PipelineException
{
super.execute();
Object isCheck = getInParamValue("isCheck");
if(isCheck.equals("true")) {
Object isDiff = getInParamValue("isXSDDiff");
if(isDiff.equals("true")) {
System.out.println("XSD documents are not consistent.");
System.out.println("Please upload the new XSD file.");
if(isValid==true) {
System.out.println("XML document accepted.");
setOutParam("isValid","true");
} else {
System.out.println("XML document rejected.");
setOutParam("isValid","false");
}
} else {
System.out.println("XSD documents are consistent.");
System.out.println("XML document rejected.");
setOutParam("isValid","false");
}
} else if(isCheck.equals("ignore")) {
setOutParam("isValid","true");
System.out.println("XML document accepted.");
} else if(isCheck.equals("false")) {
setOutParam("isValid","false");
System.out.println("XML document rejected.");
}
}
XSDValidationHandler xsdHandler;
}

If the validation process is performed and the XML document is valid, isXSDDiff is checked. If it is true, then the message indicates that the XML document is valid according to the local XSD but that the local XSD and the remote XSD are not consistent. If isXSDDiff is false, the message indicates that the XML document is valid according to the local XSD and that the local and remote XSDs are consistent (this is not shown in the example because isCheck is false in this case).

When the validation process is performed and the XML document is invalid, isXSDDiff is checked. If it is true, then the message indicates that the XML document is invalid according to the local XSD but that the XSDs are not consistent. If isXSDDiff is false, the message shows that the XML document is invalid according to the local XSD and that the XSDs are consistent.

/ 218