01: // FSizeCommand.java
02: // $Id: FSizeCommand.java,v 1.4 2000/08/16 21:37:47 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1997.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.ssi.commands;
07:
08: import java.util.Dictionary;
09:
10: import org.w3c.www.http.HTTP;
11:
12: import org.w3c.util.ArrayDictionary;
13:
14: import org.w3c.jigsaw.http.Reply;
15: import org.w3c.jigsaw.http.Request;
16:
17: import org.w3c.jigsaw.ssi.SSIFrame;
18:
19: /**
20: * Implementation of the SSI <code>fsize</code> command.
21: * It inserts the size of the unparsed file in the document,
22: * according to the current value of the variable <code>sizefmt</code>.
23: * @author Antonio Ramirez <anto@mit.edu>
24: */
25: public class FSizeCommand extends BasicCommand {
26: private final static String NAME = "fsize";
27: private static final long MBsize = 1024 * 1024;
28: private static final long KBsize = 1024;
29:
30: public Reply execute(SSIFrame ssiframe, Request request,
31: ArrayDictionary parameters, Dictionary variables) {
32: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
33:
34: Dictionary ssiVars = (Dictionary) variables.get(parameters
35: .get("here") == null ? "topSsiVars" : "ssiVars");
36:
37: String sizefmt = (String) variables.get("sizefmt");
38:
39: Long Fsize = (Long) ssiVars.get("X_FILE_SIZE");
40:
41: long fsize = Fsize.longValue();
42:
43: if (sizefmt == null || sizefmt.equalsIgnoreCase("bytes"))
44: reply.setContent(withCommas(fsize));
45: else if (sizefmt.equalsIgnoreCase("abbrev")) {
46: String unit = null;
47: long cut = 1;
48: if (fsize >= MBsize) {
49: unit = " MB";
50: cut = MBsize;
51: } else if (fsize >= KBsize) {
52: unit = " KB";
53: cut = KBsize;
54: } else {
55: reply.setContent(withCommas(fsize) + " bytes");
56: }
57: if (cut != 1) {
58: double n = (double) fsize / cut;
59: long ip = (long) n;
60: int fp = (int) (100 * (n - ip));
61:
62: reply.setContent(withCommas(ip) + "." + fp + unit);
63: }
64: }
65:
66: handleSimpleIMS(request, reply);
67: return reply;
68:
69: }
70:
71: private String withCommas(long n) {
72: String nstr = String.valueOf(n);
73: StringBuffer buf = new StringBuffer(20);
74: int length = nstr.length();
75: for (int i = 0; i < length; i++) {
76: buf.append(nstr.charAt(i));
77: if ((length - i) % 3 == 1 && i + 1 != length)
78: buf.append(',');
79: }
80: return buf.toString();
81:
82: }
83:
84: public String getName() {
85: return NAME;
86: }
87:
88: public String getValue(Dictionary variables, String variable,
89: Request request) {
90: return "null";
91: }
92:
93: }
|