KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > rdfcompare


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: rdfcompare.java,v 1.8 2005/02/21 11:49:12 andy_seaborne Exp $
28  */

29
30 package jena;
31
32 import com.hp.hpl.jena.rdf.model.*;
33
34 import java.net.URL JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36
37 /** A program which read two RDF models and determines if they are the same.
38  *
39  * <p>This program will read two RDF models, in a variety of languages,
40  * and compare them. Input can be read either from a URL or from a file.
41  * The program writes its results to the standard output stream and sets
42  * its exit code to 0 if the models are equal, to 1 if they are not and
43  * to -1 if it encounters an error.</p>
44  *
45  * <p></p>
46  *
47  * <pre>java jena.rdfcompare model1 model2 [lang1 [lang2]]
48  *
49  * model1 and model2 can be file names or URL's
50  * lang1 and lang2 specify the language of the input and can be:
51  * RDF/XML
52  * N-TRIPLE
53  * N3
54  * </pre>
55  *
56  * @author bwm
57  * @version $Name: $ $Revision: 1.8 $ $Date: 2005/02/21 11:49:12 $
58  */

59 public class rdfcompare extends java.lang.Object JavaDoc {
60
61     /**
62     * @param args the command line arguments
63     */

64     public static void main (String JavaDoc args[]) {
65         
66         if (args.length < 2 || args.length > 4) {
67             usage();
68             System.exit(-1);
69         }
70         
71         String JavaDoc in1 = args[0];
72         String JavaDoc in2 = args[1];
73         String JavaDoc lang1 = "RDF/XML";
74         if (args.length > 2) {
75             lang1 = args[2];
76         }
77         String JavaDoc lang2 = "N-TRIPLE";
78         if (args.length == 4) {
79             lang2 = args[3];
80         }
81         
82         System.out.println(in1 + " " + in2 + " " + lang1 + " " + lang2);
83         try {
84             Model m1 = ModelFactory.createDefaultModel();
85             Model m2 = ModelFactory.createDefaultModel();
86         
87             read(m1, in1, lang1);
88             read(m2, in2, lang2);
89         
90             if (m1.isIsomorphicWith(m2)) {
91                 System.out.println("models are equal");
92                 System.out.println();
93                 System.exit(0);
94             } else {
95                 System.out.println("models are unequal");
96                 System.out.println();
97                 System.exit(1);
98             }
99         } catch (Exception JavaDoc e) {
100             System.err.println("Unhandled exception:");
101             System.err.println(" " + e.toString());
102             System.exit(-1);
103         }
104     }
105     
106     protected static void usage() {
107         System.err.println("usage:");
108         System.err.println(
109             " java jena.rdfcompare source1 source2 [lang1 [lang2]]");
110         System.err.println();
111         System.err.println(" source1 and source2 can be URL's or filenames");
112         System.err.println(" lang1 and lang2 can take values:");
113         System.err.println(" RDF/XML");
114         System.err.println(" N-TRIPLE");
115         System.err.println(" N3");
116         System.err.println(" lang1 defaults to RDF/XML, lang2 to N-TRIPLE");
117         System.err.println();
118     }
119     
120     protected static void read(Model model, String JavaDoc in, String JavaDoc lang)
121       throws java.io.FileNotFoundException JavaDoc {
122         try {
123             URL JavaDoc url = new URL JavaDoc(in);
124             model.read(in, lang);
125         } catch (java.net.MalformedURLException JavaDoc e) {
126             model.read(new FileInputStream JavaDoc(in), "", lang);
127         }
128     }
129 }
130
Popular Tags