KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
26 import java.util.*;
27 import org.meshcms.util.*;
28
29 /**
30  * Compares two object of type {@link PageInfo} by comparing paths, scores and
31  * hits. This comparator can <em>only</em> be used with pages contained in the
32  * site map.
33  *
34  * @see SiteMap
35  */

36 public final class PageInfoComparator implements Comparator, Serializable {
37   private SiteMap siteMap;
38   private SiteInfo siteInfo;
39
40   public PageInfoComparator(SiteMap siteMap, SiteInfo siteInfo) {
41     this.siteMap = siteMap;
42     this.siteInfo = siteInfo;
43   }
44
45   /**
46    * Calls {@link #compare(PageInfo, PageInfo)}.
47    */

48   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
49     try {
50       return compare((PageInfo) o1, (PageInfo) o2);
51     } catch (ClassCastException JavaDoc ex) {}
52
53     return 0;
54   }
55
56   /**
57    * Compares two pages. The first criterion is the path of the page: pages
58    * are sorted comparing the first different element of the path.
59    */

60   public int compare(PageInfo pageInfo1, PageInfo pageInfo2) {
61     Path path1 = pageInfo1.getPath();
62     Path path2 = pageInfo2.getPath();
63
64     if (path1.equals(path2)) { // same path == same page
65
return 0;
66     }
67
68     if (path1.isRoot()) { // root always come first
69
return -1;
70     }
71
72     if (path2.isRoot()) { // root always come first
73
return 1;
74     }
75
76     Path commonPath = path1.getCommonPath(path2);
77
78     if (commonPath.equals(path1)) { // path2 is contained in path1
79
return -1;
80     }
81
82     if (commonPath.equals(path2)) { // path1 is contained in path2
83
return 1;
84     }
85
86     // compare the paths up to the first different element. For example, if
87
// path1 == /home/subdir/otherdir/page.html, and
88
// path2 == /home/subdir/otherpage.html,
89
// we compare /home/subdir/otherdir to /home/subdir/otherpage.html
90
Path subPath1 = path1.getPartial(commonPath.getElementCount() + 1);
91     Path subPath2 = path2.getPartial(commonPath.getElementCount() + 1);
92     return compareSameLevel(subPath1, subPath2);
93   }
94
95   private int compareSameLevel(Path subPath1, Path subPath2) {
96     // compare scores
97
int score1 = siteInfo.getPageScore(subPath1);
98     int score2 = siteInfo.getPageScore(subPath2);
99
100     if (score1 > score2) {
101       return -1;
102     }
103
104     if (score1 < score2) {
105       return 1;
106     }
107
108     // Get page infos and compares them by hit counts.
109
PageInfo pageInfo1 = siteMap.getPageInfo(subPath1);
110     PageInfo pageInfo2 = siteMap.getPageInfo(subPath2);
111
112     if (pageInfo1.getTotalHits() > pageInfo2.getTotalHits()) {
113       return -1;
114     }
115
116     if (pageInfo1.getTotalHits() < pageInfo2.getTotalHits()) {
117       return 1;
118     }
119
120     return 0;
121   }
122 }
123
Popular Tags