1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.BufferedWriter ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.OutputStreamWriter ; 26 27 import org.xml.sax.InputSource ; 28 29 import com.thaiopensource.util.PropertyMapBuilder; 30 import com.thaiopensource.validate.SchemaReader; 31 import com.thaiopensource.validate.ValidateProperty; 32 import com.thaiopensource.validate.ValidationDriver; 33 import com.thaiopensource.validate.auto.AutoSchemaReader; 34 import com.thaiopensource.xml.sax.ErrorHandlerImpl; 35 36 import org.apache.log4j.Category; 37 38 41 public class RelaxNG { 42 static Category log = Category.getInstance(RelaxNG.class); 43 44 47 public static void main(String [] args) { 48 if(args.length == 0) { 49 System.out.println("Usage: relaxng.rng sample.xml"); 50 return; 51 } 52 53 try { 54 String message = RelaxNG.validate(new File (args[0]), new File (args[1])); 55 if (message == null) { 56 System.out.println("Document is valid"); 57 } else { 58 System.out.println("Document not valid: " + message); 59 } 60 } catch (Exception e) { 61 System.err.println("" + e); 62 } 63 } 64 65 68 public static String validate(File schema, File xml) throws Exception { 69 InputSource in = ValidationDriver.uriOrFileInputSource(schema.getAbsolutePath()); 70 PropertyMapBuilder properties = new PropertyMapBuilder(); 71 ByteArrayOutputStream error = new ByteArrayOutputStream (); 72 ErrorHandlerImpl eh = new ErrorHandlerImpl(new BufferedWriter (new OutputStreamWriter (error))); 73 ValidateProperty.ERROR_HANDLER.put(properties, eh); 74 SchemaReader schemaReader = new AutoSchemaReader(); 75 ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader); 76 if (driver.loadSchema(in)) { 77 if (driver.validate(ValidationDriver.uriOrFileInputSource(xml.getAbsolutePath()))) { 78 log.debug("" + error); 79 return null; 80 } else { 81 log.error("" + error); 82 return "" + error; 83 } 84 } else { 85 throw new Exception ("Could not load schema!\n" + error); 86 } 87 } 88 } 89 | Popular Tags |