1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
import java.util.Properties;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Alambic Talon
* @since 1.5
* @version 1.0
*
*/
public class XMLTests {
private static Properties xmlProps = null;
private static Logger log = null;
private static Document docRequest = null;
private static String xmlRequest = null;
private static boolean estValide = false;
private static DefaultHandler dh = null;
/**
* Constructor
*
* @throws FileNotFoundException Cannot found the properties file
* @throws IOException Cannot read properties file
*/
public XMLTests() throws FileNotFoundException, IOException {
String propertiesFile = new String(System.getProperty("XMLTests.prefs","XMLTests.properties"));
PropertyConfigurator.configure(propertiesFile);
log = Logger.getLogger(XMLTests.class);
xmlProps = new Properties();
log.info("Starting initialization of XMLTests class.");
try {
FileInputStream xmlPropsFile = new FileInputStream(propertiesFile);
xmlProps.load(xmlPropsFile);
xmlPropsFile.close();
}
catch (FileNotFoundException e) {
log.fatal("Cannot initialize the application, properties file does not exist.", e);
throw e;
}
catch (IOException e) {
log.fatal("Cannot initialize the application, properties file does not exist.", e);
throw e;
}
dh = new DefaultHandler(){
public void fatalError(SAXException e) {
log.fatal("Parsing error with SAX:\n", e);
estValide = false;
}
public void error(SAXException e) {
log.error("Parsing error with SAX:\n", e);
estValide = false;
}
public void warning(SAXException e) {
log.warn("Parsing warning with SAX:\n", e);
estValide = false;
}
};
log.info("XMLTests initialization completed.");
}
public void setRequest() {
/*
* Read standard input and store the result in a string for use with
* the remote method.
*/
log.debug("Lecture de la requête...");
StringBuffer request = null;
try {
String line = null;
request = new StringBuffer(4096);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in,"UTF-8"));
while ( (line = in.readLine()) != null ) {
request.append(line).append('\n');
}
xmlRequest = request.toString();
log.debug("La requête a été lue avec succès.");
}
catch(UnsupportedEncodingException e) {
log.fatal("Impossible de lire la requête, l'encodage n'est pas supporté:\n", e);
}
catch(IOException e) {
log.fatal("Erreur d'entrée/sortie sur la lecture de la requête:\n",e);
}
}
public static String getRequest() {
return(xmlRequest);
}
public static String getSchemaFilename() {
return(xmlProps.getProperty("XMLTests.schema.filename"));
}
private static void validationSAXImplicite(String xmlString, String schemaFilename) {
// Création du schéma
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
File schemaFile = new File(schemaFilename);
if ( schemaFile.exists() ) {
if ( schemaFile.canRead() ) {
Schema schema = null;
try {
schema = factory.newSchema(new StreamSource(schemaFile));
}
catch (SAXException ex) {
log.error("Impossible de créer le schéma à partir du fichier: " + schemaFilename + "\n", ex);
}
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
if ( schema != null ) {
spf.setSchema(schema);
try {
SAXParser sp = spf.newSAXParser();
estValide = true;
sp.parse(new InputSource(new StringReader(xmlString)), dh);
if ( estValide ) {
log.info("La requête est valide.");
} else {
log.error("La requête n'est pas valide.");
}
}
catch (ParserConfigurationException e) {
log.error("Impossible de définir la configuratio:\n", e);
}
catch (IOException e) {
log.error("Impossible de lire le schéma:\n", e);
}
catch (SAXException e) {
log.error("La requête est invalide:\n", e);
}
}
} else {
log.error("Le fichier schéma existe, mais n'est pas accessible en lecture.");
}
} else {
log.error("Le fichier schéma: " + schemaFilename + " n'existe pas.");
}
}
private static void validationDOMImplicite(String xmlString, String schemaFilename) {
final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
dbf.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaFilename));
try {
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(dh);
estValide = true;
Document doc = db.parse(new InputSource(new StringReader(xmlString)));
if ( estValide ) {
log.info("La requête est valide.");
} else {
log.error("La requête n'est pas valide.");
}
}
catch (ParserConfigurationException e) {
log.error("Impossible de mettre en place la configuration:\n", e);
}
catch (IOException e) {
log.error("Incapable de lire le schema:\n", e);
}
catch (SAXException e) {
log.error("Requête invalide:\n", e);
}
}
private static void validationDOMExplicite(String xmlString, String schemaFilename) {
// Création du schéma
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
File schemaFile = new File(schemaFilename);
if ( schemaFile.exists() ) {
if ( schemaFile.canRead() ) {
Validator validator = null;
try {
Schema schema = factory.newSchema(new StreamSource(schemaFile));
validator = schema.newValidator();
}
catch (SAXException ex) {
log.error("Impossible de créer le schéma à partir du fichier: " + schemaFilename + "\n", ex);
}
// Création du document DOM
DocumentBuilderFactory domBFact = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = domBFact.newDocumentBuilder();
docRequest = builder.parse(new InputSource(new StringReader(xmlString)));
}
catch (IOException e) {
log.fatal("Erreur dans la lecture de la requête:\n", e);
}
catch (ParserConfigurationException e) {
log.fatal("Impossible de créer le constructeur de documents:\n", e);
}
catch (SAXException e) {
log.fatal("Incapable de lire le document:\n", e);
}
try {
DOMSource domSource = new DOMSource(docRequest);
validator.validate(domSource);
log.info("La requête est valide.");
}
catch (SAXException e) {
log.error("La requête est invalide.\n", e);
}
catch (IOException e) {
log.error("Impossible de lire la requête.\n", e);
}
} else {
log.error("Le fichier schéma existe, mais n'est pas accessible en lecture.");
}
} else {
log.error("Le fichier schéma: " + schemaFilename + " n'existe pas.");
}
}
/**
* Instantiate the class, read the standard input character string which is the email
* from sendmail, connect to the remote server and call the remote method with this
* string as the argument.
* @param args None
*
*/
public static void main(String[] args) {
int exitCode = 0;
XMLTests xmlTests = null;
// Creates the object and see if everything initialize properly
try {
xmlTests = new XMLTests();
xmlTests.setRequest();
}
catch (FileNotFoundException e) {
log.fatal("Unable to initialize the application.", e);
}
catch (IOException e) {
log.fatal("Unable to initialize the application.", e);
}
catch (NullPointerException e) {
log.fatal("Unable to initialize the application.", e);
}
log.info("Validation DOM explicite.");
validationDOMExplicite(getRequest(), getSchemaFilename());
log.info("Validation DOM implicite.");
validationDOMImplicite(getRequest(), getSchemaFilename());
log.info("Validation SAX implicite.");
validationSAXImplicite(getRequest(), getSchemaFilename());
log.info("Fin des validations.");
System.exit(exitCode);
}
} |
Partager