View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.beans.runtime;
35  
36  import javax.jcr.PropertyType;
37  
38  import info.magnolia.cms.beans.config.MIMEMapping;
39  import info.magnolia.cms.core.Content;
40  import info.magnolia.cms.core.NodeData;
41  import org.apache.commons.lang.StringUtils;
42  import org.apache.commons.lang.math.NumberUtils;
43  
44  
45  /**
46   * Similar to info.magnolia.cms.beans.runtime.File but exposes the binary's properties using constants.
47   *
48   * @author Vinzenz Wyser
49   * @version 2.0
50   */
51  public class FileProperties {
52  
53      public static final String PROPERTIES_CONTENTNODE = "properties"; //$NON-NLS-1$
54  
55      public static final String PROPERTY_CONTENTTYPE = "jcr:mimeType"; //$NON-NLS-1$
56  
57      public static final String PROPERTY_ENCODING = "jcr:encoding"; //$NON-NLS-1$
58  
59      public static final String PROPERTY_LASTMODIFIED = "jcr:lastModified"; //$NON-NLS-1$
60  
61      public static final String PROPERTY_SIZE = "size"; //$NON-NLS-1$
62  
63      public static final String PROPERTY_TEMPLATE = "nodeDataTemplate"; //$NON-NLS-1$
64  
65      public static final String PROPERTY_EXTENSION = "extension"; //$NON-NLS-1$
66  
67      public static final String PROPERTY_MIMETYPE = "mimeType";
68  
69      public static final String PROPERTY_FILENAME = "fileName"; //$NON-NLS-1$
70  
71      public static final String PROPERTY_ICON = "icon"; //$NON-NLS-1$
72  
73      public static final String PROPERTY_WIDTH = "width"; //$NON-NLS-1$
74  
75      public static final String PROPERTY_HEIGHT = "height"; //$NON-NLS-1$
76  
77      public static final String EXTENSION = "extension"; // Pdf //$NON-NLS-1$
78  
79      public static final String EXTENSION_LOWER_CASE = "extensionLowerCase"; // pdf //$NON-NLS-1$
80  
81      public static final String EXTENSION_UPPER_CASE = "extensionUpperCase"; // PDF //$NON-NLS-1$
82  
83      public static final String NAME = "name"; // report2004.Pdf //$NON-NLS-1$
84  
85      public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension"; // report2004 //$NON-NLS-1$
86  
87      public static final String CONTENT_TYPE = "jcr:mimeType"; // application/pdf //$NON-NLS-1$
88  
89      public static final String ICON = "icon"; // the icon for this type
90  
91      public static final String TEMPLATE = "template"; // ((according to dialog)) //$NON-NLS-1$
92  
93      public static final String HANDLE = "handle"; // /en/mainColumnParagraph/04/file //$NON-NLS-1$
94  
95      /**
96       * /en/mainColumnParagraph/04/file.Pdf.
97       */
98      public static final String PATH_WITHOUT_NAME = "pathWithoutName"; //$NON-NLS-1$
99  
100     /**
101      * path including fileName: <code>/en/mainColumnParagraph/04/file/report2004.Pdf</code>.
102      */
103     public static final String PATH = "path"; //$NON-NLS-1$
104 
105     /**
106      * size in bytes: <code>263492</code>.
107      */
108     public static final String SIZE_BYTES = "sizeBytes"; //$NON-NLS-1$
109 
110     /**
111      * size in KB: <code>257.3</code>.
112      */
113     public static final String SIZE_KB = "sizeKB"; //$NON-NLS-1$
114 
115     /**
116      * size in MB: <code>0.2</code>
117      */
118     public static final String SIZE_MB = "sizeMB"; //$NON-NLS-1$
119 
120     /**
121      * size and unit depending of size in bytes, KB, or MB: <code>257.3</code>
122      */
123     public static final String SIZE = "size"; //$NON-NLS-1$
124 
125     private Content content;
126 
127     private String nodeDataName;
128 
129     public FileProperties(Content content, String nodeDataName) {
130         this.setContent(content);
131         this.setNodeDataName(nodeDataName);
132     }
133 
134     public void setContent(Content c) {
135         this.content = c;
136     }
137 
138     public Content getContent() {
139         return this.content;
140     }
141 
142     public void setNodeDataName(String s) {
143         this.nodeDataName = s;
144     }
145 
146     public String getNodeDataName() {
147         return this.nodeDataName;
148     }
149 
150     public String getProperty(String property) {
151         String value = StringUtils.EMPTY;
152 
153         if (this.getContent() == null) {
154             return null;
155         }
156 
157         NodeData props = this.getContent().getNodeData(this.nodeDataName);
158         if (props.getType() != PropertyType.BINARY) {
159             return null;
160         }
161 
162         String filename = props.getAttribute(PROPERTY_FILENAME);
163         String ext = props.getAttribute(PROPERTY_EXTENSION);
164         String fullName = filename;
165         String fullExt = StringUtils.EMPTY;
166         if (StringUtils.isNotEmpty(ext)) {
167             fullExt = "." + ext; //$NON-NLS-1$
168             fullName += fullExt;
169         }
170         if (property.equals(EXTENSION)) {
171             value = ext;
172         }
173         else if (property.equals(EXTENSION_LOWER_CASE)) {
174             value = StringUtils.lowerCase(ext);
175         }
176         else if (property.equals(EXTENSION_UPPER_CASE)) {
177             value = StringUtils.upperCase(ext);
178         }
179         else if (property.equals(NAME_WITHOUT_EXTENSION)) {
180             value = filename;
181         }
182         else if (property.equals(CONTENT_TYPE)) {
183             value = props.getAttribute(PROPERTY_CONTENTTYPE);
184         }
185         else if (property.equals(TEMPLATE)) {
186             value = props.getAttribute(PROPERTY_TEMPLATE);
187         }
188         else if (property.equals(HANDLE)) {
189             value = this.getContent().getHandle() + "/" + this.getNodeDataName(); //$NON-NLS-1$
190         }
191         else if (property.equals(NAME)) {
192             value = fullName;
193         }
194         else if (property.equals(PATH_WITHOUT_NAME)) {
195             value = this.getContent().getHandle() + "/" + this.getNodeDataName() + fullExt; //$NON-NLS-1$
196         }
197         else if (property.equals(ICON) && ext != null) {
198             value = MIMEMapping.getMIMETypeIcon(ext);
199         }
200         else if (property.equals(SIZE_BYTES)) {
201             value = props.getAttribute(PROPERTY_SIZE);
202         }
203         else if (property.equals(SIZE_KB)) {
204             String sizestring = props.getAttribute(PROPERTY_SIZE);
205             if (!NumberUtils.isNumber(sizestring)) {
206                 return null;
207             }
208             double size = Long.parseLong(sizestring);
209             String sizeStr;
210             size = size / 1024;
211             sizeStr = Double.toString(size);
212             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); //$NON-NLS-1$
213             value = sizeStr;
214         }
215         else if (property.equals(SIZE_MB)) {
216             String sizestring = props.getAttribute(PROPERTY_SIZE);
217             if (!NumberUtils.isNumber(sizestring)) {
218                 return null;
219             }
220             double size = Long.parseLong(sizestring);
221             String sizeStr;
222             size = size / (1024 * 1024);
223             sizeStr = Double.toString(size);
224             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); //$NON-NLS-1$
225             value = sizeStr;
226         }
227         else if (property.equals(SIZE)) {
228             String sizestring = props.getAttribute(PROPERTY_SIZE);
229             if (!NumberUtils.isNumber(sizestring)) {
230                 return null;
231             }
232             double size = Long.parseLong(sizestring);
233             String unit = "bytes";
234             String sizeStr;
235             if (size >= 1000) {
236                 size = size / 1024;
237                 unit = "KB";
238                 if (size >= 1000) {
239                     size = size / 1024;
240                     unit = "MB";
241                 }
242                 sizeStr = Double.toString(size);
243                 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2); //$NON-NLS-1$
244             }
245             else {
246                 sizeStr = Double.toString(size);
247                 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".")); //$NON-NLS-1$
248             }
249             value = sizeStr + " " + unit; //$NON-NLS-1$
250         }
251         else if (property.equals(PROPERTY_WIDTH)) {
252             value = props.getAttribute(PROPERTY_WIDTH);
253         }
254         else if (property.equals(PROPERTY_HEIGHT)) {
255             value = props.getAttribute(PROPERTY_HEIGHT);
256         }
257         else { // property.equals(PATH|null|""|any other value)
258             value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName; //$NON-NLS-1$ //$NON-NLS-2$
259         }
260         return value;
261     }
262 }