Source Code Cross Referenced for n3.java in  » RSS-RDF » Jena-2.5.5 » jena » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » Jena 2.5.5 » jena 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.