1 package fr.in2p3.jsaga.command;
2
3 import org.ogf.saga.error.*;
4 import org.ogf.saga.file.File;
5 import org.ogf.saga.namespace.*;
6 import org.ogf.saga.permissions.Permission;
7 import org.ogf.saga.url.URL;
8
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Locale;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class EntryLongFormat {
26 private NSDirectory m_parentDir;
27
28 public EntryLongFormat(NSDirectory parentDir) {
29 m_parentDir = parentDir;
30 }
31
32 public String toString(URL url) throws SagaException {
33 NSEntry entry = m_parentDir.open(url, Flags.NONE.getValue());
34 String entry_str = this.toString(entry);
35 entry.close();
36 return entry_str;
37 }
38
39 public String toString(NSEntry entry) throws SagaException {
40 String owner = this.getOwner(entry);
41 String group = this.getGroup(entry);
42
43 StringBuffer buf = new StringBuffer();
44 buf.append(this.isDir(entry));
45 if (owner != null) {
46 buf.append(this.getPermissions(entry, "user-"+owner));
47 } else {
48 buf.append("???");
49 }
50 if (group != null) {
51 buf.append(this.getPermissions(entry, "group-"+group));
52 } else {
53 buf.append("???");
54 }
55 buf.append(this.getPermissions(entry, "*"));
56 buf.append(' ');
57 if (owner != null) {
58 if (owner.startsWith("/")) {
59 buf.append("'");
60 buf.append(owner.substring(owner.lastIndexOf("/CN=")+4));
61 buf.append("'");
62 } else {
63 buf.append(format(owner, 8));
64 }
65 buf.append(' ');
66 }
67 if (group != null) {
68 buf.append(format(group, 8));
69 buf.append(' ');
70 }
71 buf.append(format(this.getSize(entry), 10));
72 buf.append(' ');
73 final String FORMAT = "MMM-dd-yyyy HH:mm";
74 Date lastModified = this.getMTime(entry);
75 if (lastModified != null) {
76 buf.append(new SimpleDateFormat(FORMAT, Locale.ENGLISH).format(lastModified));
77 } else {
78 buf.append(format("?", FORMAT.length()));
79 }
80 buf.append(' ');
81 buf.append(this.getName(entry));
82 return buf.toString();
83 }
84
85 public char isDir(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
86 try {
87 if (entry.isDir()) {
88 return 'd';
89 } else {
90 return '-';
91 }
92 } catch (NotImplementedException e) {
93 return '?';
94 }
95 }
96
97 public String getOwner(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
98 try {
99 return entry.getOwner();
100 } catch (NotImplementedException e) {
101 return null;
102 }
103 }
104
105 public String getGroup(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
106 try {
107 return entry.getGroup();
108 } catch (NotImplementedException e) {
109 return null;
110 }
111 }
112
113 public String getPermissions(NSEntry entry, String id) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
114 StringBuffer buf = new StringBuffer();
115 buf.append(this.permissionsCheck(entry, id, Permission.READ, 'r'));
116 buf.append(this.permissionsCheck(entry, id, Permission.WRITE, 'w'));
117 buf.append(this.permissionsCheck(entry, id, Permission.EXEC, 'x'));
118 return buf.toString();
119 }
120 public char permissionsCheck(NSEntry entry, String id, Permission perm, char c) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
121 try {
122 if (entry.permissionsCheck(id, perm.getValue())) {
123 return c;
124 } else {
125 return '-';
126 }
127 } catch (BadParameterException e) {
128 return '?';
129 } catch (NotImplementedException e) {
130 return '?';
131 }
132 }
133
134 public String getSize(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
135 if (entry instanceof File) {
136 try {
137 return ""+((File)entry).getSize();
138 } catch (NotImplementedException e) {
139 return "?";
140 }
141 } else {
142 return "0";
143 }
144 }
145
146 public Date getMTime(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
147 try {
148 return new Date(entry.getMTime());
149 } catch (NotImplementedException e) {
150 return new Date(0);
151 }
152 }
153
154 public String getName(NSEntry entry) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
155 try {
156 URL nameUrl = entry.getName();
157
158 String name = (nameUrl!=null ? nameUrl.getString() : "");
159 if (entry.isDir()) {
160 name += "/";
161 }
162 return name;
163 } catch (NotImplementedException e) {
164 return "?";
165 }
166 }
167
168 private static StringBuffer format(String value, int length) {
169 StringBuffer buf = new StringBuffer();
170 for (int i=value.length(); i<length; i++) {
171 buf.append(' ');
172 }
173 buf.append(value);
174 return buf;
175 }
176 }