1 56 package org.opencrx.kernel.gui.generic.servlet.attribute; 57 58 import java.io.BufferedReader ; 59 import java.io.ByteArrayInputStream ; 60 import java.io.ByteArrayOutputStream ; 61 import java.io.IOException ; 62 import java.io.Serializable ; 63 import java.io.StringReader ; 64 import java.util.Map ; 65 import java.util.zip.ZipInputStream ; 66 67 import javax.servlet.http.HttpServletRequest ; 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 81 public class XmlForWordExportValue 82 extends BinaryValue 83 implements Serializable { 84 85 public XmlForWordExportValue( 87 Object object, 88 FieldDef fieldDef, 89 ApplicationContext application 90 ) { 91 super( 92 object, 93 fieldDef, 94 application 95 ); 96 } 97 98 public String toHtml( 100 HttpServletRequest request, 101 View view, 102 Attribute attribute, 103 Map modifiers, 104 Map popupImages, 105 Map renderingContext 106 ) { 107 int currentDocId = renderingContext.get(XmlForWordExportValue.class.getName()) != null 108 ? ((Integer )renderingContext.get(XmlForWordExportValue.class.getName())).intValue() 109 : 0; 110 Map params = this.getMimeTypeParams(); 111 112 String 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 StringBuilder exportedXml = new StringBuilder (); 142 try { 143 ByteArrayOutputStream os = new ByteArrayOutputStream (); 144 try { 145 this.getBinaryValue(os); 146 } 147 catch(ServiceException e) { 148 AppLog.warning(e.getMessage(), e.getCause(), 1); 149 } 150 ZipInputStream zip = new ZipInputStream ( 151 new ByteArrayInputStream (os.toByteArray()) 152 ); 153 while((zip.getNextEntry()) != null) { 154 ByteArrayOutputStream xmlFile = new ByteArrayOutputStream (); 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 xmlReader = new BufferedReader ( 161 new StringReader (xmlFile.toString("UTF-8")) 162 ); 163 String line = null; 164 while((line = xmlReader.readLine()) != null) { 165 exportedXml.append(line + "\n"); 166 } 167 } 168 } 169 catch(IOException e) { 170 AppLog.warning("can not read binary value. Is it a ZIP stream?"); 171 } 172 173 String 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 (currentDocId+1) 184 ); 185 186 return result; 187 } 188 189 195 private static final long serialVersionUID = 3258129167668425784L; 196 197 } 198 199 | Popular Tags |