Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jena/n3.java


1   /*
2    * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3    * [See end of file]
4    */
5   
6   package jena ;
7   
8   import java.io.* ;
9   import jena.cmdline.*;
10  
11  import java.util.*;
12  import com.hp.hpl.jena.rdf.model.*;
13  import com.hp.hpl.jena.shared.*;
14  import com.hp.hpl.jena.util.FileUtils ;
15  import com.hp.hpl.jena.n3.* ;
16  
17  /**
18      Read N3 files and print in a variery of formats.
19   * @author    Andy Seaborne
20   * @version   $Id: n3.java,v 1.14 2005/02/21 11:49:12 andy_seaborne Exp $
21   */
22  public class n3
23  {
24    static boolean firstOutput  = true ;
25    static boolean doNodeTest   = true ;
26    static boolean doErrorTests = false ;
27    
28    static int testCount = 0 ;  
29    
30    static boolean doRDF = false ;    // Attempt to create RDF
31    static boolean printRDF = false ;  // List RDF
32      static String  outputLang = "N-TRIPLE" ;
33    static boolean printN3 = true ;    // List the N3 processed
34      static boolean debug = false ;    // Help!
35    static boolean verbose = false ;
36    
37    //static final String NL = JenaRuntime.getLineSeparator();
38  
39    // Parse a file (no RDF production)
40  
41    public static void main(String[] args)
42    {
43      String dir = System.getProperty("user.dir") ;
44      
45      String usageMessage = n3.class.getName()+
46                                  " [-rdf] [-base URI] [filename]" ;
47  
48        CommandLine cmd = new CommandLine() ;
49        cmd.setUsage(usageMessage) ;
50        cmd.setOutput(System.err) ;
51        
52        //cmd.setHook(cmd.trace()) ;
53        
54        ArgDecl verboseDecl       = new ArgDecl(false, "-v", "--verbose") ;
55        ArgDecl helpDecl          = new ArgDecl(false, "-h", "--help") ;
56        ArgDecl rdfDecl           = new ArgDecl(false, "-rdf", "--rdf") ;
57        ArgDecl rdfRDFN3Decl      = new ArgDecl(false, "--rdf-n3") ;
58        ArgDecl rdfRDFXMLDecl     = new ArgDecl(false, "--rdf-xml") ;
59        ArgDecl rdfRDFNTDecl      = new ArgDecl(false, "--rdf-nt") ;
60          ArgDecl rdfRDFFormat      = new ArgDecl(true, "--format", "--fmt") ;
61        ArgDecl debugDecl         = new ArgDecl(false, "-debug") ;
62        ArgDecl baseDecl          = new ArgDecl(true, "-base") ;
63        //ArgDecl outputDecl        = new ArgDecl(true, "-output", "-o") ;
64        ArgDecl checkDecl         = new ArgDecl(false, "-n", "--check") ;
65        
66      cmd.add(verboseDecl) ;
67      cmd.add(helpDecl) ;
68      cmd.add(rdfDecl) ;
69      cmd.add(rdfRDFN3Decl) ;
70      cmd.add(rdfRDFXMLDecl) ;
71      cmd.add(rdfRDFNTDecl) ;
72          cmd.add(rdfRDFFormat) ;
73      cmd.add(debugDecl) ;
74      cmd.add(baseDecl) ;
75      cmd.add(checkDecl) ;
76      
77      try { cmd.process(args) ; }
78      catch (IllegalArgumentException illEx) { System.exit(1) ; }
79        
80        verbose = cmd.contains(verboseDecl) ;
81        
82        if ( cmd.contains(helpDecl) )
83        {
84          System.out.println(usageMessage) ;
85          System.out.println("Default action: parse an N3 file") ;
86          System.out.println("    --rdf           Read into an RDF and print") ;
87          System.out.println("    --rdf-n3        Read into an RDF and print in N3") ;
88          System.out.println("    --rdf-xml       Read into an RDF and print in XML") ;
89          System.out.println("    --rdf-nt        Read into an RDF and print in N-Triples") ;
90              System.out.println("    --format FMT    Read into an RDF and print in given format") ;
91          System.out.println("    --check | -n    Just check: no output") ;
92          System.out.println("    --base URI      Set the base URI") ;
93          System.exit(0) ;
94        }
95        
96      String baseName = null ;
97        
98        if ( cmd.contains(rdfDecl) )
99        {
100         doRDF =  true ;
101         printRDF = true ;
102         printN3 = false ;
103       }
104       
105       if ( cmd.contains(rdfRDFN3Decl) )
106       {
107         doRDF =  true ;
108         printRDF = true ;
109         outputLang = "N3" ;
110         printN3 = false ;
111       }
112 
113       if ( cmd.contains(rdfRDFXMLDecl) )
114       {
115         doRDF =  true ;
116         printRDF = true ;
117         outputLang = "RDF/XML-ABBREV" ;
118         printN3 = false ;
119       }
120 
121       if ( cmd.contains(rdfRDFNTDecl) )
122       {
123         doRDF =  true ;
124         printRDF = true ;
125         outputLang = "N-TRIPLE" ;
126         printN3 = false ;
127       }
128       
129         if ( cmd.contains(rdfRDFFormat))
130         {
131             doRDF = true ;
132             printRDF = true ;
133             outputLang = cmd.getArg(rdfRDFFormat).getValue() ;
134             printN3 = false ;
135         }
136 
137       if ( cmd.contains(debugDecl) )
138       {
139         debug = true ;
140         N3JenaWriter.DEBUG = true ;
141       }
142         
143     if ( cmd.contains(checkDecl) )
144     {
145       printRDF = false ;
146       printN3 = false ;
147     }
148 
149     if ( cmd.contains(verboseDecl) )
150     {
151       verbose = true ;
152       printN3 = true ;
153     }
154       
155     if ( cmd.contains(baseDecl) )
156       baseName = cmd.getArg(baseDecl).getValue() ;
157       
158       
159     // stdin
160 
161     if ( cmd.items().size() == 0 )
162     {
163       if ( baseName == null )
164         baseName = "stdin:/" ;
165       doOneFile(System.in, System.out, baseName, baseName) ;
166       System.exit(0) ;
167     }
168     
169     // file arguments
170 
171     for ( Iterator iter = cmd.items().iterator() ; iter.hasNext() ; )
172     {
173       String filename = (String)iter.next() ;
174       InputStream in = null ;
175       try {
176         // DO NOT use a FileReader : it gets default charset
177         in = new FileInputStream(filename) ;
178       } catch (FileNotFoundException noEx)
179       {
180         System.err.println("File not found: "+filename) ;
181         System.exit(2) ;
182       }
183       if ( baseName == null )
184       {
185         File f = new File(filename) ;
186         baseName = "file:///"+f.getAbsolutePath() ;
187         baseName = baseName.replace('\\', '/') ;
188       }
189       
190       doOneFile(in, System.out, baseName, filename) ;
191     }
192   }
193   
194   
195   static void doOneFile(InputStream input, OutputStream output, String baseName, String filename)
196   {
197     BufferedReader reader = FileUtils.asBufferedUTF8(input) ;
198     PrintWriter writer = FileUtils.asPrintWriterUTF8(output) ;
199 
200         if ( doRDF )
201       rdfOneFile(reader, writer, baseName, filename) ;
202     else
203       parseOneFile(reader, writer, baseName, filename) ;
204   }
205   
206   static void rdfOneFile(Reader reader, PrintWriter writer, String baseName, String filename)
207   {
208     try
209     {
210       Model model = ModelFactory.createDefaultModel();
211       //RDFReader n3Reader = new N3JenaReader();
212       //n3Reader.read(model, reader, baseName);
213       model.read(reader, baseName, "N3") ;
214 
215       if (printRDF)
216       {
217         if ( outputLang.equals("N3") )
218         {
219           writer.print("# Jena N3->RDF->"+outputLang+" : " + filename);
220           writer.println() ;
221           writer.println() ;
222         }
223         //RDFWriter w = new N3JenaWriter();
224         //w.write(model, writer, baseName);
225         model.write(writer, outputLang, baseName) ;
226       writer.flush();
227       }
228     } catch (JenaException rdfEx)
229     {
230             Throwable cause = rdfEx.getCause();
231             N3Exception n3Ex = (N3Exception)
232                 (rdfEx instanceof N3Exception ? rdfEx
233                 : cause instanceof N3Exception ? cause
234                 : null);
235             if ( n3Ex != null )
236                 System.err.println(n3Ex.getMessage()) ;
237             else
238             {
239                 Throwable th = (cause == null ? rdfEx : cause);
240                 System.err.println( th.getMessage() ) ;
241                 th.printStackTrace( System.err );
242             }
243             System.exit(7) ;
244         }
245   }
246   
247     
248   static private void parseOneFile(Reader reader, PrintWriter writer, String baseName, String filename)
249   {
250     N3ParserEventHandler handler = null ;
251     
252     handler = null ;
253 
254     if ( printN3 || debug )
255     {
256       //out.println("# N3: "+filename) ;
257       N3EventPrinter p = new N3EventPrinter(writer) ;
258       if ( verbose )
259         p.printStartFinish = true ;
260       handler = p ;
261     }
262     else
263       handler = new N3ErrorPrinter(writer) ;
264     
265     try {
266       N3Parser n3Parser = new N3Parser(reader, handler) ;
267       n3Parser.parse() ;
268     } catch (antlr.RecognitionException ex)
269     {
270       //System.err.println(ex.getMessage()) ;
271       //System.err.println("--------") ;      
272       //System.err.println("Exception: "+ex) ;
273       //ex.printStackTrace(System.err) ;
274       //System.err.println("--------") ;      
275       System.exit(9) ;
276     }
277     catch ( antlr.TokenStreamException tokEx)
278     {
279       System.exit(9) ;
280     }
281   }
282 }
283 
284 /*
285  *  (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
286  *  All rights reserved.
287  *
288  * Redistribution and use in source and binary forms, with or without
289  * modification, are permitted provided that the following conditions
290  * are met:
291  * 1. Redistributions of source code must retain the above copyright
292  *    notice, this list of conditions and the following disclaimer.
293  * 2. Redistributions in binary form must reproduce the above copyright
294  *    notice, this list of conditions and the following disclaimer in the
295  *    documentation and/or other materials provided with the distribution.
296  * 3. The name of the author may not be used to endorse or promote products
297  *    derived from this software without specific prior written permission.
298  *
299  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
300  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
301  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
302  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
303  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
304  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
305  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
306  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
307  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
308  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
309  */