• Get the Flash Player to see the slideshow.
  • Categories

  • Authors

  • Great Quotes

    Once we accept our limits, we go beyond them. — Albert Einstein

Get in touch...

To have a chat about
your CMS needs...

Call us 0207 193 2014
or
Email us on

Archive for the ‘Java’ Category

Read a Single XML File in Java

Monday, March 2nd, 2009

The following should complement the supplied article for reading XML files (DCRs) based on a directory.  This will read the contents of a singe file

package com.littleforest.examples;
 
import com.interwoven.livesite.dom4j.Dom4jUtils;
import com.interwoven.livesite.external.ParameterHash;
import com.interwoven.livesite.runtime.RequestContext;
import com.interwoven.livesite.external.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import org.dom4j.*;
import java.util.Properties; /* Added for proxy use */
import java.io.IOException;
import com.interwoven.livesite.file.FileDALIfc; /* Added to avoid use of CDATA write */
 
public class xmlDcrUtilities{
 
public Document readSingle(RequestContext context)
{
//look in any single directory only
Document doc = Dom4jUtils.newDocument();
Element fileElem = doc.addElement("Records");
 
FileDALIfc fileDAL = context.getFileDal();
 
String absoluteFilePath = context.getParameterString("DCRPath");
 
Document fileDCR = Dom4jUtils.newDocument(fileDAL.read(absoluteFilePath))  ;
fileElem.add(fileDCR.getRootElement().createCopy())  ;
 
return doc;
}
 
}

Read List of XML Files from Directory Tree in Java

Monday, March 2nd, 2009

This is a TeamSite 6.7.2/LiveSite 3.1 compliant mechanism that can be used to read all DCR files into a single XML files for further processing:

 
package com.littleforest.examples;
 
import com.interwoven.livesite.dom4j.Dom4jUtils;
import com.interwoven.livesite.external.ParameterHash;
import com.interwoven.livesite.runtime.RequestContext;
import com.interwoven.livesite.file.FileDALIfc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import org.dom4j.*;
import java.util.Properties; /* Added for proxy use */
import com.interwoven.livesite.external.*;
import java.io.IOException;
 
public class xmlFileListContents {
 
public Document fetchTree(RequestContext context)
{
//look in ALL directories
Document doc = Dom4jUtils.newDocument();
Element fileElem = doc.addElement("Records");
 
FileDALIfc fileDAL = context.getFileDal();
 
String areaRelativeBasePath = context.getParameterString("BaseDirectory");
 
String absoluteBasePath = fileDAL.getRoot() + fileDAL.getSeparator() + areaRelativeBasePath;
 
//recurse directories
String[] directories = fileDAL.getChildDirectories(absoluteBasePath);
int dirCount = directories.length;
 
for(int k=0;k<dirCount;k++)
{
String absoluteDirPath = absoluteBasePath + fileDAL.getSeparator() + directories[k];
 
//recurse files in directories
String[] files = fileDAL.getChildFiles(absoluteDirPath);
int count = files.length;
 
for(int j=0;j<count;j++)
{
String absoluteFilePath = absoluteDirPath + fileDAL.getSeparator() + files[j];
Document fileDCR = Dom4jUtils.newDocument(fileDAL.read(absoluteFilePath))  ;
 
//add path as an attribute
fileDCR.getRootElement().addAttribute("path",absoluteFilePath);
// add the dcr to the result doc;
fileElem.add(fileDCR.getRootElement().createCopy())  ;
//optionally, may prefer path as an element
//fileElem.addElement("../path").addText(absoluteFilePath);
}
 
}
 
//ensure we have those in the Base too
String[] baseFiles = fileDAL.getChildFiles(absoluteBasePath);
int fCount = baseFiles.length;
 
for(int i=0;i<fCount;i++)
{
String absoluteFilePath = absoluteBasePath + fileDAL.getSeparator() + baseFiles[i];
Document fileDCR = Dom4jUtils.newDocument(fileDAL.read(absoluteFilePath))  ;
 
//add path as an attribute
fileDCR.getRootElement().addAttribute("path",absoluteFilePath);
// add the dcr to the result doc;
fileElem.add(fileDCR.getRootElement().createCopy())  ;
//optionally, may prefer path as an element
//fileElem.addElement("../path").addText(absoluteFilePath);
}
 
return doc;
}
 
}

Referencing Hello World Java Class in LiveSite External Component

Tuesday, January 20th, 2009

Now that we have Hello.Java created as a class.  Please refer to the blog
Creating a “Hello World” LiveSite Java External

