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