Small demo.... had some spare time ;-)
Request:
http://localhost:8888/cocoonDemo/testfilemeta/d:/tmp/index.html
Sitemap snippets:
-----------------------------------------
<map:match pattern="testfilemeta/**">
<map:generate src="{1}" type="filemeta">
<map:parameter value="dd/MM/yyyy" name="dateformat"/>
</map:generate>
<map:serialize type="xml"/>
</map:match>
<map:components>
<map:generators>
<map:generator label="image" name="filemeta"
src="com.ciber.cocoonDemo.FileMetaGenerator" />
</map:generators>
....
</map:components>
Output:
-----------------------------------------
<file:file>
<file:name>index.html</file:name>
<file:isDirectory>false</file:isDirectory>
<file:isFile>true</file:isFile>
<file:path>d:\tmp\index.html</file:path>
<file:lastModified>16/12/2008</file:lastModified>
</file:file>
FileMetaGenerator:
-----------------------------------------
package com.ciber.cocoonDemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.generation.AbstractGenerator;
import org.apache.cocoon.xml.AttributesImpl;
import org.xml.sax.SAXException;
public class FileMetaGenerator extends AbstractGenerator {
protected static final String URI =
"http://apache.org/cocoon/file/2.0";
/** The namespace prefix for this namespace. */
protected static final String PREFIX = "file";
/* Node names */
protected static final String FILE_NODE = "file";
protected static final String FILENAME_NODE = "name";
protected static final String LASTMODIFIED_NODE = "lastModified";
protected static final String ISDIRECTORY_NODE = "isDirectory";
protected static final String ISFILE_NODE = "isFile";
protected static final String PATH_NODE = "path";
private String name;
private String isDirectory;
private String isFile;
private String path;
private String lastModified;
private SimpleDateFormat df;
public void generate() throws IOException, SAXException,
ProcessingException {
final AttributesImpl attr = new AttributesImpl();
this.contentHandler.startDocument();
this.contentHandler.startPrefixMapping(PREFIX, URI);
this.contentHandler.startElement(URI, FILE_NODE, PREFIX + ":" +
FILE_NODE, attr);
//add Name node
this.contentHandler.startElement(URI, FILENAME_NODE, PREFIX +
":" + FILENAME_NODE, attr);
this.contentHandler.characters(name.toCharArray(),0,
name.length());
this.contentHandler.endElement(URI, FILENAME_NODE, PREFIX + ":"
+ FILENAME_NODE);
//add isDirectory node
this.contentHandler.startElement(URI, ISDIRECTORY_NODE, PREFIX +
":" + ISDIRECTORY_NODE, attr);
this.contentHandler.characters(isDirectory.toCharArray(),0,
isDirectory.length());
this.contentHandler.endElement(URI, ISDIRECTORY_NODE, PREFIX +
":" + ISDIRECTORY_NODE);
//add isFile node
this.contentHandler.startElement(URI, ISFILE_NODE, PREFIX + ":"
+ ISFILE_NODE, attr);
this.contentHandler.characters(isFile.toCharArray(),0,
isFile.length());
this.contentHandler.endElement(URI, ISFILE_NODE, PREFIX + ":" +
ISFILE_NODE);
//add path node
this.contentHandler.startElement(URI, PATH_NODE, PREFIX + ":" +
PATH_NODE, attr);
this.contentHandler.characters(path.toCharArray(),0,
path.length());
this.contentHandler.endElement(URI, PATH_NODE, PREFIX + ":" +
PATH_NODE);
//add lastModified node
this.contentHandler.startElement(URI, LASTMODIFIED_NODE, PREFIX
+ ":" + LASTMODIFIED_NODE, attr);
this.contentHandler.characters(lastModified.toCharArray(),0,
lastModified.length());
this.contentHandler.endElement(URI, LASTMODIFIED_NODE, PREFIX +
":" + LASTMODIFIED_NODE);
this.contentHandler.endElement(URI, FILE_NODE, PREFIX + ":" +
FILE_NODE);
this.contentHandler.endPrefixMapping(PREFIX);
this.contentHandler.endDocument();
}
public void setup(SourceResolver resolver, Map objectModel, String
src, Parameters par) throws ProcessingException, SAXException,
IOException {
super.setup(resolver, objectModel, src, par);
String format = par.getParameter("dateformat", "dd-MM-yyyy
HH:mm:ss");
df = new SimpleDateFormat(format);
File file = new File(src);
if (file.exists()) {
name = file.getName();
isDirectory =
Boolean.valueOf(file.isDirectory()).toString();
isFile = Boolean.valueOf(file.isFile()).toString();
path = file.getPath();
lastModified = df.format(new Date(file.lastModified()));
} else {
throw new FileNotFoundException();
}
}
}
-----Original Message-----
From: Robby Pelssers [mailto:[email protected]]
Sent: donderdag 5 februari 2009 19:18
To: [email protected]
Subject: RE: sitemap parameters
Hi Mathias,
You might create your own FileMetaGenerator which extends
org.apache.cocoon.generation.AbstractGenerator
You could let this FileMetaGenerator generate an xml snippet like:
<file:file xmlns:dir="http://apache.org/cocoon/file/2.0">
<file:lastmodified>1056668768203</file:lastmodified>
<file:name>input.xml</file:name>
<file:path>...</file:path>
<file:isDirectory>false</file:isDirectory>
<file:isFile>true</file:isFile>
....
</file:file>
Then you could use the aggregator to aggregate your xml file with this
fileMeta.xml and run a stylesheet over the aggregated xml to extract
the info you need.
There are probably better ways to do this but this is the first thing
that comes to my mind.
Robby
-----Original Message-----
From: Mathias Reem [mailto:[email protected]]
Sent: donderdag 5 februari 2009 18:47
To: [email protected]
Subject: Re: sitemap parameters
Hi
> Uh..... LastModified of what?? Could you be more elaborate? Can you
> explain the usecase a bit more?
Sorry. We use xml files (and database entries in the future) that are
converted by cocoon to html. The generated page must show when it as
last changed. So for the start I would want to use the MTIME of the xml
input file to show it on the html page.
The long shot goal:
When we add the database as additional input source, I would like the
take db updates/modifications also into account. I'm not even sure if
this is possible at all.
thanks
Mathias
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]