I will demonstrate below how the class would be utilised in a LiveSite component.

- Create a component (name the component what ever you would like it to be)
- Select the component as XML/XSL format
- In Content XML, add the the following code;  

<Data>

  <External>

    <Object Scope=”local”>com.littleforest.examples.Hello</Object>

    <Method>Hello</Method>

  </External>

</Data>

 - In Appearance XSL, add the following code;

<div>

  <xsl:value-of select=”/Properties/Data/Results/Hello”/>

</div>

- Save the component, and Preview the Component
- You will see the Hello.Java class discussed in the blog
Creating a “Hello World” LiveSite Java External in your LiveSite component.

Compiling Java Classes in LiveSite 3.0.2

Tuesday, January 20th, 2009

Java classes created in TeamSite & LiveSite reference many other Java files.  These files are located in a different directory of TeamSite, so are referred to as external files to the location where Java classes.

The problem with compiling a Java class comes when the Java file to be compiled looks for these reference files, but can’t find them as they are located externally to where a Java compile takes place.  This is not documented.

Solution:

1. Make sure the Java file you create is in the location;
iw-home/local/config/lib/content_center/livesite_customer_src/src

Files created should be placed into these location corresponding to the file type

iw-home/local/config/lib/content_center/livesite_customer_src/etc/conf for config files
iw-home/local/config/lib/content_center/livesite_customer_src/etc/web for jsp files

2. you need to copy the external jar files which you may require for your own Java file located in;
iw-home/httpd/webapps/content_center/WEB-INF/lib
to
iw-home/local/config/lib/content_center/livesite_customer_src/lib

There are many Jar files which have many java classes which might be referenced in your own Java file.  Typical Jar files which are recommended to copy to the location above are;
- dom4j-1.6.1.jar
- livesite.jar
- livesite_customer.jar
- livesite-runtime.jar

3. Once  you have copied these files over, in command line, navigate to
iw-home/local/config/lib/content_center/livesite_customer_src

4. Run the command iw-home/bin/make_toolkit.ipl

This should now build your Java file successfully to the Java class you require for your LiveSite external component

Getting MultipleSelect values in a Java External

Tuesday, September 23rd, 2008

It is often useful to setup a “Multi-Select Datum” in a LiveSite component, but how do you write the Java external to receive this array of parameters?

The following code snippet from Raj Shekhawat shows you how:

//LiveSite returns Multiple select values in the form of ArrayList object
//Searchproperties is my parameter name
 ArrayList selectedProperties= (ArrayList) context.getParameters().get("SearchProperties"); 
 
String selectedValues = "";
Iterator propertiesIterator = selectedProperties.iterator();
 while (propertiesIterator.hasNext()) {
             selectedValues += propertiesIterator.next();
}

“Just Compile” – how to minimise disruptions when compiling Java

Tuesday, July 8th, 2008

To make things a little less disruptive on development servers I recommend using a simple “compile” script for running any Java code.

This is good to do to check that all compiles ok before running a “build.sh” (which takes down the TeamSite UI for a minute or so
and so disrupts any other work being done on the server).

The “just_compile.sh” file should be placed in /usr/iw-home/local/config/lib/content_center/livesite_customer_src.
You need to pass in the full path to your Java file like this (on one line though!):

./just_compile.sh
"/usr/iw-home/local/config/lib/content_center/livesite_customer_src
/src/com/littleforest/examples/Hello.java"

You may need to edit the below “just_compile.sh” to add in new jars that your Java class needs to include.
Also note the $1 at the end which is the name of the Java class you want to compile.

/usr/iw-home/tools/java/bin/javac -cp
/usr/iw-home/httpd/webapps/content_center/WEB-INF/lib/livesite-runtime.jar:
/usr/iw-home/httpd/webapps/content_center/WEB-INF/lib/dom4j-1.6.1.jar:
/usr/iw-home/httpd/webapps/content_center/WEB-INF/lib/customer.jar $1

Creating a “Hello World” LiveSite Java External

Friday, July 4th, 2008

I wanted to demonstrate the bare bones Hello World for LiveSite 3.x Java External Component, so here it is (if you can reduce this to being even simpler then please let us know):

package com.littleforest.examples;
import com.interwoven.livesite.dom4j.Dom4jUtils;
import com.interwoven.livesite.runtime.RequestContext;
import org.dom4j.Document;
 
public class Hello
{
    public static Document hello(RequestContext context)
    {
        Document doc = Dom4jUtils.newDocument("<hello/>");
 
        return doc;
    }
 
}