Source code: com/opencms/template/cache/CmsElementVariant.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/template/cache/CmsElementVariant.java,v $
3 * Date : $Date: 2003/02/15 11:14:53 $
4 * Version: $Revision: 1.13 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 package com.opencms.template.cache;
30
31 import java.io.UnsupportedEncodingException;
32 import java.util.Vector;
33
34 /**
35 * An instance of CmsElementVariant stores a single cached variant for an
36 * element. This is the generated output (content) of an element. This cache
37 * stores all generated strings of this element and all links to other elements.
38 *
39 * @author Andreas Schouten
40 * @author Alexander Lucas
41 */
42 public class CmsElementVariant {
43
44 /**
45 * The content of this variant. In this vector object of type String
46 * and of CmsElementLink can be stored.
47 */
48 Vector m_content;
49
50 /**
51 * The dependencies of this variant. In this vector objects of type String
52 * can be stored. These Strings are resources in the vfs or the cos. If one
53 * of the resources change this variant is decleared void.
54 */
55 Vector m_dependencies;
56
57 /**
58 * The date when this variant must be new generated. Only used if it is not 0.
59 */
60 private long m_nextTimeout = 0;
61
62 /**
63 * Marker that indicates if this variant was exported before.
64 */
65 private boolean m_exported = false;
66
67 /**
68 * Creates a new empty variant for an element.
69 */
70 public CmsElementVariant() {
71 m_content = new Vector();
72 }
73
74 /**
75 * Adds static content to this variant.
76 * @param staticContent - part of the variant. A peace static content of
77 * type string.
78 */
79 public void add(String staticContent) {
80 m_content.add(staticContent);
81 }
82
83 /**
84 * Adds static content to this variant.
85 * @param staticContent - part of the variant. A peace static content of
86 * type byte-array.
87 */
88 public void add(byte[] staticContent) {
89 m_content.add(staticContent);
90 }
91
92 /**
93 * Adds static content to this variant.
94 * @param staticContent - part of the variant. A peace static content of
95 * type byte-array.
96 */
97 public void add(byte[] staticContent, String encoding) {
98 try {
99 m_content.add(new String(staticContent, encoding));
100 } catch (UnsupportedEncodingException uee) {
101 m_content.add(staticContent);
102 }
103 }
104
105 /**
106 * Adds an element-link to this variant.
107 * @param elementLink - part of the variant. A link to another element.
108 */
109 public void add(CmsElementLink elementLink) {
110 m_content.add(elementLink);
111 }
112
113 /**
114 * Adds an method-link to this variant.
115 * @param methodLink - part of the variant. A link to an method.
116 */
117 public void add(CmsMethodLink methodLink) {
118 m_content.add(methodLink);
119 }
120
121 /**
122 * Get the number of objects in this variant.
123 */
124 public int size() {
125 return m_content.size();
126 }
127
128 /**
129 * Returns a peace of this variant. It can be of the type String, byte[] or
130 * CmsElementLink.
131 * @param i - the index to the vector of variant-pieces.
132 */
133 public Object get(int i) {
134 return m_content.get(i);
135 }
136
137 /**
138 * Sets the dependencies Vector for this Variant.
139 * @param dependencies A Vector of Strings.
140 */
141 public void setDependencies(Vector dependencies){
142 m_dependencies = dependencies;
143 }
144
145 /**
146 * Returns true if this variant was allready exported.
147 */
148 public boolean wasExported(){
149 return m_exported;
150 }
151
152 /**
153 * Sets the marker exported to true. Used when this variant is created in
154 * export modus.
155 */
156 public void setExported(){
157 m_exported = true;
158 }
159
160 /**
161 * Gets the dependencies Vector of this Variant.
162 *
163 * @return dependencies A Vector of Strings.
164 */
165 public Vector getDependencies(){
166 return m_dependencies;
167 }
168
169 /**
170 * Add a dependencies Vector to this.
171 *
172 * @param depVariant The Vector with the dependencies.
173 */
174 public void addDependencies(Vector depVariant){
175 if(m_dependencies == null){
176 m_dependencies = depVariant;
177 }else if (depVariant != null){
178 // both vectors not null, we have to merge
179 for (int i = 0; i < depVariant.size(); i++){
180 m_dependencies.add(depVariant.elementAt(i));
181 }
182 }
183 }
184
185 /**
186 * Get a string representation of this variant.
187 * @return String representation.
188 */
189 public String toString() {
190 int len = m_content.size();
191 StringBuffer result = new StringBuffer("[CmsElementVariant] (" + len + ") :");
192 for(int i=0; i<len; i++) {
193 Object o = m_content.elementAt(i);
194 if(o instanceof byte[] || o instanceof String) {
195 result.append("TXT");
196 } else {
197 result.append("(");
198 result.append(o.toString());
199 result.append(")");
200 }
201 if(i < len-1) result.append("-");
202 }
203 return result.toString();
204 }
205
206 /**
207 * Merges the time when this variant has to be new generated.
208 * Sets it to the minimum of the old and the new value, whereby 0 don't count.
209 * @param timeout. The date as a long.
210 */
211 public void mergeNextTimeout(long timeout){
212
213 if(m_nextTimeout == 0 || timeout == 0){
214 if(m_nextTimeout < timeout){
215 m_nextTimeout = timeout;
216 }
217 }else{
218 if(m_nextTimeout > timeout){
219 m_nextTimeout = timeout;
220 }
221 }
222 }
223 /**
224 * Returns the time when this variant has to be new generated.
225 * @return timeout. The date as a long.
226 */
227 public long getNextTimeout(){
228 return m_nextTimeout;
229 }
230 /**
231 * Returns true if this variant has an expiration date.
232 */
233 public boolean isTimeCritical(){
234 return m_nextTimeout != 0;
235 }
236 }