1 package org.openprovenance.model;
2 import java.io.File;
3 import java.io.InputStream;
4 import java.io.IOException;
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.Unmarshaller;
7 import javax.xml.bind.JAXBException;
8 import javax.xml.bind.JAXBElement;
9 import org.w3c.dom.Element;
10 import org.xml.sax.SAXException;
11
12 import javax.xml.validation.SchemaFactory;
13 import javax.xml.validation.Schema;
14 import javax.xml.transform.stream.StreamSource;
15 import javax.xml.transform.Source;
16
17 import org.openprovenance.model.printer.OPMPrinterConfiguration;
18
19 /*** Deserialiser of OPM Graphs. */
20 public class OPMDeserialiser {
21
22
23
24
25
26
27
28
29 static protected JAXBContext jc;
30
31 public OPMDeserialiser () throws JAXBException {
32 if (jc==null)
33 jc = JAXBContext.newInstance( OPMFactory.packageList );
34
35
36 }
37
38 public OPMDeserialiser (String packageList) throws JAXBException {
39 if (jc==null)
40 jc = JAXBContext.newInstance(packageList);
41 }
42
43
44 private static ThreadLocal<OPMDeserialiser> threadDeserialiser=
45 new ThreadLocal<OPMDeserialiser> () {
46 protected synchronized OPMDeserialiser initialValue () {
47 try {
48 return new OPMDeserialiser();
49 } catch (JAXBException jxb) {
50 throw new RuntimeException("OPMDeserialiser: deserialiser init failure()");
51 }
52 }
53 };
54
55 public static OPMDeserialiser getThreadOPMDeserialiser() {
56 return threadDeserialiser.get();
57 }
58
59 public OPMGraph deserialiseOPMGraph (Element serialised)
60 throws JAXBException {
61 Unmarshaller u=jc.createUnmarshaller();
62 JAXBElement<OPMGraph> root= u.unmarshal(serialised,OPMGraph.class);
63 OPMGraph res=root.getValue();
64 return res;
65 }
66
67 public OPMGraph deserialiseOPMGraph (File serialised)
68 throws JAXBException {
69 Unmarshaller u=jc.createUnmarshaller();
70 Object root= u.unmarshal(serialised);
71 OPMGraph res=(OPMGraph)((JAXBElement<OPMGraph>) root).getValue();
72 return res;
73 }
74
75
76 public OPMGraph validateOPMGraph (String[] schemaFiles, File serialised)
77 throws JAXBException,SAXException, IOException {
78 SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
79 Source [] sources=new Source[2+schemaFiles.length];
80 sources[0]=new StreamSource(this.getClass().getResourceAsStream("/"+"opm.1_1.xsd"));
81 int i=0;
82 for (String schemaFile: schemaFiles) {
83 sources[2+i]=new StreamSource(new File(schemaFile));
84 i++;
85 }
86 Schema schema = sf.newSchema(sources);
87 Unmarshaller u=jc.createUnmarshaller();
88
89 u.setSchema(schema);
90 Object root= u.unmarshal(serialised);
91 OPMGraph res=(OPMGraph)((JAXBElement<OPMGraph>) root).getValue();
92 return res;
93 }
94
95 public static void main(String [] args) {
96 OPMDeserialiser deserial=OPMDeserialiser.getThreadOPMDeserialiser();
97 if ((args==null) || (args.length==0)) {
98 System.out.println("Usage: opmxml-validate <filename> {schemaFiles}*");
99 return;
100 }
101 File f=new File(args[0]);
102 String [] schemas=new String[args.length-1];
103 for (int i=1; i< args.length; i++) {
104 schemas[i-1]=args[i];
105 }
106 try {
107 deserial.validateOPMGraph(schemas,f);
108 System.out.println(args[0] + " IS a valid OPM graph");
109 return ;
110 } catch (JAXBException je) {
111 je.printStackTrace();
112 System.out.println(args[0] + " IS NOT a valid OPM graph");
113 } catch (SAXException je) {
114 je.printStackTrace();
115 System.out.println(args[0] + " IS NOT a valid OPM graph");
116 } catch (IOException io) {
117 io.printStackTrace();
118 System.out.println(args[0] + " IS NOT a valid OPM graph");
119 }
120 }
121 }