KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > misc > FileProperties


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 /**
23  * @author Vinzenz Wyser
24  * @version 2.0
25  */

26 public class FileProperties {
27
28     public static final String JavaDoc PROPERTIES_CONTENTNODE = "properties"; //$NON-NLS-1$
29

30     public static final String JavaDoc PROPERTY_CONTENTTYPE = "jcr:mimeType"; //$NON-NLS-1$
31

32     public static final String JavaDoc PROPERTY_ENCODING = "jcr:encoding"; //$NON-NLS-1$
33

34     public static final String JavaDoc PROPERTY_LASTMODIFIED = "jcr:lastModified"; //$NON-NLS-1$
35

36     public static final String JavaDoc PROPERTY_SIZE = "size"; //$NON-NLS-1$
37

38     public static final String JavaDoc PROPERTY_TEMPLATE = "nodeDataTemplate"; //$NON-NLS-1$
39

40     public static final String JavaDoc PROPERTY_EXTENSION = "extension"; //$NON-NLS-1$
41

42     public static final String JavaDoc PROPERTY_FILENAME = "fileName"; //$NON-NLS-1$
43

44     public static final String JavaDoc PROPERTY_ICON = "icon"; //$NON-NLS-1$
45

46     public static final String JavaDoc PROPERTY_WIDTH = "width"; //$NON-NLS-1$
47

48     public static final String JavaDoc PROPERTY_HEIGHT = "height"; //$NON-NLS-1$
49

50     public static final String JavaDoc EXTENSION = "extension"; // Pdf //$NON-NLS-1$
51

52     public static final String JavaDoc EXTENSION_LOWER_CASE = "extensionLowerCase"; // pdf //$NON-NLS-1$
53

54     public static final String JavaDoc EXTENSION_UPPER_CASE = "extensionUpperCase"; // PDF //$NON-NLS-1$
55

56     public static final String JavaDoc NAME = "name"; // report2004.Pdf //$NON-NLS-1$
57

58     public static final String JavaDoc NAME_WITHOUT_EXTENSION = "nameWithoutExtension"; // report2004 //$NON-NLS-1$
59

60     public static final String JavaDoc CONTENT_TYPE = "jcr:mimeType"; // application/pdf //$NON-NLS-1$
61

62     public static final String JavaDoc ICON = "icon"; // the icon for this type
63

64     public static final String JavaDoc TEMPLATE = "template"; // ((according to dialog)) //$NON-NLS-1$
65

66     public static final String JavaDoc HANDLE = "handle"; // /en/mainColumnParagraph/04/file //$NON-NLS-1$
67

68     /**
69      * /en/mainColumnParagraph/04/file.Pdf
70      */

71     public static final String JavaDoc PATH_WITHOUT_NAME = "pathWithoutName"; //$NON-NLS-1$
72

73     /**
74      * path including fileName: <code>/en/mainColumnParagraph/04/file/report2004.Pdf</code>
75      */

76     public static final String JavaDoc PATH = "path"; //$NON-NLS-1$
77

78     /**
79      * size in bytes: <code>263492</code>
80      */

81     public static final String JavaDoc SIZE_BYTES = "sizeBytes"; //$NON-NLS-1$
82

83     /**
84      * size in KB: <code>257.3</code>
85      */

86     public static final String JavaDoc SIZE_KB = "sizeKB"; //$NON-NLS-1$
87

88     /**
89      * size in MB: <code>0.2</code>
90      */

91     public static final String JavaDoc SIZE_MB = "sizeMB"; //$NON-NLS-1$
92

93     /**
94      * size and unit depending of size in bytes, KB, or MB: <code>257.3</code>
95      */

96     public static final String JavaDoc SIZE = "size"; //$NON-NLS-1$
97

98     private Content content;
99
100     private String JavaDoc nodeDataName;
101
102     public FileProperties(Content content, String JavaDoc 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 JavaDoc s) {
116         this.nodeDataName = s;
117     }
118
119     public String JavaDoc getNodeDataName() {
120         return this.nodeDataName;
121     }
122
123     public String JavaDoc getProperty(String JavaDoc property) {
124         String JavaDoc value = StringUtils.EMPTY;
125         NodeData props = this.getContent().getNodeData(this.nodeDataName);
126         String JavaDoc filename = props.getAttribute(PROPERTY_FILENAME);
127         String JavaDoc ext = props.getAttribute(PROPERTY_EXTENSION);
128         String JavaDoc fullName = filename;
129         String JavaDoc fullExt = StringUtils.EMPTY;
130         if (StringUtils.isNotEmpty(ext)) {
131             fullExt = "." + ext; //$NON-NLS-1$
132
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(); //$NON-NLS-1$
154
}
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; //$NON-NLS-1$
160
}
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 JavaDoc sizeStr;
170             size = size / 1024;
171             sizeStr = Double.toString(size);
172             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); //$NON-NLS-1$
173
value = sizeStr;
174         }
175         else if (property.equals(SIZE_MB)) {
176             double size = Long.parseLong(props.getAttribute(PROPERTY_SIZE));
177             String JavaDoc sizeStr;
178             size = size / (1024 * 1024);
179             sizeStr = Double.toString(size);
180             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); //$NON-NLS-1$
181
value = sizeStr;
182         }
183         else if (property.equals(SIZE)) {
184             double size = Long.parseLong(props.getAttribute(PROPERTY_SIZE));
185             String JavaDoc unit = "bytes";
186             String JavaDoc 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); //$NON-NLS-1$
196
}
197             else {
198                 sizeStr = Double.toString(size);
199                 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".")); //$NON-NLS-1$
200
}
201             value = sizeStr + " " + unit; //$NON-NLS-1$
202
}
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 { // property.equals(PATH|null|""|any other value)
210
value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName; //$NON-NLS-1$ //$NON-NLS-2$
211
}
212         return value;
213     }
214 }
215
Popular Tags