KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > wiki > impl > LinkNode


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.wiki.impl;
27
28 import net.killingar.EscapedString;
29
30 import java.net.URLEncoder JavaDoc;
31
32 public class LinkNode extends SimpleNode
33 {
34     public static final String JavaDoc NO_CONTEXT_SET = "error: no context set";
35     private String JavaDoc wiki = "", context = NO_CONTEXT_SET, anchor = "", defaultContext = null, defaultWiki = null;
36
37     public static final String JavaDoc CONTEXT_TYPE_WIKI = "wiki", CONTEXT_TYPE_URL = "url", CONTEXT_TYPE_LINK = "link";
38     private String JavaDoc contextType = CONTEXT_TYPE_WIKI;
39
40     private String JavaDoc action = "/wiki.view.action";
41
42     /**
43      * Constructor for LinkNode.
44      */

45     public LinkNode(
46         String JavaDoc text,
47         int lineNumber,
48         int startIndex,
49         int endIndex)
50     {
51         super(text, lineNumber, startIndex, endIndex);
52
53         EscapedString e = new EscapedString(text);
54
55         int foo = e.indexOf(';');
56         if (foo != -1)
57         {
58             context = EscapedString.strip(e.substring(0, foo));
59             e = new EscapedString(e.substring(foo+1));
60         }
61
62         foo = e.indexOf('#');
63         if (foo != -1)
64         {
65             anchor = EscapedString.strip(e.substring(foo+1));
66             e = new EscapedString(e.substring(0, foo));
67         }
68
69         wiki = EscapedString.strip(e.toString());
70
71         setContext(context);
72     }
73
74     public void setContext(String JavaDoc in)
75     {
76         context = in;
77
78         if (context.indexOf("://") != -1 || context.regionMatches(0, "mailto:", 0, 7))
79             contextType = CONTEXT_TYPE_URL;
80         else if (context.regionMatches(0, "link:", 0, 5))
81             contextType = CONTEXT_TYPE_LINK;
82         else
83             contextType = CONTEXT_TYPE_WIKI;
84     }
85     public void setWiki(String JavaDoc in) {wiki = in;}
86     public void setDefaultContext(String JavaDoc in) {defaultContext = in;}
87     public void setDefaultWiki(String JavaDoc in) {defaultWiki = in;}
88     public void setAnchor(String JavaDoc in) {anchor = in;}
89     public void setAction(String JavaDoc in) {action = in;}
90
91     public String JavaDoc getWiki() { return wiki; }
92     public String JavaDoc getContext() { return context; }
93     public String JavaDoc getDefaultWiki() { return defaultWiki; }
94     public String JavaDoc getDefaultContext() { return defaultContext; }
95     public String JavaDoc getAnchor() { return anchor; }
96     public String JavaDoc getAction() { return action; }
97     public String JavaDoc getContextType() { return contextType;}
98
99     public String JavaDoc getText()
100     {
101         StringBuffer JavaDoc s = new StringBuffer JavaDoc("<a HREF=\"");
102
103         if (contextType == CONTEXT_TYPE_LINK)
104         {
105             s.append(context.substring(5));
106             if (anchor.length() != 0)
107             {
108                 s.append("#");
109                 s.append(anchor);
110             }
111
112             //s.append("\" target=\"_new\">");
113
s.append("\">");
114             s.append(wiki);
115             s.append("</a>");
116         }
117         else if (contextType == CONTEXT_TYPE_URL)
118         {
119             s.append(context);
120             if (anchor.length() != 0)
121             {
122                 s.append("#");
123                 s.append(anchor);
124             }
125
126             //s.append("\" target=\"_new\">");
127
s.append("\">");
128             s.append(wiki);
129             s.append("</a>");
130         }
131         else
132         {
133             /*
134             if (wiki.length() != 0)
135                 s.append(action+"?");
136
137             boolean printedPrev = false;
138             if (context != null && !context.equals(defaultContext))
139             {
140                 s.append("context=");
141                 s.append(URLEncoder.encode(context));
142                 printedPrev = true;
143             }
144
145             if (wiki.length() != 0 && !wiki.equals(defaultWiki))
146             {
147                 if (printedPrev)s.append("&amp;");
148                 s.append("wiki=");
149                 s.append(URLEncoder.encode(wiki));
150                 printedPrev = true;
151             }
152
153             if (anchor.length() != 0)
154             {
155                 s.append("#");
156                 s.append(URLEncoder.encode(anchor));
157
158                 if (wiki.length() == 0 && context.equals(NO_CONTEXT_SET))
159                 {
160                     s.append("\">");
161                     s.append(anchor);
162                     s.append("</a>");
163
164                     return s.toString();
165                 }
166             }
167
168             s.append("\">");
169             s.append(wiki);
170             s.append("</a>");
171             */

172             boolean printedPrev = false;
173             if (context != null && !context.equals(defaultContext))
174             {
175                 s.append("/wiki/");
176                 s.append(URLEncoder.encode(context));
177                 s.append("/");
178                 printedPrev = true;
179             }
180             else
181             {
182                 s.append("/documents/");
183             }
184
185             if (wiki.length() != 0 && !wiki.equals(defaultWiki))
186             {
187                 //if (printedPrev)s.append("&amp;");
188
s.append(URLEncoder.encode(wiki));
189                 printedPrev = true;
190             }
191
192             if (anchor.length() != 0)
193             {
194                 s.append("#");
195                 s.append(URLEncoder.encode(anchor));
196
197                 if (wiki.length() == 0 && context.equals(NO_CONTEXT_SET))
198                 {
199                     s.append("\">");
200                     s.append(anchor);
201                     s.append("</a>");
202
203                     return s.toString();
204                 }
205             }
206
207             s.append("\">");
208             s.append(wiki);
209             s.append("</a>");
210         }
211         return s.toString();
212     }
213
214     public String JavaDoc getEditString()
215     {
216         StringBuffer JavaDoc s = new StringBuffer JavaDoc("<a HREF=\"");
217
218         if (contextType != CONTEXT_TYPE_WIKI)
219         {
220             s.append(context);
221             if (anchor.length() != 0)
222             {
223                 s.append("#");
224                 s.append(anchor);
225             }
226
227             s.append("\">");
228             s.append(wiki);
229             s.append("</a>");
230         }
231         else
232         {
233             if (wiki.length() != 0)
234                 s.append("/wiki.edit.action?");
235
236             boolean printedPrev = false;
237             if (context != null && !context.equals(defaultContext))
238             {
239                 s.append("context=");
240                 s.append(URLEncoder.encode(context));
241                 printedPrev = true;
242             }
243
244             if (wiki.length() != 0 && !wiki.equals(defaultWiki))
245             {
246                 if (printedPrev)s.append("&amp;");
247                 s.append("wiki=");
248                 s.append(URLEncoder.encode(wiki));
249                 printedPrev = true;
250             }
251
252             if (anchor.length() != 0)
253             {
254                 s.append("#");
255                 s.append(URLEncoder.encode(anchor));
256
257                 if (wiki.length() == 0 && context.equals(NO_CONTEXT_SET))
258                 {
259                     s.append("\">");
260                     s.append(anchor);
261                     s.append("</a>(create)");
262
263                     return s.toString();
264                 }
265             }
266
267             s.append("\">");
268             s.append(wiki);
269             s.append("</a>(create)");
270         }
271         return s.toString();
272     }
273
274     public static void main(String JavaDoc[] args)
275     {
276         LinkNode n = new LinkNode("#anchor", 0, 0, 0);
277         System.out.println(n.getContext() + " | " + n.getWiki() + " | " + n.getAnchor());
278     }
279 }
280
Popular Tags