KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplets > basket > ContentItem


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.coplets.basket;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.apache.cocoon.portal.coplet.CopletInstanceData;
21
22
23 /**
24  * This is an item that contains a link or a content.
25  * The item can either reference a coplet or an URL.
26  *
27  * @version CVS $Id: ContentItem.java 125056 2005-01-13 10:33:37Z cziegeler $
28  */

29 public class ContentItem extends AbstractItem implements Serializable JavaDoc {
30     
31     /** The id of the referenced coplet */
32     protected String JavaDoc copletId;
33     /** Do we store the content or just the link? */
34     protected boolean storesContent;
35     /** The referenced url */
36     protected String JavaDoc url;
37     /** The cached string rep */
38     protected String JavaDoc stringRep;
39     /** The content */
40     protected byte[] content;
41     
42     /**
43      * Create a new item referencing a coplet instance data
44      * @param cid The coplet
45      * @param content Do we store the content (false: a link)
46      */

47     public ContentItem(CopletInstanceData cid, boolean content) {
48         this.copletId = cid.getId();
49         this.storesContent = content;
50     }
51
52     /**
53      * Create a new item referencing to a url
54      * @param url The url
55      * @param content Do we store the content (false: a link)
56      */

57     public ContentItem(String JavaDoc url, boolean content) {
58         this.url = url;
59         this.storesContent = content;
60     }
61     
62     /**
63      * Return the url of null for a coplet
64      */

65     public String JavaDoc getURL() {
66         return this.url;
67     }
68     
69     /**
70      * Return the referenced coplet or null for a url
71      */

72     public String JavaDoc getCopletId() {
73         return this.copletId;
74     }
75     
76     /**
77      * Do we store the content? (or just the link)
78      */

79     public boolean isContent() {
80         return this.storesContent;
81     }
82     
83     /**
84      * Set the content
85      */

86     public void setContent(byte[] c) {
87         this.storesContent = true;
88         this.content = c;
89     }
90     
91     /**
92      * Get the content or null
93      */

94     public byte[] getContent() {
95         return this.content;
96     }
97     
98     /**
99      * Return the size if content is stored
100      * Otherwise -1 is returned
101      */

102     public int size() {
103         if ( this.content != null ) {
104             return this.content.length;
105         }
106         return -1;
107     }
108     
109     /* (non-Javadoc)
110      * @see java.lang.Object#toString()
111      */

112     public String JavaDoc toString() {
113         if ( this.stringRep == null ) {
114             if ( this.copletId != null ) {
115                 this.stringRep = "Coplet:" + this.copletId + "(" + this.storesContent + ")";
116             } else {
117                 this.stringRep = "URL:" + this.url + "(" + this.storesContent + ")";
118             }
119         }
120         return this.stringRep;
121     }
122     
123     /**
124      * Compare one item with another
125      */

126     public boolean equalsItem(ContentItem ci) {
127         if ( ci != null && ci.storesContent == this.storesContent ) {
128             if ( ci.url != null && ci.url.equals(this.url)) {
129                 return true;
130             }
131             if ( ci.copletId != null
132                  && this.copletId != null
133                  && ci.copletId.equals(this.copletId)) {
134                 return true;
135             }
136         }
137         return false;
138     }
139     
140     public void setTitle(String JavaDoc title) {
141         this.stringRep = title;
142     }
143 }
144
Popular Tags