1 28 29 package example15; 30 31 import java.io.ByteArrayInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.FileReader ; 37 import java.io.InputStreamReader ; 38 39 import org.jibx.extras.DocumentComparator; 40 import org.jibx.runtime.*; 41 42 43 56 57 public class Test { 58 59 private static final String VERSION_URI = null; 61 private static final String VERSION_NAME = "version"; 62 63 private static String [] VERSION_TEXTS = { 65 "1.0", "1.1", "1.2" 66 }; 67 68 private static String [] VERSION_BINDINGS = { 70 "binding0", "binding1", "binding2" 71 }; 72 73 public static void main(String [] args) { 74 if (args.length == 1) { 75 76 File temp = new File ("temp.xml"); 78 if (temp.exists()) { 79 temp.delete(); 80 } 81 try { 82 83 BindingSelector select = new BindingSelector(VERSION_URI, 85 VERSION_NAME, VERSION_TEXTS, VERSION_BINDINGS); 86 IUnmarshallingContext context = select.getContext(); 87 context.setDocument(new FileInputStream (args[0]), null); 88 Customer customer = (Customer)select. 89 unmarshalVersioned(Customer.class); 90 91 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 93 select.setOutput(bos, "UTF-8"); 94 select.marshalVersioned(customer, customer.version); 95 96 InputStreamReader brdr = new InputStreamReader 98 (new ByteArrayInputStream (bos.toByteArray()), "UTF-8"); 99 FileReader frdr = new FileReader (args[0]); 100 FileOutputStream fos = new FileOutputStream ("temp.xml"); 101 fos.write(bos.toByteArray()); 102 fos.close(); 103 DocumentComparator comp = new DocumentComparator(System.err); 104 if (!comp.compare(frdr, brdr)) { 105 106 System.err.println("Error testing on input file " + args[0]); 111 System.err.println("Saved output document file path " + 112 temp.getAbsolutePath()); 113 System.exit(1); 114 } 115 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 120 } else { 121 System.err.println("Usage: java exampl15.Test in-file\n" + 122 "Leaves output as temp.xml in case of error"); 123 System.exit(1); 124 } 125 } 126 } 127 128 | Popular Tags |