[opencms-dev] need a little Java help (out.println())

Christoph P. Kukulies kuku at physik.rwth-aachen.de
Wed Sep 24 12:41:44 CEST 2008


Hi Tim,

yes, it is your code example I grabbed from the net.
(I referred to that fact already in an earlier email :-)
Thanks for the explanation. I'll try what you suggested (passing the
out through the method).
--
Chris Christoph P. U. Kukulies kukulies (at) rwth-aachen.de

On Fri, Sep 19, 2008 at 08:51:30AM -0400, Tim Howland wrote:
> Hey Christoph-
>
> I think that looks like my old sitemap code; anyway, the issue is that  
> the method declaration is in the <%! block, so it doesn't run in the  
> same method as the rest of the java code. Normally, JSP's get compiled  
> into a single method, with things like a params Map and a responswriter  
> (called out) available. Since I needed to define a recursive function, I  
> had to define this code outside of that method.
>
> You may be able to get away with it by changing the method signature for  
> the recurseTree method to:
>
> private String recurseTree(CmsObject cmso,CmsJspActionElement jsp, String path, ResponseWriter out) {
>
> }
>
> and passing the responsewriter to it from the body of the calling JSP.
>
> Hope this helps,
>
> TIm
>
>
>
> Christoph P. Kukulies wrote:
>> I'm using this piece of code that I grabbed from the net with minor
>> modifications to generate a google sitemap file.
>> I would like to modify this code to print out a recursive directory
>> directory listing of the site with all folders and files. (kind of
>> ls -lR).
>> So all I would need is to put an out.println() into the most inner
>> code section where the <loc>url</loc> is written (appended to the
>> string). Appending to the string a costly anyway when doing recursion
>> that's why this piece of code runs quite a while :).
>>
>> Anyway, whan I put an out.println() into the private function
>> recurseTree(), out is an unknown variable. 
>>
>> So I'm a) wondering what magic makes it known in the outer scope when I 
>> use it
>>    there
>> b) asking for help, what I have to do to make it known. I tried
>>    System.out.println() but I see no output in the browser window.
>>    (it goes into the logs/stdout_20080918.log file)
>>
>> Here is the code again:
>>
>> <%@ page session="false" %>
>> <%@ page
>> import="java.util.*,org.opencms.jsp.*,org.opencms.file.*,java.text.DateFormat,
>> java.text.SimpleDateFormat,org.opencms.main.*" %>
>> <%@ page import="org.opencms.lock.CmsLockType" %>
>> <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
>> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
>> <%!
>> protected String BASE_URL="";
>> public static SimpleDateFormat ISO8601FORMAT = new
>> SimpleDateFormat("yyyy-MM-dd");
>> private String recurseTree(CmsObject cmso,CmsJspActionElement jsp,
>> String path)  {
>> 	StringBuffer sb = new StringBuffer();
>>         	try {
>> 		ArrayList files = (ArrayList)
>> cmso.getFilesInFolder(path);
>> 		Iterator i = files.iterator();
>> 		while (i.hasNext()) {
>> 			CmsFile f = (CmsFile) i.next();	
>> 			String thispath = jsp.link(cmso.getSitePath(f));
>> 			CmsProperty secret =
>> cmso.readPropertyObject(f,"sitemap_hidden",true);
>> 			CmsProperty changeFreqProperty =
>> cmso.readPropertyObject(f,"sitemap_change_frequency",true);
>> 			CmsProperty priorityProperty =
>> cmso.readPropertyObject(f,"sitemap_priority",true);
>> 			String changeFrequency =
>> changeFreqProperty.getValue("weekly");
>> 			String priority =
>> priorityProperty.getValue("1");
>> 			if ((secret.getValue("false") == "false") 				&&
>> (thispath.endsWith("html")||thispath.endsWith("jsp")||thispath.endsWith("pdf")||thispath.endsWith("htm")))
>> {		
>> 				sb.append("<url>\n");			
>> 				sb.append("<loc>"+BASE_URL+thispath+"</loc>\n");
>> 				//DateFormat df =
>> 				//DateFormat.getDateInstance();
>> 				String niceDate =
>> ISO8601FORMAT.format(new Date(f.getDateLastModified()));
>> 				sb.append("<lastmod>"+niceDate+"</lastmod>\n");
>> 				sb.append("<changefreq>"+changeFrequency+"</changefreq>\n");
>> 				sb.append("<priority>"+priority+"</priority>\n");
>> 				sb.append("</url>\n");
>> 			}
>> 		}
>> 		ArrayList folders = (ArrayList)
>> cmso.getSubFolders(path);
>> 		Iterator j = folders.iterator();	
>> 		while (j.hasNext()) {
>> 			CmsFolder f = (CmsFolder) j.next();
>> 			sb.append( recurseTree(cmso,jsp,
>> cmso.getSitePath(f) ) );
>> 		}
>> 	}
>> 	catch (CmsException cmsException) {
>> 		sb.append("A CMS exception occurred:
>> "+cmsException.toString());
>> 	
>> 	}
>> 	return sb.toString();
>>
>> }
>> %>
>> <%
>>
>>  CmsJspActionElement cms = new
>> org.opencms.jsp.CmsJspActionElement(pageContext, request, response);
>>  CmsObject cmso = cms.getCmsObject();
>>  String filename="sitemap.xml";
>>  String s_filecontent;
>>  cmso.getRequestContext().setCurrentProject(cmso.readProject("Offline"));
>> if (cmso.existsResource(filename)) {
>>        cmso.lockResource(filename,CmsLock.TEMPORARY);
>>         cmso.deleteResource(filename,
>> CmsResource.DELETE_PRESERVE_SIBLINGS);
>>         if (cmso.existsResource(filename)) {
>>                 cmso.unlockResource(filename);
>>                 cmso.publishResource(filename);
>>         }
>> }
>>
>>
>> String url = cms.info("opencms.url");
>> int lastSlash = url.indexOf("/",8);
>> BASE_URL = url.substring(0,lastSlash);
>> s_filecontent=recurseTree(cmso,cms, "/");
>> s_filecontent=s_filecontent+"</urlset>\n";
>> cmso.createResource(
>>         filename,
>>         OpenCms.getResourceManager().getResourceType("plain").getTypeId(),
>>         s_filecontent.getBytes(),
>>         new ArrayList()
>> );
>> cmso.unlockResource(filename);
>> cmso.publishResource(filename);
>> %> 
>>
>> Thanks for helping,
>>
>>
>> --
>> Chris Christoph P. U. Kukulies kukulies (at) rwth-aachen.de
>>
>>   
>
>
> -- 
> Tim Howland
> th at wdogsystems.com
> http://wdogsystems.com
>



More information about the opencms-dev mailing list