001: /*
002: * Created on 1 april 2005
003: *
004: */
005: package mc.formgenerator.servlets.bonita;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010:
011: import javax.xml.parsers.DocumentBuilder;
012: import javax.xml.parsers.DocumentBuilderFactory;
013: import javax.xml.parsers.ParserConfigurationException;
014:
015: import mc.formgenerator.servlets.util.FormGeneratorConstant;
016: import org.apache.xml.serialize.OutputFormat;
017: import org.apache.xml.serialize.XMLSerializer;
018: import org.w3c.dom.Attr;
019: import org.w3c.dom.Comment;
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.NamedNodeMap;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025: import org.w3c.dom.Text;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * This class is a facade class to operate with XForm files.
030: *
031: */
032: public class RepositoryFacade {
033:
034: /**
035: * Attribute which stores the repository document
036: */
037: private Document xmlDocument;
038: private String path;
039:
040: /**
041: * Default class constructor
042: */
043: public RepositoryFacade(String xmlPath) {
044: DocumentBuilderFactory factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
045:
046: try {
047: //get file and parse it to xmlDocument
048: File xmlFile = new File(xmlPath);
049: DocumentBuilder documentBuilder = factory
050: .newDocumentBuilder();
051: this .xmlDocument = documentBuilder.parse(xmlFile);
052: this .path = xmlPath;
053: } catch (SAXException e) {
054: e.printStackTrace();
055: } catch (IOException e) {
056: e.printStackTrace();
057: } catch (ParserConfigurationException e) {
058: e.printStackTrace();
059: }
060: }
061:
062: /**
063: * Returns the path of the XForm file for a project.
064: * @param projectName: the project's name
065: * @param projectVersion: the project's version
066: * @return String formpath.
067: * @throws Exception
068: */
069: public String getProjectFormPath(String projectName,
070: String projectVersion) {
071:
072: //get "project" nodes
073: NodeList nodeList = this .xmlDocument
074: .getElementsByTagName("project");
075: int size = nodeList.getLength();
076: if (size == 0) {
077: return null;
078: } else {
079: //search projectName
080: for (int i = 0; i < nodeList.getLength(); i++) {
081: Element projectElement = (Element) nodeList.item(i);
082: //get project name
083: NodeList elementsName = projectElement
084: .getElementsByTagName("projectname");
085: Text nameElement = (Text) elementsName.item(0)
086: .getFirstChild();
087: String name = nameElement.getNodeValue();
088:
089: if (name.equals(projectName)) {
090: NodeList elementsVersion = projectElement
091: .getElementsByTagName("projectversion");
092: Text versionElement = (Text) elementsVersion
093: .item(0).getFirstChild();
094: String version = versionElement.getNodeValue();
095:
096: if (version.equals(projectVersion)) {
097: //if project name has been found in repository, lookup its xform filename
098: NodeList elementsXformFile = projectElement
099: .getElementsByTagName("projectxformfile");
100: Text xformpathElement = (Text) elementsXformFile
101: .item(0).getFirstChild();
102: if (xformpathElement != null) {
103: //this project has an xform file associated, then return file path
104: String path = File.separator + "web"
105: + File.separator + "xforms"
106: + File.separator + projectName
107: + File.separator + projectVersion
108: + File.separator
109: + xformpathElement.getNodeValue();
110: //debug
111: //System.out.println("RepositoryFacade ====> Project "+ projectName + " has been found in repository. XForm file: " + path);
112: return (path);
113: } else
114: return null;
115: }
116: }
117:
118: }
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Updates project name.
126: * @param oldProjectName: the old project's name
127: * @param newProjectName: the new project's name
128: * @param projectVersion: the project's version
129: */
130: public void updateProjectName(String oldProjectName,
131: String newProjectName, String projectVersion)
132: throws Exception {
133:
134: //get "project" nodes
135: Element root = this .xmlDocument.getDocumentElement();
136: NodeList nodeList = root.getElementsByTagName("project");
137: int size = nodeList.getLength();
138: if (size == 0) {
139: System.out
140: .println("WARNING: RepositoryFacade ==> repository.xml has no projects");
141: } else {
142: //search projectName
143: for (int i = 0; i < nodeList.getLength(); i++) {
144: Element projectElement = (Element) nodeList.item(i);
145: //get project name
146: NodeList elementsName = projectElement
147: .getElementsByTagName("projectname");
148: Text nameElement = (Text) elementsName.item(0)
149: .getFirstChild();
150: String name = nameElement.getNodeValue();
151:
152: if (name.equals(oldProjectName)) {
153: //get project version
154: NodeList elementsVersion = projectElement
155: .getElementsByTagName("projectversion");
156: Text versionElement = (Text) elementsVersion
157: .item(0).getFirstChild();
158: String version = versionElement.getNodeValue();
159:
160: if (version.equals(projectVersion)) {
161: //if project name & version has been found in repository, change name
162: nameElement.setNodeValue(newProjectName);
163: }
164: }
165: }
166: }
167: //save repository file
168: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
169: FileOutputStream output = new FileOutputStream(this .path);
170: OutputFormat of = new OutputFormat("XML",
171: FormGeneratorConstant.CHARACTER_ENCODING, true);
172: of.setIndent(1);
173: of.setIndenting(true);
174: XMLSerializer serializer = new XMLSerializer(output, of);
175: serializer.asDOMSerializer();
176: serializer.serialize(this .xmlDocument.getDocumentElement());
177: output.close();
178: }
179:
180: /**
181: * Remove project form path from repository.
182: * @param projectName: the project's name
183: * @param projectVersion: the project's version
184: * Warning: This method is not used by proed at now !
185: */
186: public void removeProjectFormPath(String projectName,
187: String projectVersion) throws Exception {
188: //get "project" nodes
189: Element root = this .xmlDocument.getDocumentElement();
190: NodeList nodeList = root.getElementsByTagName("project");
191: int size = nodeList.getLength();
192: if (size == 0) {
193: System.out
194: .println("WARNING: RepositoryFacade ==> repository.xml has no projects");
195: } else {
196: //search projectName
197: for (int i = 0; i < nodeList.getLength(); i++) {
198: Node projectElement = nodeList.item(i);
199: //get project name
200: NodeList elementsName = ((Element) projectElement)
201: .getElementsByTagName("projectname");
202: Text nameElement = (Text) elementsName.item(0)
203: .getFirstChild();
204: String name = nameElement.getNodeValue();
205:
206: if (name.equals(projectName)) {
207: NodeList elementsVersion = ((Element) projectElement)
208: .getElementsByTagName("projectversion");
209: Text versionElement = (Text) elementsVersion
210: .item(0).getFirstChild();
211: String version = versionElement.getNodeValue();
212:
213: if (version.equals(projectVersion)) {
214: //if project name has been found in repository, remove project node
215: root.removeChild(projectElement);
216: //debug
217: //System.out.println("RepositoryFacade ====> Project "+ projectName + " has been removed from repository.xml");
218: break;
219: }
220: }
221: }
222: }
223: //save repository file
224: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
225: FileOutputStream output = new FileOutputStream(this .path);
226: OutputFormat of = new OutputFormat("XML",
227: FormGeneratorConstant.CHARACTER_ENCODING, true);
228: of.setIndent(1);
229: of.setIndenting(true);
230: XMLSerializer serializer = new XMLSerializer(output, of);
231: serializer.asDOMSerializer();
232: serializer.serialize(this .xmlDocument.getDocumentElement());
233: output.close();
234: }
235:
236: /**
237: * Returns the path of the XForm file for an activity.
238: * @param projectName: the project's name
239: * @param activityName: the activity's name
240: * @param projectVersion: the project's version
241: * @return String formpath.
242: * @throws Exception
243: */
244: public String getActivityFormPath(String projectName,
245: String activityName, String projectVersion) {
246:
247: //get "project" nodes
248: NodeList nodeList = this .xmlDocument
249: .getElementsByTagName("project");
250: int size = nodeList.getLength();
251: if (size == 0) {
252: return null;
253: } else {
254: //search project name
255: for (int i = 0; i < nodeList.getLength(); i++) {
256: Element projectElement = (Element) nodeList.item(i);
257: NodeList pElementsName = projectElement
258: .getElementsByTagName("projectname");
259: Text pNameElement = (Text) pElementsName.item(0)
260: .getFirstChild();
261: String pName = pNameElement.getNodeValue();
262:
263: if (pName.equals(projectName)) {
264: NodeList elementsVersion = projectElement
265: .getElementsByTagName("projectversion");
266: Text versionElement = (Text) elementsVersion
267: .item(0).getFirstChild();
268: String version = versionElement.getNodeValue();
269:
270: if (version.equals(projectVersion)) {
271:
272: //if project name & version has been found in repository, lookup its xform filename
273: NodeList elementsActivities = projectElement
274: .getElementsByTagName("activities");
275: Element activitiesElement = (Element) elementsActivities
276: .item(0);
277: //get "activity" nodes
278: NodeList activitiesNodeList = activitiesElement
279: .getElementsByTagName("activity");
280: int activitiesSize = activitiesNodeList
281: .getLength();
282: if (activitiesSize == 0) {
283: return null;
284: } else {
285: for (int j = 0; j < activitiesNodeList
286: .getLength(); j++) {
287: Element activityElement = (Element) activitiesNodeList
288: .item(j);
289: NodeList aElementsName = activityElement
290: .getElementsByTagName("activityname");
291: Text aNameElement = (Text) aElementsName
292: .item(0).getFirstChild();
293: String aName = aNameElement
294: .getNodeValue();
295:
296: if (aName.equals(activityName)) {
297: //if activity name has been found in repository, lookup its xform filename
298: NodeList elementsXformFile = projectElement
299: .getElementsByTagName("activityxformfile");
300: Text xformpathElement = (Text) elementsXformFile
301: .item(0).getFirstChild();
302: if (xformpathElement != null) {
303: //this activity has an xform file associated, then return filename
304: String path = File.separator
305: + "web"
306: + File.separator
307: + "xforms"
308: + File.separator
309: + projectName
310: + File.separator
311: + projectVersion
312: + File.separator
313: + activityName
314: + File.separator
315: + xformpathElement
316: .getNodeValue();
317: return (path);
318: } else
319: return null;
320: }
321: }
322: }
323: }
324: }
325: }
326: }
327: return null;
328: }
329:
330: /**
331: * Updates activiy name in repository.
332: * @param projectName: the project's name
333: * @param projectVersion: the project's version
334: * @param activityName: the activity's name
335: */
336: public void updateActivityName(String projectName,
337: String projectVersion, String oldActivityName,
338: String newActivityName) throws Exception {
339: //get "project" nodes
340: NodeList nodeList = this .xmlDocument
341: .getElementsByTagName("project");
342: int size = nodeList.getLength();
343: if (size == 0) {
344: System.out.println("WARNING: RepositoryFacade ==> Project "
345: + projectName + "not found");
346: } else {
347: //search project name
348: for (int i = 0; i < nodeList.getLength(); i++) {
349: Element projectElement = (Element) nodeList.item(i);
350: NodeList pElementsName = projectElement
351: .getElementsByTagName("projectname");
352: Text pNameElement = (Text) pElementsName.item(0)
353: .getFirstChild();
354: String pName = pNameElement.getNodeValue();
355:
356: if (pName.equals(projectName)) {
357: //get project version
358: NodeList elementsVersion = projectElement
359: .getElementsByTagName("projectversion");
360: Text versionElement = (Text) elementsVersion
361: .item(0).getFirstChild();
362: String version = versionElement.getNodeValue();
363:
364: if (version.equals(projectVersion)) {
365: //if project name has been found in repository, lookup its xform filename
366: NodeList elementsActivities = projectElement
367: .getElementsByTagName("activities");
368: Element activitiesElement = (Element) elementsActivities
369: .item(0);
370: //get "activity" nodes
371: NodeList activitiesNodeList = activitiesElement
372: .getElementsByTagName("activity");
373: int activitiesSize = activitiesNodeList
374: .getLength();
375: if (activitiesSize == 0) {
376: System.out
377: .println("WARNING: RepositoryFacade ==> Activity "
378: + oldActivityName
379: + "not found");
380: break;
381: } else {
382: for (int j = 0; j < activitiesNodeList
383: .getLength(); j++) {
384: Element activityElement = (Element) activitiesNodeList
385: .item(j);
386: NodeList aElementsName = activityElement
387: .getElementsByTagName("activityname");
388: Text aNameElement = (Text) aElementsName
389: .item(0).getFirstChild();
390: String aName = aNameElement
391: .getNodeValue();
392:
393: if (aName.equals(oldActivityName)) {
394: //if activity name has been found in repository, change name
395: aNameElement
396: .setNodeValue(newActivityName);
397: //System.out.println("RepositoryFacade ==> Activity " + oldActivityName + "updated to " + newActivityName);
398: break;
399: }
400: }
401: }
402: }
403: }
404: }
405: }
406: //save repository file
407: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
408: FileOutputStream output = new FileOutputStream(this .path);
409: OutputFormat of = new OutputFormat("XML",
410: FormGeneratorConstant.CHARACTER_ENCODING, true);
411: of.setIndent(1);
412: of.setIndenting(true);
413: XMLSerializer serializer = new XMLSerializer(output, of);
414: serializer.asDOMSerializer();
415: serializer.serialize(this .xmlDocument.getDocumentElement());
416: output.close();
417: }
418:
419: /**
420: * Removes activiy form path from repository.
421: * @param projectName: the project's name
422: * @param projectVersion: the project's version
423: * @param activityName: the activity's name
424: */
425: public void removeActivityFormPath(String projectName,
426: String projectVersion, String activityName)
427: throws Exception {
428: //get "project" nodes
429: NodeList nodeList = this .xmlDocument
430: .getElementsByTagName("project");
431: int size = nodeList.getLength();
432: if (size == 0) {
433: System.out.println("WARNING: RepositoryFacade ==> Project "
434: + projectName + "not found");
435: } else {
436: //search project name
437: for (int i = 0; i < nodeList.getLength(); i++) {
438: Element projectElement = (Element) nodeList.item(i);
439: NodeList pElementsName = projectElement
440: .getElementsByTagName("projectname");
441: Text pNameElement = (Text) pElementsName.item(0)
442: .getFirstChild();
443: String pName = pNameElement.getNodeValue();
444:
445: if (pName.equals(projectName)) {
446: //if project name has been found
447: NodeList elementsVersion = projectElement
448: .getElementsByTagName("projectversion");
449: Text versionElement = (Text) elementsVersion
450: .item(0).getFirstChild();
451: String version = versionElement.getNodeValue();
452:
453: //if project version has been found
454: if (version.equals(projectVersion)) {
455: //if project name & version has been found in repository, lookup its xform filename
456: NodeList elementsActivities = projectElement
457: .getElementsByTagName("activities");
458: Element activitiesElement = (Element) elementsActivities
459: .item(0);
460: //get "activity" nodes
461: NodeList activitiesNodeList = activitiesElement
462: .getElementsByTagName("activity");
463: int activitiesSize = activitiesNodeList
464: .getLength();
465: if (activitiesSize == 0) {
466: System.out
467: .println("WARNING: RepositoryFacade ==> Activity "
468: + activityName
469: + "not found");
470: break;
471: } else {
472: for (int j = 0; j < activitiesNodeList
473: .getLength(); j++) {
474: Element activityElement = (Element) activitiesNodeList
475: .item(j);
476: NodeList aElementsName = activityElement
477: .getElementsByTagName("activityname");
478: Text aNameElement = (Text) aElementsName
479: .item(0).getFirstChild();
480: String aName = aNameElement
481: .getNodeValue();
482:
483: if (aName.equals(activityName)) {
484: //if activity name has been found in repository, remove activity node
485: activitiesElement
486: .removeChild(activityElement);
487: //System.out.println("RepositoryFacade ==> Activity " + activityName + "removed from repository");
488: break;
489: }
490: }
491: }
492: }
493: }
494: }
495: }
496: //save repository file
497: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
498: FileOutputStream output = new FileOutputStream(this .path);
499: OutputFormat of = new OutputFormat("XML",
500: FormGeneratorConstant.CHARACTER_ENCODING, true);
501: of.setIndent(1);
502: of.setIndenting(true);
503: XMLSerializer serializer = new XMLSerializer(output, of);
504: serializer.asDOMSerializer();
505: serializer.serialize(this .xmlDocument.getDocumentElement());
506: output.close();
507: }
508:
509: /**
510: * Sets the path of the XForm file for a project.
511: * @param projectName: the project's name
512: * @param projectVersion: the project's version
513: * @throws Exception
514: */
515: public void setProjectFormPath(String projectName,
516: String projectVersion) throws Exception {
517: String formPath = null;
518:
519: //get "project" nodes
520: NodeList nodeList = this .xmlDocument
521: .getElementsByTagName("project");
522: int size = nodeList.getLength();
523: // case No project
524: if (size == 0) {
525: //if does not exist any project in repository create new project node
526: Element root = this .xmlDocument.getDocumentElement();
527: Element project = this .xmlDocument.createElement("project");
528: //projectname element
529: Element projectname = this .xmlDocument
530: .createElement("projectname");
531: Text projectnameText = this .xmlDocument
532: .createTextNode(projectName);
533: projectname.appendChild(projectnameText);
534: project.appendChild(projectname);
535: //MBL: projectversion element
536: Element projectversion = this .xmlDocument
537: .createElement("projectversion");
538: Text projectversionText = this .xmlDocument
539: .createTextNode(projectVersion);
540: projectversion.appendChild(projectversionText);
541: project.appendChild(projectversion);
542:
543: Element activities = this .xmlDocument
544: .createElement("activities");
545: project.appendChild(activities);
546: Element projectxformfile = this .xmlDocument
547: .createElement("projectxformfile");
548: Text projectxformfileText = this .xmlDocument
549: .createTextNode("xform.xhtml");
550: projectxformfile.appendChild(projectxformfileText);
551: project.appendChild(projectxformfile);
552: root.appendChild(project);
553: } else {
554: //search projectName
555: boolean exist = false;
556: for (int i = 0; i < nodeList.getLength(); i++) {
557: Element projectElement = (Element) nodeList.item(i);
558: //get project name
559: NodeList elementsName = projectElement
560: .getElementsByTagName("projectname");
561: Text nameElement = (Text) elementsName.item(0)
562: .getFirstChild();
563: String name = nameElement.getNodeValue();
564:
565: if (name.equals(projectName)) {
566: //get project version
567: NodeList elementsVersion = projectElement
568: .getElementsByTagName("projectversion");
569: Text versionElement = (Text) elementsVersion
570: .item(0).getFirstChild();
571: String version = versionElement.getNodeValue();
572:
573: if (version.equals(projectVersion)) {
574:
575: NodeList elementsXformfile = projectElement
576: .getElementsByTagName("projectxformfile");
577: setStringValue(elementsXformfile.item(0),
578: "xform.xhtml");
579: //Text xformFileElement = (Text)elementsXformfile.item(0).getFirstChild();
580: //xformFileElement.setNodeValue("xform.xhtml");
581: exist = true;
582: }
583: }
584:
585: }
586: //if project name has not been found in repository, create new project node
587: if (!exist) {
588: Element root = this .xmlDocument.getDocumentElement();
589: Element project = this .xmlDocument
590: .createElement("project");
591: //projectname element
592: Element projectname = this .xmlDocument
593: .createElement("projectname");
594: Text projectnameText = this .xmlDocument
595: .createTextNode(projectName);
596: projectname.appendChild(projectnameText);
597: project.appendChild(projectname);
598: //MBL: projectversion element
599: Element projectversion = this .xmlDocument
600: .createElement("projectversion");
601: Text projectversionText = this .xmlDocument
602: .createTextNode(projectVersion);
603: projectversion.appendChild(projectversionText);
604: project.appendChild(projectversion);
605:
606: Element activities = this .xmlDocument
607: .createElement("activities");
608: project.appendChild(activities);
609: Element projectxformfile = this .xmlDocument
610: .createElement("projectxformfile");
611: Text projectxformfileText = this .xmlDocument
612: .createTextNode("xform.xhtml");
613: projectxformfile.appendChild(projectxformfileText);
614: project.appendChild(projectxformfile);
615: root.appendChild(project);
616: }
617: }
618: //save repository file
619: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
620: FileOutputStream output = new FileOutputStream(this .path);
621: OutputFormat of = new OutputFormat("XML",
622: FormGeneratorConstant.CHARACTER_ENCODING, true);
623: of.setIndent(1);
624: of.setIndenting(true);
625: XMLSerializer serializer = new XMLSerializer(output, of);
626: serializer.asDOMSerializer();
627: serializer.serialize(this .xmlDocument.getDocumentElement());
628: output.close();
629:
630: ///String xml = toPrettyXmlString(this.xmlDocument);
631: ///BufferedWriter br = new BufferedWriter(new OutputStreamWriter(output));
632: //br.write(xml,0,xml.length());
633: //br.close();
634: }
635:
636: /**
637: * Sets the path of the XForm file for an activity.
638: * @param projectName: the project's name
639: * @param projectVersion: the project's version
640: * @param activityName: the activity's name
641: * @throws Exception
642: */
643: public void setActivityFormPath(String projectName,
644: String projectVersion, String activityName)
645: throws Exception {
646: String formPath = null;
647:
648: //get "project" nodes
649: NodeList nodeList = this .xmlDocument
650: .getElementsByTagName("project");
651: int size = nodeList.getLength();
652: if (size == 0) {
653: //if does not exist any project in repository create new project node
654: Element root = this .xmlDocument.getDocumentElement();
655: Element project = this .xmlDocument.createElement("project");
656: //projectname
657: Element projectname = this .xmlDocument
658: .createElement("projectname");
659: Text projectnameText = this .xmlDocument
660: .createTextNode(projectName);
661: projectname.appendChild(projectnameText);
662: project.appendChild(projectname);
663: //projectversion element
664: Element projectversion = this .xmlDocument
665: .createElement("projectversion");
666: Text projectversionText = this .xmlDocument
667: .createTextNode(projectVersion);
668: projectversion.appendChild(projectversionText);
669: project.appendChild(projectversion);
670: //activities
671: Element activities = this .xmlDocument
672: .createElement("activities");
673: //activity
674: Element activity = this .xmlDocument
675: .createElement("activity");
676: Element activityname = this .xmlDocument
677: .createElement("activityname");
678: Text activitynameText = this .xmlDocument
679: .createTextNode(activityName);
680: activityname.appendChild(activitynameText);
681: activity.appendChild(activityname);
682: Element activityxformfile = this .xmlDocument
683: .createElement("activityxformfile");
684: Text activityxformfileText = this .xmlDocument
685: .createTextNode("xform.xhtml");
686: activityxformfile.appendChild(activityxformfileText);
687: activity.appendChild(activityxformfile);
688: activities.appendChild(activity);
689: project.appendChild(activities);
690: //end acitivity
691: //project xformfile
692: Element projectxformfile = this .xmlDocument
693: .createElement("projectxformfile");
694: project.appendChild(projectxformfile);
695: root.appendChild(project);
696: } else {
697: //search project name
698: boolean projectExist = false;
699: boolean activityExist = false;
700: for (int i = 0; i < nodeList.getLength(); i++) {
701: Element projectElement = (Element) nodeList.item(i);
702: NodeList pElementsName = projectElement
703: .getElementsByTagName("projectname");
704: Text pNameElement = (Text) pElementsName.item(0)
705: .getFirstChild();
706: String pName = pNameElement.getNodeValue();
707:
708: if (pName.equals(projectName)) {
709:
710: // if project version has been found
711: NodeList elementsVersion = projectElement
712: .getElementsByTagName("projectversion");
713: Text versionElement = (Text) elementsVersion
714: .item(0).getFirstChild();
715: String version = versionElement.getNodeValue();
716:
717: if (version.equals(projectVersion)) {
718:
719: //if project name has been found in repository, lookup its activities
720: NodeList elementsActivities = projectElement
721: .getElementsByTagName("activities");
722: Element activitiesElement = (Element) elementsActivities
723: .item(0);
724: //get "activity" nodes
725: NodeList activitiesNodeList = activitiesElement
726: .getElementsByTagName("activity");
727: int activitiesSize = activitiesNodeList
728: .getLength();
729: if (activitiesSize == 0) {
730: //if does not exist any activity in repository create new activity node
731: Element activity = this .xmlDocument
732: .createElement("activity");
733: Element activityname = this .xmlDocument
734: .createElement("activityname");
735: Text activitynameText = this .xmlDocument
736: .createTextNode(activityName);
737: activityname.appendChild(activitynameText);
738: activity.appendChild(activityname);
739: Element activityxformfile = this .xmlDocument
740: .createElement("activityxformfile");
741: Text activityxformfileText = this .xmlDocument
742: .createTextNode("xform.xhtml");
743: activityxformfile
744: .appendChild(activityxformfileText);
745: activity.appendChild(activityxformfile);
746: activitiesElement.appendChild(activity);
747: activityExist = true;
748: } else {
749: for (int j = 0; j < activitiesNodeList
750: .getLength(); j++) {
751: Element activityElement = (Element) activitiesNodeList
752: .item(j);
753: NodeList aElementsName = activityElement
754: .getElementsByTagName("activityname");
755: Text aNameElement = (Text) aElementsName
756: .item(0).getFirstChild();
757: String aName = aNameElement
758: .getNodeValue();
759:
760: if (aName.equals(activityName)) {
761: activityExist = true;
762: }
763: }
764: if (!activityExist) {
765: //if does not exist this activity in repository create new activity node
766: Element activity = this .xmlDocument
767: .createElement("activity");
768: Element activityname = this .xmlDocument
769: .createElement("activityname");
770: Text activitynameText = this .xmlDocument
771: .createTextNode(activityName);
772: activityname
773: .appendChild(activitynameText);
774: activity.appendChild(activityname);
775: Element activityxformfile = this .xmlDocument
776: .createElement("activityxformfile");
777: Text activityxformfileText = this .xmlDocument
778: .createTextNode("xform.xhtml");
779: activityxformfile
780: .appendChild(activityxformfileText);
781: activity.appendChild(activityxformfile);
782: activitiesElement.appendChild(activity);
783: activityExist = true;
784: }
785: }
786: projectExist = true;
787: }
788: }
789: }
790: if (!projectExist) {
791: //if does not exist this project in repository create new project node
792: Element root = this .xmlDocument.getDocumentElement();
793: Element project = this .xmlDocument
794: .createElement("project");
795: //projectname
796: Element projectname = this .xmlDocument
797: .createElement("projectname");
798: Text projectnameText = this .xmlDocument
799: .createTextNode(projectName);
800: projectname.appendChild(projectnameText);
801: project.appendChild(projectname);
802: //projectversion element
803: Element projectversion = this .xmlDocument
804: .createElement("projectversion");
805: Text projectversionText = this .xmlDocument
806: .createTextNode(projectVersion);
807: projectversion.appendChild(projectversionText);
808: project.appendChild(projectversion);
809: //activities
810: Element activities = this .xmlDocument
811: .createElement("activities");
812: //activity
813: Element activity = this .xmlDocument
814: .createElement("activity");
815: Element activityname = this .xmlDocument
816: .createElement("activityname");
817: Text activitynameText = this .xmlDocument
818: .createTextNode(activityName);
819: activityname.appendChild(activitynameText);
820: activity.appendChild(activityname);
821: Element activityxformfile = this .xmlDocument
822: .createElement("activityxformfile");
823: Text activityxformfileText = this .xmlDocument
824: .createTextNode("xform.xhtml");
825: activityxformfile.appendChild(activityxformfileText);
826: activity.appendChild(activityxformfile);
827: activities.appendChild(activity);
828: project.appendChild(activities);
829: //end acitivity
830: //project xformfile
831: Element projectxformfile = this .xmlDocument
832: .createElement("projectxformfile");
833: project.appendChild(projectxformfile);
834: root.appendChild(project);
835: }
836: }
837: //save repository file
838: //System.out.println("RepositoryFacade ==> SAVE PATH = " + this.path);
839: FileOutputStream output = new FileOutputStream(this .path);
840: OutputFormat of = new OutputFormat("XML",
841: FormGeneratorConstant.CHARACTER_ENCODING, true);
842: of.setIndent(1);
843: of.setIndenting(true);
844: XMLSerializer serializer = new XMLSerializer(output, of);
845: serializer.asDOMSerializer();
846: serializer.serialize(this .xmlDocument.getDocumentElement());
847: output.close();
848:
849: //FileOutputStream output = new FileOutputStream(this.path);
850: //String xml = toPrettyXmlString(this.xmlDocument);
851: //BufferedWriter br = new BufferedWriter(new OutputStreamWriter(output));
852: //br.write(xml,0,xml.length());
853: //br.close();
854: }
855:
856: private static String toPrettyXmlString(Document doc) {
857: StringBuffer buffer = new StringBuffer(200);
858: toPrettyXmlString(doc.getDocumentElement(), 0, buffer);
859: return buffer.toString();
860: }
861:
862: private static void toPrettyXmlString(Node elmnt, int level,
863: StringBuffer buffer) {
864: String piece;
865: int i;
866:
867: buffer.append("<?xml version=\"1.0\" encoding=\""
868: + FormGeneratorConstant.DEBUG_OUTPUT_FORMAT_ENCODING
869: + "\"?>");
870: buffer.append("\n");
871:
872: for (i = 0; i < level; i++)
873: buffer.append(" ");
874:
875: buffer.append("<");
876:
877: buffer.append(elmnt.getNodeName());
878: NamedNodeMap atts = elmnt.getAttributes();
879: Node node;
880:
881: for (i = 0; i < atts.getLength(); i++) {
882: node = atts.item(i);
883: piece = " " + node.getNodeName() + "=\""
884: + node.getNodeValue() + "\"";
885:
886: if ((buffer.length() - (buffer.toString().lastIndexOf('\n') + 1))
887: + piece.length() > 78) {
888: buffer.append("\n");
889: for (int j = 0; j < (level + 1); j++)
890: buffer.append(" ");
891: }
892:
893: buffer.append(piece);
894: }
895:
896: // Process child nodes:
897:
898: NodeList kids = elmnt.getChildNodes();
899:
900: int n = kids.getLength();
901:
902: Node kid;
903:
904: boolean needIndent = false;
905:
906: buffer.append(n < 1 ? "/>" : ">");
907:
908: //if (n > 1) xml += "\n";
909: if (!(elmnt.getFirstChild() instanceof Text)) {
910: // xml += "\n";
911: }
912:
913: for (i = 0; i < n; i++) {
914: kid = kids.item(i);
915:
916: if (kid instanceof Element) {
917: needIndent = true;
918: buffer.append("\n");
919: toPrettyXmlString((Element) kid, level + 1, buffer);
920: } else if (kid instanceof Text) {
921: buffer.append(kid.getNodeValue());
922: } else if (kid instanceof Comment) {
923: if (i > 0 && kids.item(i - 1) instanceof Text) {
924: if (kids.item(i - 1).getNodeValue().trim().length() < 1) {
925: buffer.append(kids.item(i - 1).getNodeValue());
926: }
927: }
928: buffer.append("<!--" + kid.getNodeValue() + "-->");
929: } else {
930: //System.out.println(kid.getClass().getName());
931: }
932: }
933:
934: // End-tag:
935: if (n > 0) {
936: if (n > 1
937: || (n == 1 && !(elmnt.getFirstChild() instanceof Text)))
938: buffer.append("\n");
939:
940: if (needIndent)
941: for (i = 0; i < level; i++)
942: buffer.append(" ");
943:
944: buffer.append("</");
945:
946: buffer.append(elmnt.getNodeName() + ">");
947: }
948: }
949:
950: private static void setStringValue(Node node, String value) {
951: if (node == null) {
952: System.out.println("WARNING: No node .....");
953: } else if (node instanceof Attr) {
954: node.setNodeValue(value);
955: } else {
956: Node n;
957: NodeList nodes = node.getChildNodes();
958:
959: for (int i = 0; i < nodes.getLength(); i++) {
960: n = nodes.item(i);
961:
962: if (n instanceof Text) {
963: n.setNodeValue(value);
964: return;
965: }
966: }
967:
968: Document document = node.getOwnerDocument();
969: Text text = document.createTextNode(value);
970: node.appendChild(text);
971: }
972: }
973:
974: }
|