1 31 32 package org.opencms.workplace.explorer; 33 34 import org.opencms.file.CmsProperty; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.types.CmsResourceTypePlain; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.jsp.CmsJspActionElement; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.main.OpenCms; 44 import org.opencms.util.CmsStringUtil; 45 import org.opencms.util.CmsXsltUtil; 46 import org.opencms.workplace.CmsWorkplace; 47 import org.opencms.workplace.CmsWorkplaceException; 48 49 import java.util.ArrayList ; 50 import java.util.Arrays ; 51 import java.util.Collections ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import javax.servlet.jsp.JspException ; 58 import javax.servlet.jsp.PageContext ; 59 60 import org.apache.commons.fileupload.FileItem; 61 import org.apache.commons.logging.Log; 62 63 81 public class CmsNewCsvFile extends CmsNewResourceUpload { 82 83 84 public static final String BEST_DELIMITER = "best"; 85 86 87 public static final String FRAMEHEIGHT = "450"; 88 89 90 public static final String PARAM_CSVCONTENT = "csvcontent"; 91 92 93 public static final String PARAM_DELIMITER = "delimiter"; 94 95 96 public static final String PARAM_XSLTFILE = "xsltfile"; 97 98 99 public static final String TABLE_XSLT_SUFFIX = ".table.xslt"; 100 101 102 public static final String TABULATOR = "tab"; 103 104 105 private static final Log LOG = CmsLog.getLog(CmsNewCsvFile.class); 106 107 108 private String m_paramCsvContent; 109 110 111 private String m_paramDelimiter; 112 113 114 private String m_paramXsltFile; 115 116 121 public CmsNewCsvFile(CmsJspActionElement jsp) { 122 123 super(jsp); 124 } 125 126 133 public CmsNewCsvFile(PageContext context, HttpServletRequest req, HttpServletResponse res) { 134 135 this(new CmsJspActionElement(context, req, res)); 136 } 137 138 145 static String toXmlBody(String content) { 146 147 StringBuffer xmlBody = new StringBuffer (1024); 148 content = content.trim(); 149 150 if (content.startsWith("<") && content.endsWith(">")) { 151 return content; 152 } else { 153 xmlBody.append("<![CDATA["); 154 xmlBody.append(content); 155 xmlBody.append("]]>"); 156 } 157 158 return xmlBody.toString(); 159 } 160 161 166 public void actionUpload() throws JspException { 167 168 String newResname = ""; 169 170 try { 171 if (CmsStringUtil.isNotEmpty(getParamCsvContent())) { 172 newResname = "csvcontent.html"; 174 setParamNewResourceName(""); 175 } else { 176 setParamCsvContent(new String (getFileContentFromUpload(), CmsEncoder.ENCODING_ISO_8859_1)); 177 newResname = getCms().getRequestContext().getFileTranslator().translateResource( 178 CmsResource.getName(getParamResource().replace('\\', '/'))); 179 newResname = CmsStringUtil.changeFileNameSuffixTo(newResname, "html"); 180 setParamNewResourceName(newResname); 181 } 182 183 setParamResource(newResname); 184 setParamResource(computeFullResourceName()); 185 int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId(); 186 187 String xmlContent = CmsXsltUtil.transformCsvContent( 189 getCms(), 190 getParamXsltFile(), 191 getParamCsvContent(), 192 (BEST_DELIMITER.equals(getParamDelimiter()) ? null : getParamDelimiter())); 193 byte[] content = xmlContent.getBytes(); 194 195 CmsProperty styleProp = CmsProperty.getNullProperty(); 198 if (CmsStringUtil.isNotEmpty(getParamXsltFile())) { 199 styleProp = getCms().readPropertyObject( 200 getParamXsltFile(), 201 CmsPropertyDefinition.PROPERTY_STYLESHEET, 202 true); 203 } 204 205 try { 206 getCms().createResource(getParamResource(), resTypeId, content, Collections.EMPTY_LIST); 208 } catch (CmsException e) { 209 getCms().lockResource(getParamResource()); 211 getCms().replaceResource(getParamResource(), resTypeId, content, null); 212 } 213 if (!styleProp.isNullProperty()) { 215 getCms().writePropertyObject(getParamResource(), styleProp); 216 } 217 } catch (Throwable e) { 218 setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_TABLE_IMPORT_FAILED_0)); 220 includeErrorpage(this, e); 221 } 222 } 223 224 229 public String buildDelimiterSelect() { 230 231 Object [] optionStrings = new Object [] { 232 key(Messages.GUI_NEWRESOURCE_CONVERSION_DELIM_BEST_0), 233 key(Messages.GUI_NEWRESOURCE_CONVERSION_DELIM_SEMICOLON_0), 234 key(Messages.GUI_NEWRESOURCE_CONVERSION_DELIM_COMMA_0), 235 key(Messages.GUI_NEWRESOURCE_CONVERSION_DELIM_TAB_0)}; 236 List options = new ArrayList (Arrays.asList(optionStrings)); 237 List values = new ArrayList (Arrays.asList(new Object [] {"best", ";", ",", "tab"})); 238 String parameters = "name=\"" + PARAM_DELIMITER + "\" class=\"maxwidth\""; 239 return buildSelect(parameters, options, values, 0); 240 } 241 242 247 public String buildXsltSelect() { 248 249 List xsltFiles = getXsltFiles(); 251 if (xsltFiles.size() > 0) { 252 List options = new ArrayList (); 253 List values = new ArrayList (); 254 255 options.add(key(Messages.GUI_NEWRESOURCE_CONVERSION_NOSTYLE_0)); 256 values.add(""); 257 258 CmsResource resource; 259 CmsProperty titleProp = null; 260 261 Iterator i = xsltFiles.iterator(); 262 while (i.hasNext()) { 263 264 resource = (CmsResource)i.next(); 265 try { 266 titleProp = getCms().readPropertyObject( 267 resource.getRootPath(), 268 CmsPropertyDefinition.PROPERTY_TITLE, 269 false); 270 } catch (CmsException e) { 271 if (LOG.isWarnEnabled()) { 272 LOG.warn(e); 273 } 274 } 275 values.add(resource.getRootPath()); 276 if (titleProp.isNullProperty()) { 278 options.add("[" + resource.getName() + "]"); 279 } else { 280 options.add(titleProp.getValue()); 281 } 282 } 283 284 StringBuffer result = new StringBuffer (512); 285 result.append("<tr><td style=\"white-space: nowrap;\" unselectable=\"on\">"); 287 result.append(key(Messages.GUI_NEWRESOURCE_CONVERSION_XSLTFILE_0)); 288 result.append("</td><td class=\"maxwidth\">"); 289 String parameters = "class=\"maxwidth\" name=\"" + PARAM_XSLTFILE + "\""; 290 result.append(buildSelect(parameters, options, values, 0)); 291 result.append("</td><tr>"); 292 return result.toString(); 293 } else { 294 return ""; 295 } 296 } 297 298 304 public byte[] getFileContentFromUpload() throws CmsWorkplaceException { 305 306 byte[] content; 307 Iterator i = getMultiPartFileItems().iterator(); 309 FileItem fi = null; 310 while (i.hasNext()) { 311 fi = (FileItem)i.next(); 312 if (fi.getName() != null) { 313 break; 315 } else { 316 continue; 318 } 319 } 320 321 if (fi != null) { 322 long size = fi.getSize(); 323 if (size == 0) { 324 throw new CmsWorkplaceException(Messages.get().container(Messages.ERR_UPLOAD_FILE_NOT_FOUND_0)); 325 } 326 long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms()); 327 if (maxFileSizeBytes > 0 && size > maxFileSizeBytes) { 329 throw new CmsWorkplaceException(Messages.get().container( 330 Messages.ERR_UPLOAD_FILE_SIZE_TOO_HIGH_1, 331 new Long (maxFileSizeBytes / 1024))); 332 } 333 content = fi.get(); 334 fi.delete(); 335 setParamResource(fi.getName()); 336 337 } else { 338 throw new CmsWorkplaceException(Messages.get().container(Messages.ERR_UPLOAD_FILE_NOT_FOUND_0)); 339 } 340 return content; 341 } 342 343 348 public String getHeadFrameSetHeight() { 349 350 return FRAMEHEIGHT; 351 } 352 353 358 public String getParamCsvContent() { 359 360 return m_paramCsvContent; 361 } 362 363 368 public String getParamDelimiter() { 369 370 return m_paramDelimiter; 371 } 372 373 378 public String getParamXsltFile() { 379 380 return m_paramXsltFile; 381 } 382 383 388 public List getXsltFiles() { 389 390 List result = new ArrayList (); 391 try { 392 Iterator xmlFiles = getCms().readResources( 394 CmsWorkplace.VFS_PATH_MODULES, 395 CmsResourceFilter.DEFAULT_FILES.addRequireType(CmsResourceTypePlain.getStaticTypeId()), 396 true).iterator(); 397 while (xmlFiles.hasNext()) { 398 CmsResource xmlFile = (CmsResource)xmlFiles.next(); 399 if (xmlFile.getName().endsWith(TABLE_XSLT_SUFFIX)) { 401 result.add(xmlFile); 402 } 403 } 404 } catch (CmsException e) { 405 LOG.error(e); 406 } 407 return result; 408 409 } 410 411 416 public void setParamCsvContent(String csvContent) { 417 418 m_paramCsvContent = csvContent; 419 } 420 421 426 public void setParamDelimiter(String delimiter) { 427 428 m_paramDelimiter = delimiter; 429 } 430 431 436 public void setParamXsltFile(String xsltFile) { 437 438 m_paramXsltFile = xsltFile; 439 } 440 } | Popular Tags |