KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > InfocenterWorkingSetManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.webapp.servlet;
12
13 import java.io.*;
14 import java.util.*;
15
16 import javax.servlet.http.*;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.help.internal.*;
20 import org.eclipse.help.internal.util.*;
21 import org.eclipse.help.internal.webapp.*;
22 import org.eclipse.help.internal.workingset.*;
23
24 /**
25  * The Infocenter working set manager stores help working sets. Working sets are
26  * persisted in client cookies whenever one is added or removed.
27  *
28  * @since 3.0
29  */

30 public class InfocenterWorkingSetManager implements IHelpWorkingSetManager {
31     private static final String JavaDoc COOKIE_NAME = "wset"; //$NON-NLS-1$
32
private static final int MAX_COOKIES = 15;
33     private HttpServletRequest request;
34     private HttpServletResponse response;
35
36     // Current working set , empty string means all documents
37
private String JavaDoc currentWorkingSet = ""; //$NON-NLS-1$
38
private SortedSet workingSets = new TreeSet(new WorkingSetComparator());
39     private String JavaDoc locale;
40     private AdaptableTocsArray root;
41
42     /**
43      * Constructor
44      *
45      * @param locale
46      */

47     public InfocenterWorkingSetManager(HttpServletRequest request,
48             HttpServletResponse response, String JavaDoc locale) {
49         this.request = request;
50         this.response = response;
51         this.locale = locale;
52         restoreState();
53     }
54
55     public AdaptableTocsArray getRoot() {
56         if (root == null)
57             root = new AdaptableTocsArray(HelpPlugin.getTocManager().getTocs(
58                     locale));
59         return root;
60     }
61
62     /**
63      * Adds a new working set and saves it
64      */

65     public void addWorkingSet(WorkingSet workingSet) throws IOException {
66         if (workingSet == null || workingSets.contains(workingSet))
67             return;
68         workingSets.add(workingSet);
69         saveState();
70     }
71
72     /**
73      * Creates a new working set
74      */

75     public WorkingSet createWorkingSet(String JavaDoc name,
76             AdaptableHelpResource[] elements) {
77         return new WorkingSet(name, elements);
78     }
79
80     /**
81      * Returns a working set by name
82      *
83      */

84     public WorkingSet getWorkingSet(String JavaDoc name) {
85         if (name == null || workingSets == null)
86             return null;
87
88         Iterator iter = workingSets.iterator();
89         while (iter.hasNext()) {
90             WorkingSet workingSet = (WorkingSet) iter.next();
91             if (name.equals(workingSet.getName()))
92                 return workingSet;
93         }
94         return null;
95     }
96
97     /**
98      * Implements IWorkingSetManager.
99      *
100      * @see org.eclipse.help.internal.workingset.IHelpWorkingSetManager#getWorkingSets()
101      */

102     public WorkingSet[] getWorkingSets() {
103         return (WorkingSet[]) workingSets.toArray(new WorkingSet[workingSets
104                 .size()]);
105     }
106
107     /**
108      * Removes specified working set
109      */

110     public void removeWorkingSet(WorkingSet workingSet) {
111         workingSets.remove(workingSet);
112         try {
113             saveState();
114         } catch (IOException ioe) {
115         }
116     }
117
118     private void restoreState() {
119         String JavaDoc data = CookieUtil.restoreString(COOKIE_NAME, request);
120         if (data == null) {
121             return;
122         }
123
124         String JavaDoc[] values = data.split("\\|", -1); //$NON-NLS-1$
125
if (values.length < 1) {
126             return;
127         }
128         currentWorkingSet = URLCoder.decode(values[0] /* , "UTF8" */
129         );
130         i : for (int i = 1; i < values.length; i++) {
131             String JavaDoc[] nameAndHrefs = values[i].split("&", -1); //$NON-NLS-1$
132

133             String JavaDoc name = URLCoder.decode(nameAndHrefs[0] /* , "UTF8" */
134             );
135
136             AdaptableHelpResource[] elements = new AdaptableHelpResource[nameAndHrefs.length - 1];
137             // for each href (working set resource)
138
for (int e = 0; e < nameAndHrefs.length - 1; e++) {
139                 int h = e + 1;
140                 elements[e] = getAdaptableToc(URLCoder.decode(nameAndHrefs[h]
141                 /* , "UTF8" */
142                 ));
143                 if (elements[e] == null) {
144                     elements[e] = getAdaptableTopic(URLCoder
145                             .decode(nameAndHrefs[h]
146                             /* , "UTF8" */
147                             ));
148                 }
149                 if (elements[e] == null) {
150                     // working set cannot be restored
151
continue i;
152                 }
153             }
154             WorkingSet ws = createWorkingSet(name, elements);
155             workingSets.add(ws);
156         }
157     }
158
159     /***************************************************************************
160      * Persists all working sets. Should only be called by the webapp working
161      * set dialog. Saves the working sets in the persistence store (cookie)
162      * format: curentWorkingSetName|name1&href11&href12|name2&href22
163      */

164     private void saveState() throws IOException {
165         StringBuffer JavaDoc data = new StringBuffer JavaDoc();
166         data.append(URLCoder.encode(currentWorkingSet /* , "UTF8" */
167         ));
168
169         for (Iterator i = workingSets.iterator(); i.hasNext();) {
170             data.append('|');
171             WorkingSet ws = (WorkingSet) i.next();
172             data.append(URLCoder.encode(ws.getName() /* , "UTF8" */
173             ));
174
175             AdaptableHelpResource[] resources = ws.getElements();
176             for (int j = 0; j < resources.length; j++) {
177                 data.append('&');
178
179                 IAdaptable parent = resources[j].getParent();
180                 if (parent == getRoot()) {
181                     // saving toc
182
data.append(URLCoder.encode(resources[j].getHref()
183                     /* , "UTF8" */
184                     ));
185                 } else {
186                     // saving topic as tochref_topic#_
187
AdaptableToc toc = (AdaptableToc) parent;
188                     AdaptableHelpResource[] siblings = (toc).getChildren();
189                     for (int t = 0; t < siblings.length; t++) {
190                         if (siblings[t] == resources[j]) {
191                             data.append(URLCoder.encode(toc.getHref()
192                             /* , "UTF8" */
193                             ));
194                             data.append('_');
195                             data.append(t);
196                             data.append('_');
197                             break;
198                         }
199                     }
200                 }
201             }
202         }
203
204         try {
205             CookieUtil.saveString(COOKIE_NAME, data.toString(), MAX_COOKIES,
206                     request, response);
207         } catch (IOException ioe) {
208             if (HelpWebappPlugin.DEBUG_WORKINGSETS) {
209                 System.out
210                         .println("InfocenterWorkingSetManager.saveState(): Too much data to save: " //$NON-NLS-1$
211
+ data.toString());
212             }
213             throw ioe;
214         }
215     }
216
217     /**
218      * *
219      *
220      * @param changedWorkingSet
221      * the working set that has changed
222      */

223     public void workingSetChanged(WorkingSet changedWorkingSet)
224             throws IOException {
225         saveState();
226     }
227
228     public AdaptableToc getAdaptableToc(String JavaDoc href) {
229         return getRoot().getAdaptableToc(href);
230     }
231
232     public AdaptableTopic getAdaptableTopic(String JavaDoc id) {
233
234         if (id == null || id.length() == 0)
235             return null;
236
237         // toc id's are hrefs: /pluginId/path/to/toc.xml
238
// topic id's are based on parent toc id and index of topic:
239
// /pluginId/path/to/toc.xml_index_
240
int len = id.length();
241         if (id.charAt(len - 1) == '_') {
242             // This is a first level topic
243
String JavaDoc indexStr = id.substring(id.lastIndexOf('_', len - 2) + 1,
244                     len - 1);
245             int index = 0;
246             try {
247                 index = Integer.parseInt(indexStr);
248             } catch (Exception JavaDoc e) {
249             }
250
251             String JavaDoc tocStr = id.substring(0, id.lastIndexOf('_', len - 2));
252             AdaptableToc toc = getAdaptableToc(tocStr);
253             if (toc == null)
254                 return null;
255             IAdaptable[] topics = toc.getChildren();
256             if (index < 0 || index >= topics.length)
257                 return null;
258             return (AdaptableTopic) topics[index];
259         }
260
261         return null;
262     }
263
264     public String JavaDoc getCurrentWorkingSet() {
265         return currentWorkingSet;
266     }
267
268     public void setCurrentWorkingSet(String JavaDoc workingSet) {
269         currentWorkingSet = workingSet;
270         try {
271             saveState();
272         } catch (IOException ioe) {
273         }
274     }
275
276 }
277
Popular Tags