KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > gui > generic > servlet > attribute > XmlForWordExportValue


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: XmlForWordExportValue.java,v 1.10 2005/05/05 23:06:36 wfro Exp $
5  * Description: openCRX application plugin
6  * Revision: $Revision: 1.10 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/05/05 23:06:36 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.gui.generic.servlet.attribute;
57
58 import java.io.BufferedReader JavaDoc;
59 import java.io.ByteArrayInputStream JavaDoc;
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.Serializable JavaDoc;
63 import java.io.StringReader JavaDoc;
64 import java.util.Map JavaDoc;
65 import java.util.zip.ZipInputStream JavaDoc;
66
67 import javax.servlet.http.HttpServletRequest JavaDoc;
68
69 import org.openmdx.application.gui.generic.servlet.ApplicationContext;
70 import org.openmdx.application.gui.generic.servlet.attribute.Attribute;
71 import org.openmdx.application.gui.generic.servlet.attribute.BinaryValue;
72 import org.openmdx.application.gui.generic.servlet.attribute.FieldDef;
73 import org.openmdx.application.gui.generic.servlet.view.View;
74 import org.openmdx.application.log.AppLog;
75 import org.openmdx.base.exception.ServiceException;
76 import org.openmdx.uses.java.lang.StringBuilder;
77
78 /**
79  * @author wfro
80  */

81 public class XmlForWordExportValue
82     extends BinaryValue
83     implements Serializable JavaDoc {
84
85     //-------------------------------------------------------------------------
86
public XmlForWordExportValue(
87         Object JavaDoc object,
88         FieldDef fieldDef,
89         ApplicationContext application
90     ) {
91         super(
92             object,
93             fieldDef,
94             application
95         );
96     }
97
98     //-------------------------------------------------------------------------
99
public String JavaDoc toHtml(
100         HttpServletRequest JavaDoc request,
101         View view,
102         Attribute attribute,
103         Map JavaDoc modifiers,
104         Map JavaDoc popupImages,
105         Map JavaDoc renderingContext
106     ) {
107         int currentDocId = renderingContext.get(XmlForWordExportValue.class.getName()) != null
108             ? ((Integer JavaDoc)renderingContext.get(XmlForWordExportValue.class.getName())).intValue()
109             : 0;
110         Map JavaDoc params = this.getMimeTypeParams();
111
112         String JavaDoc result = "";
113
114         result += "<script language=\"javascript\" type=\"text/javascript\">\n";
115         result += "<!--//\n";
116         result += "function openWordDoc" + currentDocId + "(boolVisible, strContextUrl, strDocPath, strMacroName, objectId) {\n";
117         result += " var WordApp = new ActiveXObject(\"Word.Application\");\n";
118         result += " if (WordApp != null) {\n";
119         result += " WordApp.Visible = boolVisible;\n";
120         result += " var WordDoc=WordApp.Documents.Add(strDocPath);\n";
121         result += " if (WordDoc == null) {\n";
122         result += " WordDoc = WordApp.Documents.Add(strContextUrl+strDocPath);\n";
123         result += " }\n";
124         result += " if (WordDoc != null) {\n";
125         result += " var el = getElement(objectId);\n";
126         result += " if (el) {\n";
127         result += " if (el.innerText) {\n";
128         result += " WordApp.Run(strMacroName, el.innerText);\n";
129         result += " }\n";
130         result += " }\n";
131         result += " }\n";
132         result += " }\n";
133         result += "}\n";
134         result += "\n";
135         result += "// -->\n";
136         result += "</script>\n";
137
138         // XmlForWordExportValue assumens that the binary value is a zip stream
139
// which contains the exported XML files. Append all files contained
140
// in the ZIP to one string
141
StringBuilder JavaDoc exportedXml = new StringBuilder JavaDoc();
142         try {
143             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
144             try {
145                 this.getBinaryValue(os);
146             }
147             catch(ServiceException e) {
148                 AppLog.warning(e.getMessage(), e.getCause(), 1);
149             }
150             ZipInputStream JavaDoc zip = new ZipInputStream JavaDoc(
151                 new ByteArrayInputStream JavaDoc(os.toByteArray())
152             );
153             while((zip.getNextEntry()) != null) {
154                 ByteArrayOutputStream JavaDoc xmlFile = new ByteArrayOutputStream JavaDoc();
155                 byte[] buffer = new byte[1024];
156                 int len = 0;
157                 while((len = zip.read(buffer)) != -1) {
158                     xmlFile.write(buffer, 0, len);
159                 }
160                 BufferedReader JavaDoc xmlReader = new BufferedReader JavaDoc(
161                     new StringReader JavaDoc(xmlFile.toString("UTF-8"))
162                 );
163                 String JavaDoc line = null;
164                 while((line = xmlReader.readLine()) != null) {
165                     exportedXml.append(line + "\n");
166                 }
167             }
168         }
169         catch(IOException JavaDoc e) {
170             AppLog.warning("can not read binary value. Is it a ZIP stream?");
171         }
172
173         String JavaDoc hostAndContextPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
174         result += "<td class=\"label\"><span class=\"nw\">" + attribute.getLabel() + "</span></td>";
175         result += "<td class=\"valueL\" " + modifiers.get("widthModifier") + ">";
176         result += "<div class=\"field\"><input type=button onClick=\"openWordDoc" + currentDocId + "(" + params.get("visible") + ", '" + hostAndContextPath + "', '" + params.get("template") + "', '" + params.get("macro") + "', 'xmlExport" + currentDocId + "');\"value=\"" + attribute.getLabel() + "\"><img class=\"popUpButton\" SRC=\"images/help.gif\" width=\"16\" height=\"16\" border=\"0\" onclick=\"javascript:void(window.open('helpActiveX_" + this.application.getCurrentLocale() + ".html', 'Help', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=400'));\" /></div>";
177         result += "<div id=\"showXML\" class=\"field\" style=\"display: block\"><a onclick=\"javascript:{document.getElementById('hideXML').style.display='block';document.getElementById('showXML').style.display='none';}\"><img SRC=\"images/expand_down.gif\" border=\"0\" align=\"middle\" alt=\"v\" title=\"\" /></a></div>";
178         result += "<div id=\"hideXML\" class=\"field\" style=\"display: none\"><a onclick=\"javascript:{document.getElementById('hideXML').style.display='none';document.getElementById('showXML').style.display='block';}\"><img SRC=\"images/shrink_up.gif\" border=\"0\" align=\"middle\" alt=\"^\" title=\"\" /></a><br /> <textarea id=\"xmlExport" + currentDocId + "\" rows=\"10\" cols=\"20\" style=\"overflow:auto;\" >" + exportedXml.toString() + "</textarea> </div>";
179         result += "</td>";
180                 
181         renderingContext.put(
182             XmlForWordExportValue.class.getName(),
183             new Integer JavaDoc(currentDocId+1)
184         );
185         
186         return result;
187     }
188
189     //-------------------------------------------------------------------------
190
// Members
191
//-------------------------------------------------------------------------
192
/**
193      * Comment for <code>serialVersionUID</code>
194      */

195     private static final long serialVersionUID = 3258129167668425784L;
196
197 }
198
199 // --- End of File -----------------------------------------------------------
200
Popular Tags