[opencms-dev] Java Code in Template - Questions!

CALI-GARSIA Bruno bruno.cali-garsia-renexter at renault.com
Thu Mar 27 19:52:09 CET 2003


Hello Ralf,

>Is it possible to access here other files, i.e. a XML file in the OpenCMS
>virtual filesystem, process it with XERCES or somehow and output some
results
>with setData of CmsXmlTemplateFile ?



 // -- read XML File in VFS
 CmsFile file = m_cms.readFile(filepath);
 byte[] bytecontent=file.getContents();

 // -- get doc root from the byte content
 org.w3c.dom.Node root = getDocRoot(bytecontent);

 // -- do whatever with document ....
 myresult = ....

 // -- set the process tag
 template.setData("my_process_tag", myresult);

 // -- get the html processed with your result
 StringBuffer result= new StrignBuffer();
 result.append(template.getProcessedDataValue("yourhtmlblock", this,
userObject));

 // --return html
 return result


----8<-----------
 private org.w3c.dom.Node getDocRoot(byte[] bytecontent) throws Exception{

        InputStream is = new ByteArrayInputStream(bytecontent);
        DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Node root = (org.w3c.dom.Node)builder.parse(is);
        return root;
    }

The template.setData.. is the old good way to display dyna contents in xml
templates.
If you have a recent version of ocms, I guess you could use JSP instead.

Regards,
Bruno


-----Message d'origine-----
De : owner-opencms-dev at www.opencms.org
[mailto:owner-opencms-dev at www.opencms.org]De la part de
ambiesense at gmx.de
Envoyé : jeudi 27 mars 2003 05:04
À : opencms-dev at www.opencms.org
Objet : [opencms-dev] Java Code in Template - Questions!


Hi,

I am wondering if the following would be possible.

I run a ownCodeExample which executed own java code within a content
template. Pages made from this template displayed text generated from this
Java
class. Here the code of the java file:

import com.opencms.template.*;
import com.opencms.file.*;
import com.opencms.core.*;
import java.util.*;

public class OwnCode extends CmsXmlTemplate {
    public byte[] getContent(CmsObject cms, String templateFile, String
elementName,
                             Hashtable parameters, String templateSelector)
throws CmsException {
        CmsXmlTemplateFile templateDocument =
getOwnTemplateFile(cms,templateFile, elementName,
            parameters, templateSelector);
        templateDocument.setData("greeting", "from Ralf!");

        // DEBUG OUTPUT
        System.out.println("My own code is running!!! :-)");

        return startProcessing(cms, templateDocument, elementName,
parameters, templateFile);
    }
}


Is it possible to access here other files, i.e. a XML file in the OpenCMS
virtual filesystem, process it with XERCES or somehow and output some
results
with setData of CmsXmlTemplateFile ?

Does somebody has done something similar and do have code for that ?

Thanks a lot!
Ralf

--
+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!





More information about the opencms-dev mailing list