001: package de.schlund.pfixxml.testrecording;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import javax.xml.transform.TransformerException;
009:
010: import org.apache.log4j.Logger;
011: import org.w3c.dom.DOMException;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.Node;
015: import org.w3c.dom.Text;
016:
017: import de.schlund.pfixxml.util.XPath;
018:
019: public class ApplicationList implements Serializable {
020: private static final Logger LOG = Logger
021: .getLogger(ApplicationList.class);
022:
023: private static final long serialVersionUID = 4898818721473076365L;
024:
025: public static ApplicationList load(Document projectsXml,
026: boolean tomcat, String sessionSuffix)
027: throws TransformerException {
028: ApplicationList result;
029: Iterator<?> iter;
030: Element project;
031: String name;
032: String server;
033: String defpath;
034:
035: result = new ApplicationList();
036: iter = XPath
037: .select(projectsXml,
038: "/projects/project[normalize-space(./active/text())='true']")
039: .iterator();
040: while (iter.hasNext()) {
041: project = (Element) iter.next();
042: name = project.getAttribute("name");
043: defpath = getTextOpt(project, "defpath");
044: if (defpath == null) {
045: LOG.warn("application " + name
046: + " has no defpath - ignored");
047: } else {
048: server = getText(project, "servername");
049: result.add(new Application(name, server, tomcat,
050: defpath, sessionSuffix));
051: }
052: }
053: return result;
054: }
055:
056: private static String getTextOpt(Node root, String path)
057: throws DOMException, TransformerException {
058: if (XPath.select(root, path).size() == 0) {
059: return null;
060: } else {
061: return getText(root, path);
062: }
063: }
064:
065: private static String getText(Node root, String path)
066: throws DOMException, TransformerException {
067: return ((Text) XPath.selectOne(root, path + "/text()"))
068: .getNodeValue();
069: }
070:
071: //--
072:
073: private final List<Application> apps;
074:
075: public ApplicationList() {
076: apps = new ArrayList<Application>();
077: }
078:
079: public void add(Application app) {
080: if (lookup(app.getName()) != null) {
081: throw new IllegalArgumentException("duplicate application "
082: + app.getName());
083: }
084: apps.add(app);
085: }
086:
087: public int size() {
088: return apps.size();
089: }
090:
091: public Application get(String displayName) {
092: Application result;
093:
094: result = lookup(displayName);
095: if (result == null) {
096: throw new IllegalArgumentException("unknown application: "
097: + displayName);
098: }
099: return result;
100: }
101:
102: public Application lookup(String displayName) {
103: Iterator<Application> iter;
104: Application app;
105:
106: iter = apps.iterator();
107: while (iter.hasNext()) {
108: app = (Application) iter.next();
109: if (app.getName().equals(displayName)) {
110: return app;
111: }
112: }
113: return null;
114: }
115:
116: // TODO: castor
117: public List<Application> getApplications() {
118: return apps;
119: }
120:
121: public String toString() {
122: return "applications(" + apps.toString() + ")";
123: }
124: }
|