001: /*
002: * LICENSE INFORMATION
003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
005: * <OWNER> = Max Völkel
006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
007: * <YEAR> = 2007
008: *
009: * Project information at http://semweb4j.org/rdf2go
010: */
011: package org.ontoware.rdf2go.model;
012:
013: import java.util.ArrayList;
014: import java.util.Collections;
015: import java.util.List;
016:
017: /**
018: * Class for RDF syntaxes, and registry for them. A framework can register new
019: * syntaxes by creating them with the constructor that automatically registers,
020: * or by calling the {@link #register(Syntax)} method.
021: *
022: * You can chose to use a Syntax in your application without registering it.
023: *
024: * @author voelkel, sauermann
025: */
026: public class Syntax {
027:
028: /**
029: * List of known RDF file formats. Note: This has to be defined at the top
030: * of the class, otherwise the constructor of the final Syntaxes cannot use
031: * it.
032: */
033: private static List<Syntax> SYNTAXES = new ArrayList<Syntax>();
034:
035: /**
036: * RDF syntax RDF XML
037: */
038: public static final Syntax RdfXml = new Syntax("rdfxml",
039: "application/rdf+xml", ".rdf", true);
040:
041: /**
042: * RDF syntax Turtle
043: */
044: public static final Syntax Turtle = new Syntax("turtle",
045: "application/x-turtle", ".ttl", true);
046:
047: /**
048: * RDF syntax NTriples
049: */
050: public static final Syntax Ntriples = new Syntax("ntriples",
051: "text/plain", ".nt", true);
052:
053: /**
054: * RDF syntax Trix For mroe info see: http://swdev.nokia.com/trix/TriX.html
055: */
056: public static final Syntax Trix = new Syntax("trix",
057: "application/trix", ".trix", true);
058:
059: /**
060: * RDF Syntax Trig For more info see
061: * http://sites.wiwiss.fu-berlin.de/suhl/bizer/TriG/
062: */
063: public static final Syntax Trig = new Syntax("trig",
064: "application/x-trig", ".trig", true);
065:
066: /**
067: * register a new RDF Syntax you want to have available throughout your
068: * application.
069: *
070: * @param syntax
071: * the new syntax to register.
072: */
073: public static void register(Syntax syntax) {
074: SYNTAXES.add(syntax);
075: }
076:
077: /**
078: * return the RDF syntax with the given name.
079: *
080: * @param name
081: * the name of the syntax to search
082: * @return the syntax or <code>null</code>, if none registered
083: */
084: public static Syntax forName(String name) {
085: for (Syntax x : SYNTAXES) {
086: if (x.getName().equals(name))
087: return x;
088: }
089: return null;
090: }
091:
092: /**
093: * return the RDF syntax with the given MIME-type.
094: *
095: * @param mimeType
096: * the MIME-type of the syntax to find
097: * @return the syntax or <code>null</code>, if none registered
098: */
099: public static Syntax forMimeType(String mimeType) {
100: for (Syntax x : SYNTAXES) {
101: if (x.getMimeType().equals(mimeType))
102: return x;
103: }
104: return null;
105: }
106:
107: /**
108: * unregister an RDF Syntax from which you know that your application will
109: * never ever support it. This may help you to build user interfaces where
110: * users can select RDF syntaxes. If the syntax was unknown, returns false
111: *
112: * @param syntax
113: * the syntax to unregister
114: * @return true, if the syntax was found and removed
115: */
116: public static boolean unregister(Syntax syntax) {
117: return SYNTAXES.remove(syntax);
118: }
119:
120: /**
121: * list all available syntaxes. List is not modifyable.
122: *
123: * @return a list of available syntaxes
124: */
125: public static List<Syntax> list() {
126: return Collections.unmodifiableList(SYNTAXES);
127: }
128:
129: private final String name;
130:
131: private final String mimeType;
132:
133: private final String filenameExtension;
134:
135: /**
136: * return the MIME-type of this format.
137: *
138: * @return the MIME type
139: */
140: public String getMimeType() {
141: return mimeType;
142: }
143:
144: /**
145: * @return the common name of this format
146: */
147: public String getName() {
148: return name;
149: }
150:
151: /**
152: * @return the suggested filename-extension, including the leading '.'
153: */
154: public String getFilenameExtension() {
155: return filenameExtension;
156: }
157:
158: /**
159: * Generate a new Syntax
160: *
161: * @param name
162: * the name of the RDF syntax
163: * @param mimeType
164: * the MIMEtype of the RDF syntax
165: */
166: public Syntax(final String name, final String mimeType,
167: final String filenameExtension) {
168: super ();
169: this .name = name;
170: this .mimeType = mimeType;
171: this .filenameExtension = filenameExtension;
172: }
173:
174: /**
175: * Generate a new Syntax and register it
176: *
177: * @param name
178: * the name of the RDF syntax
179: * @param mimeType
180: * the MIMEtype of the RDF syntax
181: * @param registerItNow
182: * register the new Syntax now.
183: */
184: public Syntax(final String name, final String mimeType,
185: final String filenameExtension, boolean registerItNow) {
186: this (name, mimeType, filenameExtension);
187: if (registerItNow)
188: register(this );
189: }
190:
191: /**
192: * @see java.lang.Object#equals(java.lang.Object)
193: */
194: @Override
195: public boolean equals(Object obj) {
196: if (obj instanceof Syntax) {
197: return mimeType.equals(((Syntax) obj).mimeType);
198: }
199: return false;
200: }
201:
202: /**
203: * @see java.lang.Object#hashCode()
204: */
205: @Override
206: public int hashCode() {
207: return mimeType.hashCode();
208: }
209:
210: /**
211: * @see java.lang.Object#toString()
212: */
213: @Override
214: public String toString() {
215: return "RDF Syntax '" + name + "' MIME-type=" + mimeType;
216: }
217:
218: }
|