1 28 29 package com.opencms.workplace; 30 31 import org.opencms.file.CmsObject; 32 import org.opencms.file.types.I_CmsResourceType; 33 import org.opencms.i18n.CmsEncoder; 34 import org.opencms.main.CmsException; 35 import org.opencms.main.CmsLog; 36 import org.opencms.main.OpenCms; 37 38 import com.opencms.legacy.CmsLegacyException; 39 import com.opencms.template.A_CmsXmlContent; 40 import com.opencms.template.CmsXmlTemplateFile; 41 42 import java.util.HashMap ; 43 import java.util.Hashtable ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 import java.util.Vector ; 48 49 59 60 public class CmsAdminDatatypes extends CmsWorkplaceDefault { 61 62 63 64 private static final String C_TAG_RESTYPE = "restype"; 65 66 67 68 private static final String C_TYPELISTENTRY = "extensionentry"; 69 70 71 72 private static final String C_TAG_ALLENTRIES = "allentries"; 73 74 75 76 private static final String C_TAG_RESTYPEENTRY = "restypeentry"; 77 78 79 80 private static final String C_TAG_SEPARATORENTRY = "separatorentry"; 81 82 83 84 private static final String C_TAG_SCROLLERCONTENT = "scrollercontent"; 85 86 91 92 private String format(String name) throws CmsException { 93 int z = 0; 94 while('*' == name.charAt(z) || '.' == name.charAt(z)) { 95 z++; 96 } 97 98 String res = name.substring(z, name.length()); 100 if(res.indexOf(' ') != -1) { 101 102 throw new CmsLegacyException(CmsLegacyException.C_BAD_NAME); 104 } 105 return res.toLowerCase(); } 107 108 119 120 public byte[] getContent(CmsObject cms, String templateFile, String elementName, 121 Hashtable parameters, String templateSelector) throws CmsException { 122 if(CmsLog.getLog(this).isDebugEnabled() && C_DEBUG) { 123 CmsLog.getLog(this).debug("Getting content of element " 124 + ((elementName == null) ? "<root>" : elementName)); 125 CmsLog.getLog(this).debug("Template file is: " + templateFile); 126 CmsLog.getLog(this).debug("Selected template section is: " 127 + ((templateSelector == null) ? "<default>" : templateSelector)); 128 } 129 130 CmsXmlWpTemplateFile xmlTemplateDocument = new CmsXmlWpTemplateFile(cms, templateFile); 131 CmsXmlLanguageFile lang = xmlTemplateDocument.getLanguageFile(); 132 String action = (String )parameters.get("action"); 133 String resTypeName = (String )parameters.get("restype"); 134 String extensionName = (String )parameters.get("extension"); 135 xmlTemplateDocument.setData("RESTYPE", resTypeName); 136 if("new".equals(action)) { 137 templateSelector = "newextension"; 138 String name = (String )parameters.get("NAME"); 139 if(name == null || name.equals("")) { 140 141 142 } 144 else { 145 try { 146 String formattedName; 147 formattedName = format(name); 148 Map h = OpenCms.getResourceManager().getExtensionMapping(); 149 if(h == null) { 150 h = new HashMap (); 151 } 152 if(h.containsKey(formattedName)) { 153 throw new CmsLegacyException(CmsLegacyException.C_NOT_EMPTY); 154 } 155 h.put(formattedName, resTypeName); 156 templateSelector = ""; 160 } 161 catch(CmsException e) { 162 if ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NOT_EMPTY)) { 163 templateSelector = "errorinuse"; 164 } 165 else { 166 if ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_BAD_NAME)) { 167 templateSelector = "errorformat"; 168 } 169 else { 170 StringBuffer errmesg = new StringBuffer (); 171 errmesg.append(lang.getLanguageValue("error.reason.newextension1") 172 + " '" + name + "' " + lang.getLanguageValue("error.reason.newextension2") 173 + " '" + resTypeName + "' " 174 + lang.getLanguageValue("error.reason.newextension3") + "\n\n"); 175 errmesg.append(CmsException.getStackTraceAsString(e)); 176 xmlTemplateDocument.setData("NEWDETAILS", errmesg.toString()); 177 templateSelector = "newerror"; 178 } 179 } 180 } 181 } 182 } 183 else { 184 if("delete".equals(action)) { 185 if("true".equals(parameters.get("sure"))) { 186 187 Map h = OpenCms.getResourceManager().getExtensionMapping(); 190 if(h != null) { 191 h.remove(extensionName); 192 } 193 templateSelector = ""; 197 202 } 203 else { 204 templateSelector = "RUsuredelete"; 205 } 206 xmlTemplateDocument.setData("EXTENSION_NAME", extensionName); 207 } 208 } 209 210 return startProcessing(cms, xmlTemplateDocument, elementName, parameters, templateSelector); 212 } 213 214 227 228 public String getDatatypes(CmsObject cms, A_CmsXmlContent doc, 229 CmsXmlLanguageFile lang, Hashtable parameters, Object callingObj) throws CmsException { 230 StringBuffer result = new StringBuffer (); 231 Map extensions = OpenCms.getResourceManager().getExtensionMapping(); 232 Map extByFiletypes = turnAround(extensions); 233 CmsXmlTemplateFile templateFile = (CmsXmlTemplateFile)doc; 234 235 List allResTypes = OpenCms.getResourceManager().getResourceTypes(); 237 for (int i=0; i<allResTypes.size(); i++) { 238 I_CmsResourceType type = (I_CmsResourceType)allResTypes.get(i); 239 String resTypeName = type.getTypeName(); 240 Vector suffList = (Vector )extByFiletypes.get(resTypeName); 241 result.append(getResourceEntry(cms, doc, lang, parameters, callingObj, resTypeName, suffList)); 242 if(i < (allResTypes.size() - 1)) { 243 result.append(templateFile.getProcessedDataValue(C_TAG_SEPARATORENTRY, callingObj)); 244 } 245 } 246 templateFile.setData(C_TAG_ALLENTRIES, result.toString()); 247 return templateFile.getProcessedDataValue(C_TAG_SCROLLERCONTENT, callingObj); 248 } 249 250 264 265 private String getResourceEntry(CmsObject cms, A_CmsXmlContent doc, CmsXmlLanguageFile lang, 266 Hashtable parameters, Object callingObject, String resTypeName, Vector suffList) 267 throws CmsException { 268 StringBuffer output = new StringBuffer (); 269 CmsXmlWpTemplateFile templateFile = (CmsXmlWpTemplateFile)doc; 270 templateFile.setData(C_TAG_RESTYPE, resTypeName); 271 272 templateFile.setData(C_TAG_RESTYPE + "_esc", CmsEncoder.escapeWBlanks(resTypeName, 273 cms.getRequestContext().getEncoding())); 274 output.append(templateFile.getProcessedDataValue(C_TAG_RESTYPEENTRY, callingObject)); 275 if(suffList != null) { 276 for(int z = 0;z < suffList.size();z++) { 277 String suffix = (String )suffList.elementAt(z); 278 templateFile.setData("EXTENSION_NAME", suffix); 279 templateFile.setData("EXTENSION_NAME_ESC", CmsEncoder.escapeWBlanks(suffix, 280 cms.getRequestContext().getEncoding())); 281 output.append(templateFile.getProcessedDataValue(C_TYPELISTENTRY, callingObject)); 282 } 283 } 284 return output.toString(); 285 } 286 287 297 298 public boolean isCacheable(CmsObject cms, String templateFile, String elementName, 299 Hashtable parameters, String templateSelector) { 300 return false; 301 } 302 303 311 312 private Map turnAround(Map h) { 313 if(h == null) { 314 return null; 315 } 316 Map g = new HashMap (); 317 Iterator en = h.keySet().iterator(); 318 while(en.hasNext()) { 319 Object key = en.next(); 320 Object value = h.get(key); 321 Vector List = (Vector )g.get(value); 322 if(List == null) { 323 Vector newEntry = new Vector (); 324 newEntry.addElement(key); 325 g.put(value, newEntry); 326 } 327 else { 328 List.addElement(key); 329 } 330 } 331 return g; 332 } 333 } 334
| Popular Tags
|