KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > PageInfo


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import org.meshcms.util.*;
26
27 /**
28  * This class describes a web page with all the related info (path, title, hits
29  * and so on).
30  */

31 public final class PageInfo {
32   private Path path;
33   private String JavaDoc title;
34   private int[] stats;
35   private int statSum;
36   private int lastStatsIndex;
37   private WebSite webSite;
38   private long lastModified;
39   private String JavaDoc charset;
40
41   /**
42    * Creates a page info in the specified {@link WebSite} to describe the page
43    * available at the specified path.
44    */

45   public PageInfo(WebSite webSite, Path path) {
46     this.webSite = webSite;
47     this.path = path;
48     stats = new int[webSite.getStatsLength()];
49   }
50
51   /**
52    * @return the path of the page.
53    */

54   public Path getPath() {
55     return path;
56   }
57
58   /**
59    * Sets the title of the page.
60    */

61   public void setTitle(String JavaDoc title) {
62     this.title = title;
63   }
64
65   /**
66    * @return the title of the page.
67    */

68   public String JavaDoc getTitle() {
69     return title;
70   }
71
72   /**
73    * @return the file name of the page.
74    */

75   public String JavaDoc getName() {
76     return path.getLastElement();
77   }
78
79   /**
80    * Adds a hit to the count.
81    */

82   public synchronized void addHit() {
83     stats[getIndex()]++;
84     statSum++;
85   }
86
87   /**
88    * @return the hit count for the last day.
89    */

90   public synchronized int getHits() {
91     return stats[getIndex()];
92   }
93
94   /**
95    * @return the hit count for a previous day.
96    */

97   public synchronized int getHits(int daysBefore) {
98     int index = getIndex() - daysBefore;
99     return stats[index < 0 ? index + stats.length : index];
100   }
101
102   /**
103    * @return the total hit count.
104    */

105   public synchronized int getTotalHits() {
106     getIndex(); // to update index before sum
107
return statSum;
108   }
109
110   protected synchronized int[] getStats() {
111     return stats;
112   }
113
114   protected synchronized int getLastStatsIndex() {
115     return lastStatsIndex;
116   }
117
118   protected synchronized void copyStatsFrom(PageInfo other) {
119     getIndex(); // to update index before copying
120
statSum = other.getTotalHits(); // this one calls other.getIndex()
121
stats = other.getStats();
122     lastStatsIndex = other.getLastStatsIndex();
123   }
124
125   private synchronized int getIndex() {
126     int index = webSite.getStatsIndex();
127
128     if (index != lastStatsIndex) {
129       lastStatsIndex = index;
130       statSum -= stats[index];
131       stats[index] = 0;
132     }
133
134     return index;
135   }
136
137   /**
138    * @return the depth level of the page.
139    */

140   public int getLevel() {
141     return path.getElementCount();
142   }
143
144   /**
145    * @return the title of the page (same as {@link #getTitle}).
146    */

147   public String JavaDoc toString() {
148     return title;
149   }
150
151   /**
152    * @return the time of the last modification made to the page.
153    */

154   public long getLastModified() {
155     return lastModified;
156   }
157
158   /**
159    * Sets the time of the last modification made to the page. This value
160    * should be set equal to the value of <code>java.io.File.lastModified()</code>.
161    */

162   public void setLastModified(long lastModified) {
163     this.lastModified = lastModified;
164   }
165
166   public String JavaDoc getCharset() {
167     return charset;
168   }
169
170   public void setCharset(String JavaDoc charset) {
171     this.charset = charset;
172   }
173 }
174
Popular Tags