Mercurial > hg > MetaDataManager
comparison src/de/mpiwg/itgroup/metadataManager/validation/IndexMetaValidator.java @ 1:2267d8c80a99
intial
author | dwinter |
---|---|
date | Sun, 23 Oct 2011 21:29:45 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:90a19cbda471 | 1:2267d8c80a99 |
---|---|
1 package de.mpiwg.itgroup.metadataManager.validation; | |
2 | |
3 import java.io.ByteArrayInputStream; | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 import java.io.StringReader; | |
7 import java.io.UnsupportedEncodingException; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 import org.jdom.JDOMException; | |
11 import org.jdom.input.SAXBuilder; | |
12 import org.xml.sax.SAXException; | |
13 import org.xml.sax.SAXParseException; | |
14 import org.xml.sax.helpers.DefaultHandler; | |
15 | |
16 public class IndexMetaValidator { | |
17 | |
18 private Logger logger = Logger.getRootLogger(); | |
19 | |
20 public static boolean validate(String indexMeta) throws UnsupportedEncodingException{ | |
21 IndexMetaValidator iv = new IndexMetaValidator(); | |
22 InputStream is = new ByteArrayInputStream(indexMeta.getBytes("utf-8")); | |
23 | |
24 String schemaUrl="/Users/dwinter/Documents/Projekte/ECHO-eSciDoc-MPDL/escidocMPIWG/MetaDataManager/src/de/mpiwg/itgroup/metadataManager/validation/data/index_meta.xsd"; | |
25 | |
26 iv.validateSchema(schemaUrl, is); | |
27 return false;} | |
28 | |
29 | |
30 | |
31 public boolean validateSchema(String SchemaUrl, InputStream xmlDocumentStream) { | |
32 try { //Create SAXBuilder object | |
33 SAXBuilder saxBuilder = new SAXBuilder( | |
34 "org.apache.xerces.parsers.SAXParser", true); | |
35 | |
36 //Set SAXBuilder parser to be a validating parser | |
37 saxBuilder.setValidation(true); | |
38 saxBuilder.setFeature( | |
39 "http://apache.org/xml/features/validation/schema", true); | |
40 saxBuilder.setFeature( | |
41 "http://apache.org/xml/features/validation/schema-full-checking",true); | |
42 saxBuilder.setProperty( | |
43 "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",SchemaUrl); | |
44 | |
45 //Create a ErrorHandler and set ErrorHandler on parser. | |
46 Validator handler = new Validator(); | |
47 saxBuilder.setErrorHandler(handler); | |
48 //Parse XML Document | |
49 saxBuilder.build(xmlDocumentStream); | |
50 //Output Validation Errors | |
51 if (handler.validationError == true){ | |
52 logger.debug("XML Document has Error:" | |
53 + handler.validationError + " " | |
54 + handler.saxParseException.getMessage()); | |
55 return false;} | |
56 else{ | |
57 logger.debug("XML Document is valid"); | |
58 return true;} | |
59 | |
60 } catch (JDOMException jde) { | |
61 logger.debug(jde); | |
62 } | |
63 | |
64 catch (IOException ioe) { | |
65 } | |
66 return false; | |
67 | |
68 } | |
69 | |
70 //Error Handler class | |
71 private class Validator extends DefaultHandler { | |
72 public boolean validationError = false; | |
73 | |
74 public SAXParseException saxParseException = null; | |
75 | |
76 public void error(SAXParseException exception) throws SAXException { | |
77 validationError = true; | |
78 saxParseException = exception; | |
79 } | |
80 | |
81 public void fatalError(SAXParseException exception) throws SAXException { | |
82 validationError = true; | |
83 saxParseException = exception; | |
84 } | |
85 | |
86 public void warning(SAXParseException exception) throws SAXException { | |
87 } | |
88 } | |
89 | |
90 | |
91 | |
92 } |