1 2 import com.saxonica.SchemaAwareTransformerFactory; 3 import net.sf.saxon.FeatureKeys; 4 import net.sf.saxon.om.Validation; 5 import org.xml.sax.helpers.DefaultHandler ; 6 7 import javax.xml.transform.Transformer ; 8 import javax.xml.transform.TransformerException ; 9 import javax.xml.transform.TransformerFactory ; 10 import javax.xml.transform.sax.SAXResult ; 11 import javax.xml.transform.stream.StreamSource ; 12 import java.io.File ; 13 14 18 19 public class QuickValidator { 20 21 private QuickValidator() { 22 } 23 24 29 30 public static void main(String [] args) throws Exception { 31 if (args.length < 1) { 32 System.err.println("No source file supplied"); 33 } 34 try { 35 System.setProperty("javax.xml.transform.TransformerFactory", 36 "com.saxonica.SchemaAwareTransformerFactory"); 37 TransformerFactory factory = TransformerFactory.newInstance(); 38 factory.setAttribute(FeatureKeys.SCHEMA_VALIDATION, new Integer (Validation.STRICT)); 39 factory.setAttribute(FeatureKeys.VALIDATION_WARNINGS, Boolean.TRUE); 40 if (args.length > 1) { 41 StreamSource schema = new StreamSource (new File (args[1]).toURI().toString()); 42 ((SchemaAwareTransformerFactory)factory).addSchema(schema); 43 } 44 Transformer trans = factory.newTransformer(); 45 StreamSource source = new StreamSource (new File (args[0]).toURI().toString()); 46 SAXResult sink = new SAXResult (new DefaultHandler ()); 47 trans.transform(source, sink); 48 } catch (TransformerException err) { 49 System.err.println("Validation failed: " + err.getMessage()); 50 } 51 } 52 53 } 54 | Popular Tags |