View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: LinkStack.java,v 1.1 2004/10/22 17:34:05 tassos Exp $
23  //
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Date;
28  import java.util.Iterator;
29  
30  import net.sourceforge.blogentis.om.Post;
31  import net.sourceforge.blogentis.turbine.BlogParameterParser;
32  
33  public class LinkStack implements Collection {
34      protected ArrayList keys = new ArrayList();
35  
36      protected ArrayList values = new ArrayList();
37  
38      public class LSItem {
39          String key, value;
40  
41          LSItem(String key, String value) {
42              this.key = key;
43              this.value = value;
44          }
45  
46          public String getKey() {
47              return key;
48          }
49  
50          public String getValue() {
51              return value;
52          }
53  
54      }
55  
56      public class LSIterator implements Iterator {
57          int i = 0;
58  
59          public boolean hasNext() {
60              return i < keys.size();
61          }
62  
63          public Object next() {
64              LSItem lsi = new LSItem((String)keys.get(i), (String)values.get(i));
65              i++;
66              return lsi;
67          }
68  
69          public void remove() {}
70      }
71  
72      public void push(String key, String value) {
73          keys.add(key);
74          values.add(value);
75      }
76  
77      public String get(String key) {
78          int i = keys.indexOf(key);
79          if (i == -1)
80              return null;
81          return (String)values.get(i);
82      }
83  
84      public int size() {
85          return keys.size();
86      }
87  
88      public void pop() {
89          if (keys.size() < 1)
90              return;
91          keys.remove(keys.size() - 1);
92          values.remove(values.size() - 1);
93      }
94  
95      public Iterator iterator() {
96          return new LSIterator();
97      }
98  
99      public String lastLink() {
100         if (values.size() == 0)
101             return null;
102         return (String)values.get(values.size() - 1);
103     }
104 
105     public boolean isEmpty() {
106         return false;
107     }
108 
109     public boolean contains(Object o) {
110         return keys.contains(o);
111     }
112 
113     public Object[] toArray() {
114         return null;
115     }
116 
117     public Object[] toArray(Object[] a) {
118         return null;
119     }
120 
121     public boolean add(Object o) {
122         return false;
123     }
124 
125     public boolean remove(Object o) {
126         return false;
127     }
128 
129     public boolean containsAll(Collection c) {
130         return false;
131     }
132 
133     public boolean addAll(Collection c) {
134         return false;
135     }
136 
137     public boolean removeAll(Collection c) {
138         return false;
139     }
140 
141     public boolean retainAll(Collection c) {
142         return false;
143     }
144 
145     public void clear() {}
146 
147     public void initFromParameters(BlogParameterParser pp) {
148         if (pp.getBlog() == null)
149             return;
150         AbsoluteLinkURL link = new AbsoluteLinkURL();
151         push(pp.getBlog().getTitle(), link.permaLink(pp.getBlog()).toString());
152         if (pp.getSection() != null)
153             push(pp.getSection().getTitle(), link
154                                                 .permaLink(pp.getSection())
155                                                     .toString());
156         if (pp.getPost() != null) {
157             Post post = pp.getPost();
158             if (post.getPostedTime() != null)
159                 addDateLink(post.getPostedTime(), link, pp);
160             push(post.getTitle(), link.permaLink(post).toString());
161         } else if (pp.getDate() != null) {
162             addDateLinks(pp.getDate(), link, pp);
163         }
164     }
165 
166     private void addDateLink(Date date, AbsoluteLinkURL link,
167                              BlogParameterParser pp) {
168         addDateLinks(new DateSpecification(date), link, pp);
169     }
170 
171     private void addDateLinks(DateSpecification ds, AbsoluteLinkURL link,
172                               BlogParameterParser pp) {
173         push(AbsoluteLinkURL.YearFormat.format(ds.getYear()),
174                 link.permaLink(pp.getBlog()).setDate(
175                         new DateSpecification(ds.getYear())).toString());
176         if (ds.hasMonth()) {
177             push(AbsoluteLinkURL.MonthFormat.format(ds.getMonth()),
178                     link
179                         .permaLink(pp.getBlog())
180                             .setDate(
181                                     new DateSpecification(ds.getYear(),
182                                         ds.getMonth()))
183                             .toString());
184             if (ds.hasDay()) {
185                 push(AbsoluteLinkURL.DayFormat.format(ds.getDay()),
186                         link.permaLink(pp.getBlog()).setDate(
187                                 new DateSpecification(ds.getYear(),
188                                     ds.getMonth(), ds.getDay())).toString());
189             }
190         }
191     }
192 }