001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio;
007:
008: import java.io.FileInputStream;
009: import java.io.FileOutputStream;
010: import java.io.IOException;
011: import java.io.OutputStream;
012: import java.io.Writer;
013:
014: import org.openrdf.model.ValueFactory;
015:
016: /**
017: * Factory class providing static methods for creating RDF parsers and -writers
018: * for various RDF file formats.
019: *
020: * @author Arjohn Kampman
021: */
022: public class Rio {
023:
024: /**
025: * Tries to match a MIME type against the list of RDF formats that can be
026: * parsed.
027: *
028: * @param mimeType
029: * A MIME type, e.g. "application/rdf+xml".
030: * @return An RDFFormat object if a match was found, or <tt>null</tt>
031: * otherwise.
032: * @see #getParserFormatForMIMEType(String, RDFFormat)
033: */
034: public static RDFFormat getParserFormatForMIMEType(String mimeType) {
035: return getParserFormatForMIMEType(mimeType, null);
036: }
037:
038: /**
039: * Tries to match a MIME type against the list of RDF formats that can be
040: * parsed. This method calls
041: * {@link RDFFormat#matchMIMEType(String, Iterable)} with the specified MIME
042: * type, the keys of {@link RDFParserRegistry#getInstance()} and the fallback
043: * format as parameters.
044: *
045: * @param mimeType
046: * A MIME type, e.g. "application/rdf+xml".
047: * @param fallback
048: * The format that will be returned if no match was found.
049: * @return The matching RDFFormat, or <tt>fallback</tt> if no match was
050: * found.
051: */
052: public static RDFFormat getParserFormatForMIMEType(String mimeType,
053: RDFFormat fallback) {
054: return RDFFormat.matchMIMEType(mimeType, RDFParserRegistry
055: .getInstance().getKeys(), fallback);
056: }
057:
058: /**
059: * Tries to match the extension of a file name against the list of RDF
060: * formats that can be parsed.
061: *
062: * @param fileName
063: * A file name.
064: * @return An RDFFormat object if a match was found, or <tt>null</tt>
065: * otherwise.
066: * @see #getParserFormatForFileName(String, RDFFormat)
067: */
068: public static RDFFormat getParserFormatForFileName(String fileName) {
069: return getParserFormatForFileName(fileName, null);
070: }
071:
072: /**
073: * Tries to match the extension of a file name against the list of RDF
074: * formats that can be parsed. This method calls
075: * {@link RDFFormat#matchFileName(String, Iterable, info.aduna.lang.FileFormat)}
076: * with the specified MIME type, the keys of
077: * {@link RDFParserRegistry#getInstance()} and the fallback format as
078: * parameters.
079: *
080: * @param fileName
081: * A file name.
082: * @param fallback
083: * The format that will be returned if no match was found.
084: * @return The matching RDFFormat, or <tt>fallback</tt> if no match was
085: * found.
086: */
087: public static RDFFormat getParserFormatForFileName(String fileName,
088: RDFFormat fallback) {
089: return RDFFormat.matchFileName(fileName, RDFParserRegistry
090: .getInstance().getKeys(), fallback);
091: }
092:
093: /**
094: * Tries to match a MIME type against the list of RDF formats that can be
095: * written.
096: *
097: * @param mimeType
098: * A MIME type, e.g. "application/rdf+xml".
099: * @return An RDFFormat object if a match was found, or <tt>null</tt>
100: * otherwise.
101: * @see #getWriterFormatForMIMEType(String, RDFFormat)
102: */
103: public static RDFFormat getWriterFormatForMIMEType(String mimeType) {
104: return getWriterFormatForMIMEType(mimeType, null);
105: }
106:
107: /**
108: * Tries to match a MIME type against the list of RDF formats that can be
109: * written. This method calls
110: * {@link RDFFormat#matchMIMEType(String, Iterable, info.aduna.lang.FileFormat)}
111: * with the specified MIME type, the keys of
112: * {@link RDFWriterRegistry#getInstance()} and the fallback format as
113: * parameters.
114: *
115: * @param mimeType
116: * A MIME type, e.g. "application/rdf+xml".
117: * @param fallback
118: * The format that will be returned if no match was found.
119: * @return The matching RDFFormat, or <tt>fallback</tt> if no match was
120: * found.
121: */
122: public static RDFFormat getWriterFormatForMIMEType(String mimeType,
123: RDFFormat fallback) {
124: return RDFFormat.matchMIMEType(mimeType, RDFWriterRegistry
125: .getInstance().getKeys(), fallback);
126: }
127:
128: /**
129: * Tries to match the extension of a file name against the list of RDF
130: * formats that can be written.
131: *
132: * @param fileName
133: * A file name.
134: * @return An RDFFormat object if a match was found, or <tt>null</tt>
135: * otherwise.
136: * @see #getWriterFormatForFileName(String, RDFFormat)
137: */
138: public static RDFFormat getWriterFormatForFileName(String fileName) {
139: return getWriterFormatForFileName(fileName, null);
140: }
141:
142: /**
143: * Tries to match the extension of a file name against the list of RDF
144: * formats that can be written. This method calls
145: * {@link RDFFormat#matchFileName(String, Iterable, info.aduna.lang.FileFormat)}
146: * with the specified MIME type, the keys of
147: * {@link RDFWriterRegistry#getInstance()} and the fallback format as
148: * parameters.
149: *
150: * @param fileName
151: * A file name.
152: * @param fallback
153: * The format that will be returned if no match was found.
154: * @return The matching RDFFormat, or <tt>fallback</tt> if no match was
155: * found.
156: */
157: public static RDFFormat getWriterFormatForFileName(String fileName,
158: RDFFormat fallback) {
159: return RDFFormat.matchFileName(fileName, RDFWriterRegistry
160: .getInstance().getKeys(), fallback);
161: }
162:
163: /**
164: * Convenience methods for creating RDFParser objects. This method uses the
165: * registry returned by {@link RDFParserRegistry#getInstance()} to get a
166: * factory for the specified format and uses this factory to create the
167: * appropriate parser.
168: *
169: * @throws UnsupportedRDFormatException
170: * If no parser is available for the specified RDF format.
171: */
172: public static RDFParser createParser(RDFFormat format)
173: throws UnsupportedRDFormatException {
174: RDFParserFactory factory = RDFParserRegistry.getInstance().get(
175: format);
176:
177: if (factory != null) {
178: return factory.getParser();
179: }
180:
181: throw new UnsupportedRDFormatException(
182: "No parser factory available for RDF format " + format);
183: }
184:
185: /**
186: * Convenience methods for creating RDFParser objects that use the specified
187: * ValueFactory to create RDF model objects.
188: *
189: * @throws UnsupportedRDFormatException
190: * If no parser is available for the specified RDF format.
191: * @see #createParser(RDFFormat)
192: * @see RDFParser#setValueFactory(ValueFactory)
193: */
194: public static RDFParser createParser(RDFFormat format,
195: ValueFactory valueFactory)
196: throws UnsupportedRDFormatException {
197: RDFParser rdfParser = createParser(format);
198: rdfParser.setValueFactory(valueFactory);
199: return rdfParser;
200: }
201:
202: /**
203: * Convenience methods for creating RDFWriter objects. This method uses the
204: * registry returned by {@link RDFWriterRegistry#getInstance()} to get a
205: * factory for the specified format and uses this factory to create the
206: * appropriate writer.
207: *
208: * @throws UnsupportedRDFormatException
209: * If no writer is available for the specified RDF format.
210: */
211: public static RDFWriter createWriter(RDFFormat format,
212: OutputStream out) throws UnsupportedRDFormatException {
213: RDFWriterFactory factory = RDFWriterRegistry.getInstance().get(
214: format);
215:
216: if (factory != null) {
217: return factory.getWriter(out);
218: }
219:
220: throw new UnsupportedRDFormatException(
221: "No writer factory available for RDF format " + format);
222: }
223:
224: /**
225: * Convenience methods for creating RDFWriter objects. This method uses the
226: * registry returned by {@link RDFWriterRegistry#getInstance()} to get a
227: * factory for the specified format and uses this factory to create the
228: * appropriate writer.
229: *
230: * @throws UnsupportedRDFormatException
231: * If no writer is available for the specified RDF format.
232: */
233: public static RDFWriter createWriter(RDFFormat format, Writer writer)
234: throws UnsupportedRDFormatException {
235: RDFWriterFactory factory = RDFWriterRegistry.getInstance().get(
236: format);
237:
238: if (factory != null) {
239: return factory.getWriter(writer);
240: }
241:
242: throw new UnsupportedRDFormatException(
243: "No writer factory available for RDF format " + format);
244: }
245:
246: public static void main(String[] args) throws IOException,
247: RDFParseException, RDFHandlerException,
248: UnsupportedRDFormatException {
249: if (args.length < 2) {
250: System.out
251: .println("Usage: java org.openrdf.rio.Rio <inputFile> <outputFile>");
252: return;
253: }
254:
255: // Create parser for input file
256: String inputFile = args[0];
257: FileInputStream inStream = new FileInputStream(inputFile);
258: RDFFormat inputFormat = getParserFormatForFileName(inputFile,
259: RDFFormat.RDFXML);
260: RDFParser rdfParser = createParser(inputFormat);
261:
262: // Create writer for output file
263: String outputFile = args[1];
264: FileOutputStream outStream = new FileOutputStream(outputFile);
265: RDFFormat outputFormat = getWriterFormatForFileName(outputFile,
266: RDFFormat.RDFXML);
267: RDFWriter rdfWriter = createWriter(outputFormat, outStream);
268:
269: rdfParser.setRDFHandler(rdfWriter);
270: rdfParser.parse(inStream, "file:" + inputFile);
271:
272: inStream.close();
273: outStream.close();
274: }
275: }
|