KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mc > formgenerator > bonita > DocumentParser


1 /*
2  * Created on 28 avr. 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package mc.formgenerator.bonita;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14 import org.apache.xerces.dom.DOMImplementationImpl;
15 import org.w3c.dom.DOMImplementation JavaDoc;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19 import org.w3c.dom.Text JavaDoc;
20
21
22 /**
23  * @author delfourr
24  *
25  */

26 public class DocumentParser {
27
28     
29     
30     /**
31      * Default class constructor.
32      */

33     public DocumentParser() {
34         
35     }
36
37
38
39
40     
41     /**
42      * Set the attributes of th DataProcess structure with the Document information.
43      * @param doc Document which owns the process information.
44      * @param dataProcess Structure which has to be set.
45      */

46     private void parseProcess(Document JavaDoc doc, DataProcess dataProcess) {
47         
48         //new information will be saved in these variables
49
String JavaDoc newName;
50                 
51         HashMap JavaDoc newTable = new HashMap JavaDoc();
52         
53         
54         //Root is the first tag of the document. Its name is "project"
55
Element JavaDoc root = doc.getDocumentElement();
56
57
58
59         //**************************************************************************
60
// GET THE PROCESS NAME FROM THE DOCUMENT
61
//**************************************************************************
62

63
64         //Save the process name
65
NodeList JavaDoc elementName = root.getElementsByTagName("name");
66     
67         if (elementName.getLength() == 0) {
68             
69                 //default process name
70
newName = "defaultProject";
71         }
72         else {
73                 Text JavaDoc nameProcess = (Text JavaDoc)elementName.item(0).getFirstChild();
74
75                 newName = nameProcess.getNodeValue();
76         }
77         
78         
79         
80         //**************************************************************************
81
// GET THE PROCESS PROPERTIES FROM THE DOCUMENT
82
//**************************************************************************
83

84         
85
86         //List of nodes named properties
87
NodeList JavaDoc elementProperty = root.getElementsByTagName("properties");
88         if (elementProperty.getLength() == 0) {
89         }
90         else {
91                 
92                 //Is unique, it's the first
93
Element JavaDoc propertyElement = (Element JavaDoc)elementProperty.item(0);
94         
95                 //List of child nodes of element "properties" : the key-value
96
//NodeList elementsKeyValue = propertyElement.getElementsByTagName("*");
97
NodeList JavaDoc elementsProperty = propertyElement.getElementsByTagName("property");
98                 int size = elementsProperty.getLength();
99
100                 //Save name-value if they exist
101
if (size > 0) {
102
103                         for (int i=0; i<size; i++)
104                         {
105                             // new xml Format
106
Element JavaDoc propertyElementNameValue = (Element JavaDoc)elementsProperty.item(i);
107                             
108                             NodeList JavaDoc elementsPropertyNameValue = propertyElementNameValue.getElementsByTagName("*");
109                             //Element propertyElementValue = (Element)elementsPropertyNameValue.item(0);
110
Text JavaDoc valueElement = (Text JavaDoc)elementsPropertyNameValue.item(0).getFirstChild();
111                             String JavaDoc currentKey = valueElement.getNodeValue();
112                             String JavaDoc currentValue = "";
113                             if ((Text JavaDoc)elementsPropertyNameValue.item(1).getFirstChild() != null)
114                             {
115                                valueElement = (Text JavaDoc)elementsPropertyNameValue.item(1).getFirstChild();
116                                currentValue = valueElement.getNodeValue();
117                                 }
118                                 //old cml format
119
//current node which gives the value
120
//Text valueElement = (Text)elementsKeyValue.item(i).getFirstChild();
121

122                                 //Takes the current node information
123
//String currentValue = valueElement.getNodeValue();
124
//String currentKey = elementsKeyValue.item(i).getNodeName();
125

126                                 
127                                                     
128                                                         
129                                 //Puts them in the HashMap
130
newTable.put(currentKey, currentValue);
131                         }
132                 
133                 }
134         }
135                 
136         //**************************************************************************
137
// SET DATAPROCESS ATTRIBUTES
138
//**************************************************************************
139

140         dataProcess.setProcessName(newName);
141         dataProcess.setProcessProperties(newTable);
142
143     }
144
145
146
147
148     /**
149      * Say if the document has properties about the process.
150      * @param doc Document which owns the process information.
151      * @return boolean
152      */

153     public boolean hasProperties(Document JavaDoc doc) {
154
155         //Root is the first tag of the document. Its name is "project"
156
Element JavaDoc root = doc.getDocumentElement();
157         
158         //List of nodes named properties
159
NodeList JavaDoc elementProperty = root.getElementsByTagName("properties");
160         
161         Element JavaDoc propertyElement = (Element JavaDoc)elementProperty.item(0);
162                 
163         return (propertyElement.getElementsByTagName("*").getLength() != 0);
164         
165     }
166
167
168
169
170
171
172     /**
173      * Creates a DataProject structure thanks to the Document information.
174      * @param doc Document which represents the project.
175      * @return dataProject Structure which represents the project.
176      */

177     public DataProject parseProject(Document JavaDoc doc) {
178         
179         //the structure to return
180
DataProject dataProject = new DataProject();
181         
182         //parse document to obtain the name and the properties of the activity
183
this.parseProcess(doc, dataProject);
184         
185         return dataProject;
186     }
187
188
189
190
191
192
193
194     /**
195      * Creates a DataActivity structure thanks to the Document information.
196      * @param doc Document which represents the activity.
197      * @return dataProject Structure which represents the activity.
198      */

199     public DataActivity parseActivity(Document JavaDoc doc) {
200         
201         //the structure to return
202
DataActivity dataActivity = new DataActivity();
203         
204         //parse document to obtain the name and the properties of the activity
205
this.parseProcess(doc, dataActivity);
206         
207         String JavaDoc activityName ="";
208         
209         
210         //**************************************************************************
211
// GET THE Activity NAME FROM THE DOCUMENT
212
//**************************************************************************
213

214         //Root is the first tag of the document. Its name is "project"
215
Element JavaDoc root = doc.getDocumentElement();
216             
217         //Save the process name
218
NodeList JavaDoc elementName = root.getElementsByTagName("activityName");
219
220         if (elementName.getLength() == 0) {
221                 
222                 //default activity name
223
activityName = "defaultActivity";
224         }
225         else {
226                 Text JavaDoc nameProcess = (Text JavaDoc)elementName.item(0).getFirstChild();
227                 activityName = nameProcess.getNodeValue();
228         }
229         
230         
231         //**************************************************************************
232
// SET THE PROJECTNAME ATTRIBUTE AND RETURNS DATAACTIVITY INSTANCE
233
//**************************************************************************
234

235         dataActivity.setActivityName(activityName);
236                 
237         return dataActivity;
238     }
239
240
241
242
243
244     
245     /**
246      * Creation of a xml document by using Xerces
247      * @param data The data to put in the Document.
248      * @return Document contains process information given by the DataProcess.
249      */

250     public Document JavaDoc createDocument(DataProcess data){
251         
252        //Out Document for manipulation by Xerces API
253
Document JavaDoc document = null;
254         
255        //Set for all the hmap keys
256
Set JavaDoc keySet = null;
257         
258        //Interface
259
DOMImplementation JavaDoc domImpl = new DOMImplementationImpl();
260         
261        //Document creation with the root element called project
262
document = domImpl.createDocument(null, "process" , null);
263         
264        //Setting the root element always called "project"
265
Element JavaDoc root = document.getDocumentElement();
266         
267         
268        //Name of the Bonita project***************************************
269
Element JavaDoc elementName = document.createElement("name");
270         
271        //Value of the node called "name"
272
Text JavaDoc textName = document.createTextNode(data.getProcessName());
273        elementName.appendChild(textName);
274        root.appendChild(elementName);
275        //*****************************************************************
276

277             
278     
279        //Node "properties" that will contain project properties************
280
Element JavaDoc elementProperties = document.createElement("properties");
281        //******************************************************************
282

283         
284        //Getting keys set
285
keySet = data.getProcessProperties().keySet();
286         
287        //Current key
288
String JavaDoc currentKey = "";
289         
290        //Current value
291
String JavaDoc currentValue = "";
292         
293        //For each key we must get the value associeted
294
Iterator JavaDoc it = keySet.iterator();
295        while(it.hasNext())
296        {
297                                 //New format : <properties><property><name>key</name><type></type><value>value></value>
298
//<possible-values><possible></possible></possible-values><property></properties>
299
Element JavaDoc elementProperty = document.createElement("property");
300
301                 //Getting the key
302
currentKey = (String JavaDoc)it.next();
303
304                 //Getting the value
305
currentValue = (String JavaDoc)data.getProcessProperties().get(currentKey);
306                                 
307                 //old format : <properties><key>value</key><key>value</key></properties>
308
//Element elementKey = document.createElement(currentKey);
309
//Text textKey = document.createTextNode(currentValue);
310

311                 Element JavaDoc elementPropertyName = document.createElement("name");
312                 Text JavaDoc textPropertyName = document.createTextNode(currentKey);
313                 elementPropertyName.appendChild(textPropertyName);
314                 //Adding this element to the "property" element
315
elementProperty.appendChild(elementPropertyName);
316                                 
317                 // We test if there is a multiple possible values
318
// value1|value2|value3 ...
319
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(currentValue, "|");
320                     Element JavaDoc elementPropertyValue = null;
321                     Element JavaDoc elementPropertyPossible = null;
322                     Element JavaDoc elementPropertyType = document.createElement("type");
323                     if (st.countTokens() > 1)
324                     {
325
326            
327                    boolean first = true;
328                    Element JavaDoc elementPropertyPossibleValues = document.createElement("possible-values");
329                        while (st.hasMoreTokens())
330                        {
331                         String JavaDoc value = st.nextToken();
332                         if (first)
333                         {
334                         elementPropertyValue = document.createElement("value");
335                         Text JavaDoc textPropertyValue = document.createTextNode(value);
336                         elementPropertyValue.appendChild(textPropertyValue);
337                         elementProperty.appendChild(elementPropertyValue);
338         
339                         Text JavaDoc textPropertyType = document.createTextNode("select");
340                         elementPropertyType.appendChild(textPropertyType);
341                         elementProperty.appendChild(elementPropertyType);
342                         first = false;
343                         }
344                         else
345                         {
346                         elementPropertyPossible = document.createElement("possible");
347                         Text JavaDoc textPropertyPossible = document.createTextNode(value);
348                         elementPropertyPossible.appendChild(textPropertyPossible);
349                         elementPropertyPossibleValues.appendChild(elementPropertyPossible);
350                     }
351                    }
352                    
353                    elementProperty.appendChild(elementPropertyPossibleValues);
354                 }
355                 else
356                 {
357                    elementPropertyValue = document.createElement("value");
358                    Text JavaDoc textPropertyValue = document.createTextNode(currentValue);
359                    elementPropertyValue.appendChild(textPropertyValue);
360                    elementProperty.appendChild(elementPropertyValue);
361
362                    Text JavaDoc textPropertyType = document.createTextNode("input");
363                    elementPropertyType.appendChild(textPropertyType);
364                    elementProperty.appendChild(elementPropertyType);
365                 }
366                 //Adding this element to the "properties" element
367
elementProperties.appendChild(elementProperty);
368         }
369         
370         //Adding the node to the root element
371
root.appendChild(elementProperties);
372             
373         //Return the document
374
return document;
375     }
376     
377     
378     
379     
380
381
382     /**
383      * Creation of a xml document by using Xerces
384      * @param data The data to put in the Document.
385      * @return Document contains activity information given by the DataActivity.
386      */

387     public Document JavaDoc createDocument(DataActivity data){
388         
389         //Out Document for manipulation by Xerces API
390
Document JavaDoc document = null;
391             
392         document = this.createDocument((DataProcess)data);
393         
394         //Getting the root element
395
Element JavaDoc root = document.getDocumentElement();
396         
397         //Element creation
398
Element JavaDoc elementNameProject = document.createElement("activityName");
399         
400         //Value of the node called "nameProject"
401
Text JavaDoc textNameProject = document.createTextNode(data.getActivityName());
402         
403         //Adding the text to the element
404
elementNameProject.appendChild(textNameProject);
405         
406         //Adding the node to the root element
407
root.appendChild(elementNameProject);
408             
409         //Return the document
410
return document;
411     }
412 }
413
Popular Tags