1 4 package com.openedit.generators; 5 6 import java.io.IOException ; 7 import java.io.Writer ; 8 9 import com.openedit.OpenEditException; 10 import com.openedit.WebPageRequest; 11 import com.openedit.page.Page; 12 import com.openedit.page.manage.PageManager; 13 import com.openedit.util.PathUtilities; 14 15 public class WikiGenerator extends BaseGenerator 16 { 17 PageManager fieldPageManager; 18 19 20 public void generate(WebPageRequest inContext, Page inPage, Output inOut) throws OpenEditException 21 { 22 String text = inPage.getContent(); 23 String directory = PathUtilities.extractDirectoryPath(inPage.getPath() ); 24 String converted = parseWiki( text , directory ); 25 Writer inOutput = inOut.getWriter(); 26 try 27 { 28 inOutput.write(converted); 29 } catch (IOException ex) 30 { 31 throw new OpenEditException(ex); 32 } 33 } 34 35 public String parseWiki(String text, String directory) throws OpenEditException 36 { 37 StringBuffer out = new StringBuffer (text.length() + 100); 39 StringBuffer link = new StringBuffer (); 40 boolean inlink = false; 41 for (int i = 0; i < text.length(); i++) 42 { 43 char c = text.charAt(i); 44 if ( c == '[') 45 { 46 inlink = true; 47 out.append("<a HREF=\""); 48 } 49 else if( c == ']' ) 50 { 51 inlink = false; 52 String path = makelink(link.toString() ); 53 out.append(path); 54 out.append("\">"); 55 out.append(link); 56 if( !path.startsWith("http")) 57 { 58 Page page = getPageManager().getPage(directory + "/" + path); 59 if ( !page.exists() ) 60 { 61 out.append("*"); 62 } 63 } 64 out.append("</a>"); 65 link = new StringBuffer (); 66 } 67 else if( inlink ) 68 { 69 link.append(c); 70 } 71 else 72 { 73 out.append(c); 74 } 75 } 76 return out.toString(); 77 } 78 79 private String makelink(String inString) 80 { 81 String lower = inString.toLowerCase(); 82 { 83 if ( lower.startsWith("http") || lower.startsWith("mailto")) 84 return inString; 85 } 86 String shortname = inString.replaceAll(" ","_"); 87 return shortname + ".html"; 88 } 89 90 protected void markLinks(String [] words, Writer inOutput) throws IOException 91 { 92 for (int i = 0; i < words.length; i++) 93 { 94 String word = words[i]; 95 96 StringBuffer part = new StringBuffer (); 98 for (int j = 0; j < word.length(); j++) 99 { 100 if ( Character.isLetter( word.charAt(j) ) ) 102 { 103 part.append(word.charAt(j)); 104 } 105 else 106 { 107 if ( part.length() > 0 ) 108 { 109 dumpWord(inOutput, part.toString()); 110 part = new StringBuffer (); 111 } 112 inOutput.append(word.charAt(j)); 113 } 114 } 115 if ( part.length() > 0 ) 116 { 117 dumpWord(inOutput, part.toString()); 118 } 119 120 inOutput.write(' '); 121 } 122 } 123 124 private void dumpWord(Writer inOutput, String part) throws IOException 125 { 126 if (isWiki(part)) 127 { 128 129 inOutput.write("<a HREF='"); 130 131 133 if ( part.indexOf("OpenEdit") > -1 ) { 135 inOutput.write("http://www.openedit.org/'>"); 136 } 137 else 138 { 139 inOutput.write(part); 140 inOutput.write(".html'>"); 141 } 142 inOutput.write(part); 143 inOutput.write("</a>"); 144 145 } 146 else 147 { 148 inOutput.write(part); 149 } 150 } 151 152 156 protected boolean isWiki(String inWord) 157 { 158 159 if (inWord != null && inWord.length() > 4) 160 { 161 int start = 0; 162 int type = Character.getType(inWord.charAt(start)); 163 int secondtype = Character.getType(inWord.charAt(start+1)); 164 165 if (type == Character.UPPERCASE_LETTER && secondtype == Character.LOWERCASE_LETTER) 166 { 167 for (int i = start + 2; i < inWord.length(); i++) 169 { 170 int ctype = Character.getType(inWord.charAt(i)); 171 if (ctype == Character.UPPERCASE_LETTER) 172 { 173 return true; 174 } 175 } 176 } 177 } 178 return false; 179 } 180 181 public PageManager getPageManager() 182 { 183 return fieldPageManager; 184 } 185 186 public void setPageManager(PageManager inPageManager) 187 { 188 fieldPageManager = inPageManager; 189 } 190 191 } 192 | Popular Tags |