Integrating Lotus Notes applications with web services(Part 2)

Share

Once we received the information from the web services, the next step was to parse that stream in order to get the necessary data. I needed a quick and easy way of doing this. After a little search on the existing solutions, I have chosen to use Xstream library. It’s simple to work with and you don’t need extra mapping. You just create the Java classes, that will represent our xml file.

<?xml version="1.0" encoding="UTF-8" ?>
<projects>
	<project id="15" name="Project A">
		<plan id="0">
			<name>RootPlan</name>
			<plan id="37">
				<name>Plan A.1</name>
			</plan>
			<plan id="67">
				<name>Plan A.2</name>
			</plan>
		</plan>
		<plan id="31">
			<name>Plan B</name>
			<plan id="48">
				<name>Plan B.1</name>
			</plan>
		</plan>
	</project>
</projects>

Every project has an id and a name, and a plan. An plan has also an id and a name, and a list of plans.

public class Project {
	private String id;
	private String name;
	private Plan plan;
	// ... constructors and methods
}
 
public class Plan {
	private String id;
	private String name;
	private List plans;
	// ... constructors and methods
}
 
public class Projects {
	private List projects;
	// ... constructors and methods
}

To use XStream, simply instantiate the XStream class with a standard JAXP DOM parser as the constructor’s parameter. In this case only xstream-[version].jar is required it the classpath. The no parameter constructor for the XStream class uses the XPP3 parser (XPP3 is a very fast XML pull-parser implementation). In this case you must include the xpp3-[version].jar as a dependency.

In the xml file the project’s id is an attribute. To work properly you need the useAttributeFor method. The addImplicitCollection method treats “Plan A” as it would be inside a plans tag. A link between the xml element and a Java class is made by the alias method.

If you want to use Xstream with Lotus Notes you will get an exception like this: “java.lang.ExceptionInInitializerError: Cannot access defaults field of Properties”. The solution is to add an entry to Lotus Domino JVM’s java.policy file to allow XStream jar use reflection.
Adding this grant solves the issue:
permission java.lang.reflect.ReflectPermission “suppressAccessChecks”;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
 
public class XstreamParser {
	public static void main(String[] args) {
		XStream xstream = new XStream(new DomDriver()); // does not require XPP3
		FileInputStream fis;
		try {
			fis = new FileInputStream("D:/XML/GetProjectTree.xml");
 
			xstream.useAttributeFor(Project.class, "id");
			xstream.useAttributeFor(Project.class, "name");
			xstream.useAttributeFor(Plan.class, "id");
			xstream.alias("projects", Projects.class);
			xstream.alias("project", Project.class);
			xstream.alias("plan", Plan.class);
			xstream.addImplicitCollection(Projects.class, "projects");
			xstream.addImplicitCollection(Plan.class, "plans");
 
			Projects projects = (Projects) xstream.fromXML(fis);
 
			for (Project project : projects.getProjects()) {
				PlanUtils planUtils = new PlanUtils();
				planUtils.printPlan(project.getPlan());
			}
 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
 
	}
}
import java.util.ArrayList;
import java.util.List;
 
public class PlanUtils {
 
	private List planNames = new ArrayList();
 
	private void iteratePlan(Plan plan, String str) {
		if (plan.getPlans() != null) {
			planNames.add(str + " - id= " + plan.getId());
			for(Plan tempPlan : plan.getPlans()) {
				iteratePlan(tempPlan, str + "/" + tempPlan.getName());
			}
		} else {
			planNames.add(str + " - id= " + plan.getId());
		}
	}
 
	public void printPlan(Plan plan) {
		iteratePlan(plan, plan.getName());
		for (String planName : planNames) {
			System.out.println(planName);
		}
	}
}

This example contains few of the Xstream’s capabilities. For further information see:

Xstream web site
XPP3 web site

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close