Source code: jena/rdfcopy.java
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: rdfcopy.java,v 1.11 2005/02/21 11:49:12 andy_seaborne Exp $
28 */
29
30 package jena;
31
32 import com.hp.hpl.jena.shared.JenaException ;
33 import com.hp.hpl.jena.rdf.model.*;
34
35 import java.net.*;
36 import java.io.*;
37
38 /** A program which read an RDF model and copy it to the standard output stream.
39 *
40 * <p>This program will read an RDF model, in a variety of languages,
41 * and copy it to the output stream in a possibly different langauge.
42 * Input can be read either from a URL or from a file.
43 * The program writes its results to the standard output stream and sets
44 * its exit code to 0 if the program terminate normally, and
45 * to -1 if it encounters an error.</p>
46 *
47 * <p></p>
48 *
49 * <pre>java jena.rdfcopy model [inlang [outlang]]
50 *
51 * model1 and model2 can be file names or URL's
52 * inlang and outlang specify the language of the input and output
53 * respectively and can be:
54 * RDF/XML
55 * N-TRIPLE
56 * N3
57 * The input language defaults to RDF/XML and the output language
58 * defaults to N-TRIPLE.
59 * </pre>
60 *
61 * @author bwm
62 * @version $Name: $ $Revision: 1.11 $ $Date: 2005/02/21 11:49:12 $
63 */
64 public class rdfcopy extends java.lang.Object {
65
66 /**
67 * @param args the command line arguments
68 */
69 public static void main(String args[]) {
70
71 if (args.length < 1) {
72 usage();
73 System.exit(-1);
74 }
75
76 String in = args[0];
77 String inlang = "RDF/XML";
78 int j;
79 for (j = 1; j < args.length && args[j].indexOf("=") != -1; j++);
80 int lastInProp = j;
81 if (j < args.length) {
82 inlang = args[j];
83 }
84 j++;
85 String outlang = "N-TRIPLE";
86 for (; j < args.length && args[j].indexOf("=") != -1; j++);
87 int lastOutProp = j;
88 if (j < args.length) {
89 outlang = args[j];
90 }
91 if (j + 1 < args.length) {
92 // System.err.println(j+"<<"+args.length);
93 usage();
94 System.exit(-1);
95 }
96
97 try {
98 Model m = ModelFactory.createDefaultModel();
99 String base = in ;
100 RDFReader rdr = m.getReader(inlang);
101 for (j = 1; j < lastInProp; j++) {
102 int eq = args[j].indexOf("=");
103 rdr.setProperty(
104 args[j].substring(0, eq),
105 args[j].substring(eq + 1));
106 }
107
108 try {
109 rdr.read(m, in);
110 } catch (JenaException ex)
111 {
112 if ( ! ( ex.getCause() instanceof MalformedURLException ) )
113 throw ex ;
114 // Tried as a URL. Try as a file name.
115 // Make absolute
116 File f = new File(in) ;
117 base = "file:///"+f.getCanonicalPath().replace('\\','/') ;
118 rdr.read(m, new FileInputStream(in), base) ;
119 }
120 //rdr.read(m, in);
121 RDFWriter w = m.getWriter(outlang);
122 j++;
123 for (; j < lastOutProp; j++) {
124 int eq = args[j].indexOf("=");
125 w.setProperty(
126 args[j].substring(0, eq),
127 args[j].substring(eq + 1));
128 }
129 w.write(m,System.out,base);
130 System.exit(0);
131 } catch (Exception e) {
132 System.err.println("Unhandled exception:");
133 System.err.println(" " + e.toString());
134 System.exit(-1);
135 }
136 }
137
138 protected static void usage() {
139 System.err.println("usage:");
140 System.err.println(" java jena.rdfcopy in {inprop=inval}* [ inlang {outprop=outval}* outlang]]");
141 System.err.println();
142 System.err.println(" in can be a URL or a filename");
143 System.err.println(" inlang and outlang can take values:");
144 System.err.println(" RDF/XML");
145 System.err.println(" RDF/XML-ABBREV");
146 System.err.println(" N-TRIPLE");
147 System.err.println(" N3");
148 System.err.println(
149 " inlang defaults to RDF/XML, outlang to N-TRIPLE");
150 System.err.println(" The legal values for inprop and outprop depend on inlang and outlang.");
151 System.err.println(" The legal values for inval and outval depend on inprop and outprop.");
152 System.err.println();
153 }
154
155 protected static void read(Model model, String in, String lang)
156 throws java.io.FileNotFoundException {
157 try {
158 URL url = new URL(in);
159 model.read(in, lang);
160 } catch (java.net.MalformedURLException e) {
161 model.read(new FileInputStream(in), "", lang);
162 }
163 }
164 }