Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.

Similar presentations


Presentation on theme: "1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops."— Presentation transcript:

1 1 Validation SAX-DOM

2 Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops

3 3 Definition & Purpose  XML Schema is a document follows the XML syntax  A XML document is called valid if it conform to a schema.  The validation API introduced in JAXP1.3 are schema-independent validation Framework

4 Classes in javax.xml.validation ClassesDescriptions Schema - Represents a set of constraints against which an XML document is validated. - This set of constraints should not change once the document has been created. - The result of the validation should be the same every time the document is validated SchemaFactory - Is responsible for creating Schema objects. It is the first step for the Validation API. - The external representation of schemas is read and prepared for validation by SchemaFactory. It is required on application’s part to ensure that at least one thread is using SchemaFactory object at any given point of time SchemaFactoryLoader - Was omitted from JAXP 1.3. Its inclusion in Java 5 was out of mistake. - Its presence is just for the sake of compatibility reasons and applications should avoid using it TypeInfoProvider - Provides information about the type assigned to each attribute or element in an XML document. - Applications can access this type information through Validator Handler.getTypeInfoProvider ( ) method Validator - Checks the XML document against an XML schema. - Must ensure that it uses only one validator object at any given instance of time. This object cannot be used from multiple threads created within the application. Also, a single thread should not reenter the code recursively ValidatorHandler - Uses the Schema object to validate that the SAX events follow the set of constraints described in the associated schema and sometimes may modify SAX events. - Is thread-unsafe. By this is means that there can be unwanted interaction between the threads. - Is also a non-re-entrant object. The validating application is required to check that not more than one thread at any given point of time

5 XML Validation against a Schema  Step 1 – Creating a Parser Instance: creating an instance of the DOM/SAX parser factory.  Step 2 – Configuring the Parser Instance: Configuring the DOM/SAX parser factory instance to support schema validation  Step 3 – Obtaining DOM/SAX Parser: Obtaining a DOM/SAX parser from the configured DOM/SAX parser factory  Step 4 – Configuring the Parser for Errors: for DOM, a parser instance is configured with an error handler so that the parser can report validation errors. In case of SAX, the instance should be configured to specify the schema location and error handler.  Step 5 – Parsing of Document: document is parsed using configured DOM/SAX parser.

6 Schema compiling  The SchemaFactory class in the Validation APIs  Loads and prepares an XML schema by creating a javax.xml.validation.Schema object. This object is used for validating documents against the schema.  Resolves the resources and it can also encounter errors.  A schema object is that representation of memory which cannot be changed and schema instance can be used by many parser instances, even though they are running in different threads.

7 XML Validation against a Compiled Schema  The schemaLanguage and schemasource properties of a SAXParser are set by the user and the validation property is also set to true  Validation is achieved by the application by setting schema through schemaSource property or by relying on xsi:schemaLocation to specify the location of schema  In new Validation APIs only Schema instance is set on the factory and there is no need to set the validation to true. The setting of schemaLanguage or schemaSource property is also not required. Validation of XML document is done against the compiled schema set on the factory parser.setProperty(“http://java.sun.com/xml/properties/jaxp/schemaLanguage”, “http://www.w3.org/2001/XMLSchema”) parser.setProperty(“http://java.sun.com/xml/properties/jaxp/schemaSource”, “mySchema.xsd”)

8 Validating SAX & DOM Source  Create a ValidatorHandler object, which can validate a SAX stream  A stand-alone Validator object can also be created  The validation of SAXSource, a DOMSource, or an XML document against any schema can be done by a stand-alone Validator object

9 XML Validation after Transformation  Validating SAX stream  XML document and XSL are given as inputs to a Transformer object.  This object processes the XML document and XSL and passes the output to a ValidatorHandler object.  The ValidatorHandler object is responsible for validating SAX stream. Similarly, the schema is fed to the ValidatorHandler along with the SAX stream, which is the output of Transformer  Validating DOM  The transformed result from Transformation APIs can be obtained as a DOM object.  Schema can be used to validate this DOM object in the memory. Since there is no parsing involved when validating a transformed XML document, this approach boosts performance

10 Example public class SAXValidator { public static void main (String[] args) { try{ InputSource is = new InputSource(new BufferedReader(new FileReader(args[0]))); SchemaFactory factory = SchemaFactory.newInstance (XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File(args[1])); Schema schema = factory.newSchema (schemaFile); Validator validator = schema.newValidator (); validator.validate (new SAXSource(is)); System.out.println(args[0] + " document is valid!!!!"); }catch(Exception e){ e.printStackTrace (); }

11 Example (cont)

12 Example (cont)  xsd file

13 Example public class DOMValidator { public static void main (String[] args) { try{ DocumentBuilder parser = DocumentBuilderFactory.newInstance ().newDocumentBuilder (); Document doc = parser.parse (new File(args[0])); SchemaFactory factory = SchemaFactory.newInstance (XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File(args[1])); Schema schema = factory.newSchema (schemaFile); Validator validator = schema.newValidator (); validator.validate (new DOMSource(doc)); System.out.println(args[0] + " document is valid!"); }catch(Exception e){ e.printStackTrace (); }

14 Handling Schema Information  TypeInfoProvider provides access to the type information by javax.xml.validation.ValidatorHandler class  The declaration of Attribute( as ID type)  The declaration of Attribute in the original XML document  The type information of an element or attribute, as declare in schema.  The type info is returned by org.w3c.dom.TypeInfo object and is immutable object 14

15 Data Security  Validating against trusted schema also ensures that incoming instance document conforms to constraints and rules  Validating against a schema prevents documents from being validated against untrusted schemas. This prevents the errors that occur due to the elements and attributes that are part of different schema and are not part of the javax.xml.validation.Schema representation. On the whole, this approach prevents mistakes and access to malicious documents

16 XML Schema to Java Type Mapping  Data types, such as, xs:data, xs:dataTime, and xs:duration, can be created by DatatypeFactory.  It’s not necessary to understand the complexities of XML schema data types such as what types of operations are allowed on a data type, how to write a lexical representation, and so on.  The javax.xml.datatype APIs has defined a rich set to perform basic operations, such as addition, subtraction, and multiplication, over data types W3C XML Schema Data TypesJava Platform Data Types xs:date- XMLGregorianCalendar xs:dateTime- XMLGregorianCalendar xs:duration- Duration xs:gDay- XMLGregorianCalendar xs:gMonth- XMLGregorianCalendar xs:gMonthDay- XMLGregorianCalendar xs:gYear- XMLGregorianCalendar xs:YearMonth- XMLGregorianCalendar xs:time- XMLGregorianCalendar

17 WORKSHOP ACTIVITIES Building the console Java application using Validation Framework to validate DOM SAX


Download ppt "1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops."

Similar presentations


Ads by Google