001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MimeType.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf;
009:
010: import com.uwyn.rife.cmf.format.Formatter;
011: import com.uwyn.rife.cmf.format.ImageFormatter;
012: import com.uwyn.rife.cmf.format.PlainTextFormatter;
013: import com.uwyn.rife.cmf.format.RawFormatter;
014: import com.uwyn.rife.cmf.format.XhtmlFormatter;
015: import com.uwyn.rife.cmf.validation.CmfPropertyValidationRule;
016: import com.uwyn.rife.cmf.validation.SupportedImage;
017: import com.uwyn.rife.cmf.validation.SupportedXhtml;
018: import com.uwyn.rife.datastructures.EnumClass;
019: import com.uwyn.rife.site.ConstrainedProperty;
020:
021: /**
022: * This is a typed enumeration of all the mime types that the content
023: * management framework specifically knows about.
024: * <p>The types that are defined here can be validated and transformed.
025: *
026: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
027: * @version $Revision: 3634 $
028: * @since 1.0
029: */
030: public abstract class MimeType extends EnumClass<String> {
031: /**
032: * The <code>application/xhtml+xml</code> mime type.
033: */
034: public static final MimeType APPLICATION_XHTML = new MimeType(
035: "application/xhtml+xml") {
036: public CmfPropertyValidationRule getValidationRule(
037: ConstrainedProperty constrainedProperty) {
038: return new SupportedXhtml(constrainedProperty
039: .getPropertyName(), constrainedProperty
040: .isFragment());
041: }
042:
043: public Formatter getFormatter() {
044: return new XhtmlFormatter();
045: }
046: };
047: /**
048: * The <code>image/gif</code> mime type.
049: */
050: public static final MimeType IMAGE_GIF = new MimeType("image/gif") {
051: public CmfPropertyValidationRule getValidationRule(
052: ConstrainedProperty constrainedProperty) {
053: return new SupportedImage(constrainedProperty
054: .getPropertyName());
055: }
056:
057: public Formatter getFormatter() {
058: return new ImageFormatter();
059: }
060: };
061: /**
062: * The <code>image/jpeg</code> mime type.
063: */
064: public static final MimeType IMAGE_JPEG = new MimeType("image/jpeg") {
065: public CmfPropertyValidationRule getValidationRule(
066: ConstrainedProperty constrainedProperty) {
067: return new SupportedImage(constrainedProperty
068: .getPropertyName());
069: }
070:
071: public Formatter getFormatter() {
072: return new ImageFormatter();
073: }
074: };
075: /**
076: * The <code>image/png</code> mime type.
077: */
078: public static final MimeType IMAGE_PNG = new MimeType("image/png") {
079: public CmfPropertyValidationRule getValidationRule(
080: ConstrainedProperty constrainedProperty) {
081: return new SupportedImage(constrainedProperty
082: .getPropertyName());
083: }
084:
085: public Formatter getFormatter() {
086: return new ImageFormatter();
087: }
088: };
089: /**
090: * The <code>text/plain</code> mime type.
091: */
092: public static final MimeType TEXT_PLAIN = new MimeType("text/plain") {
093: public CmfPropertyValidationRule getValidationRule(
094: ConstrainedProperty constrainedProperty) {
095: return null;
096: }
097:
098: public Formatter getFormatter() {
099: return new PlainTextFormatter();
100: }
101: };
102: /**
103: * The <code>text/plain</code> mime type.
104: */
105: public static final MimeType TEXT_XML = new MimeType("text/xml") {
106: public CmfPropertyValidationRule getValidationRule(
107: ConstrainedProperty constrainedProperty) {
108: return null;
109: }
110:
111: public Formatter getFormatter() {
112: return new PlainTextFormatter();
113: }
114: };
115: /**
116: * A generic mime type indicating that the content should be stored as raw
117: * data without any mime-type related processing.
118: */
119: public static final MimeType RAW = new MimeType("raw") {
120: public CmfPropertyValidationRule getValidationRule(
121: ConstrainedProperty constrainedProperty) {
122: return null;
123: }
124:
125: public Formatter getFormatter() {
126: return new RawFormatter();
127: }
128: };
129:
130: /**
131: * Constructs and returns a CMF-specific validation rule that is able to
132: * validate data for this mime type.
133: *
134: * @param constrainedProperty an instance of the property for which the
135: * validation rule has to be built
136: * @return an instance of the validation rule
137: * @since 1.0
138: */
139: public abstract CmfPropertyValidationRule getValidationRule(
140: ConstrainedProperty constrainedProperty);
141:
142: /**
143: * Returns an instance of the formatter for this mime type.
144: *
145: * @return an instance of the formatter
146: * @since 1.0
147: */
148: public abstract Formatter getFormatter();
149:
150: /**
151: * Returns the <code>MimeType</code> instance that corresponds to a given
152: * textual identifier.
153: *
154: * @param identifier the identifier of the mime type that has to be
155: * retrieved
156: * @return the requested <code>MimeType</code>; or
157: * <p><code>null</code> if the <code>MimeType</code> is not supported
158: * @since 1.0
159: */
160: public static MimeType getMimeType(String identifier) {
161: return getMember(MimeType.class, identifier);
162: }
163:
164: MimeType(String identifier) {
165: super (MimeType.class, identifier);
166: }
167: }
|