@@ -66,6 +66,16 @@ public class DirectoryList extends BasicFunction {
66
66
final static String NAMESPACE_URI = FileModule .NAMESPACE_URI ;
67
67
final static String PREFIX = FileModule .PREFIX ;
68
68
69
+ final static QName FILE_ELEMENT = new QName ("file" , NAMESPACE_URI , PREFIX );
70
+ final static QName LIST_ELEMENT = new QName ("list" , NAMESPACE_URI , PREFIX );
71
+
72
+ final static QName DIRECTORY_ATTRIBUTE = new QName ("directory" , null , null );
73
+ final static QName NAME_ATTRIBUTE = new QName ("name" , null , null );
74
+ final static QName SIZE_ATTRIBUTE = new QName ("size" , null , null );
75
+ final static QName HUMAN_SIZE_ATTRIBUTE = new QName ("human-size" , null , null );
76
+ final static QName MODIFIED_ATTRIBUTE = new QName ("modified" , null , null );
77
+ final static QName SUBDIR_ATTRIBUTE = new QName ("subdir" , null , null );
78
+
69
79
public final static FunctionSignature [] signatures
70
80
= {
71
81
new FunctionSignature (
@@ -114,8 +124,8 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
114
124
final MemTreeBuilder builder = context .getDocumentBuilder ();
115
125
116
126
builder .startDocument ();
117
- builder .startElement (new QName ( "list" , NAMESPACE_URI , PREFIX ) , null );
118
- builder .addAttribute (new QName ( "directory" , null , null ) , baseDir .toString ());
127
+ builder .startElement (LIST_ELEMENT , null );
128
+ builder .addAttribute (DIRECTORY_ATTRIBUTE , baseDir .toString ());
119
129
try {
120
130
final int patternsLen = patterns .getItemCount ();
121
131
final String [] includes = new String [patternsLen ];
@@ -136,34 +146,29 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
136
146
logger .debug ("Found: {}" , file .toAbsolutePath ());
137
147
}
138
148
139
- String relPath = file .toString ().substring (baseDir .toString ().length () + 1 );
140
-
141
- int lastSeparatorPosition = relPath .lastIndexOf (java .io .File .separatorChar );
149
+ final String relPath = file .toString ().substring (baseDir .toString ().length () + 1 );
142
150
143
- String relDir = null ;
144
- if (lastSeparatorPosition >= 0 ) {
145
- relDir = relPath .substring (0 , lastSeparatorPosition );
146
- relDir = relDir .replace (java .io .File .separatorChar , '/' );
147
- }
151
+ builder .startElement (FILE_ELEMENT , null );
152
+ builder .addAttribute (NAME_ATTRIBUTE , FileUtils .fileName (file ));
148
153
149
- builder .startElement (new QName ("file" , NAMESPACE_URI , PREFIX ), null );
154
+ final long sizeLong = FileUtils .sizeQuietly (file );
155
+ builder .addAttribute (SIZE_ATTRIBUTE , Long .toString (sizeLong ));
156
+ builder .addAttribute (HUMAN_SIZE_ATTRIBUTE , getHumanSize (sizeLong ));
150
157
151
- builder .addAttribute (new QName ("name" , null , null ), FileUtils .fileName (file ));
158
+ builder .addAttribute (MODIFIED_ATTRIBUTE ,
159
+ new DateTimeValue (this ,
160
+ new Date (Files .getLastModifiedTime (file ).toMillis ())).getStringValue ());
152
161
153
- Long sizeLong = FileUtils .sizeQuietly (file );
154
- String sizeString = Long .toString (sizeLong );
155
- String humanSize = getHumanSize (sizeLong , sizeString );
156
-
157
- builder .addAttribute (new QName ("size" , null , null ), sizeString );
158
- builder .addAttribute (new QName ("human-size" , null , null ), humanSize );
159
- builder .addAttribute (new QName ("modified" , null , null ), new DateTimeValue (this , new Date (Files .getLastModifiedTime (file ).toMillis ())).getStringValue ());
160
-
161
- if (relDir != null && !relDir .isEmpty ()) {
162
- builder .addAttribute (new QName ("subdir" , null , null ), relDir );
162
+ final int lastSeparatorPosition = relPath .lastIndexOf (java .io .File .separatorChar );
163
+ if (lastSeparatorPosition >= 0 ) {
164
+ final String relDir = relPath .substring (0 , lastSeparatorPosition );
165
+ if (!relDir .isEmpty ()) {
166
+ builder .addAttribute (SUBDIR_ATTRIBUTE ,
167
+ relDir .replace (java .io .File .separatorChar , '/' ));
168
+ }
163
169
}
164
170
165
171
builder .endElement ();
166
-
167
172
}
168
173
169
174
builder .endElement ();
@@ -176,36 +181,17 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
176
181
}
177
182
}
178
183
179
- private String getHumanSize (final Long sizeLong , final String sizeString ) {
180
- String humanSize = "n/a" ;
181
- int sizeDigits = sizeString .length ();
182
-
183
- if (sizeDigits < 4 ) {
184
- humanSize = Long .toString (Math .abs (sizeLong ));
185
-
186
- } else if (sizeDigits >= 4 && sizeDigits <= 6 ) {
187
- if (sizeLong < 1024 ) {
188
- // We don't want 0KB för e.g. 1006 Bytes.
189
- humanSize = Long .toString (Math .abs (sizeLong ));
190
- } else {
191
- humanSize = Math .abs (sizeLong / 1024 ) + "KB" ;
192
- }
193
-
194
- } else if (sizeDigits >= 7 && sizeDigits <= 9 ) {
195
- if (sizeLong < 1048576 ) {
196
- humanSize = Math .abs (sizeLong / 1024 ) + "KB" ;
197
- } else {
198
- humanSize = Math .abs (sizeLong / (1024 * 1024 )) + "MB" ;
199
- }
200
-
201
- } else if (sizeDigits > 9 ) {
202
- if (sizeLong < 1073741824 ) {
203
- humanSize = Math .abs ((sizeLong / (1024 * 1024 ))) + "MB" ;
204
- } else {
205
- humanSize = Math .abs ((sizeLong / (1024 * 1024 * 1024 ))) + "GB" ;
206
- }
184
+ private String getHumanSize (final Long sizeLong ) {
185
+ if (sizeLong < 1024 ) {
186
+ return Math .abs (sizeLong ) + "B" ;
187
+ }
188
+ if (sizeLong < 1048576 ) {
189
+ return Math .abs (sizeLong / 1024 ) + "KB" ;
190
+ }
191
+ if (sizeLong < 1073741824 ) {
192
+ return Math .abs ((sizeLong / (1024 * 1024 ))) + "MB" ;
207
193
}
208
- return humanSize ;
194
+ return Math . abs (( sizeLong / ( 1024 * 1024 * 1024 ))) + "GB" ;
209
195
}
210
196
211
197
}
0 commit comments