]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/ajaxplorer/file/AjxpFile.java
2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.web
.ajaxplorer
.file
;
19 import java
.net
.URLEncoder
;
20 import java
.text
.SimpleDateFormat
;
21 import java
.util
.Date
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpDriverException
;
27 public class AjxpFile
{
28 private final static Log log
= LogFactory
.getLog(AjxpFile
.class);
30 // FIXME: more generic modif time format?
31 private final static SimpleDateFormat sdf
= new SimpleDateFormat(
34 private final File file
;
35 private final String parentPath
;
36 private final String filePath
;
38 private final String ext
;
39 private final FileType type
;
41 public AjxpFile(File file
, String parentPath
) {
43 this.parentPath
= parentPath
;
44 if (parentPath
.equals("/")) {
45 this.filePath
= "/" + file
.getName();
47 this.filePath
= parentPath
+ "/" + file
.getName();
49 this.ext
= file
.isDirectory() ?
null : file
.getName().substring(
50 file
.getName().indexOf('.') + 1);
51 this.type
= FileType
.findType(ext
);
54 public String
toXml(final LsMode mode
, final String encoding
) {
56 StringBuffer buf
= new StringBuffer();
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
);
67 addAttr("modiftime", formatModifTime(), buf
);
68 addAttr("mimestring", type
.getMimeString(), buf
);
69 addAttr("filesize", formatFileSize(), buf
);
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
78 addAttr("is_image", "0", buf
);
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",
89 addAttr("parentName", parentPath
, buf
);
90 addAttr("src", "content.php?dir="
91 + URLEncoder
.encode(filePath
, encoding
), buf
);
94 "javascript:ajaxplorer.getFoldersTree().clickNode(CURRENT_ID)",
96 } else if (mode
== LsMode
.FILE_LIST
) {
97 addAttr("icon", type
.getIcon(), buf
);// FIXME:
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
);
109 addAdditionalAttrs(buf
, mode
, encoding
);
113 if (log
.isTraceEnabled())
114 log
.trace(buf
.toString());
116 return buf
.toString();
117 } catch (Exception e
) {
118 throw new AjxpDriverException("Could not serialize file " + file
, e
);
122 private String
formatModifTime() {
123 return sdf
.format(new Date(file
.lastModified()));
126 private String
formatFileSize() {
127 return (file
.length() / 1024) + " Kb";
130 protected void addAttr(String attrName
, String attrValue
, StringBuffer buf
) {
131 buf
.append(" ").append(attrName
).append("=\"").append(attrValue
)
135 /** To be overridden, do nothing by default. */
136 protected void addAdditionalAttrs(final StringBuffer buf
,
137 final LsMode mode
, final String encoding
) {