Add a generalized Formatter class yFormatter inspired by http://forum.yacy-websuche.de/viewtopic.php?f=5&t=437

At the current state it allows formatting of numbers (integer + decimal types) for output according to the Locale derived from the language setting in yacy. Network.(html|xml) and Status.html have been changed to use it for now (TODO: should be integrated into other servlets as well to reduce duplicate formatting code).
NOTE: For now the output format for Network.xml simulates the old behaviour which is wrong (it uses '.' as decimal and grouping separator), to make sure external scripts like the yacystats.de one won't break with this update.

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4162 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
fuchsi 18 years ago
parent e77aec8c9d
commit 06e6a1ff62

@ -61,6 +61,7 @@ import de.anomic.server.serverCodings;
import de.anomic.server.serverDate;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.tools.yFormatter;
import de.anomic.yacy.yacyClient;
import de.anomic.yacy.yacyCore;
import de.anomic.yacy.yacyNewsPool;
@ -73,8 +74,12 @@ public class Network {
private static final String STR_TABLE_LIST = "table_list_";
public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch switchboard) {
boolean isXML = ((String)header.get("PATH")).endsWith(".xml");
plasmaSwitchboard sb = (plasmaSwitchboard) switchboard;
final long start = System.currentTimeMillis();
if (isXML) {
yFormatter.setLocale("none");
}
// return variable that accumulates replacements
final serverObjects prop = new serverObjects();
@ -146,19 +151,19 @@ public class Network {
prop.put("table_my-version", seed.get(yacySeed.VERSION, "-"));
prop.put("table_my-utc", seed.get(yacySeed.UTC, "-"));
prop.put("table_my-uptime", serverDate.intervalToString(60000 * Long.parseLong(seed.get(yacySeed.UPTIME, ""))));
prop.put("table_my-LCount", groupDigits(LCount));
prop.put("table_my-ICount", groupDigits(ICount));
prop.put("table_my-RCount", groupDigits(RCount));
prop.put("table_my-sI", groupDigits(seed.get(yacySeed.INDEX_OUT, "0")));
prop.put("table_my-sU", groupDigits(seed.get(yacySeed.URL_OUT, "0")));
prop.put("table_my-rI", groupDigits(seed.get(yacySeed.INDEX_IN, "0")));
prop.put("table_my-rU", groupDigits(seed.get(yacySeed.URL_IN, "0")));
prop.put("table_my-LCount", yFormatter.number(LCount));
prop.put("table_my-ICount", yFormatter.number(ICount));
prop.put("table_my-RCount", yFormatter.number(RCount));
prop.put("table_my-sI", yFormatter.number(seed.get(yacySeed.INDEX_OUT, "0")));
prop.put("table_my-sU", yFormatter.number(seed.get(yacySeed.URL_OUT, "0")));
prop.put("table_my-rI", yFormatter.number(seed.get(yacySeed.INDEX_IN, "0")));
prop.put("table_my-rU", yFormatter.number(seed.get(yacySeed.URL_IN, "0")));
prop.put("table_my-ppm", myppm);
prop.put("table_my-qph", Double.toString(Math.round(100d * myqph) / 100d));
prop.put("table_my-totalppm", sb.totalPPM);
prop.put("table_my-totalqph", Double.toString(Math.round(6000d * sb.totalQPM) / 100d));
prop.put("table_my-seeds", seed.get(yacySeed.SCOUNT, "-"));
prop.put("table_my-connects", groupDigits(seed.get(yacySeed.CCOUNT, "0")));
prop.put("table_my-qph", yFormatter.number(Math.round(100d * myqph) / 100d));
prop.put("table_my-totalppm", yFormatter.number(sb.totalPPM));
prop.put("table_my-totalqph", yFormatter.number(Math.round(6000d * sb.totalQPM) / 100d));
prop.put("table_my-seeds", yFormatter.number(seed.get(yacySeed.SCOUNT, "-")));
prop.put("table_my-connects", yFormatter.number(seed.get(yacySeed.CCOUNT, "0")));
prop.put("table_my-url", seed.get("seedURL", ""));
// generating the location string
@ -171,20 +176,20 @@ public class Network {
// overall results: Network statistics
if (iAmActive) conCount++; else if (mySeedType.equals(yacySeed.PEERTYPE_JUNIOR)) potCount++;
prop.put("table_active-count", conCount);
prop.put("table_active-links", groupDigits(accActLinks));
prop.put("table_active-words", groupDigits(accActWords));
prop.put("table_active-links", yFormatter.number(accActLinks));
prop.put("table_active-words", yFormatter.number(accActWords));
prop.put("table_passive-count", disconCount);
prop.put("table_passive-links", groupDigits(accPassLinks));
prop.put("table_passive-words", groupDigits(accPassWords));
prop.put("table_passive-links", yFormatter.number(accPassLinks));
prop.put("table_passive-words", yFormatter.number(accPassWords));
prop.put("table_potential-count", potCount);
prop.put("table_potential-links", groupDigits(accPotLinks));
prop.put("table_potential-words", groupDigits(accPotWords));
prop.put("table_potential-links", yFormatter.number(accPotLinks));
prop.put("table_potential-words", yFormatter.number(accPotWords));
prop.put("table_all-count", (conCount + disconCount + potCount));
prop.put("table_all-links", groupDigits(accActLinks + accPassLinks + accPotLinks));
prop.put("table_all-words", groupDigits(accActWords + accPassWords + accPotWords));
prop.put("table_all-links", yFormatter.number(accActLinks + accPassLinks + accPotLinks));
prop.put("table_all-words", yFormatter.number(accActWords + accPassWords + accPotWords));
prop.put("table_gppm", otherppm + ((iAmActive) ? myppm : 0));
prop.put("table_gqph", Double.toString(Math.round(6000d * otherqpm + 100d * ((iAmActive) ? myqph : 0d)) / 100d));
prop.put("table_gqph", yFormatter.number(Math.round(6000d * otherqpm + 100d * ((iAmActive) ? myqph : 0d)) / 100d));
// String comment = "";
prop.put("table_comment", 0);
@ -393,7 +398,7 @@ public class Network {
prop.put(STR_TABLE_LIST + conCount + "_complete_CRWCnt", seed.get(yacySeed.CRWCNT, "0"));
prop.put(STR_TABLE_LIST + conCount + "_complete_CRTCnt", seed.get(yacySeed.CRTCNT, "0"));
prop.put(STR_TABLE_LIST + conCount + "_complete_seeds", seed.get(yacySeed.SCOUNT, "-"));
prop.put(STR_TABLE_LIST + conCount + "_complete_connects", groupDigits(seed.get(yacySeed.CCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_complete_connects", yFormatter.number(seed.get(yacySeed.CCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_complete_userAgent", userAgent);
} else {
prop.put(STR_TABLE_LIST + conCount + "_complete", 0);
@ -452,15 +457,15 @@ public class Network {
prop.put(STR_TABLE_LIST + conCount + "_lastSeen", /*seed.getLastSeenString() + " " +*/ lastseen);
prop.put(STR_TABLE_LIST + conCount + "_utc", seed.get(yacySeed.UTC, "-"));
prop.put(STR_TABLE_LIST + conCount + "_uptime", serverDate.intervalToString(60000 * Long.parseLong(seed.get(yacySeed.UPTIME, "0"))));
prop.put(STR_TABLE_LIST + conCount + "_LCount", groupDigits(seed.get(yacySeed.LCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_ICount", groupDigits(seed.get(yacySeed.ICOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_RCount", groupDigits(seed.get(yacySeed.RCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_sI", groupDigits(seed.get(yacySeed.INDEX_OUT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_sU", groupDigits(seed.get(yacySeed.URL_OUT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_rI", groupDigits(seed.get(yacySeed.INDEX_IN, "0")));
prop.put(STR_TABLE_LIST + conCount + "_rU", groupDigits(seed.get(yacySeed.URL_IN, "0")));
prop.put(STR_TABLE_LIST + conCount + "_LCount", yFormatter.number(seed.get(yacySeed.LCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_ICount", yFormatter.number(seed.get(yacySeed.ICOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_RCount", yFormatter.number(seed.get(yacySeed.RCOUNT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_sI", yFormatter.number(seed.get(yacySeed.INDEX_OUT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_sU", yFormatter.number(seed.get(yacySeed.URL_OUT, "0")));
prop.put(STR_TABLE_LIST + conCount + "_rI", yFormatter.number(seed.get(yacySeed.INDEX_IN, "0")));
prop.put(STR_TABLE_LIST + conCount + "_rU", yFormatter.number(seed.get(yacySeed.URL_IN, "0")));
prop.put(STR_TABLE_LIST + conCount + "_ppm", PPM);
prop.put(STR_TABLE_LIST + conCount + "_qph", Double.toString(Math.round(6000d * QPM) / 100d));
prop.put(STR_TABLE_LIST + conCount + "_qph", yFormatter.number(Math.round(6000d * QPM) / 100d));
conCount++;
} // seed != null
} // while
@ -482,26 +487,13 @@ public class Network {
default: break;
}
}
prop.put("table_rt", System.currentTimeMillis() - start);
if (isXML) {
// restore locale for formatter
yFormatter.setLocale(sb.getConfig("locale.lang", "default"));
}
// return rewrite properties
return prop;
}
private static String groupDigits(String sValue) {
long lValue;
try {
if (sValue.endsWith(".0")) { sValue = sValue.substring(0, sValue.length() - 2); } // for Connects per hour, why float ?
lValue = Long.parseLong(sValue);
} catch (Exception e) {lValue = 0;}
if (lValue == 0) { return "-"; }
return groupDigits(lValue);
}
private static String groupDigits(long Number) {
final String s = Long.toString(Number);
String t = "";
for (int i = 0; i < s.length(); i++) t = s.charAt(s.length() - i - 1) + (((i % 3) == 0) ? "." : "") + t;
return t.substring(0, t.length() - 1);
}
}

@ -47,7 +47,6 @@
// if the shell's current path is HTROOT
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import de.anomic.http.httpHeader;
@ -61,6 +60,7 @@ import de.anomic.server.serverDate;
import de.anomic.server.serverMemory;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.tools.yFormatter;
import de.anomic.yacy.yacyCore;
import de.anomic.yacy.yacySeed;
import de.anomic.yacy.yacyURL;
@ -217,14 +217,14 @@ public class Status {
prop.put("peerStatistics", 1);
prop.put("peerStatistics_uptime", serverDate.intervalToString(uptime));
prop.put("peerStatistics_pagesperminute", yacyCore.seedDB.mySeed().get(yacySeed.ISPEED, "unknown"));
prop.put("peerStatistics_queriesperhour", Double.toString(Math.round(6000d * yacyCore.seedDB.mySeed().getQPM()) / 100d));
prop.put("peerStatistics_links", groupDigits(yacyCore.seedDB.mySeed().get(yacySeed.LCOUNT, "0")));
prop.put("peerStatistics_words", groupDigits(yacyCore.seedDB.mySeed().get(yacySeed.ICOUNT, "0")));
prop.put("peerStatistics_queriesperhour", yFormatter.number(Math.round(6000d * yacyCore.seedDB.mySeed().getQPM()) / 100d));
prop.put("peerStatistics_links", yFormatter.number(yacyCore.seedDB.mySeed().get(yacySeed.LCOUNT, "0")));
prop.put("peerStatistics_words", yFormatter.number(yacyCore.seedDB.mySeed().get(yacySeed.ICOUNT, "0")));
prop.put("peerStatistics_juniorConnects", yacyCore.peerActions.juniorConnects);
prop.put("peerStatistics_seniorConnects", yacyCore.peerActions.seniorConnects);
prop.put("peerStatistics_principalConnects", yacyCore.peerActions.principalConnects);
prop.put("peerStatistics_disconnects", yacyCore.peerActions.disconnects);
prop.put("peerStatistics_connects", yacyCore.seedDB.mySeed().get(yacySeed.CCOUNT, "0"));
prop.put("peerStatistics_connects", yFormatter.number(yacyCore.seedDB.mySeed().get(yacySeed.CCOUNT, "0")));
if (yacyCore.seedDB.mySeed().getPublicAddress() == null) {
thisHash = yacyCore.seedDB.mySeed().hash;
prop.put("peerAddress", 0); // not assigned + instructions
@ -285,7 +285,7 @@ public class Status {
if (yacyCore.seedDB != null && yacyCore.seedDB.sizeConnected() > 0){
prop.put("otherPeers", 1);
prop.put("otherPeers_num", yacyCore.seedDB.sizeConnected());
prop.put("otherPeers_num", yFormatter.number(yacyCore.seedDB.sizeConnected()));
}else{
prop.put("otherPeers", 0); // not online
}
@ -361,18 +361,17 @@ public class Status {
try {
final StringBuffer byteString = new StringBuffer();
final DecimalFormat df = new DecimalFormat( "0.00" );
if (byteCount > 1073741824) {
byteString.append(df.format((double)byteCount / (double)1073741824 ))
byteString.append(yFormatter.number(byteCount / 1073741824.0 ))
.append(" GB");
} else if (byteCount > 1048576) {
byteString.append(df.format((double)byteCount / (double)1048576))
byteString.append(yFormatter.number(byteCount / 1048576.0))
.append(" MB");
} else if (byteCount > 1024) {
byteString.append(df.format((double)byteCount / (double)1024))
byteString.append(yFormatter.number(byteCount / 1024.0))
.append(" KB");
} else {
byteString.append(Long.toString(byteCount))
byteString.append(yFormatter.number(byteCount))
.append(" Bytes");
}
@ -386,21 +385,4 @@ public class Status {
//TODO: groupDigits-functions (Status.java & Network.java) should
// be referenced in a single class (now double-implemented)
private static String groupDigits(String sValue) {
long lValue;
try {
if (sValue.endsWith(".0")) { sValue = sValue.substring(0, sValue.length() - 2); } // for Connects per hour, why float ?
lValue = Long.parseLong(sValue);
} catch (Exception e) {lValue = 0;}
if (lValue == 0) { return "-"; }
return groupDigits(lValue);
}
private static String groupDigits(long Number) {
final String s = Long.toString(Number);
String t = "";
for (int i = 0; i < s.length(); i++) t = s.charAt(s.length() - i - 1) + (((i % 3) == 0) ? "." : "") + t;
return t.substring(0, t.length() - 1);
}
}

@ -69,6 +69,7 @@ import java.util.regex.Pattern;
import de.anomic.server.serverSwitch;
import de.anomic.server.logging.serverLog;
import de.anomic.tools.yFormatter;
/**
* Wordlist based translator
@ -266,6 +267,7 @@ public class translator {
if(translator.translateFilesRecursive(sourceDir, destDir,
translationFile, "html,template,inc", "locale")){
env.setConfig("locale.language", lang.substring(0, lang.length() - 4));
yFormatter.setLocale(env.getConfig("locale.language", "en"));
try {
BufferedWriter bw = new BufferedWriter(new PrintWriter(new FileWriter(new File(destDir, "version"))));
bw.write(env.getConfig("svnRevision", "Error getting Version"));

@ -0,0 +1,118 @@
// yFormatter.java
// -----------------------
// part of YACY
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
//
// (C) 2007 Bjoern 'Fuchs' Krombholz; fox.box@gmail.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.
package de.anomic.tools;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
/**
* This class provides simple Formatters to unify YaCy's output
* formattings.
*
* At the moment yFormatter can be used to format numbers according
* to the locale set for YaCy.
*/
public final class yFormatter {
private static NumberFormat numForm = NumberFormat.getInstance(new Locale("en"));
static {
initDefaults();
}
/**
* @param locale the locale to set
*/
public static void setLocale(Locale locale) {
numForm = NumberFormat.getInstance(locale);
}
public static void setLocale(String lang) {
String l = (lang.equalsIgnoreCase("default") ? "en" : lang.toLowerCase());
if (l.equals("none")) {
// TODO fix the following without breaking yacystats and similar.
// special format for backwards compatibility in .xml files
// #.###.## (dots for grouping + decimal separator)
DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.ENGLISH);
dfs.setGroupingSeparator('.');
numForm = new DecimalFormat("#,###.##", dfs);
} else {
setLocale(new Locale(l));
initDefaults();
}
}
private static void initDefaults() {
numForm.setGroupingUsed(true); // always group int digits
numForm.setParseIntegerOnly(false); // allow int/double/float
numForm.setMaximumFractionDigits(2); // 2 decimal digits for float/double
}
public static String number(double d) {
return numForm.format(d);
}
public static String number(long l) {
return numForm.format(l);
}
/**
* Method formats String representation of numbers according to the formatting
* rules for numbers defined by this class. This method is probably only useful
* for "numbers" read from property files.
* @param s string to parse into a number and reformat
* @return the formatted number as a String or "-" in case of a parsing error
*/
public static String number(String s) {
String ret = null;
try {
if (s.indexOf('.') == -1) {
ret = number(Long.parseLong(s));
} else {
ret = number(Double.parseDouble(s));
}
} catch (NumberFormatException e) { /* empty */ }
return (ret == null ? "-" : ret);
}
}

@ -89,6 +89,7 @@ import de.anomic.server.serverSemaphore;
import de.anomic.server.serverSystem;
import de.anomic.server.logging.serverLog;
import de.anomic.tools.enumerateFiles;
import de.anomic.tools.yFormatter;
import de.anomic.yacy.yacyClient;
import de.anomic.yacy.yacySeedDB;
import de.anomic.yacy.yacyURL;
@ -383,7 +384,9 @@ public final class yacy {
}
} catch (IOException e) {}
}
// initialize number formatter with this locale
yFormatter.setLocale(lang);
// registering shutdown hook
serverLog.logConfig("STARTUP", "Registering Shutdown Hook");
final Runtime run = Runtime.getRuntime();

Loading…
Cancel
Save