]> git.argeo.org Git - gpl/argeo-slc.git/blob - file/AjxpFile.java
Prepare next development cycle
[gpl/argeo-slc.git] / file / AjxpFile.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.web.ajaxplorer.file;
17
18 import java.io.File;
19 import java.net.URLEncoder;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.web.ajaxplorer.AjxpDriverException;
26
27 public class AjxpFile {
28 private final static Log log = LogFactory.getLog(AjxpFile.class);
29
30 // FIXME: more generic modif time format?
31 private final static SimpleDateFormat sdf = new SimpleDateFormat(
32 "dd/MM/yyyy hh:mm");
33
34 private final File file;
35 private final String parentPath;
36 private final String filePath;
37
38 private final String ext;
39 private final FileType type;
40
41 public AjxpFile(File file, String parentPath) {
42 this.file = file;
43 this.parentPath = parentPath;
44 if (parentPath.equals("/")) {
45 this.filePath = "/" + file.getName();
46 } else {
47 this.filePath = parentPath + "/" + file.getName();
48 }
49 this.ext = file.isDirectory() ? null : file.getName().substring(
50 file.getName().indexOf('.') + 1);
51 this.type = FileType.findType(ext);
52 }
53
54 public String toXml(final LsMode mode, final String encoding) {
55 try {
56 StringBuffer buf = new StringBuffer();
57 buf.append("<tree");
58 addAttr("text", file.getName(), buf);
59 if (type != FileType.FOLDER) {
60 if (mode == LsMode.SEARCH)
61 addAttr("is_file", "true", buf);// FIXME: consistent value?
62 else if (mode == LsMode.FILE_LIST) {
63 addAttr("filename", filePath, buf);
64 addAttr("is_file", "1", buf);
65 addAttr("icon", type.getIcon(), buf);
66
67 addAttr("modiftime", formatModifTime(), buf);
68 addAttr("mimestring", type.getMimeString(), buf);
69 addAttr("filesize", formatFileSize(), buf);
70
71 if (type.isImage()) {
72 addAttr("is_image", "1", buf);
73 addAttr("image_type", type.getImageType(), buf);
74 addAttr("image_width", "100", buf);// FIXME: read image
75 addAttr("image_height", "100", buf);// FIXME: read image
76
77 } else {
78 addAttr("is_image", "0", buf);
79 }
80 }
81
82 } else {// dir
83 if (mode == LsMode.NULL || mode == LsMode.FILE_LIST) {
84 addAttr("filename", filePath, buf);
85 if (mode == LsMode.NULL) {
86 addAttr("icon", "client/images/foldericon.png", buf);
87 addAttr("openicon", "client/images/openfoldericon.png",
88 buf);
89 addAttr("parentName", parentPath, buf);
90 addAttr("src", "content.php?dir="
91 + URLEncoder.encode(filePath, encoding), buf);
92 addAttr(
93 "action",
94 "javascript:ajaxplorer.getFoldersTree().clickNode(CURRENT_ID)",
95 buf);
96 } else if (mode == LsMode.FILE_LIST) {
97 addAttr("icon", type.getIcon(), buf);// FIXME:
98 // consistent?
99 addAttr("is_file", "0", buf);
100 addAttr("is_image", "0", buf);
101 addAttr("mimestring", "Directory", buf);
102 addAttr("modiftime", formatModifTime(), buf);
103 addAttr("filesize", "-", buf);
104 }
105 }
106
107 }
108
109 addAdditionalAttrs(buf, mode, encoding);
110
111 buf.append("/>");
112
113 if (log.isTraceEnabled())
114 log.trace(buf.toString());
115
116 return buf.toString();
117 } catch (Exception e) {
118 throw new AjxpDriverException("Could not serialize file " + file, e);
119 }
120 }
121
122 private String formatModifTime() {
123 return sdf.format(new Date(file.lastModified()));
124 }
125
126 private String formatFileSize() {
127 return (file.length() / 1024) + " Kb";
128 }
129
130 protected void addAttr(String attrName, String attrValue, StringBuffer buf) {
131 buf.append(" ").append(attrName).append("=\"").append(attrValue)
132 .append("\"");
133 }
134
135 /** To be overridden, do nothing by default. */
136 protected void addAdditionalAttrs(final StringBuffer buf,
137 final LsMode mode, final String encoding) {
138
139 }
140 }