001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.ssi;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.text.DecimalFormat;
022:
023: /**
024: * Implements the Server-side #fsize command
025: *
026: * @author Bip Thelin
027: * @author Dan Sandberg
028: * @version $Revision: 1.2 $, $Date: 2004/02/27 14:58:47 $
029: */
030: public final class SSIFsize implements SSICommand {
031: protected final static int ONE_KILOBYTE = 1024;
032: protected final static int ONE_MEGABYTE = 1024 * 1024;
033:
034: /**
035: * @see SSICommand
036: */
037: public void process(SSIMediator ssiMediator, String[] paramNames,
038: String[] paramValues, PrintWriter writer) {
039:
040: String configErrMsg = ssiMediator.getConfigErrMsg();
041: for (int i = 0; i < paramNames.length; i++) {
042: String paramName = paramNames[i];
043: String paramValue = paramValues[i];
044:
045: try {
046: if (paramName.equalsIgnoreCase("file")
047: || paramName.equalsIgnoreCase("virtual")) {
048: boolean virtual = paramName
049: .equalsIgnoreCase("virtual");
050: long size = ssiMediator.getFileSize(paramValue,
051: virtual);
052: String configSizeFmt = ssiMediator
053: .getConfigSizeFmt();
054: writer.write(formatSize(size, configSizeFmt));
055: } else {
056: ssiMediator.log("#fsize--Invalid attribute: "
057: + paramName);
058: writer.write(configErrMsg);
059: }
060: } catch (IOException e) {
061: ssiMediator.log("#fsize--Couldn't get size for file: "
062: + paramValue, e);
063: writer.write(configErrMsg);
064: }
065: }
066: }
067:
068: public String repeat(char aChar, int numChars) {
069: if (numChars < 0) {
070: throw new IllegalArgumentException(
071: "Num chars can't be negative");
072: }
073: StringBuffer buf = new StringBuffer();
074: for (int i = 0; i < numChars; i++) {
075: buf.append(aChar);
076: }
077: return buf.toString();
078: }
079:
080: public String padLeft(String str, int maxChars) {
081: String result = str;
082: int charsToAdd = maxChars - str.length();
083: if (charsToAdd > 0) {
084: result = repeat(' ', charsToAdd) + str;
085: }
086: return result;
087: }
088:
089: //We try to mimick Apache here, as we do everywhere
090: //All the 'magic' numbers are from the util_script.c Apache source file.
091: protected String formatSize(long size, String format) {
092: String retString = "";
093:
094: if (format.equalsIgnoreCase("bytes")) {
095: DecimalFormat decimalFormat = new DecimalFormat("#,##0");
096: retString = decimalFormat.format(size);
097: } else {
098: if (size == 0) {
099: retString = "0k";
100: } else if (size < ONE_KILOBYTE) {
101: retString = "1k";
102: } else if (size < ONE_MEGABYTE) {
103: retString = Long.toString((size + 512) / ONE_KILOBYTE);
104: retString += "k";
105: } else if (size < 99 * ONE_MEGABYTE) {
106: DecimalFormat decimalFormat = new DecimalFormat("0.0M");
107: retString = decimalFormat.format(size
108: / (double) ONE_MEGABYTE);
109: } else {
110: retString = Long.toString((size + (529 * ONE_KILOBYTE))
111: / ONE_MEGABYTE);
112: retString += "M";
113: }
114: retString = padLeft(retString, 5);
115: }
116:
117: return retString;
118: }
119: }
|