Source Code Cross Referenced for IOUtils.java in  » Search-Engine » semweb4j » org » ontoware » rdf2go » impl » jena24 » 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 » Search Engine » semweb4j » org.ontoware.rdf2go.impl.jena24 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.ontoware.rdf2go.impl.jena24;
002:
003:        import java.io.File;
004:        import java.io.FileInputStream;
005:        import java.io.FileNotFoundException;
006:        import java.io.FileOutputStream;
007:        import java.io.FileWriter;
008:        import java.io.IOException;
009:        import java.io.InputStreamReader;
010:        import java.io.OutputStreamWriter;
011:        import java.io.Reader;
012:        import java.io.UnsupportedEncodingException;
013:        import java.io.Writer;
014:        import java.net.URL;
015:
016:        import org.apache.commons.logging.Log;
017:        import org.apache.commons.logging.LogFactory;
018:        import org.ontoware.rdf2go.Reasoning;
019:        import org.ontoware.rdf2go.exception.ModelRuntimeException;
020:        import org.ontoware.rdf2go.model.Model;
021:
022:        import com.hp.hpl.jena.rdf.model.ModelFactory;
023:
024:        /**
025:         * @author voelkel
026:         * 
027:         */
028:        public class IOUtils {
029:
030:            private static final Log log = LogFactory.getLog(IOUtils.class);
031:
032:            /**
033:             * Read the file. Assume RDF/XML, unless file extension is '.nt' (N-TRIPLES)
034:             * or '.n3' (N3).
035:             * 
036:             * @param filename
037:             * @return a Jena Model
038:             */
039:            public static Model read(String filename) {
040:                File f = new File(filename);
041:                return read(f);
042:            }
043:
044:            /** assume RDF/XML */
045:            public static void readIntoJenaModel(
046:                    com.hp.hpl.jena.rdf.model.Model m, Reader reader) {
047:                m.read(reader, "", "RDF/XML");
048:            }
049:
050:            public static void readIntoJenaModel(
051:                    com.hp.hpl.jena.rdf.model.Model m, File f) {
052:                log.warn("reading model from " + f.getAbsoluteFile());
053:                try {
054:                    FileInputStream fis = new FileInputStream(f);
055:                    Reader r = new InputStreamReader(fis, "utf8");
056:
057:                    String lang = "RDF/XML";
058:
059:                    if (f.getName().endsWith(".n3")) {
060:                        log.warn("file end with '.n3', reading as N3");
061:                        lang = "N3";
062:                    } else if (f.getName().endsWith(".nt")) {
063:                        log.warn("file end with '.nt', reading as N-TRIPLE");
064:                        lang = "N-TRIPLE";
065:                    } else {
066:                        log.warn("default: read as RDF/XML");
067:                    }
068:
069:                    m.read(r, "", lang);
070:
071:                } catch (FileNotFoundException e) {
072:                    throw new RuntimeException("Could not find file: "
073:                            + f.getAbsolutePath() + " Exception: " + e);
074:                } catch (UnsupportedEncodingException e) {
075:                    throw new RuntimeException(e);
076:                }
077:            }
078:
079:            public static Model read(File f) {
080:                com.hp.hpl.jena.rdf.model.Model m = ModelFactory
081:                        .createDefaultModel();
082:                readIntoJenaModel(m, f);
083:                return new ModelImplJena24(null, m);
084:            }
085:
086:            public static Model read(URL url) {
087:                com.hp.hpl.jena.rdf.model.Model m = ModelFactory
088:                        .createDefaultModel();
089:
090:                // TODO guess language
091:                m.read("" + url, "" + url, "RDF/XML");
092:                return new ModelImplJena24(null, m);
093:            }
094:
095:            public static Model read(Reader reader) {
096:                com.hp.hpl.jena.rdf.model.Model m = ModelFactory
097:                        .createDefaultModel();
098:                readIntoJenaModel(m, reader);
099:                return new ModelImplJena24(null, m);
100:            }
101:
102:            /** TODO test xml ecnoding 
103:             * @throws IOException */
104:            public static void writeJenaModel(
105:                    com.hp.hpl.jena.rdf.model.Model jm, Writer w)
106:                    throws ModelRuntimeException, IOException {
107:                DumpUtils.addCommonPrefixesToJenaModel(jm);
108:
109:                log.error("Writing RDF/XML in UTF-8...");
110:                // Jena is not adding this to the XML file, when choosing UTF-8
111:                // encoding
112:                w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
113:                jm.write(w, "RDF/XML", "");
114:            }
115:
116:            public static void write(Model m, Writer w)
117:                    throws ModelRuntimeException {
118:                Model jenaModel = new ModelImplJena24(Reasoning.none);
119:                jenaModel.addAll(m.iterator());
120:                com.hp.hpl.jena.rdf.model.Model jm = (com.hp.hpl.jena.rdf.model.Model) jenaModel
121:                        .getUnderlyingModelImplementation();
122:                try {
123:                    writeJenaModel(jm, w);
124:                } catch (IOException e) {
125:                    throw new ModelRuntimeException(e);
126:                }
127:            }
128:
129:            public static void write(Model m, String filename, String format)
130:                    throws ModelRuntimeException {
131:                Model jenaModel = new ModelImplJena24(Reasoning.none);
132:                jenaModel.addAll(m.iterator());
133:                com.hp.hpl.jena.rdf.model.Model jm = (com.hp.hpl.jena.rdf.model.Model) jenaModel
134:                        .getUnderlyingModelImplementation();
135:                DumpUtils.addCommonPrefixesToJenaModel(jm);
136:
137:                try {
138:                    // encoding!
139:                    if (format.equalsIgnoreCase("rdf/xml")) {
140:                        log.error("Writing RDF/XML in UTF-8...");
141:                        FileOutputStream fos;
142:                        fos = new FileOutputStream(new File(filename));
143:                        OutputStreamWriter osw = new OutputStreamWriter(fos,
144:                                "UTF-8");
145:                        // Jena is not adding this to the XML file, when choosing UTF-8
146:                        // encoding
147:                        osw
148:                                .write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
149:                        jm.write(osw, "RDF/XML", "");
150:                    } else
151:                        jm
152:                                .write(new FileWriter(new File(filename)),
153:                                        format, "");
154:
155:                } catch (FileNotFoundException e) {
156:                    throw new ModelRuntimeException(e);
157:                } catch (IOException e) {
158:                    throw new ModelRuntimeException(e);
159:                }
160:            }
161:
162:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.