KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mc > formgenerator > servlets > bonita > RepositoryFacade


1 /*
2  * Created on 1 april 2005
3  *
4  */

5 package mc.formgenerator.servlets.bonita;
6
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import javax.xml.parsers.ParserConfigurationException JavaDoc;
13
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17 import org.w3c.dom.Text JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20
21 /**
22  * This class returns the XForm's filename associated with a project or activity.
23  * @author cesterbj
24  *
25  */

26 public class RepositoryFacade {
27
28     
29     /**
30      * Attribute which stores the repository document
31      */

32     private Document JavaDoc xmlDocument;
33
34
35     /**
36      * Default class constructor
37      */

38     public RepositoryFacade(String JavaDoc xmlPath) {
39         DocumentBuilderFactory JavaDoc factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
40         
41         try {
42             //get file and parse it to xmlDocument
43
File JavaDoc xmlFile = new File JavaDoc(xmlPath);
44             DocumentBuilder JavaDoc documentBuilder = factory.newDocumentBuilder();
45             this.xmlDocument = documentBuilder.parse(xmlFile);
46         } catch (SAXException JavaDoc e) {
47             e.printStackTrace();
48         } catch (IOException JavaDoc e) {
49             e.printStackTrace();
50         } catch (ParserConfigurationException JavaDoc e) {
51             e.printStackTrace();
52         }
53     }
54
55
56
57     /**
58      * Returns the path of the XForm file for a project.
59      * @param projectName: the project's name
60      * @return String formpath.
61      * @throws Exception
62      */

63     public String JavaDoc getProjectFormPath (String JavaDoc projectName) throws Exception JavaDoc {
64         String JavaDoc formPath = null;
65         
66         //get "project" nodes
67
NodeList JavaDoc nodeList = this.xmlDocument.getElementsByTagName("project");
68         int size = nodeList.getLength();
69         if (size == 0) {
70             return null;
71         }
72         else {
73             //search projectName
74
for (int i=0;i<nodeList.getLength();i++){
75                 Element JavaDoc projectElement = (Element JavaDoc)nodeList.item(i);
76                 //get project name
77
NodeList JavaDoc elementsName = projectElement.getElementsByTagName("projectname");
78                 Text JavaDoc nameElement = (Text JavaDoc)elementsName.item(0).getFirstChild();
79                 String JavaDoc name = nameElement.getNodeValue();
80     
81                 if(name.equals(projectName)){
82                     //if project name has been found in repository, lookup its xform filename
83
NodeList JavaDoc elementsXformFile = projectElement.getElementsByTagName("projectxformfile");
84                     Text JavaDoc xformpathElement = (Text JavaDoc)elementsXformFile.item(0).getFirstChild();
85                     if(xformpathElement!=null){
86                         //this project has an xform file associated, then return filename
87
String JavaDoc path = xformpathElement.getNodeValue();
88                         System.out.println("RepositoryFacade ====> Project "+ projectName + " has been found in repository. XForm file: " + path);
89                         return (path);
90                     }else return null;
91                 }
92                 
93             }
94         }
95         
96         return null;
97     }
98     
99     /**
100      * Returns the path of the XForm file for an activity.
101      * @param projectName: the project's name
102      * @param activityName: the activity's name
103      * @return String formpath.
104      * @throws Exception
105      */

106     public String JavaDoc getActivityFormPath (String JavaDoc projectName, String JavaDoc activityName) throws Exception JavaDoc {
107         String JavaDoc formPath = null;
108         
109         //get "project" nodes
110
NodeList JavaDoc nodeList = this.xmlDocument.getElementsByTagName("project");
111         int size = nodeList.getLength();
112         if (size == 0) {
113             return null;
114         }
115         else {
116             //search project name
117
for (int i=0;i<nodeList.getLength();i++){
118                 Element JavaDoc projectElement = (Element JavaDoc)nodeList.item(i);
119                 NodeList JavaDoc pElementsName = projectElement.getElementsByTagName("projectname");
120                 Text JavaDoc pNameElement = (Text JavaDoc)pElementsName.item(0).getFirstChild();
121                 String JavaDoc pName = pNameElement.getNodeValue();
122                 
123                 if(pName.equals(projectName)){
124                     //if project name has been found in repository, lookup its xform filename
125
NodeList JavaDoc elementsActivities = projectElement.getElementsByTagName("activities");
126                     Element JavaDoc activitiesElement = (Element JavaDoc)elementsActivities.item(0);
127                     //get "activity" nodes
128
NodeList JavaDoc activitiesNodeList = activitiesElement.getElementsByTagName("activity");
129                     int activitiesSize = activitiesNodeList.getLength();
130                     if (activitiesSize == 0) {
131                         return null;
132                     }
133                     else {
134                         for (int j=0;i<nodeList.getLength();i++){
135                             Element JavaDoc activityElement = (Element JavaDoc)nodeList.item(i);
136                             NodeList JavaDoc aElementsName = activityElement.getElementsByTagName("activityname");
137                             Text JavaDoc aNameElement = (Text JavaDoc)aElementsName.item(0).getFirstChild();
138                             String JavaDoc aName = aNameElement.getNodeValue();
139
140                             if(aName.equals(activityName)){
141                                 //if activity name has been found in repository, lookup its xform filename
142
NodeList JavaDoc elementsXformFile = projectElement.getElementsByTagName("activityxformfile");
143                                 Text JavaDoc xformpathElement = (Text JavaDoc)elementsXformFile.item(0).getFirstChild();
144                                 if(xformpathElement!=null){
145                                     //this activity has an xform file associated, then return filename
146
String JavaDoc path = xformpathElement.getNodeValue();
147                                     System.out.println("RepositoryFacade ====> Activity "+ activityName +" of project "+ projectName + " has been found in repository. XForm file: " + path);
148                                     return (path);
149                                 }else return null;
150                             }
151                         }
152                     }
153                 }
154             }
155         }
156         return null;
157     }
158
159 }
160
Popular Tags