From f717beecb1f062b0edb5e1a92164e22ea5b87d44 Mon Sep 17 00:00:00 2001 From: fuchsi Date: Fri, 19 Oct 2007 04:13:46 +0000 Subject: [PATCH] - Changed yFormatter handling to be more flexible and produce more readable code for server pages. There are serverObject.putNum() methods to allow adding of number type values in a formatted form, and put() methods for number types that add them without formatting. This reduces the need to transform them into Strings in server pages and removes the HTML encoding step which is unecessary for numbers. - some minor code cleanups (mostly unnecessary casts, null checks) git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4166 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/ConfigBasic.java | 2 +- htroot/ConfigUpdate_p.java | 2 +- htroot/Connections_p.java | 2 +- htroot/CrawlStartExpert_p.java | 2 +- htroot/CrawlStartSimple_p.java | 2 +- htroot/IndexControl_p.java | 8 +- htroot/Network.java | 2 +- htroot/PerformanceMemory_p.java | 6 +- htroot/ViewFile.java | 4 +- htroot/ViewImage.java | 4 +- htroot/WebStructurePicture_p.java | 6 +- htroot/htdocsdefault/dir.java | 2 +- htroot/yacy/search.java | 2 +- htroot/yacy/transferRWI.java | 2 +- .../htmlFilter/htmlFilterContentScraper.java | 2 +- source/de/anomic/http/httpdProxyHandler.java | 6 +- .../anomic/kelondro/kelondroBufferedRA.java | 2 +- source/de/anomic/kelondro/kelondroRow.java | 2 +- source/de/anomic/plasma/plasmaCondenser.java | 4 +- source/de/anomic/plasma/plasmaGrafics.java | 6 +- .../de/anomic/plasma/plasmaSnippetCache.java | 4 +- .../de/anomic/server/logging/serverLog.java | 6 +- source/de/anomic/server/serverCodings.java | 8 +- source/de/anomic/server/serverObjects.java | 94 +++++++++++++++---- source/de/anomic/tools/yFormatter.java | 34 ++++--- source/de/anomic/ymage/ymageBMPParser.java | 18 ++-- source/de/anomic/ymage/ymageCaptcha.java | 10 +- 27 files changed, 156 insertions(+), 86 deletions(-) diff --git a/htroot/ConfigBasic.java b/htroot/ConfigBasic.java index c5ea20fa4..d639579a5 100644 --- a/htroot/ConfigBasic.java +++ b/htroot/ConfigBasic.java @@ -145,7 +145,7 @@ public class ConfigBasic { // port settings String port = env.getConfig("port", "8080"); //this allows a low port, but it will only get one, if the user edits the config himself. if(post!=null && Integer.parseInt((String)post.get("port"))>1023){ - port = (String)post.get("port", "8080"); + port = post.get("port", "8080"); } // admin password diff --git a/htroot/ConfigUpdate_p.java b/htroot/ConfigUpdate_p.java index 381e2835f..037e18464 100644 --- a/htroot/ConfigUpdate_p.java +++ b/htroot/ConfigUpdate_p.java @@ -130,7 +130,7 @@ public class ConfigUpdate_p { TreeSet downloadedreleases = new TreeSet(); for (int j = 0; j < downloaded.length; j++) { try { - release = (yacyVersion) new yacyVersion(downloaded[j]); + release = new yacyVersion(downloaded[j]); downloadedreleases.add(release); } catch (RuntimeException e) { // not a valid release diff --git a/htroot/Connections_p.java b/htroot/Connections_p.java index 770ab01e2..353ee1e46 100644 --- a/htroot/Connections_p.java +++ b/htroot/Connections_p.java @@ -244,7 +244,7 @@ public final class Connections_p { Arrays.sort(a, httpc.connectionTimeComparatorInstance); int c = 0; for (int i = 0; i < a.length; i++) { - httpc clientConnection = (httpc) a[i]; + httpc clientConnection = a[i]; if (clientConnection != null) { prop.put("clientList_" + c + "_clientProtocol", (clientConnection.ssl) ? "HTTPS" : "HTTP"); prop.put("clientList_" + c + "_clientLifetime", System.currentTimeMillis() - clientConnection.initTime); diff --git a/htroot/CrawlStartExpert_p.java b/htroot/CrawlStartExpert_p.java index cfa5cb92b..3dc403258 100644 --- a/htroot/CrawlStartExpert_p.java +++ b/htroot/CrawlStartExpert_p.java @@ -60,7 +60,7 @@ public class CrawlStartExpert_p { prop.put("crawlingIfOlderNumber", Math.round((float)crawlingIfOlder / (float)(60*24))); prop.put("crawlingIfOlderUnitDayCheck", 1); } else if (crawlingIfOlder >= 60) { - prop.put("crawlingIfOlderNumber", Math.round((float)crawlingIfOlder / 60f)); + prop.put("crawlingIfOlderNumber", Math.round(crawlingIfOlder / 60f)); prop.put("crawlingIfOlderUnitHourCheck", 1); } else { prop.put("crawlingIfOlderNumber", crawlingIfOlder); diff --git a/htroot/CrawlStartSimple_p.java b/htroot/CrawlStartSimple_p.java index 138f41339..84a3008c7 100644 --- a/htroot/CrawlStartSimple_p.java +++ b/htroot/CrawlStartSimple_p.java @@ -67,7 +67,7 @@ public class CrawlStartSimple_p { prop.put("crawlingIfOlderNumber", Math.round((float)crawlingIfOlder / (float)(60*24))); prop.put("crawlingIfOlderUnitDayCheck", 1); } else if (crawlingIfOlder >= 60) { - prop.put("crawlingIfOlderNumber", Math.round((float)crawlingIfOlder / 60f)); + prop.put("crawlingIfOlderNumber", Math.round(crawlingIfOlder / 60f)); prop.put("crawlingIfOlderUnitHourCheck", 1); } else { prop.put("crawlingIfOlderNumber", crawlingIfOlder); diff --git a/htroot/IndexControl_p.java b/htroot/IndexControl_p.java index 84e5d4f48..58b870b1b 100644 --- a/htroot/IndexControl_p.java +++ b/htroot/IndexControl_p.java @@ -97,10 +97,10 @@ public class IndexControl_p { } // default values - String keystring = ((String) post.get("keystring", "")).trim(); - String keyhash = ((String) post.get("keyhash", "")).trim(); - String urlstring = ((String) post.get("urlstring", "")).trim(); - String urlhash = ((String) post.get("urlhash", "")).trim(); + String keystring = post.get("keystring", "").trim(); + String keyhash = post.get("keyhash", "").trim(); + String urlstring = post.get("urlstring", "").trim(); + String urlhash = post.get("urlhash", "").trim(); if (!urlstring.startsWith("http://") && !urlstring.startsWith("https://")) { urlstring = "http://" + urlstring; } diff --git a/htroot/Network.java b/htroot/Network.java index 36f76cc00..d050c19f5 100644 --- a/htroot/Network.java +++ b/htroot/Network.java @@ -229,7 +229,7 @@ public class Network { peer = yacyCore.seedDB.getConnected(peer.hash); if (peer == null) { prop.put("table_comment",1); - prop.put("table_comment_status","publish: disconnected peer '" + peer.getName() + "/" + post.get("peerHash") + "' from " + peer.getPublicAddress()); + prop.put("table_comment_status","publish: disconnected peer 'UNKNOWN/" + post.get("peerHash") + "' from UNKNOWN"); } else { prop.put("table_comment",2); prop.put("table_comment_status","publish: handshaked " + peer.get(yacySeed.PEERTYPE, yacySeed.PEERTYPE_SENIOR) + " peer '" + peer.getName() + "' at " + peer.getPublicAddress()); diff --git a/htroot/PerformanceMemory_p.java b/htroot/PerformanceMemory_p.java index ad0f281e6..99e55e465 100644 --- a/htroot/PerformanceMemory_p.java +++ b/htroot/PerformanceMemory_p.java @@ -123,7 +123,7 @@ public class PerformanceMemory_p { long mem, totalmem = 0; while (i.hasNext()) { filename = (String) i.next(); - map = (Map) kelondroFlexTable.memoryStats(filename); + map = kelondroFlexTable.memoryStats(filename); mem = Long.parseLong((String) map.get("tableIndexMem")); totalmem += mem; prop.put("TableList_" + c + "_tableIndexPath", ((p = filename.indexOf("DATA")) < 0) ? filename : filename.substring(p)); @@ -141,7 +141,7 @@ public class PerformanceMemory_p { totalmem = 0; while (i.hasNext()) { filename = (String) i.next(); - map = (Map) kelondroCachedRecords.memoryStats(filename); + map = kelondroCachedRecords.memoryStats(filename); mem = Long.parseLong((String) map.get("nodeCacheMem")); totalmem += mem; prop.put("NodeList_" + c + "_nodeCachePath", ((p = filename.indexOf("DATA")) < 0) ? filename : filename.substring(p)); @@ -167,7 +167,7 @@ public class PerformanceMemory_p { long hitmem, missmem, totalhitmem = 0, totalmissmem = 0; while (i.hasNext()) { filename = (String) i.next(); - map = (Map) kelondroCache.memoryStats(filename); + map = kelondroCache.memoryStats(filename); prop.put("ObjectList_" + c + "_objectCachePath", ((p = filename.indexOf("DATA")) < 0) ? filename : filename.substring(p)); // hit cache diff --git a/htroot/ViewFile.java b/htroot/ViewFile.java index 806c0a0d9..9182df837 100644 --- a/htroot/ViewFile.java +++ b/htroot/ViewFile.java @@ -349,8 +349,8 @@ public class ViewFile { prop.put("viewMode_links_" + i + "_dark", ((dark) ? 1 : 0)); prop.put("viewMode_links_" + i + "_type", "image"); prop.putASIS("viewMode_links_" + i + "_text", markup(wordArray, entry.alt())); - prop.put("viewMode_links_" + i + "_url", (String) entry.url().toNormalform(false, true)); - prop.putASIS("viewMode_links_" + i + "_link", markup(wordArray, (String) entry.url().toNormalform(false, true))); + prop.put("viewMode_links_" + i + "_url", entry.url().toNormalform(false, true)); + prop.putASIS("viewMode_links_" + i + "_link", markup(wordArray, entry.url().toNormalform(false, true))); if (entry.width() > 0 && entry.height() > 0) prop.putASIS("viewMode_links_" + i + "_attr", entry.width() + "x" + entry.height() + " Pixel"); else diff --git a/htroot/ViewImage.java b/htroot/ViewImage.java index 65000d707..c898ee77f 100644 --- a/htroot/ViewImage.java +++ b/htroot/ViewImage.java @@ -155,8 +155,8 @@ public class ViewImage { double scale = Math.min(hs, vs); if (!auth) scale = Math.min(scale, 0.6); // this is for copyright purpose if (scale < 1.0) { - width = (int) (((double) w) * scale); - height = (int) (((double) h) * scale); + width = (int) (w * scale); + height = (int) (h * scale); } else { width = w; height = h; diff --git a/htroot/WebStructurePicture_p.java b/htroot/WebStructurePicture_p.java index df533f12d..22f57ddb9 100644 --- a/htroot/WebStructurePicture_p.java +++ b/htroot/WebStructurePicture_p.java @@ -43,7 +43,7 @@ import de.anomic.ymage.ymageToolPrint; public class WebStructurePicture_p { - private static final double maxlongd = (double) Long.MAX_VALUE; + private static final double maxlongd = Long.MAX_VALUE; public static ymageMatrix respond(httpHeader header, serverObjects post, serverSwitch env) { plasmaSwitchboard sb = (plasmaSwitchboard) env; @@ -125,7 +125,7 @@ public class WebStructurePicture_p { } if (nextlayer == maxlayer) return mynodes; nextlayer++; - double radius = 1.0 / ((double) (1 << nextlayer)); + double radius = 1.0 / (1 << nextlayer); Map next = structure.references(centerhash); Map.Entry entry; String targethash, targethost; @@ -147,7 +147,7 @@ public class WebStructurePicture_p { targets.add(new String[] {targethash, targethost}); if (graph.getPoint(targethost) != null) continue; // set a new point. It is placed on a circle around the host point - double angle = ((double) kelondroBase64Order.enhancedCoder.cardinal((targethash + "____").getBytes())) / maxlongd * 2 * Math.PI; + double angle = kelondroBase64Order.enhancedCoder.cardinal((targethash + "____").getBytes()) / maxlongd * 2 * Math.PI; //System.out.println("ANGLE = " + angle); rr = radius * 0.25 * (1 - targetrefs / maxtargetrefs); re = radius * 0.5 * (thisrefs / maxthisrefs); diff --git a/htroot/htdocsdefault/dir.java b/htroot/htdocsdefault/dir.java index 2498fe571..200cd4a18 100644 --- a/htroot/htdocsdefault/dir.java +++ b/htroot/htdocsdefault/dir.java @@ -377,7 +377,7 @@ public class dir { new Date(), // freshtime "AAAAAAAAAAAA", // referrer md5, // md5 - (long) phrase.length(), // size + phrase.length(), // size condenser.RESULT_NUMB_WORDS, // word count plasmaHTCache.DT_SHARE, // doctype new kelondroBitfield(4), diff --git a/htroot/yacy/search.java b/htroot/yacy/search.java index c13543ed3..6e05d1029 100644 --- a/htroot/yacy/search.java +++ b/htroot/yacy/search.java @@ -226,7 +226,7 @@ public final class search { //yacyCore.log.logFine("DEBUG HASH SEARCH: " + indexabstract); } } - if (partitions > 0) sb.requestedQueries = sb.requestedQueries + 1d / (double) partitions; // increase query counter + if (partitions > 0) sb.requestedQueries = sb.requestedQueries + 1d / partitions; // increase query counter // prepare reference hints localProcess.startTimer(); diff --git a/htroot/yacy/transferRWI.java b/htroot/yacy/transferRWI.java index bbf831975..7ff658500 100644 --- a/htroot/yacy/transferRWI.java +++ b/htroot/yacy/transferRWI.java @@ -75,7 +75,7 @@ public final class transferRWI { // request values final String iam = post.get("iam", ""); // seed hash of requester - final String youare = (String) post.get("youare", ""); // seed hash of the target peer, needed for network stability + final String youare = post.get("youare", ""); // seed hash of the target peer, needed for network stability // final String key = (String) post.get("key", ""); // transmission key final int wordc = Integer.parseInt(post.get("wordc", "")); // number of different words final int entryc = Integer.parseInt(post.get("entryc", "")); // number of entries in indexes diff --git a/source/de/anomic/htmlFilter/htmlFilterContentScraper.java b/source/de/anomic/htmlFilter/htmlFilterContentScraper.java index dfd8603eb..59814fda3 100644 --- a/source/de/anomic/htmlFilter/htmlFilterContentScraper.java +++ b/source/de/anomic/htmlFilter/htmlFilterContentScraper.java @@ -149,7 +149,7 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen if ((b.length() != 0) && (!(punctuation(b.charAt(b.length() - 1))))) b.append((int) '.'); //System.out.println("*** Appended dot: " + b.toString()); } - if (b.length() != 0) content.append(b).append((int) 32); + if (b.length() != 0) content.append(b).append(32); } public static final String splitrex = " |/|\\(|\\)|-|\\:|_|\\.|,|\\?|!|'|" + '"'; diff --git a/source/de/anomic/http/httpdProxyHandler.java b/source/de/anomic/http/httpdProxyHandler.java index 6c3a47295..1e6dfaefd 100644 --- a/source/de/anomic/http/httpdProxyHandler.java +++ b/source/de/anomic/http/httpdProxyHandler.java @@ -793,9 +793,9 @@ public final class httpdProxyHandler { if (charSet == null) charSet = httpHeader.DEFAULT_CHARSET; // make a transformer - if ((!(transformer.isIdentityTransformer())) && - ((ext == null) || (!(plasmaParser.supportedRealtimeFileExtContains(url)))) && - ((cachedResponseHeader == null) || (plasmaParser.realtimeParsableMimeTypesContains(cachedResponseHeader.mime())))) { + if (( !transformer.isIdentityTransformer()) && + (ext == null || !plasmaParser.supportedRealtimeFileExtContains(url)) && + (plasmaParser.realtimeParsableMimeTypesContains(cachedResponseHeader.mime()))) { hfos = new htmlFilterWriter((chunkedOut != null) ? chunkedOut : respond, charSet, null, transformer, (ext.length() == 0)); } else { hfos = (gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond); diff --git a/source/de/anomic/kelondro/kelondroBufferedRA.java b/source/de/anomic/kelondro/kelondroBufferedRA.java index 27f44590d..58a61f5e8 100644 --- a/source/de/anomic/kelondro/kelondroBufferedRA.java +++ b/source/de/anomic/kelondro/kelondroBufferedRA.java @@ -76,7 +76,7 @@ public class kelondroBufferedRA extends kelondroAbstractRA implements kelondroRA } public int read() throws IOException { - return (int) 0xff & sbb.byteAt((int) pos++); + return 0xff & sbb.byteAt((int) pos++); } public int read(byte[] b, int off, int len) throws IOException { diff --git a/source/de/anomic/kelondro/kelondroRow.java b/source/de/anomic/kelondro/kelondroRow.java index 0ae7495bc..5b5354d1d 100644 --- a/source/de/anomic/kelondro/kelondroRow.java +++ b/source/de/anomic/kelondro/kelondroRow.java @@ -538,7 +538,7 @@ public class kelondroRow { bb.append((new kelondroBitfield(getColBytes(i))).exportB64()); } else if ((decimalCardinal) && (row[i].celltype() == kelondroColumn.celltype_binary)) { assert row[i].cellwidth() == 1; - bb.append(Integer.toString((int) (0xff & getColByte(i)))); + bb.append(Integer.toString((0xff & getColByte(i)))); } else { bb.append(rowinstance, offset + colstart[i], row[i].cellwidth()); } diff --git a/source/de/anomic/plasma/plasmaCondenser.java b/source/de/anomic/plasma/plasmaCondenser.java index d2235a8ce..e47d14ecd 100644 --- a/source/de/anomic/plasma/plasmaCondenser.java +++ b/source/de/anomic/plasma/plasmaCondenser.java @@ -208,8 +208,8 @@ public final class plasmaCondenser { htmlFilterImageEntry ientry; while (i.hasNext()) { ientry = (htmlFilterImageEntry) i.next(); - insertTextToWords((String) ientry.url().toNormalform(false, true), 99, flag_cat_hasimage, wflags); - insertTextToWords((String) ientry.alt(), 99, flag_cat_hasimage, wflags); + insertTextToWords(ientry.url().toNormalform(false, true), 99, flag_cat_hasimage, wflags); + insertTextToWords(ientry.alt(), 99, flag_cat_hasimage, wflags); } // finally check all words for missing flag entry diff --git a/source/de/anomic/plasma/plasmaGrafics.java b/source/de/anomic/plasma/plasmaGrafics.java index b8f23760b..0bb761976 100644 --- a/source/de/anomic/plasma/plasmaGrafics.java +++ b/source/de/anomic/plasma/plasmaGrafics.java @@ -96,8 +96,8 @@ public class plasmaGrafics { this.color = color; } - public int getAngle() { return (int)Math.round(360f*this.fraction); } - public int getFractionPercent() { return (int)Math.round(100f*this.fraction); } + public int getAngle() { return Math.round(360f*this.fraction); } + public int getFractionPercent() { return Math.round(100f*this.fraction); } public Color getColor() { return this.color; } public long getExecTime() { return this.execTime; } public String getPieceName() { return this.pieceName; } @@ -133,7 +133,7 @@ public class plasmaGrafics { // get a copy of a recent network picture ymageMatrix eventPicture = getNetworkPicture(120000, plasmaSwitchboard.getSwitchboard().getConfig("network.unit.name", "unspecified"), plasmaSwitchboard.getSwitchboard().getConfig("network.unit.description", "unspecified"), COL_BACKGROUND); - if (eventPicture instanceof ymageMatrix) eventPicture = (ymageMatrix) eventPicture; //new ymageMatrix((ymageMatrix) eventPicture); + //if (eventPicture instanceof ymageMatrix) eventPicture = (ymageMatrix) eventPicture; //new ymageMatrix((ymageMatrix) eventPicture); // TODO: fix cloning of ymageMatrix pictures // get dimensions diff --git a/source/de/anomic/plasma/plasmaSnippetCache.java b/source/de/anomic/plasma/plasmaSnippetCache.java index dc0576d40..fcd91a775 100644 --- a/source/de/anomic/plasma/plasmaSnippetCache.java +++ b/source/de/anomic/plasma/plasmaSnippetCache.java @@ -682,8 +682,8 @@ public class plasmaSnippetCache { ArrayList result = new ArrayList(); while (i.hasNext()) { ientry = (htmlFilterImageEntry) i.next(); - url = (String) ientry.url().toNormalform(true, true); - desc = (String) ientry.alt(); + url = ientry.url().toNormalform(true, true); + desc = ientry.alt(); s = removeAppearanceHashes(url, queryhashes); if (s.size() == 0) { result.add(new MediaSnippet(plasmaSearchQuery.CONTENTDOM_IMAGE, url, desc, ientry.width() + " x " + ientry.height())); diff --git a/source/de/anomic/server/logging/serverLog.java b/source/de/anomic/server/logging/serverLog.java index 0b0e1dfc4..5ad30b947 100644 --- a/source/de/anomic/server/logging/serverLog.java +++ b/source/de/anomic/server/logging/serverLog.java @@ -225,8 +225,8 @@ public final class serverLog { if (b.length == 0) return "[]"; length = Math.min(length, b.length - start); StringBuffer sb = new StringBuffer(b.length * 4); - sb.append('[').append(Integer.toString((int) b[start])).append(','); - for (int i = 1; i < length; i++) sb.append(' ').append(Integer.toString((int) b[start + i])).append(','); + sb.append('[').append(Integer.toString(b[start])).append(','); + for (int i = 1; i < length; i++) sb.append(' ').append(Integer.toString(b[start + i])).append(','); sb.append(']'); return sb.toString(); } @@ -240,7 +240,7 @@ public final class serverLog { sb.append('\n').append("# ").append(Integer.toHexString(i)).append(": "); else sb.append(','); - sb.append(' ').append(Integer.toString(0xff & (int) b[i])); + sb.append(' ').append(Integer.toString(0xff & b[i])); if (i >= 65535) break; } sb.append('\n'); diff --git a/source/de/anomic/server/serverCodings.java b/source/de/anomic/server/serverCodings.java index bd48a266c..2ca70d9ed 100644 --- a/source/de/anomic/server/serverCodings.java +++ b/source/de/anomic/server/serverCodings.java @@ -72,8 +72,8 @@ public final class serverCodings { if (in == null) return ""; StringBuffer result = new StringBuffer(in.length * 8 / 3); for (int i = 0; i < in.length; i++) { - if ((0Xff & (int) in[i]) < 8) result.append('0'); - result.append(Integer.toOctalString(0Xff & (int) in[i])); + if ((0Xff & in[i]) < 8) result.append('0'); + result.append(Integer.toOctalString(0Xff & in[i])); } return new String(result); } @@ -82,8 +82,8 @@ public final class serverCodings { if (in == null) return ""; StringBuffer result = new StringBuffer(in.length * 2); for (int i = 0; i < in.length; i++) { - if ((0Xff & (int) in[i]) < 16) result.append('0'); - result.append(Integer.toHexString(0Xff & (int) in[i])); + if ((0Xff & in[i]) < 16) result.append('0'); + result.append(Integer.toHexString(0Xff & in[i])); } return new String(result); } diff --git a/source/de/anomic/server/serverObjects.java b/source/de/anomic/server/serverObjects.java index 7ce63ff6f..68acaa273 100644 --- a/source/de/anomic/server/serverObjects.java +++ b/source/de/anomic/server/serverObjects.java @@ -69,6 +69,7 @@ import java.util.Map; import de.anomic.data.htmlTools; import de.anomic.plasma.plasmaSwitchboard; +import de.anomic.tools.yFormatter; public class serverObjects extends Hashtable implements Cloneable { @@ -99,35 +100,102 @@ public class serverObjects extends Hashtable implements Cloneable { } } - // byte[] variant + /** + * Add byte array to the map, value is kept as it is. + * @param key key name as String. + * @param value mapped value as a byte array. + * @return the added value. + */ public byte[] put(String key, byte[] value) { return (byte[]) this.put((Object) key, (Object) value); //TODO: use wikiCode.replaceXMLEntities?! } - // string variant + + /** + * Add a String to the map. The content of the String is escaped to be usable in HTML output. + * @param key key name as String. + * @param value a String that will be reencoded for HTML output. + * @return the modified String that was added to the map. + * @see htmlTools#encodeUnicode2html(String, boolean) + */ public String put(String key, String value) { return (String) put((Object) key, (Object) htmlTools.encodeUnicode2html(value, true)); } + + /** + * Add an unformatted String representation of a double/float value + * to the map. + * @param key key name as String. + * @param value value as double/float. + * @return value as it was added to the map or NaN if an error occured. + */ + public double put(String key, double value) { + String ret = this.put(key, Double.toString(value)); + if (ret == null) { + return Double.NaN; + } else try { + return Double.parseDouble(ret); + } catch (NumberFormatException e) { + return Double.NaN; + } + } + + /** + * same as {@link #put(String, double)} but for integer types + * @return Returns 0 for the error case. + */ + public long put(String key, long value) { + String result = this.put(key, Long.toString(value)); + if (result == null) { + return 0; + } else try { + return Long.parseLong(result); + } catch (NumberFormatException e) { + return 0; + } + } + + /** + * Add a byte/long/integer to the map. The number will be encoded into a String using + * a localized format specified by {@link yFormatter}. + * @param key key name as String. + * @param value integer type value to be added to the map in its formatted String + * representation. + * @return the String value added to the map. + */ + public String putNum(String key, long value) { + return (String) this.put((Object) key, (Object) yFormatter.number(value)); + } + /** + * Variant for double/float types. + * @see #putNum(String, long) + */ + public String putNum(String key, double value) { + return (String) this.put((Object) key, (Object) yFormatter.number(value)); + } + + // ASIS methods don't reencode the values before adding them to the map public byte[] putASIS(String key, byte[] value) { return (byte[]) this.put((Object) key, (Object) value); } - public String putASIS(Object key, String value) { - return (String) this.put(key, (Object) value); + public String putASIS(String key, String value) { + return (String) this.put((Object) key, (Object) value); } - public String putWiki(Object key, String wikiCode){ + + public String putWiki(String key, String wikiCode){ return this.putASIS(key, plasmaSwitchboard.wikiParser.transform(wikiCode)); } - public String putWiki(Object key, byte[] wikiCode) { + public String putWiki(String key, byte[] wikiCode) { try { return this.putASIS(key, plasmaSwitchboard.wikiParser.transform(wikiCode)); } catch (UnsupportedEncodingException e) { return this.putASIS(key, "Internal error pasting wiki-code: " + e.getMessage()); } } - public String putWiki(Object key, String wikiCode, String publicAddress) { + public String putWiki(String key, String wikiCode, String publicAddress) { return this.putASIS(key, plasmaSwitchboard.wikiParser.transform(wikiCode, publicAddress)); } - public String putWiki(Object key, byte[] wikiCode, String publicAddress) { + public String putWiki(String key, byte[] wikiCode, String publicAddress) { try { return this.putASIS(key, plasmaSwitchboard.wikiParser.transform(wikiCode, "UTF-8", publicAddress)); } catch (UnsupportedEncodingException e) { @@ -135,16 +203,6 @@ public class serverObjects extends Hashtable implements Cloneable { } } - // long variant - public long put(String key, long value) { - String result = this.put(key, Long.toString(value)); - if (result == null) return 0; else try { - return Long.parseLong(result); - } catch (NumberFormatException e) { - return 0; - } - } - // inc variant: for counters public long inc(String key) { String c = (String) super.get(key); diff --git a/source/de/anomic/tools/yFormatter.java b/source/de/anomic/tools/yFormatter.java index 96e5cd314..288f61c44 100644 --- a/source/de/anomic/tools/yFormatter.java +++ b/source/de/anomic/tools/yFormatter.java @@ -54,29 +54,35 @@ import java.util.Locale; * to the locale set for YaCy. */ public final class yFormatter { + // default formatter private static NumberFormat numForm = NumberFormat.getInstance(new Locale("en")); + + // generic formatter that can be used when no localized formatting is allowed + private static final NumberFormat cleanNumForm = + new DecimalFormat("####.##", new DecimalFormatSymbols(Locale.ENGLISH)); + static { + // just initialize defaults on class load initDefaults(); } /** - * @param locale the locale to set + * @param locale the {@link Locale} to set or null to set the special + * empty locale to create unformatted numbers */ public static void setLocale(Locale locale) { - numForm = NumberFormat.getInstance(locale); + numForm = (locale == null ? cleanNumForm : NumberFormat.getInstance(locale)); + initDefaults(); } - + + /** + * @param lang an ISO 639 language code which is used to generate a {@link Locale} + */ public static void setLocale(String lang) { String l = (lang.equalsIgnoreCase("default") ? "en" : lang.toLowerCase()); - if (l.equals("none")) { - DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.ENGLISH); - dfs.setGroupingSeparator('.'); // not used - numForm = new DecimalFormat("####.##", dfs); - } else { - setLocale(new Locale(l)); - initDefaults(); - } + + setLocale(l.equals("none") ? null : new Locale(l)); } private static void initDefaults() { @@ -85,10 +91,16 @@ public final class yFormatter { numForm.setMaximumFractionDigits(2); // 2 decimal digits for float/double } + public static String number(double d, boolean localized) { + return (localized ? number(d) : cleanNumForm.format(d)); + } public static String number(double d) { return numForm.format(d); } + public static String number(long l, boolean localized) { + return (localized ? number(l) : cleanNumForm.format(l)); + } public static String number(long l) { return numForm.format(l); } diff --git a/source/de/anomic/ymage/ymageBMPParser.java b/source/de/anomic/ymage/ymageBMPParser.java index 975d2b99e..64b872aa6 100644 --- a/source/de/anomic/ymage/ymageBMPParser.java +++ b/source/de/anomic/ymage/ymageBMPParser.java @@ -76,20 +76,20 @@ public class ymageBMPParser { public static final int DWORD(byte[] b, int offset) { if (offset + 3 >= b.length) return 0; - int ret = ((int) b[offset + 3] & 0xff); - ret = (ret << 8) | ((int) b[offset + 2] & 0xff); - ret = (ret << 8) | ((int) b[offset + 1] & 0xff); - ret = (ret << 8) | ((int) b[offset] & 0xff); + int ret = (b[offset + 3] & 0xff); + ret = (ret << 8) | (b[offset + 2] & 0xff); + ret = (ret << 8) | (b[offset + 1] & 0xff); + ret = (ret << 8) | (b[offset] & 0xff); return ret; } public static final int WORD(byte[] b, int offset) { - int ret = (((int) b[offset + 1] & 0xff) << 8) | ((int) b[offset] & 0xff); + int ret = ((b[offset + 1] & 0xff) << 8) | (b[offset] & 0xff); return ret; } public static final int BYTE(byte[] b, int offset) { - int ret = ((int) b[offset] & 0xff); + int ret = (b[offset] & 0xff); return ret; } @@ -172,7 +172,7 @@ public class ymageBMPParser { int b; for (int rows = 0; rows < height; rows++) { for (int columns = 0; columns < width; columns = columns + 8) { - b = ((int) s[offset + n] & 0xff); + b = (s[offset + n] & 0xff); n++; image.setRGB(columns, (height - rows - 1), colortable.colorindex[(b & 0x80) >> 7]); image.setRGB(columns + 1, (height - rows - 1), colortable.colorindex[(b & 0x40) >> 6]); @@ -192,7 +192,7 @@ public class ymageBMPParser { int b; for (int rows = 0; rows < height; rows++) { for (int columns = 0; columns < width; columns = columns + 2) { - b = ((int) s[offset + n] & 0xff); + b = (s[offset + n] & 0xff); n++; image.setRGB(columns, (height - rows - 1), colortable.colorindex[(b & 0xf0) >> 4]); image.setRGB(columns + 1, (height - rows - 1), colortable.colorindex[b & 0xf]); @@ -205,7 +205,7 @@ public class ymageBMPParser { int n = 0; for (int rows = 0; rows < height; rows++) { for (int columns = 0; columns < width; columns++) { - image.setRGB(columns, (height - rows - 1), colortable.colorindex[((int) s[offset + n] & 0xff)]); + image.setRGB(columns, (height - rows - 1), colortable.colorindex[(s[offset + n] & 0xff)]); n++; } n += fill4(n); diff --git a/source/de/anomic/ymage/ymageCaptcha.java b/source/de/anomic/ymage/ymageCaptcha.java index 998ccf877..1e60555c4 100644 --- a/source/de/anomic/ymage/ymageCaptcha.java +++ b/source/de/anomic/ymage/ymageCaptcha.java @@ -63,21 +63,21 @@ public class ymageCaptcha extends ymageMatrix { int x; int y; int ub = 0; - int widthPerChar = (int)(width/chars); + int widthPerChar = width/chars; int pixels = width * height; //printing code for(int i=0;i