1+ package javaxt .portal ;
2+
3+ //******************************************************************************
4+ //** Portal Content
5+ //******************************************************************************
6+ /**
7+ * Used to dynamically create HTML content from text files.
8+ *
9+ ******************************************************************************/
10+
11+ public class Content {
12+
13+ private javaxt .io .File file ;
14+ private javaxt .io .Directory share ;
15+ private javax .servlet .http .HttpServletRequest request ;
16+ private boolean isIndex = false ;
17+ private String title ;
18+ private String description ;
19+ private String keywords ;
20+ private String content = "" ;
21+ private String Path ;
22+
23+ //**************************************************************************
24+ //** Constructor
25+ //**************************************************************************
26+ /** Creates a new instance of this class.
27+ * @param share Path to the javaxt share directory
28+ * @param request HttpServletRequest
29+ */
30+ public Content (javax .servlet .http .HttpServletRequest request , javaxt .io .Directory share ){
31+
32+
33+ this .request = request ;
34+ this .share = share ;
35+
36+ //Set Path variable
37+ Path = Utils .getPath (request );
38+
39+
40+
41+ //Get path to wiki file
42+ String path = share + "wiki/" ;
43+ if (request .getServletPath ().startsWith ("/wiki/" )){
44+ if (request .getQueryString ()==null ){
45+ path += "index.txt" ;
46+ isIndex = true ;
47+ }
48+ else {
49+ java .util .Enumeration params = request .getParameterNames ();
50+ String relPath = (String ) params .nextElement ();
51+ if (relPath .startsWith ("/" ) || relPath .startsWith ("\\ " )){
52+ relPath = relPath .substring (1 );
53+ }
54+ if (relPath .endsWith ("/" ) || relPath .endsWith ("\\ " )){
55+ relPath = relPath .substring (0 , relPath .length ()-1 );
56+ }
57+ if (!relPath .endsWith (".txt" )) relPath +=".txt" ;
58+ path += relPath ;
59+ }
60+ }
61+ else {
62+
63+ //String relPath = request.getServletPath().substring(1);
64+ String relPath = request .getServletPath ();
65+ if (relPath .length ()>1 ) relPath = relPath .substring (1 );
66+ else {
67+
68+ //Special case where the entire site is hosted from a servlet with a pattern of "/*"
69+ String service = new javaxt .utils .URL (request .getRequestURL ().toString ()).getPath ();
70+ if (service .length ()<Path .length ()) service = Path ;
71+ service = service .substring (service .indexOf (Path )).substring (Path .length ());
72+ if (service .length ()>1 && service .startsWith ("/" )) service = service .substring (1 );
73+ if (service .length ()>1 && service .endsWith ("/" )) service = service .substring (0 , service .length ()-1 );
74+
75+
76+ //Special case when app is deployed as root
77+ String contextPath = request .getContextPath ();
78+ if (contextPath .length ()>1 ){
79+ contextPath = contextPath .substring (1 );
80+ if (Path .equals ("/" ) && service .startsWith (contextPath )){
81+ if (service .contains ("/" )){
82+ service = service .substring (service .indexOf ("/" ));
83+ if (service .startsWith ("/" )){
84+ if (service .length ()>1 ) service = service .substring (1 );
85+ else service = "" ;
86+ }
87+ }
88+ else service = "" ;
89+ }
90+ }
91+
92+ relPath = service ;
93+
94+ }
95+
96+ //System.out.println("relPath: " + relPath);
97+ if (relPath .toLowerCase ().endsWith ("index.jsp" )){
98+
99+ if (relPath .equalsIgnoreCase ("index.jsp" )){
100+ path +="home.txt" ;
101+ }
102+ else {
103+ relPath = relPath .substring (0 , relPath .length ()-10 );
104+ path +=relPath + ".txt" ;
105+ }
106+ }
107+ else {
108+
109+ //Special case for servlets where there is no index.jsp in the file path
110+ if (relPath .equals ("" ) || relPath .equals ("/" )){
111+ path +="home.txt" ;
112+ }
113+ else {
114+ path +=relPath + ".txt" ;
115+ //path=null;
116+ }
117+ }
118+ }
119+
120+ //System.out.println("path: " + path);
121+
122+ //Parse file and extract content
123+ if (path !=null ) {
124+ file = new javaxt .io .File (path );
125+ if (file .exists ()){
126+
127+ content = file .getText ().replace ("<%=Path%>" , Path );
128+ javaxt .html .Parser html = new javaxt .html .Parser (content );
129+
130+ //Extract Title
131+ try {
132+ javaxt .html .Parser .Element title = html .getElementByTagName ("title" );
133+ content = content .replace (title .outerHTML , "" );
134+ this .title = title .innerHTML ;
135+ }
136+ catch (Exception e ){}
137+
138+ if (title ==null ){
139+ try {
140+ this .title = html .getElementByTagName ("h1" ).innerHTML ;
141+ }
142+ catch (Exception e ){}
143+ }
144+
145+ //Extract Description
146+ try {
147+ javaxt .html .Parser .Element description = html .getElementByTagName ("description" );
148+ content = content .replace (description .outerHTML , "" );
149+ this .description = description .innerHTML ;
150+ }
151+ catch (Exception e ){}
152+
153+
154+ //Extract Keywords
155+ try {
156+ javaxt .html .Parser .Element keywords = html .getElementByTagName ("keywords" );
157+ content = content .replace (keywords .outerHTML , "" );
158+ this .keywords = keywords .innerHTML ;
159+ }
160+ catch (Exception e ){}
161+
162+ }
163+
164+ if (file .getName ().equalsIgnoreCase ("img.txt" )){
165+ System .out .println ("path: " + path );
166+ System .out .println ("query: " + request .getQueryString ());
167+ return ;
168+ }
169+ }
170+ else {
171+ return ;
172+ }
173+
174+
175+ //Create/Update wiki content
176+ /*
177+ try{
178+ String newContent = request.getParameterValues("content")[0];
179+ if (newContent!=null){
180+
181+ javaxt.utils.URL currURL = new javaxt.utils.URL(request.getRequestURL().toString());
182+ javaxt.utils.URL prevURL = new javaxt.utils.URL(request.getHeader("Referer"));
183+
184+ if (currURL.getHost().equalsIgnoreCase(prevURL.getHost())){
185+ if (prevURL.getPath().endsWith("edit.jsp")){
186+ javaxt.portal.User user = (javaxt.portal.User) request.getSession().getAttribute("PortalUser");
187+ if (user!=null){
188+ write(newContent);
189+ }
190+ }
191+ }
192+ }
193+ }
194+ catch(Exception e){
195+ }
196+ */
197+ }
198+
199+ public void write (String text ){
200+ if (file !=null ) file .write (text );
201+ }
202+
203+
204+ //**************************************************************************
205+ //** getFile
206+ //**************************************************************************
207+ /** Used to return the physical path to the file used to store this content.
208+ */
209+ public javaxt .io .File getFile (){
210+ return file ;
211+ }
212+
213+
214+
215+
216+ public String getTitle (){
217+ return title ;
218+ }
219+
220+ public String getDescription (){
221+ return description ;
222+ }
223+ public String getKeywords (){
224+ return keywords ;
225+ }
226+
227+ //**************************************************************************
228+ //** toString
229+ //**************************************************************************
230+ /** Used to return html content for this wiki file/entry.
231+ */
232+ public String toString (){
233+
234+
235+ //Get User
236+ User user = (User ) request .getSession ().getAttribute ("PortalUser" );
237+
238+
239+ //If this is the wiki index, generate a table of contents
240+ StringBuffer toc = null ;
241+ if (isIndex ){
242+
243+ javaxt .io .Directory dir = new javaxt .io .Directory (share + "wiki/" );
244+ javaxt .io .File [] files = dir .getFiles ("*.txt" , true );
245+ java .util .HashSet <String > dirs = new java .util .HashSet <String >();
246+
247+ toc = new StringBuffer ();
248+ toc .append ("<ul>" );
249+ for (int i =0 ; i <files .length ; i ++){
250+ if (!files [i ].equals (file )){
251+ String relPath = files [i ].getPath ().replace (dir .toString (),"" ) + files [i ].getName (false );
252+ relPath = relPath .replace ("\\ " ,"/" );
253+ if (relPath .contains ("/" )){
254+
255+ String currDir = relPath .substring (0 , relPath .lastIndexOf ("/" ));
256+ if (!dirs .contains (currDir )){
257+
258+ if (!dirs .isEmpty ()) toc .append ("</ul>" );
259+ toc .append ("<li>" + currDir +"</li>" );
260+ toc .append ("<ul>" );
261+
262+ dirs .add (currDir );
263+ }
264+
265+
266+ toc .append ("<li><a href=\" " + Path + "wiki/?/" + relPath + "\" >" + relPath +"</a></li>" );
267+ }
268+ }
269+ }
270+ toc .append ("</ul>" );
271+ }
272+
273+
274+ //Set Default Content
275+ if (file ==null || !file .exists ()){
276+ if (user ==null ){
277+ content = "<h1>Page Not Found</h1>" ;
278+ }
279+ }
280+
281+
282+ //Parse Content
283+ javaxt .html .Parser html = new javaxt .html .Parser (content );
284+ javaxt .html .Parser .Element h1 = html .getElementByTagName ("h1" );
285+ if (h1 !=null ) content = content .replace (h1 .outerHTML , "" );
286+
287+
288+ StringBuffer text = new StringBuffer ();
289+ text .append ("<div class=\" content\" >" );
290+ text .append ("<table border=\" 0\" cellpadding=\" 0\" cellspacing=\" 0\" style=\" background-color:#FFFFFF;border-collapse:collapse;margin-left:0px;margin-right: 0px;\" bordercolor=\" #111111\" width=\" 100%\" align=\" center\" height=\" 100%\" >" );
291+
292+ text .append ("<tr>" );
293+ text .append ("<td valign=\" top\" >" );
294+
295+ if (h1 !=null ) text .append (h1 .outerHTML );
296+
297+ /*
298+ text.append(" <div class=\"sidebar\">");
299+
300+ javaxt.html.Parser.Element[] h2 = html.getElementsByTagName("h2");
301+ javaxt.html.Parser.Element[] link = html.getElementsByTagName("a");
302+ if (h2.length>0){
303+ text.append(" <div class=\"block block-one\">");
304+ text.append(" <div class=\"wrap1\">");
305+ text.append(" <div class=\"wrap2\">");
306+ text.append(" <div class=\"wrap3\">");
307+ text.append(" <div class=\"title\"><h3>Quick Nav</h3></div>");
308+ text.append(" <div class=\"inner\">");
309+ text.append(" <ul class=\"sidebar-list\">");
310+ for (int i=0; i<h2.length; i++){
311+ text.append(" <li><a href=\"#\">" + h2[i].innerHTML + "</a></li>");
312+ }
313+ text.append(" </ul>");
314+ text.append(" </div>");
315+ text.append(" </div>");
316+ text.append(" </div>");
317+ text.append(" </div>");
318+ text.append(" </div>");
319+ }
320+ */
321+
322+
323+ /*
324+ if (link.length>0){
325+ text.append(" <div class=\"block block-one\">");
326+ text.append(" <div class=\"wrap1\">");
327+ text.append(" <div class=\"wrap2\">");
328+ text.append(" <div class=\"wrap3\">");
329+ text.append(" <div class=\"title\"><h3>Related Links</h3></div>");
330+ text.append(" <div class=\"inner\">");
331+ text.append(" <ul class=\"sidebar-list\">");
332+ for (int i=0; i<link.length; i++){
333+ text.append(" <li><a href=\"#\">" + link[i].innerHTML + "</a></li>");
334+ }
335+ text.append(" </ul>");
336+ text.append(" </div>");
337+ text.append(" </div>");
338+ text.append(" </div>");
339+ text.append(" </div>");
340+ text.append(" </div>");
341+ }
342+
343+
344+ text.append(" </div>");
345+ */
346+
347+ text .append (content );
348+
349+
350+
351+ if (toc !=null ){
352+ text .append ("<div id=\" wiki-index\" >" + toc .toString () + "</div>" );
353+ }
354+
355+ text .append ("</td>" );
356+ text .append ("</tr>" );
357+
358+
359+
360+ if (user !=null ){
361+
362+ String relPath = this .getFile ().toString ().substring (share .toString ().length ());
363+ relPath = relPath .replace ("\\ " , "/" );
364+
365+ text .append ("<tr>" );
366+ text .append ("<td style=\" height:1px;\" >" );
367+ text .append ("<table align=\" right\" >" );
368+ text .append ("<tr>" );
369+ text .append ("<td class=\" smgrytxt\" ><a href=\" " + Path + "wiki/edit.jsp?file=" + relPath + "\" >Edit Page</a></td>" );
370+ text .append ("</tr>" );
371+ text .append ("</table>" );
372+ text .append ("</td>" );
373+ text .append ("</tr>" );
374+ }
375+
376+ text .append ("</table>" );
377+ text .append ("</div>" );
378+
379+
380+ return text .toString ();
381+ }
382+
383+
384+
385+ }
0 commit comments