001: /*
002: * Created on September 2004, the 16th
003: * By the Message Center Team
004: */
005: package mc.formgenerator.servlets.bonita;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010: import java.io.PrintWriter;
011: import java.io.StringReader;
012: import java.io.StringWriter;
013:
014: import javax.servlet.http.*;
015: import javax.xml.parsers.DocumentBuilder;
016: import javax.xml.parsers.DocumentBuilderFactory;
017:
018: import mc.formgenerator.servlets.util.FormGeneratorConstant;
019: import org.apache.xerces.dom.DOMImplementationImpl;
020: import org.apache.xerces.parsers.DOMParser;
021: import org.apache.xml.serialize.DOMSerializer;
022: import org.apache.xml.serialize.OutputFormat;
023: import org.apache.xml.serialize.XMLSerializer;
024: import org.w3c.dom.Attr;
025: import org.w3c.dom.Comment;
026: import org.w3c.dom.DOMImplementation;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NamedNodeMap;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.Text;
033: import org.xml.sax.InputSource;
034:
035: /**
036: * Servlet for XForm Editor to get or write XForm documents
037: *
038: */
039: public class ServletXformEditor extends HttpServlet {
040:
041: private RepositoryFacade repositoryFacade = null;
042: private String contextPath = null;
043: private String projectName = null;
044: private String projectVersion = null;
045: private String activityName = null;
046: private String lang1 = null;
047: private String lang2 = null;
048: private String lang3 = null;
049:
050: /**
051: * Post request
052: * @throws IOException
053: */
054: public void doPost(HttpServletRequest req, HttpServletResponse res)
055: throws IOException {
056:
057: //Get the session of the user.
058: //HttpSession session = req.getSession(true);
059:
060: // set content-type and charset for the request/response
061: res.setContentType("text/html; charset="
062: + FormGeneratorConstant.CHARACTER_ENCODING);
063: req
064: .setCharacterEncoding(FormGeneratorConstant.CHARACTER_ENCODING);
065:
066: //get parameters
067: String xmlRequest = req.getParameter("xml");
068: this .projectName = req.getParameter("projectname");
069: this .projectVersion = req.getParameter("projectversion");
070: this .activityName = req.getParameter("activityname");
071: this .lang1 = req.getParameter("lang1");
072: this .lang2 = req.getParameter("lang2");
073: this .lang3 = req.getParameter("lang3");
074:
075: this .contextPath = getServletConfig().getServletContext()
076: .getRealPath("");
077:
078: PrintWriter out = res.getWriter();
079:
080: try {
081: //Create the repository facade corresponding to this controller
082: this .repositoryFacade = new RepositoryFacade(
083: getServletConfig().getServletContext().getRealPath(
084: "")
085: + File.separator
086: + "web"
087: + File.separator
088: + "repository.xml");
089: if ((repositoryFacade == null))
090: System.out
091: .println("ERROR: ServletXformEditor - Invalid session");
092: else {
093: if (xmlRequest.equals("")) {
094: Document xmlResponse = createXmlResponse(xmlRequest);
095: //System.out.println("ServletXformEditor ==> XML Response = " + toXmlString(xmlResponse));
096: String xmlResponseString = toXmlString(xmlResponse);
097: //clean response
098: //xmlResponseString = xmlResponseString.replaceAll("\n","");
099: //xmlResponseString = xmlResponseString.replaceAll("\t","");
100: //System.out.println("ServletXformEditor ==> CLEANED XML Response = " + xmlResponseString);
101: out.print(xmlResponseString);
102: } else {
103: //build documents, save them and update repository
104: String response = writeDocuments(xmlRequest);
105: out.print(response);
106: }
107: }
108: } catch (Exception e) {
109: e.printStackTrace();
110: res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
111: e.getMessage());
112: }
113: }
114:
115: // lookup in repository and build xmlresponse
116: /** 17-05-2005
117: * Lookup in repository and build xmlresponse
118: * <p/>
119: *
120: * @param xmlRequest xml string with xform and languages documents.
121: */
122: public Document createXmlResponse(String xmlRequest) {
123:
124: Document xmlResponse = null; //XML Document with XForm and Languages
125: try {
126: DOMImplementation domImpl = new DOMImplementationImpl();
127:
128: //XForm Document creation with the root element called servletResponse
129: //System.out.println("ServletXformEditor ==> Creating xform document...");
130: xmlResponse = domImpl.createDocument(null,
131: "servletResponse", null);
132: Element root = xmlResponse.getDocumentElement();
133:
134: String realProjectName = this .projectName.replaceFirst(
135: "_instance.+", "");
136: String formPath = null;
137: String langPath = null;
138: String projectLangPath = null;
139: //lookup for a formPath
140: if (this .activityName.equals("")) {
141: //working with project
142: formPath = repositoryFacade.getProjectFormPath(
143: realProjectName, this .projectVersion);
144: langPath = File.separator + "web" + File.separator
145: + "xforms" + File.separator + realProjectName
146: + File.separator + this .projectVersion
147: + File.separator;
148: } else {
149: //working with activity
150: formPath = repositoryFacade.getActivityFormPath(
151: realProjectName, activityName,
152: this .projectVersion);
153: //versionning xforms : adding the projectVersion
154: langPath = File.separator + "web" + File.separator
155: + "xforms" + File.separator + realProjectName
156: + File.separator + this .projectVersion
157: + File.separator + this .activityName
158: + File.separator;
159:
160: //langPath = File.separator + "web" + File.separator + "xforms" + File.separator + realProjectName + File.separator + this.activityName + File.separator;
161: //if exist xform file for activity's project, get project languages path
162: String tmp = repositoryFacade.getProjectFormPath(
163: realProjectName, this .projectVersion);
164: if (tmp != null) {
165: projectLangPath = File.separator + "web"
166: + File.separator + "xforms"
167: + File.separator + realProjectName
168: + File.separator + this .projectVersion
169: + File.separator;
170: }
171: }
172:
173: //documentBuilder to get documents
174: DocumentBuilderFactory factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
175: DocumentBuilder documentBuilder = factory
176: .newDocumentBuilder();
177: if (formPath != null) {
178: //XForm file exist. Get files, parse them to xml Documents and build xmlResponse
179:
180: //xform
181: File xformFile = new File(this .contextPath + formPath);
182: Document xformDocument = documentBuilder
183: .parse(xformFile);
184: //Element xform = xmlResponse.createElement("xform");
185: Node html = xformDocument.getDocumentElement();
186: root.appendChild(xmlResponse.importNode(html, true));
187:
188: //lang1
189: File lang1File = new File(this .contextPath + langPath
190: + this .lang1 + ".xml");
191: Document lang1Document = documentBuilder
192: .parse(lang1File);
193: //Element lang1 = xmlResponse.createElement("lang1");
194: Node lang1 = lang1Document.getDocumentElement();
195: if (projectLangPath != null) {
196: //We are working with an activity whose project has xform file.
197: //Include project name into language xml.
198: //If string project name exist, modify it (maybe it has been changed!).
199: File lang1ProjectFile = new File(this .contextPath
200: + projectLangPath + this .lang1 + ".xml");
201: Document lang1ProjectDocument = documentBuilder
202: .parse(lang1ProjectFile);
203: Node lang1Project = lang1ProjectDocument
204: .getDocumentElement();
205: NodeList projectNodeList = lang1Project
206: .getChildNodes();
207: int i = 0;
208: int m = projectNodeList.getLength();
209: boolean ok = false;
210: Node projectStringsNode = projectNodeList.item(i);
211: while (i < m && !ok) {
212: if (getAttribute(projectStringsNode, "id") != null) {
213: if ((getAttribute(projectStringsNode, "id"))
214: .equalsIgnoreCase(realProjectName)) {
215: //get old project label node and change it to new value
216: Node oldprojectStringsNode = getNodeById(
217: lang1, realProjectName);
218: if (oldprojectStringsNode != null) {
219: setStringValue(
220: oldprojectStringsNode,
221: getStringValue(projectStringsNode));
222: ok = true;
223: }
224: } else {
225: i++;
226: projectStringsNode = projectNodeList
227: .item(i);
228: }
229: } else {
230: i++;
231: projectStringsNode = projectNodeList
232: .item(i);
233: }
234: }
235: if (!ok) {
236: System.out
237: .println("ServletXformEditor ==> ERROR: project name not found in "
238: + this .lang1 + ".");
239: }
240: }
241: root.appendChild(xmlResponse.importNode(lang1, true));
242:
243: //lang2
244: File lang2File = new File(this .contextPath + langPath
245: + this .lang2 + ".xml");
246: Document lang2Document = documentBuilder
247: .parse(lang2File);
248: //Element lang2 = xmlResponse.createElement("lang2");
249: Node lang2 = lang2Document.getDocumentElement();
250: if (projectLangPath != null) {
251: //We are working with an activity whose project has xform file.
252: //Include project name into language xml.
253: //If string project name exist, modify it (maybe it has been changed!).
254: File lang2ProjectFile = new File(this .contextPath
255: + projectLangPath + this .lang2 + ".xml");
256: Document lang2ProjectDocument = documentBuilder
257: .parse(lang2ProjectFile);
258: Node lang2Project = lang2ProjectDocument
259: .getDocumentElement();
260: NodeList projectNodeList = lang2Project
261: .getChildNodes();
262: int i = 0;
263: int m = projectNodeList.getLength();
264: boolean ok = false;
265: Node projectStringsNode = projectNodeList.item(i);
266: while (i < m && !ok) {
267: if (getAttribute(projectStringsNode, "id") != null) {
268: if ((getAttribute(projectStringsNode, "id"))
269: .equalsIgnoreCase(realProjectName)) {
270: //get old project label node and change it to new value
271: Node oldprojectStringsNode = getNodeById(
272: lang2, realProjectName);
273: if (oldprojectStringsNode != null) {
274: setStringValue(
275: oldprojectStringsNode,
276: getStringValue(projectStringsNode));
277: ok = true;
278: }
279: } else {
280: i++;
281: projectStringsNode = projectNodeList
282: .item(i);
283: }
284: } else {
285: i++;
286: projectStringsNode = projectNodeList
287: .item(i);
288: }
289: }
290: if (!ok) {
291: System.out
292: .println("ServletXformEditor ==> ERROR: project name not found in "
293: + this .lang1 + ".");
294: }
295: }
296: root.appendChild(xmlResponse.importNode(lang2, true));
297:
298: //lang3
299: File lang3File = new File(this .contextPath + langPath
300: + this .lang3 + ".xml");
301: Document lang3Document = documentBuilder
302: .parse(lang3File);
303: //Element lang3 = xmlResponse.createElement("lang3");
304: Node lang3 = lang3Document.getDocumentElement();
305: if (projectLangPath != null) {
306: //We are working with an activity whose project has xform file.
307: //Include project name into language xml.
308: //If string project name exist, modify it (maybe it has been changed!).
309: File lang3ProjectFile = new File(this .contextPath
310: + projectLangPath + this .lang3 + ".xml");
311: Document lang3ProjectDocument = documentBuilder
312: .parse(lang3ProjectFile);
313: Node lang3Project = lang3ProjectDocument
314: .getDocumentElement();
315: NodeList projectNodeList = lang3Project
316: .getChildNodes();
317: int i = 0;
318: int m = projectNodeList.getLength();
319: boolean ok = false;
320: Node projectStringsNode = projectNodeList.item(i);
321: while (i < m && !ok) {
322: if (getAttribute(projectStringsNode, "id") != null) {
323: if ((getAttribute(projectStringsNode, "id"))
324: .equalsIgnoreCase(realProjectName)) {
325: //get old project label node and change it to new value
326: Node oldprojectStringsNode = getNodeById(
327: lang3, realProjectName);
328: if (oldprojectStringsNode != null) {
329: setStringValue(
330: oldprojectStringsNode,
331: getStringValue(projectStringsNode));
332: ok = true;
333: }
334: } else {
335: i++;
336: projectStringsNode = projectNodeList
337: .item(i);
338: }
339: } else {
340: i++;
341: projectStringsNode = projectNodeList
342: .item(i);
343: }
344: }
345: if (!ok) {
346: System.out
347: .println("ServletXformEditor ==> ERROR: project name not found in "
348: + this .lang1 + ".");
349: }
350: }
351: root.appendChild(xmlResponse.importNode(lang3, true));
352: //formPath = "/web/xforms/"+formPath;
353:
354: } else {
355: //XForm file does not exist
356: //we must pass language files with project properties labels
357: //lang1
358: Element lang1 = xmlResponse.createElement("strings");
359: if (projectLangPath != null) {
360: //include project name into language xml
361: File lang1ProjectFile = new File(this .contextPath
362: + projectLangPath + this .lang1 + ".xml");
363: Document lang1ProjectDocument = documentBuilder
364: .parse(lang1ProjectFile);
365: Node lang1Project = lang1ProjectDocument
366: .getDocumentElement();
367: NodeList nodeList = lang1Project.getChildNodes();
368: //20-06-2005 copy childs
369: for (int i = 0; i < nodeList.getLength(); i++) {
370: lang1.appendChild(xmlResponse.importNode(
371: nodeList.item(i), true));
372: }
373: /*
374: int i = 0;
375: int m = nodeList.getLength();
376: boolean ok = false;
377: Node stringsNode = nodeList.item(i);
378: while(i<m && !ok){
379: if(getAttribute(stringsNode,"id") != null){
380: if((getAttribute(stringsNode,"id")).equalsIgnoreCase(realProjectName)){
381: //lang1.appendChild(stringsNode.cloneNode(true));
382: lang1.appendChild(xmlResponse.importNode(stringsNode, true));
383: ok = true;
384: }else{
385: i++;
386: stringsNode = nodeList.item(i);
387: }
388: }else{
389: i++;
390: stringsNode = nodeList.item(i);
391: }
392: }
393: if(!ok){
394: Element projectLabel = xmlResponse.createElement("string");
395: projectLabel.setAttribute("id",realProjectName);
396: Text projectLabelText = xmlResponse.createTextNode(realProjectName);
397: projectLabel.appendChild(projectLabelText);
398: lang1.appendChild(projectLabel);
399: }
400: */
401:
402: } else {
403: Element projectLabel = xmlResponse
404: .createElement("string");
405: projectLabel.setAttribute("id", realProjectName);
406: Text projectLabelText = xmlResponse
407: .createTextNode(realProjectName);
408: projectLabel.appendChild(projectLabelText);
409: lang1.appendChild(projectLabel);
410: }
411: root.appendChild(lang1);
412:
413: //lang2
414: Element lang2 = xmlResponse.createElement("strings");
415: if (projectLangPath != null) {
416: //include project name into language xml
417: //if string project name exist, modify it (maybe it has been changed)
418: File lang2ProjectFile = new File(this .contextPath
419: + projectLangPath + this .lang2 + ".xml");
420: Document lang2ProjectDocument = documentBuilder
421: .parse(lang2ProjectFile);
422: Node lang2Project = lang2ProjectDocument
423: .getDocumentElement();
424: NodeList nodeList = lang2Project.getChildNodes();
425: //20-06-2005 copy childs
426: for (int i = 0; i < nodeList.getLength(); i++) {
427: lang2.appendChild(xmlResponse.importNode(
428: nodeList.item(i), true));
429: }
430: /*
431: int i = 0;
432: int m = nodeList.getLength();
433: boolean ok = false;
434: Node stringsNode = nodeList.item(i);
435: while(i<m && !ok){
436: if(getAttribute(stringsNode,"id") != null){
437: if((getAttribute(stringsNode,"id")).equalsIgnoreCase(realProjectName)){
438: //lang2.appendChild(stringsNode.cloneNode(true));
439: lang2.appendChild(xmlResponse.importNode(stringsNode, true));
440: ok = true;
441: }else{
442: i++;
443: stringsNode = nodeList.item(i);
444: }
445: }else{
446: i++;
447: stringsNode = nodeList.item(i);
448: }
449: }
450: if(!ok){
451: Element projectLabel = xmlResponse.createElement("string");
452: projectLabel.setAttribute("id",realProjectName);
453: Text projectLabelText = xmlResponse.createTextNode(realProjectName);
454: projectLabel.appendChild(projectLabelText);
455: lang2.appendChild(projectLabel);
456: }
457: */
458:
459: } else {
460: Element projectLabel = xmlResponse
461: .createElement("string");
462: projectLabel.setAttribute("id", realProjectName);
463: Text projectLabelText = xmlResponse
464: .createTextNode(realProjectName);
465: projectLabel.appendChild(projectLabelText);
466: lang2.appendChild(projectLabel);
467: }
468: root.appendChild(lang2);
469:
470: //lang3
471: Element lang3 = xmlResponse.createElement("strings");
472: if (projectLangPath != null) {
473: //include project name into language xml
474: //if string project name exist, modify it (maybe it has been changed)
475: File lang3ProjectFile = new File(this .contextPath
476: + projectLangPath + this .lang3 + ".xml");
477: Document lang3ProjectDocument = documentBuilder
478: .parse(lang3ProjectFile);
479: Node lang3Project = lang3ProjectDocument
480: .getDocumentElement();
481: NodeList nodeList = lang3Project.getChildNodes();
482: //20-06-2005 copy childs
483: for (int i = 0; i < nodeList.getLength(); i++) {
484: lang3.appendChild(xmlResponse.importNode(
485: nodeList.item(i), true));
486: }
487: /*
488: int i = 0;
489: int m = nodeList.getLength();
490: boolean ok = false;
491: Node stringsNode = nodeList.item(i);
492: while(i<m && !ok){
493: if(getAttribute(stringsNode,"id") != null){
494: if((getAttribute(stringsNode,"id")).equalsIgnoreCase(realProjectName)){
495: //lang3.appendChild(stringsNode.cloneNode(true));
496: lang3.appendChild(xmlResponse.importNode(stringsNode, true));
497: ok = true;
498: }else{
499: i++;
500: stringsNode = nodeList.item(i);
501: }
502: }else{
503: i++;
504: stringsNode = nodeList.item(i);
505: }
506: }
507: if(!ok){
508: Element projectLabel = xmlResponse.createElement("string");
509: projectLabel.setAttribute("id",realProjectName);
510: Text projectLabelText = xmlResponse.createTextNode(realProjectName);
511: projectLabel.appendChild(projectLabelText);
512: lang3.appendChild(projectLabel);
513: }
514: */
515:
516: } else {
517: Element projectLabel = xmlResponse
518: .createElement("string");
519: projectLabel.setAttribute("id", realProjectName);
520: Text projectLabelText = xmlResponse
521: .createTextNode(realProjectName);
522: projectLabel.appendChild(projectLabelText);
523: lang3.appendChild(projectLabel);
524: }
525: root.appendChild(lang3);
526:
527: }
528: } catch (Exception e) {
529: e.printStackTrace();
530: }
531: return xmlResponse;
532: }
533:
534: /** 17-05-2005
535: * This method builds xform and language documents and saves them
536: * in application/web/xforms/project-activity_name
537: * <p/>
538: *
539: * @param xmlRequest xml input Document.
540: */
541: public String writeDocuments(String xmlRequest) {
542: Document xformDocument = null;
543: Document instance = null;
544: Document lang1Document = null;
545: Document lang2Document = null;
546: Document lang3Document = null;
547: String realProjectName = this .projectName.replaceFirst(
548: "_instance.+", "");
549: //versionning for xforms
550: String projectVersion = this .projectVersion;
551: //System.out.println("ServletXformEditor ==> Writing xform documents...");
552: try {
553: //parse string to document
554: DOMParser parser = new DOMParser();
555: parser.parse(new InputSource(new StringReader(xmlRequest)));
556: Document xmlDocument = parser.getDocument();
557: Element root = xmlDocument.getDocumentElement();
558: NodeList rootChilds = root.getChildNodes();
559:
560: //build documents
561: DOMImplementation domImpl = new DOMImplementationImpl();
562: Element tmpRoot = null; //to build documents
563: NodeList tmpRootChilds = null; //to build documents
564: int i;
565:
566: //build xform document
567: xformDocument = domImpl.createDocument(null, "html", null);
568: Element xformRoot = xformDocument.getDocumentElement();
569: tmpRoot = (Element) rootChilds.item(0);
570: //set root attributes
571: NamedNodeMap map = tmpRoot.getAttributes();
572: for (i = 0; i < map.getLength(); i++) {
573: xformRoot.setAttribute(map.item(i).getNodeName(), map
574: .item(i).getNodeValue());
575: }
576: //copy childs
577: tmpRootChilds = tmpRoot.getChildNodes();
578: for (i = 0; i < tmpRootChilds.getLength(); i++) {
579: xformRoot.appendChild(xformDocument.importNode(
580: tmpRootChilds.item(i), true));
581: }
582:
583: //build instance document
584: instance = domImpl.createDocument(null, "process", null);
585: Element instanceRoot = instance.getDocumentElement();
586: tmpRoot = (Element) rootChilds.item(1);
587: //copy childs
588: tmpRootChilds = tmpRoot.getChildNodes();
589: for (i = 0; i < tmpRootChilds.getLength(); i++) {
590: instanceRoot.appendChild(instance.importNode(
591: tmpRootChilds.item(i), true));
592: }
593:
594: //build lang1 document
595: lang1Document = domImpl.createDocument(null, "strings",
596: null);
597: Element lang1Root = lang1Document.getDocumentElement();
598: tmpRoot = (Element) rootChilds.item(2);
599: //copy childs
600: tmpRootChilds = tmpRoot.getChildNodes();
601: for (i = 0; i < tmpRootChilds.getLength(); i++) {
602: lang1Root.appendChild(lang1Document.importNode(
603: tmpRootChilds.item(i), true));
604: }
605:
606: //build lang2 document
607: lang2Document = domImpl.createDocument(null, "strings",
608: null);
609: Element lang2Root = lang2Document.getDocumentElement();
610: tmpRoot = (Element) rootChilds.item(3);
611: //copy childs
612: tmpRootChilds = tmpRoot.getChildNodes();
613: for (i = 0; i < tmpRootChilds.getLength(); i++) {
614: lang2Root.appendChild(lang2Document.importNode(
615: tmpRootChilds.item(i), true));
616: }
617:
618: //build lang3 document
619: lang3Document = domImpl.createDocument(null, "strings",
620: null);
621: Element lang3Root = lang3Document.getDocumentElement();
622: tmpRoot = (Element) rootChilds.item(4);
623: //copy childs
624: tmpRootChilds = tmpRoot.getChildNodes();
625: for (i = 0; i < tmpRootChilds.getLength(); i++) {
626: lang3Root.appendChild(lang3Document.importNode(
627: tmpRootChilds.item(i), true));
628: }
629:
630: //save documents
631: //adding projectVersion sub directory
632: String path = null;
633: if (this .activityName.equals("")) {
634: this .repositoryFacade.setProjectFormPath(
635: realProjectName, projectVersion);
636: File directory = new File(this .contextPath
637: + File.separator + "web" + File.separator
638: + "xforms" + File.separator + realProjectName);
639: directory.mkdir();
640: //create the sub directory for the project version
641: directory = new File(this .contextPath + File.separator
642: + "web" + File.separator + "xforms"
643: + File.separator + realProjectName
644: + File.separator + projectVersion);
645: directory.mkdir();
646: path = this .contextPath + File.separator + "web"
647: + File.separator + "xforms" + File.separator
648: + realProjectName + File.separator
649: + projectVersion + File.separator;
650: //debug
651: //System.out.println("ServletXformEditor:writeDocuments() - path = " + path);
652: } else {
653: this .repositoryFacade.setActivityFormPath(
654: realProjectName, projectVersion,
655: this .activityName);
656: File directory = new File(this .contextPath
657: + File.separator + "web" + File.separator
658: + "xforms" + File.separator + realProjectName
659: + File.separator + projectVersion
660: + File.separator + this .activityName);
661: directory.mkdirs();
662: //create the sub directory for the project version
663: directory = new File(this .contextPath + File.separator
664: + "web" + File.separator + "xforms"
665: + File.separator + realProjectName
666: + File.separator + projectVersion
667: + File.separator + this .activityName);
668: directory.mkdir();
669:
670: path = this .contextPath + File.separator + "web"
671: + File.separator + "xforms" + File.separator
672: + realProjectName + File.separator
673: + projectVersion + File.separator
674: + this .activityName + File.separator;
675: }
676:
677: FileOutputStream output = null;
678: OutputFormat of = new OutputFormat("XML",
679: FormGeneratorConstant.CHARACTER_ENCODING, true);
680: of.setIndent(1);
681: of.setIndenting(true);
682: XMLSerializer serializer = null;
683: //BufferedWriter br = null;
684: String xml = null;
685: //xform
686: output = new FileOutputStream(path + "xform.xhtml");
687: serializer = new XMLSerializer(output, of);
688: serializer.asDOMSerializer();
689: serializer.serialize(xformDocument.getDocumentElement());
690: output.close();
691: //xml = toPrettyXmlString(xformDocument);
692: //br = new BufferedWriter(new OutputStreamWriter(output));
693: //br.write(xml,0,xml.length());
694: //br.close();
695: //instance
696: output = new FileOutputStream(path + "instance.xml");
697: serializer = new XMLSerializer(output, of);
698: serializer.asDOMSerializer();
699: serializer.serialize(instance.getDocumentElement());
700: output.close();
701: //lang1
702: output = new FileOutputStream(path + this .lang1 + ".xml");
703: serializer = new XMLSerializer(output, of);
704: serializer.asDOMSerializer();
705: serializer.serialize(lang1Document.getDocumentElement());
706: output.close();
707: //xml = toPrettyXmlString(lang1Document);
708: //br = new BufferedWriter(new OutputStreamWriter(output));
709: //br.write(xml,0,xml.length());
710: //br.close();
711: //lang2
712: output = new FileOutputStream(path + this .lang2 + ".xml");
713: serializer = new XMLSerializer(output, of);
714: serializer.asDOMSerializer();
715: serializer.serialize(lang2Document.getDocumentElement());
716: output.close();
717: //xml = toPrettyXmlString(lang2Document);
718: //br = new BufferedWriter(new OutputStreamWriter(output));
719: //br.write(xml,0,xml.length());
720: //br.close();
721: //lang3
722: output = new FileOutputStream(path + this .lang3 + ".xml");
723: serializer = new XMLSerializer(output, of);
724: serializer.asDOMSerializer();
725: serializer.serialize(lang3Document.getDocumentElement());
726: output.close();
727: //xml = toPrettyXmlString(lang3Document);
728: //br = new BufferedWriter(new OutputStreamWriter(output));
729: //br.write(xml,0,xml.length());
730: //br.close();
731: } catch (Exception e) {
732: e.printStackTrace();
733: }
734:
735: return ("ok");
736: }
737:
738: private static String toXmlString(Document document) {
739: try {
740: OutputFormat outputFormat = new OutputFormat(document,
741: FormGeneratorConstant.DEBUG_OUTPUT_FORMAT_ENCODING,
742: true);
743: outputFormat.setIndenting(true);
744: outputFormat.setIndent(1);
745: outputFormat.setPreserveSpace(true);
746: StringWriter writer = new StringWriter();
747: DOMSerializer serializer = new XMLSerializer(writer,
748: outputFormat);
749: serializer.serialize(document);
750: writer.close();
751: return writer.toString();
752: } catch (Exception ex) {
753: ex.printStackTrace();
754: }
755: return null;
756: }
757:
758: private static String toPrettyXmlString(Document doc) {
759: StringBuffer buffer = new StringBuffer(200);
760: toPrettyXmlString(doc.getDocumentElement(), 0, buffer);
761: return buffer.toString();
762: }
763:
764: private static void toPrettyXmlString(Node elmnt, int level,
765: StringBuffer buffer) {
766: String piece;
767: int i;
768:
769: buffer.append("<?xml version=\"1.0\" encoding=\""
770: + FormGeneratorConstant.DEBUG_OUTPUT_FORMAT_ENCODING
771: + "\"?>");
772: buffer.append("\n");
773:
774: for (i = 0; i < level; i++)
775: buffer.append(" ");
776:
777: buffer.append("<");
778:
779: buffer.append(elmnt.getNodeName());
780: NamedNodeMap atts = elmnt.getAttributes();
781: Node node;
782:
783: for (i = 0; i < atts.getLength(); i++) {
784: node = atts.item(i);
785: piece = " " + node.getNodeName() + "=\""
786: + node.getNodeValue() + "\"";
787:
788: if ((buffer.length() - (buffer.toString().lastIndexOf('\n') + 1))
789: + piece.length() > 78) {
790: buffer.append("\n");
791: for (int j = 0; j < (level + 1); j++)
792: buffer.append(" ");
793: }
794:
795: buffer.append(piece);
796: }
797:
798: // Process child nodes:
799:
800: NodeList kids = elmnt.getChildNodes();
801:
802: int n = kids.getLength();
803:
804: Node kid;
805:
806: boolean needIndent = false;
807:
808: buffer.append(n < 1 ? "/>" : ">");
809:
810: //if (n > 1) xml += "\n";
811: if (!(elmnt.getFirstChild() instanceof Text)) {
812: // xml += "\n";
813: }
814:
815: for (i = 0; i < n; i++) {
816: kid = kids.item(i);
817:
818: if (kid instanceof Element) {
819: needIndent = true;
820: buffer.append("\n");
821: toPrettyXmlString((Element) kid, level + 1, buffer);
822: } else if (kid instanceof Text) {
823: buffer.append(kid.getNodeValue());
824: } else if (kid instanceof Comment) {
825: if (i > 0 && kids.item(i - 1) instanceof Text) {
826: if (kids.item(i - 1).getNodeValue().trim().length() < 1) {
827: buffer.append(kids.item(i - 1).getNodeValue());
828: }
829: }
830: buffer.append("<!--" + kid.getNodeValue() + "-->");
831: } else {
832: System.out.println(kid.getClass().getName());
833: }
834: }
835:
836: // End-tag:
837: if (n > 0) {
838: if (n > 1
839: || (n == 1 && !(elmnt.getFirstChild() instanceof Text)))
840: buffer.append("\n");
841:
842: if (needIndent)
843: for (i = 0; i < level; i++)
844: buffer.append(" ");
845:
846: buffer.append("</");
847:
848: buffer.append(elmnt.getNodeName() + ">");
849: }
850: }
851:
852: private static String getAttribute(Node node, String attrName) {
853: if (node instanceof Element) {
854: if (((Element) node).getAttributeNode(attrName) == null) {
855: return null;
856: } else {
857: return ((Element) node).getAttribute(attrName);
858: }
859: }
860:
861: NamedNodeMap map = node.getAttributes();
862:
863: if (map == null)
864: return null;
865:
866: Node value = map.getNamedItem(attrName);
867:
868: return value != null ? value.getNodeValue() : null;
869: }
870:
871: private static String getStringValue(Node node) {
872: if (node == null) {
873: return null;
874: } else if (node instanceof Attr) {
875: return node.getNodeValue();
876: } else {
877: Node n;
878: NodeList nodes = node.getChildNodes();
879:
880: for (int i = 0; i < nodes.getLength(); i++) {
881: n = nodes.item(i);
882:
883: if (n instanceof Text) {
884: return n.getNodeValue();
885: }
886: }
887: }
888:
889: return null;
890: }
891:
892: private static void setStringValue(Node node, String value) {
893: if (node == null) {
894: System.out.println("Warning! No node...");
895: } else if (node instanceof Attr) {
896: node.setNodeValue(value);
897: } else {
898: Node n;
899: NodeList nodes = node.getChildNodes();
900:
901: for (int i = 0; i < nodes.getLength(); i++) {
902: n = nodes.item(i);
903:
904: if (n instanceof Text) {
905: n.setNodeValue(value);
906: return;
907: }
908: }
909:
910: Document document = node.getOwnerDocument();
911: Text text = document.createTextNode(value);
912: node.appendChild(text);
913: }
914: }
915:
916: private static Node getNodeById(Node lang1, String realProjectName) {
917: Node projectNameNode = null;
918:
919: NodeList nodeList = lang1.getChildNodes();
920: int i = 0;
921: int m = nodeList.getLength();
922: Node stringsNode = nodeList.item(i);
923: while (i < m) {
924: if (getAttribute(stringsNode, "id") != null) {
925: if ((getAttribute(stringsNode, "id"))
926: .equalsIgnoreCase(realProjectName)) {
927: return nodeList.item(i);
928: } else {
929: i++;
930: stringsNode = nodeList.item(i);
931: }
932: } else {
933: i++;
934: stringsNode = nodeList.item(i);
935: }
936: }
937: return null;
938: }
939:
940: }
|