001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package jena;
007:
008: import java.io.*;
009: import jena.cmdline.*;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.shared.*;
013: import com.hp.hpl.jena.util.FileUtils;
014: import com.hp.hpl.jena.n3.*;
015:
016: /**
017: Read N3 files and print in a variery of formats.
018: * @author Andy Seaborne
019: * @version $Id: n3.java,v 1.19 2008/01/02 12:08:16 andy_seaborne Exp $
020: */
021: public class n3 {
022: static boolean firstOutput = true;
023: static boolean doNodeTest = true;
024: static boolean doErrorTests = false;
025:
026: static int testCount = 0;
027:
028: static boolean doRDF = false; // Attempt to create RDF
029: static boolean printRDF = false; // List RDF
030: static String outputLang = "N-TRIPLE";
031: static boolean printN3 = true; // List the N3 processed
032: static boolean debug = false; // Help!
033: static boolean verbose = false;
034:
035: //static final String NL = JenaRuntime.getLineSeparator();
036:
037: // Parse a file (no RDF production)
038:
039: public static void main(String[] args) {
040: String dir = System.getProperty("user.dir");
041:
042: String usageMessage = n3.class.getName()
043: + " [-rdf] [-base URI] [filename]";
044:
045: CommandLine cmd = new CommandLine();
046: cmd.setUsage(usageMessage);
047: //cmd.setOutput(System.err) ;
048:
049: //cmd.setHook(cmd.trace()) ;
050:
051: ArgDecl verboseDecl = new ArgDecl(false, "-v", "--verbose");
052: ArgDecl helpDecl = new ArgDecl(false, "-h", "--help");
053: ArgDecl rdfDecl = new ArgDecl(false, "-rdf", "--rdf");
054: ArgDecl rdfRDFN3Decl = new ArgDecl(false, "--rdf-n3");
055: ArgDecl rdfRDFXMLDecl = new ArgDecl(false, "--rdf-xml");
056: ArgDecl rdfRDFNTDecl = new ArgDecl(false, "--rdf-nt");
057: ArgDecl rdfRDFFormat = new ArgDecl(true, "--format", "--fmt");
058: ArgDecl debugDecl = new ArgDecl(false, "-debug");
059: ArgDecl baseDecl = new ArgDecl(true, "-base");
060: //ArgDecl outputDecl = new ArgDecl(true, "-output", "-o") ;
061: ArgDecl checkDecl = new ArgDecl(false, "-n", "--check");
062:
063: cmd.add(verboseDecl);
064: cmd.add(helpDecl);
065: cmd.add(rdfDecl);
066: cmd.add(rdfRDFN3Decl);
067: cmd.add(rdfRDFXMLDecl);
068: cmd.add(rdfRDFNTDecl);
069: cmd.add(rdfRDFFormat);
070: cmd.add(debugDecl);
071: cmd.add(baseDecl);
072: cmd.add(checkDecl);
073:
074: try {
075: cmd.process(args);
076: } catch (IllegalArgumentException illEx) {
077: System.exit(1);
078: }
079:
080: verbose = cmd.contains(verboseDecl);
081:
082: if (cmd.contains(helpDecl)) {
083: System.out.println(usageMessage);
084: System.out.println("Default action: parse an N3 file");
085: System.out
086: .println(" --rdf Read into an RDF and print");
087: System.out
088: .println(" --rdf-n3 Read into an RDF and print in N3");
089: System.out
090: .println(" --rdf-xml Read into an RDF and print in XML");
091: System.out
092: .println(" --rdf-nt Read into an RDF and print in N-Triples");
093: System.out
094: .println(" --format FMT Read into an RDF and print in given format");
095: System.out
096: .println(" --check | -n Just check: no output");
097: System.out.println(" --base URI Set the base URI");
098: System.exit(0);
099: }
100:
101: String baseName = null;
102:
103: if (cmd.contains(rdfDecl)) {
104: doRDF = true;
105: printRDF = true;
106: printN3 = false;
107: }
108:
109: if (cmd.contains(rdfRDFN3Decl)) {
110: doRDF = true;
111: printRDF = true;
112: outputLang = "N3";
113: printN3 = false;
114: }
115:
116: if (cmd.contains(rdfRDFXMLDecl)) {
117: doRDF = true;
118: printRDF = true;
119: outputLang = "RDF/XML-ABBREV";
120: printN3 = false;
121: }
122:
123: if (cmd.contains(rdfRDFNTDecl)) {
124: doRDF = true;
125: printRDF = true;
126: outputLang = "N-TRIPLE";
127: printN3 = false;
128: }
129:
130: if (cmd.contains(rdfRDFFormat)) {
131: doRDF = true;
132: printRDF = true;
133: outputLang = cmd.getArg(rdfRDFFormat).getValue();
134: printN3 = false;
135: }
136:
137: if (cmd.contains(debugDecl)) {
138: debug = true;
139: N3JenaWriter.DEBUG = true;
140: }
141:
142: if (cmd.contains(checkDecl)) {
143: printRDF = false;
144: printN3 = false;
145: }
146:
147: if (cmd.contains(verboseDecl)) {
148: verbose = true;
149: printN3 = true;
150: }
151:
152: if (cmd.contains(baseDecl))
153: baseName = cmd.getArg(baseDecl).getValue();
154:
155: // stdin
156:
157: if (cmd.numItems() == 0) {
158: if (baseName == null)
159: baseName = "stdin:/";
160: doOneFile(System.in, System.out, baseName, baseName);
161: System.exit(0);
162: }
163:
164: // file arguments
165:
166: //for ( Iterator iter = cmd.items().iterator() ; iter.hasNext() ; )
167: for (int i = 0; i < cmd.numItems(); i++) {
168: //String filename = (String)iter.next() ;
169: String filename = cmd.getItem(i);
170: InputStream in = null;
171: try {
172: // DO NOT use a FileReader : it gets default charset
173: in = new FileInputStream(filename);
174: } catch (FileNotFoundException noEx) {
175: System.err.println("File not found: " + filename);
176: System.exit(2);
177: }
178: if (baseName == null) {
179: File f = new File(filename);
180: baseName = "file:///" + f.getAbsolutePath();
181: baseName = baseName.replace('\\', '/');
182: }
183:
184: doOneFile(in, System.out, baseName, filename);
185: }
186: }
187:
188: static void doOneFile(InputStream input, OutputStream output,
189: String baseName, String filename) {
190: BufferedReader reader = FileUtils.asBufferedUTF8(input);
191: PrintWriter writer = FileUtils.asPrintWriterUTF8(output);
192:
193: if (doRDF)
194: rdfOneFile(reader, writer, baseName, filename);
195: else
196: parseOneFile(reader, writer, baseName, filename);
197: }
198:
199: static void rdfOneFile(Reader reader, PrintWriter writer,
200: String baseName, String filename) {
201: try {
202: Model model = ModelFactory.createDefaultModel();
203: //RDFReader n3Reader = new N3JenaReader();
204: //n3Reader.read(model, reader, baseName);
205: model.read(reader, baseName, "N3");
206:
207: if (printRDF) {
208: if (outputLang.equals("N3")) {
209: writer.print("# Jena N3->RDF->" + outputLang
210: + " : " + filename);
211: writer.println();
212: writer.println();
213: }
214: //RDFWriter w = new N3JenaWriter();
215: //w.write(model, writer, baseName);
216: model.write(writer, outputLang, baseName);
217: writer.flush();
218: }
219: } catch (JenaException rdfEx) {
220: Throwable cause = rdfEx.getCause();
221: N3Exception n3Ex = (N3Exception) (rdfEx instanceof N3Exception ? rdfEx
222: : cause instanceof N3Exception ? cause : null);
223: if (n3Ex != null)
224: // Avoid a warning.
225: System.err.println(((Exception) n3Ex).getMessage());
226: else {
227: Throwable th = (cause == null ? rdfEx : cause);
228: System.err.println(th.getMessage());
229: th.printStackTrace(System.err);
230: }
231: System.exit(7);
232: }
233: }
234:
235: static private void parseOneFile(Reader reader, PrintWriter writer,
236: String baseName, String filename) {
237: N3ParserEventHandler handler = null;
238:
239: handler = null;
240:
241: if (printN3 || debug) {
242: //out.println("# N3: "+filename) ;
243: N3EventPrinter p = new N3EventPrinter(writer);
244: if (verbose)
245: p.printStartFinish = true;
246: handler = p;
247: } else
248: handler = new N3ErrorPrinter(writer);
249:
250: try {
251: N3Parser n3Parser = new N3Parser(reader, handler);
252: n3Parser.parse();
253: } catch (antlr.RecognitionException ex) {
254: //System.err.println(ex.getMessage()) ;
255: //System.err.println("--------") ;
256: //System.err.println("Exception: "+ex) ;
257: //ex.printStackTrace(System.err) ;
258: //System.err.println("--------") ;
259: System.exit(9);
260: } catch (antlr.TokenStreamException tokEx) {
261: System.exit(9);
262: }
263: }
264: }
265:
266: /*
267: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
268: * All rights reserved.
269: *
270: * Redistribution and use in source and binary forms, with or without
271: * modification, are permitted provided that the following conditions
272: * are met:
273: * 1. Redistributions of source code must retain the above copyright
274: * notice, this list of conditions and the following disclaimer.
275: * 2. Redistributions in binary form must reproduce the above copyright
276: * notice, this list of conditions and the following disclaimer in the
277: * documentation and/or other materials provided with the distribution.
278: * 3. The name of the author may not be used to endorse or promote products
279: * derived from this software without specific prior written permission.
280: *
281: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
282: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
283: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
284: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
285: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
286: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
287: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
288: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
290: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291: */
|