001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * $Id: rdfcopy.java,v 1.14 2008/01/02 12:08:16 andy_seaborne Exp $
028: */
029:
030: package jena;
031:
032: import com.hp.hpl.jena.shared.JenaException;
033: import com.hp.hpl.jena.rdf.model.*;
034:
035: import java.net.*;
036: import java.io.*;
037:
038: /** A program which read an RDF model and copy it to the standard output stream.
039: *
040: * <p>This program will read an RDF model, in a variety of languages,
041: * and copy it to the output stream in a possibly different langauge.
042: * Input can be read either from a URL or from a file.
043: * The program writes its results to the standard output stream and sets
044: * its exit code to 0 if the program terminate normally, and
045: * to -1 if it encounters an error.</p>
046: *
047: * <p></p>
048: *
049: * <pre>java jena.rdfcopy model [inlang [outlang]]
050: *
051: * model1 and model2 can be file names or URL's
052: * inlang and outlang specify the language of the input and output
053: * respectively and can be:
054: * RDF/XML
055: * N-TRIPLE
056: * N3
057: * The input language defaults to RDF/XML and the output language
058: * defaults to N-TRIPLE.
059: * </pre>
060: *
061: * @author bwm
062: * @version $Name: $ $Revision: 1.14 $ $Date: 2008/01/02 12:08:16 $
063: */
064: public class rdfcopy extends java.lang.Object {
065:
066: /**
067: * @param args the command line arguments
068: */
069: public static void main(String args[]) {
070:
071: if (args.length < 1) {
072: usage();
073: System.exit(-1);
074: }
075:
076: String in = args[0];
077: String inlang = "RDF/XML";
078: int j;
079: for (j = 1; j < args.length && args[j].indexOf("=") != -1; j++)
080: ;
081: int lastInProp = j;
082: if (j < args.length) {
083: inlang = args[j];
084: }
085: j++;
086: String outlang = "N-TRIPLE";
087: for (; j < args.length && args[j].indexOf("=") != -1; j++)
088: ;
089: int lastOutProp = j;
090: if (j < args.length) {
091: outlang = args[j];
092: }
093: if (j + 1 < args.length) {
094: // System.err.println(j+"<<"+args.length);
095: usage();
096: System.exit(-1);
097: }
098:
099: try {
100: Model m = ModelFactory.createDefaultModel();
101: String base = in;
102: RDFReader rdr = m.getReader(inlang);
103: for (j = 1; j < lastInProp; j++) {
104: int eq = args[j].indexOf("=");
105: rdr.setProperty(args[j].substring(0, eq), args[j]
106: .substring(eq + 1));
107: }
108:
109: try {
110: rdr.read(m, in);
111: } catch (JenaException ex) {
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:///"
118: + f.getCanonicalPath().replace('\\', '/');
119: rdr.read(m, new FileInputStream(in), base);
120: }
121: //rdr.read(m, in);
122: RDFWriter w = m.getWriter(outlang);
123: j++;
124: for (; j < lastOutProp; j++) {
125: int eq = args[j].indexOf("=");
126: w.setProperty(args[j].substring(0, eq), args[j]
127: .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
141: .println(" java jena.rdfcopy in {inprop=inval}* [ inlang {outprop=outval}* outlang]]");
142: System.err.println();
143: System.err.println(" in can be a URL or a filename");
144: System.err.println(" inlang and outlang can take values:");
145: System.err.println(" RDF/XML");
146: System.err.println(" RDF/XML-ABBREV");
147: System.err.println(" N-TRIPLE");
148: System.err.println(" N3");
149: System.err
150: .println(" inlang defaults to RDF/XML, outlang to N-TRIPLE");
151: System.err
152: .println(" The legal values for inprop and outprop depend on inlang and outlang.");
153: System.err
154: .println(" The legal values for inval and outval depend on inprop and outprop.");
155: System.err.println();
156: }
157:
158: protected static void read(Model model, String in, String lang)
159: throws java.io.FileNotFoundException {
160: try {
161: URL url = new URL(in);
162: model.read(in, lang);
163: } catch (java.net.MalformedURLException e) {
164: model.read(new FileInputStream(in), "", lang);
165: }
166: }
167: }
|