1 25 26 package net.killingar.wiki.impl; 27 28 import net.killingar.EscapedString; 29 30 import java.net.URLEncoder ; 31 32 public class LinkNode extends SimpleNode 33 { 34 public static final String NO_CONTEXT_SET = "error: no context set"; 35 private String wiki = "", context = NO_CONTEXT_SET, anchor = "", defaultContext = null, defaultWiki = null; 36 37 public static final String CONTEXT_TYPE_WIKI = "wiki", CONTEXT_TYPE_URL = "url", CONTEXT_TYPE_LINK = "link"; 38 private String contextType = CONTEXT_TYPE_WIKI; 39 40 private String action = "/wiki.view.action"; 41 42 45 public LinkNode( 46 String 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 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 in) {wiki = in;} 86 public void setDefaultContext(String in) {defaultContext = in;} 87 public void setDefaultWiki(String in) {defaultWiki = in;} 88 public void setAnchor(String in) {anchor = in;} 89 public void setAction(String in) {action = in;} 90 91 public String getWiki() { return wiki; } 92 public String getContext() { return context; } 93 public String getDefaultWiki() { return defaultWiki; } 94 public String getDefaultContext() { return defaultContext; } 95 public String getAnchor() { return anchor; } 96 public String getAction() { return action; } 97 public String getContextType() { return contextType;} 98 99 public String getText() 100 { 101 StringBuffer s = new StringBuffer ("<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("\">"); 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("\">"); 128 s.append(wiki); 129 s.append("</a>"); 130 } 131 else 132 { 133 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 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 getEditString() 215 { 216 StringBuffer s = new StringBuffer ("<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("&"); 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 [] 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 |