001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.coplets.basket;
018:
019: import java.io.Serializable;
020:
021: import org.apache.cocoon.portal.coplet.CopletInstanceData;
022:
023: /**
024: * This is an item that contains a link or a content.
025: * The item can either reference a coplet or an URL.
026: *
027: * @version CVS $Id: ContentItem.java 433543 2006-08-22 06:22:54Z crossley $
028: */
029: public class ContentItem extends AbstractItem implements Serializable {
030:
031: /** The id of the referenced coplet */
032: protected String copletId;
033: /** Do we store the content or just the link? */
034: protected boolean storesContent;
035: /** The referenced url */
036: protected String url;
037: /** The cached string rep */
038: protected String stringRep;
039: /** The content */
040: protected byte[] content;
041:
042: /**
043: * Create a new item referencing a coplet instance data
044: * @param cid The coplet
045: * @param content Do we store the content (false: a link)
046: */
047: public ContentItem(CopletInstanceData cid, boolean content) {
048: this .copletId = cid.getId();
049: this .storesContent = content;
050: }
051:
052: /**
053: * Create a new item referencing to a url
054: * @param url The url
055: * @param content Do we store the content (false: a link)
056: */
057: public ContentItem(String url, boolean content) {
058: this .url = url;
059: this .storesContent = content;
060: }
061:
062: /**
063: * Return the url of null for a coplet
064: */
065: public String getURL() {
066: return this .url;
067: }
068:
069: /**
070: * Return the referenced coplet or null for a url
071: */
072: public String getCopletId() {
073: return this .copletId;
074: }
075:
076: /**
077: * Do we store the content? (or just the link)
078: */
079: public boolean isContent() {
080: return this .storesContent;
081: }
082:
083: /**
084: * Set the content
085: */
086: public void setContent(byte[] c) {
087: this .storesContent = true;
088: this .content = c;
089: }
090:
091: /**
092: * Get the content or null
093: */
094: public byte[] getContent() {
095: return this .content;
096: }
097:
098: /**
099: * 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 toString() {
113: if (this .stringRep == null) {
114: if (this .copletId != null) {
115: this .stringRep = "Coplet:" + this .copletId + "("
116: + this .storesContent + ")";
117: } else {
118: this .stringRep = "URL:" + this .url + "("
119: + this .storesContent + ")";
120: }
121: }
122: return this .stringRep;
123: }
124:
125: /**
126: * Compare one item with another
127: */
128: public boolean equalsItem(ContentItem ci) {
129: if (ci != null && ci.storesContent == this .storesContent) {
130: if (ci.url != null && ci.url.equals(this .url)) {
131: return true;
132: }
133: if (ci.copletId != null && this .copletId != null
134: && ci.copletId.equals(this .copletId)) {
135: return true;
136: }
137: }
138: return false;
139: }
140:
141: public void setTitle(String title) {
142: this.stringRep = title;
143: }
144: }
|