1 7 package mc.formgenerator.bonita; 8 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 import java.util.StringTokenizer ; 13 14 import org.apache.xerces.dom.DOMImplementationImpl; 15 import org.w3c.dom.DOMImplementation ; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.NodeList ; 19 import org.w3c.dom.Text ; 20 21 22 26 public class DocumentParser { 27 28 29 30 33 public DocumentParser() { 34 35 } 36 37 38 39 40 41 46 private void parseProcess(Document doc, DataProcess dataProcess) { 47 48 String newName; 50 51 HashMap newTable = new HashMap (); 52 53 54 Element root = doc.getDocumentElement(); 56 57 58 59 63 64 NodeList elementName = root.getElementsByTagName("name"); 66 67 if (elementName.getLength() == 0) { 68 69 newName = "defaultProject"; 71 } 72 else { 73 Text nameProcess = (Text )elementName.item(0).getFirstChild(); 74 75 newName = nameProcess.getNodeValue(); 76 } 77 78 79 80 84 85 86 NodeList elementProperty = root.getElementsByTagName("properties"); 88 if (elementProperty.getLength() == 0) { 89 } 90 else { 91 92 Element propertyElement = (Element )elementProperty.item(0); 94 95 NodeList elementsProperty = propertyElement.getElementsByTagName("property"); 98 int size = elementsProperty.getLength(); 99 100 if (size > 0) { 102 103 for (int i=0; i<size; i++) 104 { 105 Element propertyElementNameValue = (Element )elementsProperty.item(i); 107 108 NodeList elementsPropertyNameValue = propertyElementNameValue.getElementsByTagName("*"); 109 Text valueElement = (Text )elementsPropertyNameValue.item(0).getFirstChild(); 111 String currentKey = valueElement.getNodeValue(); 112 String currentValue = ""; 113 if ((Text )elementsPropertyNameValue.item(1).getFirstChild() != null) 114 { 115 valueElement = (Text )elementsPropertyNameValue.item(1).getFirstChild(); 116 currentValue = valueElement.getNodeValue(); 117 } 118 122 126 127 128 129 newTable.put(currentKey, currentValue); 131 } 132 133 } 134 } 135 136 140 dataProcess.setProcessName(newName); 141 dataProcess.setProcessProperties(newTable); 142 143 } 144 145 146 147 148 153 public boolean hasProperties(Document doc) { 154 155 Element root = doc.getDocumentElement(); 157 158 NodeList elementProperty = root.getElementsByTagName("properties"); 160 161 Element propertyElement = (Element )elementProperty.item(0); 162 163 return (propertyElement.getElementsByTagName("*").getLength() != 0); 164 165 } 166 167 168 169 170 171 172 177 public DataProject parseProject(Document doc) { 178 179 DataProject dataProject = new DataProject(); 181 182 this.parseProcess(doc, dataProject); 184 185 return dataProject; 186 } 187 188 189 190 191 192 193 194 199 public DataActivity parseActivity(Document doc) { 200 201 DataActivity dataActivity = new DataActivity(); 203 204 this.parseProcess(doc, dataActivity); 206 207 String activityName =""; 208 209 210 214 Element root = doc.getDocumentElement(); 216 217 NodeList elementName = root.getElementsByTagName("activityName"); 219 220 if (elementName.getLength() == 0) { 221 222 activityName = "defaultActivity"; 224 } 225 else { 226 Text nameProcess = (Text )elementName.item(0).getFirstChild(); 227 activityName = nameProcess.getNodeValue(); 228 } 229 230 231 235 dataActivity.setActivityName(activityName); 236 237 return dataActivity; 238 } 239 240 241 242 243 244 245 250 public Document createDocument(DataProcess data){ 251 252 Document document = null; 254 255 Set keySet = null; 257 258 DOMImplementation domImpl = new DOMImplementationImpl(); 260 261 document = domImpl.createDocument(null, "process" , null); 263 264 Element root = document.getDocumentElement(); 266 267 268 Element elementName = document.createElement("name"); 270 271 Text textName = document.createTextNode(data.getProcessName()); 273 elementName.appendChild(textName); 274 root.appendChild(elementName); 275 277 278 279 Element elementProperties = document.createElement("properties"); 281 283 284 keySet = data.getProcessProperties().keySet(); 286 287 String currentKey = ""; 289 290 String currentValue = ""; 292 293 Iterator it = keySet.iterator(); 295 while(it.hasNext()) 296 { 297 Element elementProperty = document.createElement("property"); 300 301 currentKey = (String )it.next(); 303 304 currentValue = (String )data.getProcessProperties().get(currentKey); 306 307 311 Element elementPropertyName = document.createElement("name"); 312 Text textPropertyName = document.createTextNode(currentKey); 313 elementPropertyName.appendChild(textPropertyName); 314 elementProperty.appendChild(elementPropertyName); 316 317 StringTokenizer st = new StringTokenizer (currentValue, "|"); 320 Element elementPropertyValue = null; 321 Element elementPropertyPossible = null; 322 Element elementPropertyType = document.createElement("type"); 323 if (st.countTokens() > 1) 324 { 325 326 327 boolean first = true; 328 Element elementPropertyPossibleValues = document.createElement("possible-values"); 329 while (st.hasMoreTokens()) 330 { 331 String value = st.nextToken(); 332 if (first) 333 { 334 elementPropertyValue = document.createElement("value"); 335 Text textPropertyValue = document.createTextNode(value); 336 elementPropertyValue.appendChild(textPropertyValue); 337 elementProperty.appendChild(elementPropertyValue); 338 339 Text 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 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 textPropertyValue = document.createTextNode(currentValue); 359 elementPropertyValue.appendChild(textPropertyValue); 360 elementProperty.appendChild(elementPropertyValue); 361 362 Text textPropertyType = document.createTextNode("input"); 363 elementPropertyType.appendChild(textPropertyType); 364 elementProperty.appendChild(elementPropertyType); 365 } 366 elementProperties.appendChild(elementProperty); 368 } 369 370 root.appendChild(elementProperties); 372 373 return document; 375 } 376 377 378 379 380 381 382 387 public Document createDocument(DataActivity data){ 388 389 Document document = null; 391 392 document = this.createDocument((DataProcess)data); 393 394 Element root = document.getDocumentElement(); 396 397 Element elementNameProject = document.createElement("activityName"); 399 400 Text textNameProject = document.createTextNode(data.getActivityName()); 402 403 elementNameProject.appendChild(textNameProject); 405 406 root.appendChild(elementNameProject); 408 409 return document; 411 } 412 } 413 | Popular Tags |