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.query.resultio;
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 tuple query result serialization format. Tuple
019: * query result formats are identified by a {@link #getName() name} and can have
020: * one or more associated MIME types, zero or more associated file extensions
021: * and can specify a (default) character encoding.
022: *
023: * @author Arjohn Kampman
024: */
025: public class TupleQueryResultFormat extends FileFormat {
026:
027: /*-----------*
028: * Constants *
029: *-----------*/
030:
031: /**
032: * SPARQL Query Results XML Format.
033: */
034: public static final TupleQueryResultFormat SPARQL = new TupleQueryResultFormat(
035: "SPARQL/XML", Arrays
036: .asList("application/sparql-results+xml",
037: "application/xml"), Charset
038: .forName("UTF-8"), Arrays.asList("srx", "xml"));
039:
040: /**
041: * Binary RDF results table format.
042: */
043: public static final TupleQueryResultFormat BINARY = new TupleQueryResultFormat(
044: "BINARY", "application/x-binary-rdf-results-table", null,
045: "brt");
046:
047: /**
048: * SPARQL Query Results JSON Format.
049: */
050: public static final TupleQueryResultFormat JSON = new TupleQueryResultFormat(
051: "SPARQL/JSON", "application/sparql-results+json", Charset
052: .forName("UTF-8"), "srj");
053:
054: /*------------------*
055: * Static variables *
056: *------------------*/
057:
058: /**
059: * List of known tuple query result formats.
060: */
061: private static List<TupleQueryResultFormat> VALUES = new ArrayList<TupleQueryResultFormat>(
062: 8);
063:
064: /*--------------------*
065: * Static initializer *
066: *--------------------*/
067:
068: static {
069: register(SPARQL);
070: register(BINARY);
071: register(JSON);
072: }
073:
074: /*----------------*
075: * Static methods *
076: *----------------*/
077:
078: /**
079: * Returns all known/registered tuple query result formats.
080: */
081: public static Collection<TupleQueryResultFormat> values() {
082: return Collections.unmodifiableList(VALUES);
083: }
084:
085: /**
086: * Registers the specified tuple query result format.
087: *
088: * @param name
089: * The name of the format, e.g. "SPARQL/XML".
090: * @param mimeType
091: * The MIME type of the format, e.g.
092: * <tt>application/sparql-results+xml</tt> for the SPARQL/XML file
093: * format.
094: * @param fileExt
095: * The (default) file extension for the format, e.g. <tt>srx</tt>
096: * for SPARQL/XML files.
097: */
098: public static TupleQueryResultFormat register(String name,
099: String mimeType, String fileExt) {
100: TupleQueryResultFormat format = new TupleQueryResultFormat(
101: name, mimeType, fileExt);
102: register(format);
103: return format;
104: }
105:
106: /**
107: * Registers the specified tuple query result format.
108: */
109: public static void register(TupleQueryResultFormat format) {
110: VALUES.add(format);
111: }
112:
113: /**
114: * Tries to determine the appropriate tuple file format based on the a MIME
115: * type that describes the content type.
116: *
117: * @param mimeType
118: * A MIME type, e.g. "application/sparql-results+xml".
119: * @return A TupleQueryResultFormat object if the MIME type was recognized,
120: * or <tt>null</tt> otherwise.
121: * @see #forMIMEType(String,TupleQueryResultFormat)
122: * @see #getMIMEType
123: */
124: public static TupleQueryResultFormat forMIMEType(String mimeType) {
125: return forMIMEType(mimeType, null);
126: }
127:
128: /**
129: * Tries to determine the appropriate tuple file format based on the a MIME
130: * type that describes the content type. The supplied fallback format will be
131: * returned when the MIME type was not recognized.
132: *
133: * @param mimeType
134: * a MIME type, e.g. "application/sparql-results+xml"
135: * @param fallback
136: * a fallback TupleQueryResultFormat that will be returned by the
137: * method if no match for the supplied MIME type can be found.
138: * @return A TupleQueryResultFormat that matches the MIME type, or the
139: * fallback format if the extension was not recognized.
140: * @see #forMIMEType(String)
141: * @see #getMIMEType
142: */
143: public static TupleQueryResultFormat forMIMEType(String mimeType,
144: TupleQueryResultFormat fallback) {
145: return matchMIMEType(mimeType, VALUES, fallback);
146: }
147:
148: /**
149: * Tries to determine the appropriate tuple file format for a file, based on
150: * the extension specified in a file name.
151: *
152: * @param fileName
153: * A file name.
154: * @return A TupleQueryResultFormat object if the file extension was
155: * recognized, or <tt>null</tt> otherwise.
156: * @see #forFileName(String,TupleQueryResultFormat)
157: * @see #getFileExtension
158: */
159: public static TupleQueryResultFormat forFileName(String fileName) {
160: return forFileName(fileName, null);
161: }
162:
163: /**
164: * Tries to determine the appropriate tuple file format for a file, based on
165: * the extension specified in a file name. The supplied fallback format will
166: * be returned when the file name extension was not recognized.
167: *
168: * @param fileName
169: * A file name.
170: * @return A TupleQueryResultFormat that matches the file name extension, or
171: * the fallback format if the extension was not recognized.
172: * @see #forFileName(String)
173: * @see #getFileExtension
174: */
175: public static TupleQueryResultFormat forFileName(String fileName,
176: TupleQueryResultFormat fallback) {
177: return matchFileName(fileName, VALUES, fallback);
178: }
179:
180: /*--------------*
181: * Constructors *
182: *--------------*/
183:
184: /**
185: * Creates a new TupleQueryResultFormat object.
186: *
187: * @param name
188: * The name of the format, e.g. "SPARQL/XML".
189: * @param mimeType
190: * The MIME type of the format, e.g.
191: * <tt>application/sparql-results+xml</tt> for the SPARQL/XML
192: * format.
193: * @param fileExt
194: * The (default) file extension for the format, e.g. <tt>srx</tt>
195: * for SPARQL/XML.
196: */
197: public TupleQueryResultFormat(String name, String mimeType,
198: String fileExt) {
199: this (name, mimeType, null, fileExt);
200: }
201:
202: /**
203: * Creates a new TupleQueryResultFormat object.
204: *
205: * @param name
206: * The name of the format, e.g. "SPARQL/XML".
207: * @param mimeType
208: * The MIME type of the format, e.g.
209: * <tt>application/sparql-results+xml</tt> for the SPARQL/XML
210: * format.
211: * @param charset
212: * The default character encoding of the format. Specify <tt>null</tt>
213: * if not applicable.
214: * @param fileExt
215: * The (default) file extension for the format, e.g. <tt>srx</tt>
216: * for SPARQL/XML.
217: */
218: public TupleQueryResultFormat(String name, String mimeType,
219: Charset charset, String fileExt) {
220: super (name, mimeType, charset, fileExt);
221: }
222:
223: /**
224: * Creates a new TupleQueryResultFormat object.
225: *
226: * @param name
227: * The name of the format, e.g. "SPARQL/XML".
228: * @param mimeTypes
229: * The MIME types of the format, e.g.
230: * <tt>application/sparql-results+xml</tt> for the SPARQL/XML
231: * format. The first item in the list is interpreted as the default
232: * MIME type for the format.
233: * @param charset
234: * The default character encoding of the format. Specify <tt>null</tt>
235: * if not applicable.
236: * @param fileExtensions
237: * The format's file extensions, e.g. <tt>srx</tt> for SPARQL/XML
238: * files. The first item in the list is interpreted as the default
239: * file extension for the format.
240: */
241: public TupleQueryResultFormat(String name,
242: Collection<String> mimeTypes, Charset charset,
243: Collection<String> fileExtensions) {
244: super(name, mimeTypes, charset, fileExtensions);
245: }
246: }
|