001: package wingscms;
002:
003: import org.wings.*;
004:
005: /**
006: * Wiki integration example
007: *
008: * @author <a href="mailto:christian.fetzer@uni-ulm.de">Christian Fetzer</a>
009: */
010: public class WikiIntegration {
011:
012: private SFrame rootFrame = new SFrame();
013: private SPanel panel = new SPanel();
014: private STemplateLayout templateLayout = new STemplateLayout();
015:
016: /**
017: * Default constructor
018: */
019: public WikiIntegration() {
020:
021: // // Sets the session's resource mapper
022: // SessionManager.getSession().setResourceMapper(new WikiResourceMapper(rootFrame));
023: //
024: // // Shoppingcart contents (Key = product index in PLCONTENT, Value = amount)
025: // //final HashMap<Integer, Integer> SCCONTENT = new HashMap<Integer, Integer>();
026: // // SessionManager.getSession().setProperty("shoppingcart", SCCONTENT);
027: // final LinkedList<ShoppingCartItem> SCLIST = new LinkedList<ShoppingCartItem>();
028: //
029: // // Pricelist panel contains the pricelist table,
030: // // an addtoshoppingcart Button and a message label.
031: // final SPanel ppricelist = new SPanel(new SFlowDownLayout());
032: //
033: // // Result label
034: // final SLabel message = new SLabel();
035: //
036: // // Products
037: // final LinkedList<Product> PRODUCTS = new LinkedList<Product>();
038: // PRODUCTS.add(new Product(1001, "Rotes Auto", 50000d));
039: // PRODUCTS.add(new Product(1002, "Blaues Auto", 75000d));
040: // PRODUCTS.add(new Product(1003, "Schwarzes Auto", 100000d));
041: //
042: // // Productlist table
043: // final String[] PLCOLNAMES = { "Art. Nr.", "Artikel", "Stückpreis (in €)" };
044: // final STable productlist = new STable(new DefaultTableModel() {
045: // public Object getValueAt(int row, int col) {
046: // Product product = PRODUCTS.get(row);
047: // switch (col) {
048: // case 0:
049: // return product.getItemnumber();
050: // case 1:
051: // return product.getDescription();
052: // case 2:
053: // return product.getPrice();
054: // default:
055: // return null;
056: // }
057: // }
058: //
059: // public int getRowCount() {
060: // return PRODUCTS.size();
061: // }
062: //
063: // public int getColumnCount() {
064: // return PLCOLNAMES.length;
065: // }
066: //
067: // public String getColumnName(int i) {
068: // return PLCOLNAMES[i];
069: // }
070: // });
071: // productlist.addSelectionListener(new ListSelectionListener() {
072: // public void valueChanged(ListSelectionEvent e) {
073: // if (productlist.getSelectedRow() != -1) {
074: // message.setText("");
075: // ppricelist.reload();
076: // }
077: // }
078: // });
079: // productlist.setEditable(false);
080: //
081: // // Shoppingcart table
082: // final String[] SCCOLNAMES = { "Art. Nr.", "Artikel", "Stückpreis (in €)", "Anzahl", "Gesamtpreis (in €)" };
083: // final STable shoppingcart = new STable(new DefaultTableModel() {
084: // public Object getValueAt(int row, int col) {
085: // ShoppingCartItem item = SCLIST.get(row);
086: // switch (col) {
087: // case 0:
088: // return item.getProduct().getItemnumber();
089: // case 1:
090: // return item.getProduct().getDescription();
091: // case 2:
092: // return item.getProduct().getPrice();
093: // case 3:
094: // return item.getAmount();
095: // case 4:
096: // return item.getAllRoundPrice();
097: // default:
098: // return null;
099: // }
100: // }
101: //
102: // public int getRowCount() {
103: // return SCLIST.size();
104: // }
105: //
106: // public int getColumnCount() {
107: // return SCCOLNAMES.length;
108: // }
109: //
110: // public String getColumnName(int i) {
111: // return SCCOLNAMES[i];
112: // }
113: // });
114: // shoppingcart.setEditable(false);
115: //
116: // // Addtoshoppingcart button
117: // SButton addtoshoppingcart = new SButton("In den Warenkorb");
118: // addtoshoppingcart.addActionListener(new ActionListener() {
119: // public void actionPerformed(ActionEvent ae) {
120: // if (productlist.getSelectedRow() != -1) {
121: // Product selection = PRODUCTS.get(productlist.getSelectedRow());
122: //
123: // // If the selected item is already in the shoppingcart just increase the amount
124: // boolean isinlist = false;
125: // for (ShoppingCartItem item : SCLIST) {
126: // if (item.getProduct().equals(selection)) {
127: // item.setAmount(item.getAmount() + 1);
128: // isinlist = true;
129: // break;
130: // }
131: // }
132: // if (!isinlist) {
133: // SCLIST.add(new ShoppingCartItem(selection));
134: // shoppingcart.reload();
135: // }
136: //
137: // message.setForeground(Color.BLACK);
138: // message.setText(
139: // "Das Produkt \"" + selection.getDescription() + "\" wurde zum Warenkorb hinzugefügt.");
140: // productlist.clearSelection();
141: // ppricelist.reload();
142: // } else {
143: // message.setForeground(Color.RED);
144: // message.setText("Fehler: Kein Produkt ausgewählt.");
145: // }
146: // }
147: // });
148: //
149: // // Pricelist panel
150: // ppricelist.add(productlist);
151: // ppricelist.add(addtoshoppingcart);
152: //
153: // panel.add(ppricelist, "PRICELIST");
154: // panel.add(shoppingcart, "SHOPPINGCART");
155: // panel.add(message, "MESSAGE");
156: //
157: // panel.setLayout(templateLayout);
158: // panel.setPreferredSize(SDimension.FULLAREA);
159: // panel.setVerticalAlignment(SConstants.TOP_ALIGN);
160: //
161: // rootFrame.getContentPane().add(panel);
162: // rootFrame.getContentPane().setPreferredSize(SDimension.FULLAREA);
163: // rootFrame.getContentPane().setVerticalAlignment(SConstants.TOP_ALIGN);
164: // rootFrame.setBorder(null);
165: // rootFrame.setAttribute("margin", "0px");
166: // rootFrame.setVisible(true);
167: }
168:
169: // /**
170: // * Implements a wiki resource mapper:
171: // * Load wiki page as template (with caching)
172: // */
173: // class WikiResourceMapper
174: // implements ResourceMapper {
175: // final static String WIKIHOST = "localhost";
176: // final static String TEMPLATEPATH = "/joomla";
177: //
178: // DynamicResource defaultResource;
179: // Map<String, String> contentMap = new HashMap<String, String>();
180: // Map<String, Date> obtainedMap = new HashMap<String, Date>();
181: // SimpleDateFormat httpdate;
182: // private String path;
183: //
184: // private SFrame frame;
185: // private List<Script> scripts = new ArrayList<Script>();
186: // private Collection<Link> links = new ArrayList<Link>();
187: //
188: // /**
189: // * Determine the DefaultResource and initialize the httpdate
190: // *
191: // * @param frame
192: // */
193: // public WikiResourceMapper(SFrame frame) {
194: // this.frame = frame;
195: // defaultResource = frame.getDynamicResource(ReloadResource.class);
196: //
197: // // httpdate parses and formats dates in HTTP Date Format (RFC1123)
198: // httpdate = new SimpleDateFormat(DateParser.PATTERN_RFC1123, Locale.ENGLISH);
199: // httpdate.setTimeZone(TimeZone.getTimeZone("GMT"));
200: //
201: // navigate(SessionManager.getSession().getServletRequest().getPathInfo());
202: // }
203: //
204: // /**
205: // * Load wiki page as template
206: // *
207: // * @param url the path info
208: // * @return the default resource
209: // */
210: // public Resource mapResource(String url) {
211: // navigate(url);
212: // return defaultResource;
213: // }
214: //
215: //// private void navigate(String url) {
216: ////
217: //// Map<String, String[]> parameters = SessionManager.getSession().getServletRequest().getParameterMap();
218: ////
219: //// url += "?";
220: //// for (String key : parameters.keySet()) {
221: //// url += key + "=" + parameters.get(key)[0] + "&";
222: //// }
223: //// url = url.substring(0, url.length() - 1);
224: ////
225: //// HttpMethod method = new GetMethod("http://" + WIKIHOST + TEMPLATEPATH + url);
226: //// HttpClient httpclient = new HttpClient();
227: //// String templateString = null;
228: ////
229: //// // Sets the "If-Modified-Since" header to the date in cache
230: //// if (obtainedMap.containsKey(url)) {
231: //// method.addRequestHeader("If-Modified-Since", httpdate.format(obtainedMap.get(url)));
232: //// }
233: ////
234: //// try {
235: //// // Execute http request
236: //// int httpstatus = httpclient.executeMethod(method);
237: ////
238: //// // Invoke handleUnknownResourceRequested
239: //// if (httpstatus != HttpStatus.SC_OK && httpstatus != HttpStatus.SC_NOT_MODIFIED)
240: //// return;
241: ////
242: //// // If the 'If-Modified-Since' header is sent, the server should set the status to
243: //// // SC_NOT_MODIFIED (304). Sometimes this does not work. In this case we try to compare the
244: //// // 'Last-Modified' response header with the date in cache ourselves.
245: //// boolean cached = true;
246: //// if (httpstatus == HttpStatus.SC_OK) {
247: //// try {
248: //// Date httplastmodified = httpdate.parse(method.getResponseHeader("Last-Modified").getValue());
249: //// if (!httplastmodified.before(obtainedMap.get(url))) cached = false;
250: //// }
251: //// catch (Exception ex) {
252: //// // Cannot parse the Last-Modified header or file is not in cache --> Don't use caching
253: //// cached = false;
254: //// }
255: //// }
256: ////
257: //// // Load the template from cache or use the response body as new template
258: //// if (cached) {
259: //// templateString = contentMap.get(url);
260: //// } else {
261: //// templateString = method.getResponseBodyAsString();
262: //// templateString = processDocument(templateString);
263: ////
264: //// // Add template to cache
265: //// contentMap.put(url, templateString);
266: //// obtainedMap.put(url, new Date());
267: //// }
268: ////
269: //// method.releaseConnection();
270: ////
271: //// templateLayout.setTemplate(new StringTemplateSource(templateString));
272: ////
273: //// }
274: //// catch (Exception ex) {
275: //// // Http get request failed or can't set template --> Invoke handleUnknownResourceRequested
276: //// ex.printStackTrace();
277: //// System.err.println(templateString);
278: //// }
279: //// }
280: //
281: //// private String processDocument(String templateString) {
282: //// String path = getPath();
283: ////
284: //// Source source = new Source(templateString);
285: ////
286: //// grabTitle(source);
287: //// grabLinks(source);
288: //// grabScripts(source);
289: ////
290: ////// Segment content = source.getElementById("body").getContent();
291: ////
292: //// Element body = source.findNextElement(0, "body");
293: //// if (body == null) {
294: //// return templateString;
295: //// }
296: //// Segment content = body.getContent();
297: //// source = new Source(content.toString());
298: ////
299: //// OutputDocument outputDocument = new OutputDocument(source);
300: ////
301: //// List<StartTag> anchorTags = source.findAllStartTags(Tag.A);
302: //// for (StartTag anchorTag : anchorTags) {
303: //// Attributes attributes = anchorTag.getAttributes();
304: //// Attribute attribute = attributes.get("href");
305: //// if (attribute != null) {
306: //// String value = attribute.getValue();
307: ////
308: //// String replacedValue = value.replaceFirst("http://" + WIKIHOST + TEMPLATEPATH, path);
309: //// if (!replacedValue.equals(value)) {
310: //// outputDocument.replace(attribute.getValueSegment(), replacedValue);
311: //// }
312: //// }
313: //// }
314: ////
315: //// List<StartTag> imgTags = source.findAllStartTags(Tag.IMG);
316: //// for (StartTag imgTag : imgTags) {
317: //// Attributes attributes = imgTag.getAttributes();
318: //// Attribute attribute = attributes.get("src");
319: //// if (attribute != null) {
320: //// String value = attribute.getValue();
321: ////
322: //// if (!value.startsWith("http")) {
323: //// value = "http://" + WIKIHOST + TEMPLATEPATH + "/" + value;
324: //// }
325: //// outputDocument.replace(attribute.getValueSegment(), value);
326: //// }
327: //// }
328: ////
329: //// templateString = outputDocument.toString();
330: //// return templateString;
331: //// }
332: //
333: // private void grabTitle(Source source) {
334: //// Element titleElement = source.findNextElement(0, "title");
335: ////
336: //// if (titleElement != null) {
337: //// String title = titleElement.getTextExtractor().toString();
338: //// frame.setTitle(title);
339: //// }
340: // }
341: //
342: // private void grabLinks(Source source) {
343: //// List<Link> newLinks = new ArrayList<Link>();
344: //// List<StartTag> linkTags = source.findAllStartTags("link");
345: //// for (StartTag linkTag : linkTags) {
346: //// Attributes attributes = linkTag.getAttributes();
347: //// String rel = attributes.getValue("rel");
348: //// String rev = attributes.getValue("rev");
349: //// String type = attributes.getValue("type");
350: //// String target = attributes.getValue("target");
351: //// String href = attributes.getValue("href");
352: //// if (!href.startsWith("http")) {
353: //// href = "http://" + WIKIHOST + TEMPLATEPATH + "/" + href;
354: //// }
355: ////
356: //// newLinks.add(new Link(rel, rev, type, target, new Url(href)));
357: //// }
358: //// if (!newLinks.equals(links)) {
359: //// System.out.println("links = " + links);
360: //// System.out.println("newLinks = " + newLinks);
361: ////
362: //// for (Link link : links)
363: //// frame.removeHeader(link);
364: //// for (Link link : newLinks)
365: //// frame.addHeader(link);
366: ////
367: //// links.clear();
368: //// links.addAll(newLinks);
369: //// }
370: // }
371: //
372: // private void grabScripts(Source source) {
373: //// List<Script> newScripts = new ArrayList<Script>();
374: //// List<StartTag> scriptTags = source.findAllStartTags("script");
375: //// for (StartTag scriptTag : scriptTags) {
376: //// Attributes attributes = scriptTag.getAttributes();
377: //// String type = attributes.getValue("type");
378: //// String src = attributes.getValue("src");
379: //// if (src != null)
380: //// newScripts.add(new Script(type, new Url(src)));
381: //// }
382: //// if (!newScripts.equals(scripts)) {
383: //// System.out.println("scripts = " + scripts);
384: //// System.out.println("newScripts = " + newScripts);
385: ////
386: //// for (Script script : scripts)
387: //// frame.removeHeader(script);
388: //// for (Script script : newScripts)
389: //// frame.addHeader(script);
390: ////
391: //// scripts.clear();
392: //// scripts.addAll(newScripts);
393: //// }
394: // }
395: //
396: //// public String getPath() {
397: //// if (path == null) {
398: //// //path = SessionManager.getSession().getServletRequest().getContextPath() + SessionManager.getSession().getServletRequest().getServletPath();
399: //// HttpServletRequest request = SessionManager.getSession().getServletRequest();
400: ////
401: //// String path = request.getRequestURL().toString();
402: //// String pathInfo = request.getPathInfo();
403: //// this.path = path.substring(0, path.lastIndexOf(pathInfo));
404: //// }
405: ////
406: //// return path;
407: //// }
408: //
409: //// private class Url
410: //// implements URLResource {
411: //// private SimpleURL url;
412: ////
413: //// public Url(String href) {
414: //// this.url = new SimpleURL(href);
415: //// }
416: ////
417: //// public SimpleURL getURL() {
418: //// return url;
419: //// }
420: //// }
421: // }
422: }
|