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.nio.charset.Charset;
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Collection;
012: import java.util.Collections;
013: import java.util.List;
014:
015: import info.aduna.lang.FileFormat;
016:
017: /**
018: * Represents the concept of an RDF data serialization format. RDF formats are
019: * identified by a {@link #getName() name} and can have one or more associated
020: * MIME types, zero or more associated file extensions and can specify a
021: * (default) character encoding. Some formats are able to encode context
022: * information while other are not; this is indicated by the value of
023: * {@link #supportsContexts}.
024: *
025: * @author Arjohn Kampman
026: */
027: public class RDFFormat extends FileFormat {
028:
029: /*-----------*
030: * Constants *
031: *-----------*/
032:
033: /**
034: * The RDF/XML file format.
035: */
036: public static final RDFFormat RDFXML = new RDFFormat("RDF/XML",
037: Arrays.asList("application/rdf+xml", "application/xml"),
038: Charset.forName("UTF-8"), Arrays.asList("rdf", "rdfs",
039: "owl", "xml"), true, false);
040:
041: /**
042: * The N-Triples file format.
043: */
044: public static final RDFFormat NTRIPLES = new RDFFormat("N-Triples",
045: "text/plain", Charset.forName("US-ASCII"), "nt", false,
046: false);
047:
048: /**
049: * The Turtle file format.
050: */
051: public static final RDFFormat TURTLE = new RDFFormat("Turtle",
052: "application/x-turtle", Charset.forName("UTF-8"), "ttl",
053: true, false);
054:
055: /**
056: * The N3/Notation3 file format.
057: */
058: public static final RDFFormat N3 = new RDFFormat("N3",
059: "text/rdf+n3", Charset.forName("UTF-8"), "n3", true, false);
060:
061: /**
062: * The TriX file format.
063: */
064: public static final RDFFormat TRIX = new RDFFormat("TriX",
065: "application/trix", Charset.forName("UTF-8"), Arrays
066: .asList("xml", "trix"), false, true);
067:
068: /**
069: * The <a href="http://www.wiwiss.fu-berlin.de/suhl/bizer/TriG/Spec/">TriG</a>
070: * file format.
071: */
072: public static final RDFFormat TRIG = new RDFFormat("TriG",
073: "application/x-trig", Charset.forName("UTF-8"), "trig",
074: true, true);
075:
076: /*------------------*
077: * Static variables *
078: *------------------*/
079:
080: /**
081: * List of known RDF file formats.
082: */
083: // FIXME: remove/deprecate this list?
084: private static List<RDFFormat> RDF_FORMATS = new ArrayList<RDFFormat>(
085: 8);
086:
087: /*--------------------*
088: * Static initializer *
089: *--------------------*/
090:
091: static {
092: // FIXME: base available format on available parsers/writers?
093: register(RDFXML);
094: register(NTRIPLES);
095: register(TURTLE);
096: register(N3);
097: register(TRIX);
098: register(TRIG);
099: }
100:
101: /*----------------*
102: * Static methods *
103: *----------------*/
104:
105: /**
106: * Returns all known/registered RDF formats.
107: */
108: public static Collection<RDFFormat> values() {
109: return Collections.unmodifiableList(RDF_FORMATS);
110: }
111:
112: /**
113: * Registers the specified RDF file format.
114: *
115: * @param name
116: * The name of the RDF file format, e.g. "RDF/XML".
117: * @param mimeType
118: * The MIME type of the RDF file format, e.g.
119: * <tt>application/rdf+xml</tt> for the RDF/XML file format.
120: * @param fileExt
121: * The (default) file extension for the RDF file format, e.g.
122: * <tt>rdf</tt> for RDF/XML files.
123: */
124: public static RDFFormat register(String name, String mimeType,
125: String fileExt, Charset charset) {
126: RDFFormat rdfFormat = new RDFFormat(name, mimeType, charset,
127: fileExt, false, false);
128: register(rdfFormat);
129: return rdfFormat;
130: }
131:
132: /**
133: * Registers the specified RDF file format.
134: */
135: public static void register(RDFFormat rdfFormat) {
136: RDF_FORMATS.add(rdfFormat);
137: }
138:
139: /**
140: * Tries to determine the appropriate RDF file format based on the a MIME
141: * type that describes the content type.
142: *
143: * @param mimeType
144: * A MIME type, e.g. "application/rdf+xml".
145: * @return An RDFFormat object if the MIME type was recognized, or
146: * <tt>null</tt> otherwise.
147: * @see #forMIMEType(String,RDFFormat)
148: * @see #getMIMEType
149: */
150: public static RDFFormat forMIMEType(String mimeType) {
151: return forMIMEType(mimeType, null);
152: }
153:
154: /**
155: * Tries to determine the appropriate RDF file format based on the a MIME
156: * type that describes the content type. The supplied fallback format will be
157: * returned when the MIME type was not recognized.
158: *
159: * @param mimeType
160: * A file name.
161: * @return An RDFFormat that matches the MIME type, or the fallback format if
162: * the extension was not recognized.
163: * @see #forMIMEType(String)
164: * @see #getMIMEType
165: */
166: public static RDFFormat forMIMEType(String mimeType,
167: RDFFormat fallback) {
168: return matchMIMEType(mimeType, RDF_FORMATS, fallback);
169: }
170:
171: /**
172: * Tries to determine the appropriate RDF file format based on the extension
173: * of a file name.
174: *
175: * @param fileName
176: * A file name.
177: * @return An RDFFormat object if the file extension was recognized, or
178: * <tt>null</tt> otherwise.
179: * @see #forFileName(String,RDFFormat)
180: * @see #getFileExtension
181: */
182: public static RDFFormat forFileName(String fileName) {
183: return forFileName(fileName, null);
184: }
185:
186: /**
187: * Tries to determine the appropriate RDF file format based on the extension
188: * of a file name. The supplied fallback format will be returned when the
189: * file name extension was not recognized.
190: *
191: * @param fileName
192: * A file name.
193: * @return An RDFFormat that matches the file name extension, or the fallback
194: * format if the extension was not recognized.
195: * @see #forFileName(String)
196: * @see #getFileExtension
197: */
198: public static RDFFormat forFileName(String fileName,
199: RDFFormat fallback) {
200: return matchFileName(fileName, RDF_FORMATS, fallback);
201: }
202:
203: /**
204: * Returns the RDF format whose name matches the specified name.
205: *
206: * @param formatName
207: * A format name.
208: * @return The RDF format whose name matches the specified name, or
209: * <tt>null</tt> if there is no such format.
210: */
211: public static RDFFormat valueOf(String formatName) {
212: for (RDFFormat format : RDF_FORMATS) {
213: if (format.getName().equalsIgnoreCase(formatName)) {
214: return format;
215: }
216: }
217:
218: return null;
219: }
220:
221: /*-----------*
222: * Variables *
223: *-----------*/
224:
225: /**
226: * Flag indicating whether the RDFFormat can encode namespace information.
227: */
228: private boolean supportsNamespaces = false;
229:
230: /**
231: * Flag indicating whether the RDFFormat can encode context information.
232: */
233: private boolean supportsContexts = false;
234:
235: /*--------------*
236: * Constructors *
237: *--------------*/
238:
239: /**
240: * Creates a new RDFFormat object.
241: *
242: * @param name
243: * The name of the RDF file format, e.g. "RDF/XML".
244: * @param mimeType
245: * The MIME type of the RDF file format, e.g.
246: * <tt>application/rdf+xml</tt> for the RDF/XML file format.
247: * @param charset
248: * The default character encoding of the RDF file format. Specify
249: * <tt>null</tt> if not applicable.
250: * @param fileExtension
251: * The (default) file extension for the RDF file format, e.g.
252: * <tt>rdf</tt> for RDF/XML files.
253: */
254: public RDFFormat(String name, String mimeType, Charset charset,
255: String fileExtension, boolean supportsNamespaces,
256: boolean supportsContexts) {
257: this (name, Arrays.asList(mimeType), charset, Arrays
258: .asList(fileExtension), supportsNamespaces,
259: supportsContexts);
260: }
261:
262: /**
263: * Creates a new RDFFormat object.
264: *
265: * @param name
266: * The name of the RDF file format, e.g. "RDF/XML".
267: * @param mimeType
268: * The MIME type of the RDF file format, e.g.
269: * <tt>application/rdf+xml</tt> for the RDF/XML file format.
270: * @param charset
271: * The default character encoding of the RDF file format. Specify
272: * <tt>null</tt> if not applicable.
273: * @param fileExtensions
274: * The RDF format's file extensions, e.g. <tt>rdf</tt> for RDF/XML
275: * files. The first item in the list is interpreted as the default
276: * file extension for the format.
277: */
278: public RDFFormat(String name, String mimeType, Charset charset,
279: Collection<String> fileExtensions,
280: boolean supportsNamespaces, boolean supportsContexts) {
281: this (name, Arrays.asList(mimeType), charset, fileExtensions,
282: supportsNamespaces, supportsContexts);
283: }
284:
285: /**
286: * Creates a new RDFFormat object.
287: *
288: * @param name
289: * The name of the RDF file format, e.g. "RDF/XML".
290: * @param mimeTypes
291: * The MIME types of the RDF file format, e.g.
292: * <tt>application/rdf+xml</tt> for the RDF/XML file format. The
293: * first item in the list is interpreted as the default MIME type for
294: * the format.
295: * @param charset
296: * The default character encoding of the RDF file format. Specify
297: * <tt>null</tt> if not applicable.
298: * @param fileExtensions
299: * The RDF format's file extensions, e.g. <tt>rdf</tt> for RDF/XML
300: * files. The first item in the list is interpreted as the default
301: * file extension for the format.
302: */
303: public RDFFormat(String name, Collection<String> mimeTypes,
304: Charset charset, Collection<String> fileExtensions,
305: boolean supportsNamespaces, boolean supportsContexts) {
306: super (name, mimeTypes, charset, fileExtensions);
307:
308: this .supportsNamespaces = supportsNamespaces;
309: this .supportsContexts = supportsContexts;
310: }
311:
312: /*---------*
313: * Methods *
314: *---------*/
315:
316: /*
317: * Return <tt>true</tt> if the RDFFormat supports the encoding of
318: * namespace/prefix information.
319: */
320: public boolean supportsNamespaces() {
321: return supportsNamespaces;
322: }
323:
324: /*
325: * Return <tt>true</tt> if the RDFFormat supports the encoding of
326: * contexts/named graphs.
327: */
328: public boolean supportsContexts() {
329: return supportsContexts;
330: }
331: }
|