KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > jsp > DublinCoreTag


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.jsp;
26
27 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
28 import org.radeox.util.logging.Logger;
29 import org.radeox.util.Encoder;
30 import org.snipsnap.semanticweb.DublinCore;
31 import org.snipsnap.snip.Snip;
32
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 public class DublinCoreTag extends TagSupport JavaDoc {
41   Snip snip = null;
42   String JavaDoc format = null;
43
44   private String JavaDoc capitalize(String JavaDoc s) {
45     char chars[] = s.toCharArray();
46     chars[0] = Character.toUpperCase(chars[0]);
47     return new String JavaDoc(chars);
48   }
49
50   public int doStartTag() throws JspException JavaDoc {
51     if (null != snip) {
52       Map JavaDoc dublinCore = DublinCore.generate(snip);
53       try {
54         JspWriter JavaDoc out = pageContext.getOut();
55
56         if ("xml".equals(format)) {
57           Iterator JavaDoc iterator = dublinCore.keySet().iterator();
58           while (iterator.hasNext()) {
59             String JavaDoc name = (String JavaDoc) iterator.next();
60             String JavaDoc value = (String JavaDoc) dublinCore.get(name);
61             out.print("<dc:");
62             out.print(name.toLowerCase());
63             out.print(">");
64             out.print(Encoder.escape(value));
65             out.print("</dc:");
66             out.print(name);
67             out.println(">");
68           }
69         } else {
70           out.println("<link rel=\"schema.DC\" HREF=\"http://purl.org/DC/elements/1.1/\"/>");
71           Iterator JavaDoc iterator = dublinCore.keySet().iterator();
72           while (iterator.hasNext()) {
73             String JavaDoc name = (String JavaDoc) iterator.next();
74             String JavaDoc value = (String JavaDoc) dublinCore.get(name);
75             out.print("<meta name=\"DC.");
76             out.print(capitalize(name));
77             out.print("\" content=\"");
78             out.print(Encoder.escape(value));
79             out.println("\"/>");
80           }
81         }
82       } catch (IOException JavaDoc e) {
83         Logger.warn("doStartTag in DublinCore", e);
84       }
85     }
86     return super.doStartTag();
87   }
88
89
90   public void setFormat(String JavaDoc format) {
91     this.format = format;
92   }
93
94   public void setSnip(String JavaDoc snip) {
95     try {
96       this.snip = (Snip) ExpressionEvaluatorManager.evaluate("snip", snip, Snip.class, this, pageContext);
97     } catch (JspException JavaDoc e) {
98       Logger.warn("unable to evaluate expression", e);
99     }
100   }
101 }
102
Popular Tags