KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > cmr > repository > ContentData


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.cmr.repository;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22 import org.alfresco.util.EqualsHelper;
23
24 /**
25  * The compound property representing content
26  *
27  * @author Derek Hulley
28  */

29 public class ContentData implements Serializable JavaDoc
30 {
31     private static final long serialVersionUID = 8979634213050121462L;
32
33     private static char[] INVALID_CONTENT_URL_CHARS = new char[] {'|'};
34     
35     private final String JavaDoc contentUrl;
36     private final String JavaDoc mimetype;
37     private final long size;
38     private final String JavaDoc encoding;
39     
40     /**
41      * Construct a content property from a string
42      *
43      * @param contentPropertyStr the string representing the content details
44      * @return Returns a bean version of the string
45      */

46     public static ContentData createContentProperty(String JavaDoc contentPropertyStr)
47     {
48         // get the content url
49
int contentUrlIndex = contentPropertyStr.indexOf("contentUrl=");
50         if (contentUrlIndex == -1)
51         {
52             throw new AlfrescoRuntimeException(
53                     "ContentData string does not have a content URL: " +
54                     contentPropertyStr);
55         }
56         int mimetypeIndex = contentPropertyStr.indexOf("|mimetype=", contentUrlIndex + 11);
57         if (mimetypeIndex == -1)
58         {
59             throw new AlfrescoRuntimeException(
60                     "ContentData string does not have a mimetype: " +
61                     contentPropertyStr);
62         }
63         int sizeIndex = contentPropertyStr.indexOf("|size=", mimetypeIndex + 10);
64         if (sizeIndex == -1)
65         {
66             throw new AlfrescoRuntimeException(
67                     "ContentData string does not have a size: " +
68                     contentPropertyStr);
69         }
70         int encodingIndex = contentPropertyStr.indexOf("|encoding=", sizeIndex + 6);
71         if (encodingIndex == -1)
72         {
73             throw new AlfrescoRuntimeException(
74                     "ContentData string does not have an encoding: " +
75                     contentPropertyStr);
76         }
77         
78         String JavaDoc contentUrl = contentPropertyStr.substring(contentUrlIndex + 11, mimetypeIndex);
79         if (contentUrl.length() == 0)
80             contentUrl = null;
81         String JavaDoc mimetype = contentPropertyStr.substring(mimetypeIndex + 10, sizeIndex);
82         if (mimetype.length() == 0)
83             mimetype = null;
84         String JavaDoc sizeStr = contentPropertyStr.substring(sizeIndex + 6, encodingIndex);
85         if (sizeStr.length() == 0)
86             sizeStr = "0";
87         String JavaDoc encoding = contentPropertyStr.substring(encodingIndex + 10);
88         if (encoding.length() == 0)
89             encoding = null;
90         
91         long size = Long.valueOf(sizeStr);
92         
93         ContentData property = new ContentData(contentUrl, mimetype, size, encoding);
94         // done
95
return property;
96     }
97     
98     /**
99      * Constructs a new instance using the existing one as a template, but replacing the
100      * mimetype
101      *
102      * @param existing an existing set of content data, null to use default values
103      * @param mimetype the mimetype to set
104      * @return Returns a new, immutable instance of the data
105      */

106     public static ContentData setMimetype(ContentData existing, String JavaDoc mimetype)
107     {
108         ContentData ret = new ContentData(
109                 existing == null ? null : existing.contentUrl,
110                 mimetype,
111                 existing == null ? 0L : existing.size,
112                 existing == null ? "UTF-8" : existing.encoding);
113         // done
114
return ret;
115     }
116     
117     /**
118      * Create a compound set of data representing a single instance of <i>content</i>.
119      * <p>
120      * In order to ensure data integrity, the {@link #getMimetype() mimetype}
121      * must be set if the {@link #getContentUrl() content URL} is set.
122      *
123      * @param contentUrl the content URL. If this value is non-null, then the
124      * <b>mimetype</b> must be supplied.
125      * @param mimetype the content mimetype. This is mandatory if the <b>contentUrl</b> is specified.
126      * @param size the content size.
127      * @param encoding the content encoding.
128      */

129     public ContentData(String JavaDoc contentUrl, String JavaDoc mimetype, long size, String JavaDoc encoding)
130     {
131         checkContentUrl(contentUrl, mimetype);
132         
133         this.contentUrl = contentUrl;
134         this.mimetype = mimetype;
135         this.size = size;
136         this.encoding = encoding;
137     }
138     
139     public boolean equals(Object JavaDoc obj)
140     {
141         if (obj == this)
142             return true;
143         else if (obj == null)
144             return false;
145         else if (!(obj instanceof ContentData))
146             return false;
147         ContentData that = (ContentData) obj;
148         return (EqualsHelper.nullSafeEquals(this.contentUrl, that.contentUrl) &&
149                 EqualsHelper.nullSafeEquals(this.mimetype, that.mimetype) &&
150                 this.size == that.size &&
151                 EqualsHelper.nullSafeEquals(this.encoding, that.encoding));
152     }
153     
154     /**
155      * @return Returns a string of form: <code>contentUrl=xxx;mimetype=xxx;size=xxx;encoding=xxx</code>
156      */

157     public String JavaDoc toString()
158     {
159         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(80);
160         sb.append("contentUrl=").append(contentUrl == null ? "" : contentUrl)
161           .append("|mimetype=").append(mimetype == null ? "" : mimetype)
162           .append("|size=").append(size)
163           .append("|encoding=").append(encoding == null ? "" : encoding);
164         return sb.toString();
165     }
166     
167     /**
168      * @return Returns a URL identifying the specific location of the content.
169      * The URL must identify, within the context of the originating content
170      * store, the exact location of the content.
171      * @throws ContentIOException
172      */

173     public String JavaDoc getContentUrl()
174     {
175         return contentUrl;
176     }
177     
178     /**
179      * Checks that the content URL is correct, and also that the mimetype is
180      * non-null if the URL is present.
181      *
182      * @param contentUrl the content URL to check
183      * @param mimetype
184      */

185     private void checkContentUrl(String JavaDoc contentUrl, String JavaDoc mimetype)
186     {
187         // check the URL
188
if (contentUrl != null && contentUrl.length() > 0)
189         {
190             for (int i = 0; i < INVALID_CONTENT_URL_CHARS.length; i++)
191             {
192                 for (int j = contentUrl.length() - 1; j > -1; j--)
193                 {
194                     if (contentUrl.charAt(j) == INVALID_CONTENT_URL_CHARS[i])
195                     {
196                         throw new IllegalArgumentException JavaDoc(
197                                 "The content URL contains an invalid char: \n" +
198                                 " content URL: " + contentUrl + "\n" +
199                                 " char: " + INVALID_CONTENT_URL_CHARS[i] + "\n" +
200                                 " position: " + j);
201                     }
202                 }
203             }
204             // check that mimetype is present if URL is present
205
if (mimetype == null)
206             {
207                 throw new IllegalArgumentException JavaDoc(
208                         "The content mimetype must be set whenever the URL is set: \n" +
209                         " content URL: " + contentUrl + "\n" +
210                         " mimetype: " + mimetype);
211             }
212         }
213     }
214     
215     /**
216      * Gets content's mimetype.
217      *
218      * @return Returns a standard mimetype for the content or null if the mimetype
219      * is unkown
220      */

221     public String JavaDoc getMimetype()
222     {
223         return mimetype;
224     }
225     
226     /**
227      * Get the content's size
228      *
229      * @return Returns the size of the content
230      */

231     public long getSize()
232     {
233         return size;
234     }
235     
236     /**
237      * Gets the content's encoding.
238      *
239      * @return Returns a valid Java encoding, typically a character encoding, or
240      * null if the encoding is unkown
241      */

242     public String JavaDoc getEncoding()
243     {
244         return encoding;
245     }
246 }
247
Popular Tags