File: Test.java

package info (click to toggle)
libjibx1.2-java 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,260 kB
  • sloc: java: 75,013; xml: 14,068; makefile: 17
file content (101 lines) | stat: -rw-r--r-- 3,931 bytes parent folder | download | duplicates (6)
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

package example22;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;

import org.jibx.extras.DocumentComparator;
import org.jibx.runtime.*;


/**
 * Test program for the JiBX framework. Works with two or three command line
 * arguments: mapped-class, in-file, and out-file (optional, only needed if
 * different from in-file). You can also supply a multiple of three input
 * arguments, in which case each set of three is processed in turn (in this case
 * the out-file is required). Unmarshals documents from files using the binding
 * defined for the mapped class, then marshals them back out using the same
 * bindings and compares the results. In case of a comparison error the output
 * file is left as <i>temp.xml</i>.
 * 
 * @author Dennis M. Sosnoski
 * @version 1.0
 */

public class Test {
    
    // definitions for version attribute on document root element
    private static final String VERSION_URI = null;
    private static final String VERSION_NAME = "version";
    
    // attribute text strings used for different document versions
    private static String[] VERSION_TEXTS = {
        "1.0", "1.1", "1.2"
    };
    
    // binding names corresponding to text strings
    private static String[] VERSION_BINDINGS = {
        "binding0", "binding1", "binding2"
    };

    public static void main(String[] args) {
        if (args.length == 1) {
            
            // delete generated output file if present
            File temp = new File("temp.xml");
            if (temp.exists()) {
                temp.delete();
            }
            try {
                
                // process input file according to declared version
                BindingSelector select = new BindingSelector(VERSION_URI,
                    VERSION_NAME, VERSION_TEXTS, VERSION_BINDINGS);
                IUnmarshallingContext context = select.getContext();
                context.setDocument(new FileInputStream(args[0]), null);
                Customer customer = (Customer)select.
                    unmarshalVersioned(Customer.class);
                
                // now marshal to in-memory array with same document version
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                select.setOutput(bos, "UTF-8");
                select.marshalVersioned(customer, customer.version);
                
                // run comparison of output with original document
                InputStreamReader brdr = new InputStreamReader
                    (new ByteArrayInputStream(bos.toByteArray()), "UTF-8");
                FileReader frdr = new FileReader(args[0]);
                FileOutputStream fos = new FileOutputStream("temp.xml");
                fos.write(bos.toByteArray());
                fos.close();
                DocumentComparator comp = new DocumentComparator(System.err);
                if (!comp.compare(frdr, brdr)) {
                    
                    // report mismatch with output saved to file
//                    FileOutputStream fos = new FileOutputStream("temp.xml");
//                    fos.write(bos.toByteArray());
//                    fos.close();
                    System.err.println("Error testing on input file " + args[0]);
                    System.err.println("Saved output document file path " +
                        temp.getAbsolutePath());
                    System.exit(1);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
            
        } else {
            System.err.println("Usage: java exampl22.Test in-file\n" +
                "Leaves output as temp.xml in case of error");
            System.exit(1);
        }
    }
}