KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > generators > WikiGenerator


1 /*
2  * Created on Feb 8, 2006
3  */

4 package com.openedit.generators;
5
6 import java.io.IOException JavaDoc;
7 import java.io.Writer JavaDoc;
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 JavaDoc text = inPage.getContent();
23         String JavaDoc directory = PathUtilities.extractDirectoryPath(inPage.getPath() );
24         String JavaDoc converted = parseWiki( text , directory );
25         Writer JavaDoc inOutput = inOut.getWriter();
26         try
27         {
28             inOutput.write(converted);
29         } catch (IOException JavaDoc ex)
30         {
31             throw new OpenEditException(ex);
32         }
33     }
34
35     public String JavaDoc parseWiki(String JavaDoc text, String JavaDoc directory) throws OpenEditException
36     {
37         //first check for []
38
StringBuffer JavaDoc out = new StringBuffer JavaDoc(text.length() + 100);
39         StringBuffer JavaDoc link = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc();
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 JavaDoc makelink(String JavaDoc inString)
80     {
81         String JavaDoc lower = inString.toLowerCase();
82         {
83             if ( lower.startsWith("http") || lower.startsWith("mailto"))
84             return inString;
85         }
86         String JavaDoc shortname = inString.replaceAll(" ","_");
87         return shortname + ".html";
88     }
89
90     protected void markLinks(String JavaDoc[] words, Writer JavaDoc inOutput) throws IOException JavaDoc
91     {
92         for (int i = 0; i < words.length; i++)
93         {
94             String JavaDoc word = words[i];
95             
96             //break this into more parts based on html and other tags?
97
StringBuffer JavaDoc part = new StringBuffer JavaDoc();
98             for (int j = 0; j < word.length(); j++)
99             {
100                 //build up a word
101
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 JavaDoc();
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 JavaDoc inOutput, String JavaDoc part) throws IOException JavaDoc
125     {
126         if (isWiki(part))
127         {
128             
129             inOutput.write("<a HREF='");
130             
131             //trip some junk off the word
132

133             if ( part.indexOf("OpenEdit") > -1 ) //TODO: Read in ignore list
134
{
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     /**
153      * @param inWord
154      * @return
155      */

156     protected boolean isWiki(String JavaDoc 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                 //make sure there is another upper case in here
168
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