1 13 package info.magnolia.cms.gui.misc; 14 15 import info.magnolia.cms.beans.config.MIMEMapping; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.NodeData; 18 19 import org.apache.commons.lang.StringUtils; 20 21 22 26 public class FileProperties { 27 28 public static final String PROPERTIES_CONTENTNODE = "properties"; 30 public static final String PROPERTY_CONTENTTYPE = "jcr:mimeType"; 32 public static final String PROPERTY_ENCODING = "jcr:encoding"; 34 public static final String PROPERTY_LASTMODIFIED = "jcr:lastModified"; 36 public static final String PROPERTY_SIZE = "size"; 38 public static final String PROPERTY_TEMPLATE = "nodeDataTemplate"; 40 public static final String PROPERTY_EXTENSION = "extension"; 42 public static final String PROPERTY_FILENAME = "fileName"; 44 public static final String PROPERTY_ICON = "icon"; 46 public static final String PROPERTY_WIDTH = "width"; 48 public static final String PROPERTY_HEIGHT = "height"; 50 public static final String EXTENSION = "extension"; 52 public static final String EXTENSION_LOWER_CASE = "extensionLowerCase"; 54 public static final String EXTENSION_UPPER_CASE = "extensionUpperCase"; 56 public static final String NAME = "name"; 58 public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension"; 60 public static final String CONTENT_TYPE = "jcr:mimeType"; 62 public static final String ICON = "icon"; 64 public static final String TEMPLATE = "template"; 66 public static final String HANDLE = "handle"; 68 71 public static final String PATH_WITHOUT_NAME = "pathWithoutName"; 73 76 public static final String PATH = "path"; 78 81 public static final String SIZE_BYTES = "sizeBytes"; 83 86 public static final String SIZE_KB = "sizeKB"; 88 91 public static final String SIZE_MB = "sizeMB"; 93 96 public static final String SIZE = "size"; 98 private Content content; 99 100 private String nodeDataName; 101 102 public FileProperties(Content content, String nodeDataName) { 103 this.setContent(content); 104 this.setNodeDataName(nodeDataName); 105 } 106 107 public void setContent(Content c) { 108 this.content = c; 109 } 110 111 public Content getContent() { 112 return this.content; 113 } 114 115 public void setNodeDataName(String s) { 116 this.nodeDataName = s; 117 } 118 119 public String getNodeDataName() { 120 return this.nodeDataName; 121 } 122 123 public String getProperty(String property) { 124 String value = StringUtils.EMPTY; 125 NodeData props = this.getContent().getNodeData(this.nodeDataName); 126 String filename = props.getAttribute(PROPERTY_FILENAME); 127 String ext = props.getAttribute(PROPERTY_EXTENSION); 128 String fullName = filename; 129 String fullExt = StringUtils.EMPTY; 130 if (StringUtils.isNotEmpty(ext)) { 131 fullExt = "." + ext; fullName += fullExt; 133 } 134 if (property.equals(EXTENSION)) { 135 value = ext; 136 } 137 else if (property.equals(EXTENSION_LOWER_CASE)) { 138 value = ext.toLowerCase(); 139 } 140 else if (property.equals(EXTENSION_UPPER_CASE)) { 141 value = ext.toUpperCase(); 142 } 143 else if (property.equals(NAME_WITHOUT_EXTENSION)) { 144 value = filename; 145 } 146 else if (property.equals(CONTENT_TYPE)) { 147 value = props.getAttribute(PROPERTY_CONTENTTYPE); 148 } 149 else if (property.equals(TEMPLATE)) { 150 value = props.getAttribute(PROPERTY_TEMPLATE); 151 } 152 else if (property.equals(HANDLE)) { 153 value = this.getContent().getHandle() + "/" + this.getNodeDataName(); } 155 else if (property.equals(NAME)) { 156 value = fullName; 157 } 158 else if (property.equals(PATH_WITHOUT_NAME)) { 159 value = this.getContent().getHandle() + "/" + this.getNodeDataName() + fullExt; } 161 else if (property.equals(ICON)) { 162 value = MIMEMapping.getMIMETypeIcon(ext); 163 } 164 else if (property.equals(SIZE_BYTES)) { 165 value = props.getAttribute(PROPERTY_SIZE); 166 } 167 else if (property.equals(SIZE_KB)) { 168 double size = Long.parseLong(props.getAttribute(PROPERTY_SIZE)); 169 String sizeStr; 170 size = size / 1024; 171 sizeStr = Double.toString(size); 172 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); value = sizeStr; 174 } 175 else if (property.equals(SIZE_MB)) { 176 double size = Long.parseLong(props.getAttribute(PROPERTY_SIZE)); 177 String sizeStr; 178 size = size / (1024 * 1024); 179 sizeStr = Double.toString(size); 180 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); value = sizeStr; 182 } 183 else if (property.equals(SIZE)) { 184 double size = Long.parseLong(props.getAttribute(PROPERTY_SIZE)); 185 String unit = "bytes"; 186 String sizeStr; 187 if (size >= 1000) { 188 size = size / 1024; 189 unit = "KB"; 190 if (size >= 1000) { 191 size = size / 1024; 192 unit = "MB"; 193 } 194 sizeStr = Double.toString(size); 195 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); } 197 else { 198 sizeStr = Double.toString(size); 199 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".")); } 201 value = sizeStr + " " + unit; } 203 else if (property.equals(PROPERTY_WIDTH)) { 204 value = props.getAttribute(PROPERTY_WIDTH); 205 } 206 else if (property.equals(PROPERTY_HEIGHT)) { 207 value = props.getAttribute(PROPERTY_HEIGHT); 208 } 209 else { value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName; } 212 return value; 213 } 214 } 215 | Popular Tags |