Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

*) restructuring needed to support parsing of documents using various charsets - serverFileUtils.java: -- adding methods to copy from stream to writer and readers to writers -- moving httpc writeX methods into serverFileUtils class - serverCharBuffer.java: removing inheritance from Writer class - replacing htmlFilterOutputStream by htmlFilterWriter class which handles content as char stream - htmlFilterContentTransformer.java: deactivating getText mode (still needs to be migrated to use char streams instead of byte streams) - changes in several classes to use htmlFilterWriter instead of htmlFilterOutputStream - changes in Scraper and Transformer classes to operate on chars instead of bytes - httpdProxyHandler.java: bugfix. clientTimeout setting was missing in config file

theli97d2a08ef1c8
diff --git a/htroot/CacheAdmin_p.java b/htroot/CacheAdmin_p.java
index cb9c41a39..3fbc3ccbf 100644
--- a/htroot/CacheAdmin_p.java
+++ b/htroot/CacheAdmin_p.java
@@ -48,13 +48,15 @@
import java.io.File;
import java.io.OutputStream;
-import de.anomic.net.URL;
+import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
+
import de.anomic.htmlFilter.htmlFilterContentScraper;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
+import de.anomic.htmlFilter.htmlFilterWriter;
import de.anomic.http.httpHeader;
+import de.anomic.net.URL;
import de.anomic.plasma.plasmaHTCache;
import de.anomic.plasma.plasmaParserDocument;
import de.anomic.plasma.plasmaSwitchboard;
@@ -112,9 +114,14 @@ public class CacheAdmin_p {
info.append("<img src=\"" + "CacheResource_p.html?path=").append(pathString).append("\">");
} else {
final htmlFilterContentScraper scraper = new htmlFilterContentScraper(url);
- final OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
- serverFileUtils.copy(file, os);
- final plasmaParserDocument document = switchboard.parser.transformScraper(url, "text/html", scraper);
+ //final OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
+ Writer writer = new htmlFilterWriter(null,null,scraper,null,false);
+ String sourceCharset = resInfo.getCharacterEncoding();
+ if (sourceCharset == null) sourceCharset = "UTF-8";
+ String mimeType = resInfo.getMimeType();
+ serverFileUtils.copy(file, sourceCharset, writer);
+ writer.close();
+ final plasmaParserDocument document = switchboard.parser.transformScraper(url, mimeType, sourceCharset, scraper);
info.append("<b>TITLE:</b><br>").append(scraper.getTitle()).append("<br>").append("<br>")
.append("<b>SECTION HEADLINES:</b><br>").append(formatTitles(document.getSectionTitles())).append("<br>")
.append("<b>HREF:</b><br>").append(formatAnchor(document.getHyperlinks())).append("<br>")
diff --git a/htroot/IndexCreate_p.java b/htroot/IndexCreate_p.java
index 8387e554a..9eb74eabb 100644
--- a/htroot/IndexCreate_p.java
+++ b/htroot/IndexCreate_p.java
@@ -44,10 +44,9 @@
// if the shell's current path is HTROOT
import java.io.File;
-import java.io.OutputStream;
import java.io.IOException;
+import java.io.Writer;
import java.net.MalformedURLException;
-import de.anomic.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
@@ -58,9 +57,10 @@ import java.util.regex.PatternSyntaxException;
import de.anomic.data.wikiCode;
import de.anomic.htmlFilter.htmlFilterContentScraper;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
+import de.anomic.htmlFilter.htmlFilterWriter;
import de.anomic.http.httpHeader;
import de.anomic.index.indexURL;
+import de.anomic.net.URL;
import de.anomic.plasma.plasmaCrawlEURL;
import de.anomic.plasma.plasmaCrawlProfile;
import de.anomic.plasma.plasmaSwitchboard;
@@ -70,9 +70,9 @@ import de.anomic.server.serverSwitch;
import de.anomic.server.serverThread;
import de.anomic.tools.bitfield;
import de.anomic.yacy.yacyCore;
-import de.anomic.yacy.yacySeed;
-import de.anomic.yacy.yacyNewsRecord;
import de.anomic.yacy.yacyNewsPool;
+import de.anomic.yacy.yacyNewsRecord;
+import de.anomic.yacy.yacySeed;
public class IndexCreate_p {
@@ -234,11 +234,15 @@ public class IndexCreate_p {
// getting the content of the bookmark file
byte[] fileContent = (byte[]) post.get("crawlingFile$file");
+ // TODO: determine the real charset here ....
+ String fileString = new String(fileContent,"UTF-8");
+
// parsing the bookmark file and fetching the headline and contained links
htmlFilterContentScraper scraper = new htmlFilterContentScraper(new URL(file));
- OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
- serverFileUtils.write(fileContent,os);
- os.close();
+ //OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
+ Writer writer = new htmlFilterWriter(null,null,scraper,null,false);
+ serverFileUtils.write(fileString,writer);
+ writer.close();
//String headline = scraper.getHeadline();
HashMap hyperlinks = (HashMap) scraper.getAnchors();
diff --git a/source/de/anomic/data/bookmarksDB.java b/source/de/anomic/data/bookmarksDB.java
index 98eba79db..a657371d2 100644
--- a/source/de/anomic/data/bookmarksDB.java
+++ b/source/de/anomic/data/bookmarksDB.java
@@ -44,8 +44,7 @@ package de.anomic.data;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
-import java.io.OutputStream;
-import de.anomic.net.URL;
+import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -67,13 +66,15 @@ import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
+
import de.anomic.htmlFilter.htmlFilterContentScraper;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
+import de.anomic.htmlFilter.htmlFilterWriter;
import de.anomic.index.indexEntryAttribute;
import de.anomic.index.indexURL;
import de.anomic.kelondro.kelondroDyn;
import de.anomic.kelondro.kelondroException;
import de.anomic.kelondro.kelondroMap;
+import de.anomic.net.URL;
import de.anomic.server.serverFileUtils;
import de.anomic.server.logging.serverLog;
@@ -441,9 +442,10 @@ public class bookmarksDB {
try {
//load the links
htmlFilterContentScraper scraper = new htmlFilterContentScraper(baseURL);
- OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
- serverFileUtils.write(input.getBytes(),os);
- os.close();
+ //OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
+ Writer writer= new htmlFilterWriter(null,null,scraper, null, false);
+ serverFileUtils.write(input,writer);
+ writer.close();
links = (HashMap) scraper.getAnchors();
} catch (IOException e) {}
it=links.keySet().iterator();
diff --git a/source/de/anomic/htmlFilter/htmlFilterAbstractScraper.java b/source/de/anomic/htmlFilter/htmlFilterAbstractScraper.java
index 0a2e8583a..092cf6514 100644
--- a/source/de/anomic/htmlFilter/htmlFilterAbstractScraper.java
+++ b/source/de/anomic/htmlFilter/htmlFilterAbstractScraper.java
@@ -1 +1 @@
-// htmlFilterAbstractScraper.java // --------------------------- // (C) by Michael Peter Christen; [email protected] // first published on http://www.anomic.de // Frankfurt, Germany, 2004 // last major change: 18.02.2004 // // You agree that the Author(s) is (are) not responsible for cost, // loss of data or any harm that may be caused 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. // // THE SOFTWARE THAT FOLLOWS AS ART OF PROGRAMMING BELOW THIS SECTION // IS PUBLISHED UNDER THE GPL AS DOCUMENTED IN THE FILE gpl.txt ASIDE THIS // FILE AND AS IN http://www.gnu.org/licenses/gpl.txt // ANY CHANGES TO THIS FILE ACCORDING TO THE GPL CAN BE DONE TO THE // LINES THAT FOLLOWS THIS COPYRIGHT NOTICE HERE, BUT CHANGES MUST NOT // BE DONE ABOVE OR INSIDE THE COPYRIGHT NOTICE. A RE-DISTRIBUTION // MUST CONTAIN THE INTACT AND UNCHANGED COPYRIGHT NOTICE. // CONTRIBUTIONS AND CHANGES TO THE PROGRAM CODE SHOULD BE MARKED AS SUCH. package de.anomic.htmlFilter; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Properties; import java.util.TreeSet; import de.anomic.server.serverByteBuffer; public abstract class htmlFilterAbstractScraper implements htmlFilterScraper { public static final byte lb = (byte) '<'; public static final byte rb = (byte) '>'; public static final byte sl = (byte) '/'; private TreeSet tags0; private TreeSet tags1; // define a translation table for html character codings private static HashMap trans = new HashMap(300); static { trans.put("&quot;", "\""); //Anf&uuml;hrungszeichen oben trans.put("&amp;", "&"); //Ampersand-Zeichen, kaufm&auml;nnisches Und trans.put("&lt;", "<"); //&ouml;ffnende spitze Klammer trans.put("&gt;", ">"); //schlie&szlig;ende spitze Klammer trans.put("&nbsp;", " "); //Erzwungenes Leerzeichen trans.put("&iexcl;", "!"); //umgekehrtes Ausrufezeichen trans.put("&cent;", " cent "); //Cent-Zeichen trans.put("&pound;", " pound "); //Pfund-Zeichen trans.put("&curren;", " currency "); //W&auml;hrungs-Zeichen trans.put("&yen;", " yen "); //Yen-Zeichen trans.put("&brvbar;", " "); //durchbrochener Strich trans.put("&sect;", " paragraph "); //Paragraph-Zeichen trans.put("&uml;", " "); //P&uuml;nktchen oben trans.put("&copy;", " copyright "); //Copyright-Zeichen trans.put("&ordf;", " "); //Ordinal-Zeichen weiblich trans.put("&laquo;", " "); //angewinkelte Anf&uuml;hrungszeichen links trans.put("&not;", " not "); //Verneinungs-Zeichen trans.put("&shy;", "-"); //kurzer Trennstrich trans.put("&reg;", " trademark "); //Registriermarke-Zeichen trans.put("&macr;", " "); //&Uuml;berstrich trans.put("&deg;", " degree "); //Grad-Zeichen trans.put("&plusmn;", " +/- "); //Plusminus-Zeichen trans.put("&sup2;", " square "); //Hoch-2-Zeichen trans.put("&sup3;", " 3 "); //Hoch-3-Zeichen trans.put("&acute;", " "); //Acute-Zeichen trans.put("&micro;", " micro "); //Mikro-Zeichen trans.put("&para;", " paragraph "); //Absatz-Zeichen trans.put("&middot;", " "); //Mittelpunkt trans.put("&cedil;", " "); //H&auml;kchen unten trans.put("&sup1;", " "); //Hoch-1-Zeichen trans.put("&ordm;", " degree "); //Ordinal-Zeichen m&auml;nnlich trans.put("&raquo;", " "); //angewinkelte Anf&uuml;hrungszeichen rechts trans.put("&frac14;", " quarter "); //ein Viertel trans.put("&frac12;", " half "); //ein Halb trans.put("&frac34;", " 3/4 "); //drei Viertel trans.put("&iquest;", "?"); //umgekehrtes Fragezeichen trans.put("&Agrave;", "A"); //A mit Accent grave trans.put("&Aacute;", "A"); //A mit Accent acute trans.put("&Acirc;", "A"); //A mit Circumflex trans.put("&Atilde;", "A"); //A mit Tilde trans.put("&Auml;", "Ae"); //A Umlaut trans.put("&Aring;", "A"); //A mit Ring trans.put("&AElig;", "A"); //A mit legiertem E trans.put("&Ccedil;", "C"); //C mit H&auml;kchen trans.put("&Egrave;", "E"); //E mit Accent grave trans.put("&Eacute;", "E"); //E mit Accent acute trans.put("&Ecirc;", "E"); //E mit Circumflex trans.put("&Euml;", "E"); //E Umlaut trans.put("&Igrave;", "I"); //I mit Accent grave trans.put("&Iacute;", "I"); //I mit Accent acute trans.put("&Icirc;", "I"); //I mit Circumflex trans.put("&Iuml;", "I"); //I Umlaut trans.put("&ETH;", "D"); //Eth (isl&auml;ndisch) trans.put("&Ntilde;", "N"); //N mit Tilde trans.put("&Ograve;", "O"); //O mit Accent grave trans.put("&Oacute;", "O"); //O mit Accent acute trans.put("&Ocirc;", "O"); //O mit Circumflex trans.put("&Otilde;", "O"); //O mit Tilde trans.put("&Ouml;", "Oe"); //O Umlaut trans.put("&times;", " times "); //Mal-Zeichen trans.put("&Oslash;", "O"); //O mit Schr&auml;gstrich trans.put("&Ugrave;", "U"); //U mit Accent grave trans.put("&Uacute;", "U"); //U mit Accent acute trans.put("&Ucirc;", "U"); //U mit Circumflex trans.put("&Uuml;", "Ue"); //U Umlaut trans.put("&Yacute;", "Y"); //Y mit Accent acute trans.put("&THORN;", "P"); //THORN (isl&auml;ndisch) trans.put("&szlig;", "ss"); //scharfes S trans.put("&agrave;", "a"); //a mit Accent grave trans.put("&aacute;", "a"); //a mit Accent acute trans.put("&acirc;", "a"); //a mit Circumflex trans.put("&atilde;", "a"); //a mit Tilde trans.put("&auml;", "ae"); //a Umlaut trans.put("&aring;", "a"); //a mit Ring trans.put("&aelig;", "a"); //a mit legiertem e trans.put("&ccedil;", "c"); //c mit H&auml;kchen trans.put("&egrave;", "e"); //e mit Accent grave trans.put("&eacute;", "e"); //e mit Accent acute trans.put("&ecirc;", "e"); //e mit Circumflex trans.put("&euml;", "e"); //e Umlaut trans.put("&igrave;", "i"); //i mit Accent grave trans.put("&iacute;", "i"); //i mit Accent acute trans.put("&icirc;", "i"); //i mit Circumflex trans.put("&iuml;", "i"); //i Umlaut trans.put("&eth;", "d"); //eth (isl&auml;ndisch) trans.put("&ntilde;", "n"); //n mit Tilde trans.put("&ograve;", "o"); //o mit Accent grave trans.put("&oacute;", "o"); //o mit Accent acute trans.put("&ocirc;", "o"); //o mit Circumflex trans.put("&otilde;", "o"); //o mit Tilde trans.put("&ouml;", "oe"); //o Umlaut trans.put("&divide;", "%"); //Divisions-Zeichen trans.put("&oslash;", "o"); //o mit Schr&auml;gstrich trans.put("&ugrave;", "u"); //u mit Accent grave trans.put("&uacute;", "u"); //u mit Accent acute trans.put("&ucirc;", "u"); //u mit Circumflex trans.put("&uuml;", "ue"); //u Umlaut trans.put("&yacute;", "y"); //y mit Accent acute trans.put("&thorn;", "p"); //thorn (isl&auml;ndisch) trans.put("&yuml;", "y"); //y Umlaut trans.put("&Alpha;", " Alpha "); //Alpha gro&szlig; trans.put("&alpha;", " alpha "); //alpha klein trans.put("&Beta;", " Beta "); //Beta gro&szlig; trans.put("&beta;", " beta "); //beta klein trans.put("&Gamma;", " Gamma "); //Gamma gro&szlig; trans.put("&gamma;", " gamma "); //gamma klein trans.put("&Delta;", " Delta "); //Delta gro&szlig; trans.put("&delta;", " delta "); //delta klein trans.put("&Epsilon;", " Epsilon "); //Epsilon gro&szlig; trans.put("&epsilon;", " epsilon "); //epsilon klein trans.put("&Zeta;", " Zeta "); //Zeta gro&szlig; trans.put("&zeta;", " zeta "); //zeta klein trans.put("&Eta;", " Eta "); //Eta gro&szlig; trans.put("&eta;", " eta "); //eta klein trans.put("&Theta;", " Theta "); //Theta gro&szlig; trans.put("&theta;", " theta "); //theta klein trans.put("&Iota;", " Iota "); //Iota gro&szlig; trans.put("&iota;", " iota "); //iota klein trans.put("&Kappa;", " Kappa "); //Kappa gro&szlig; trans.put("&kappa;", " kappa "); //kappa klein trans.put("&Lambda;", " Lambda "); //Lambda gro&szlig; trans.put("&lambda;", " lambda "); //lambda klein trans.put("&Mu;", " Mu "); //Mu gro&szlig; trans.put("&mu;", " mu "); //mu klein trans.put("&Nu;", " Nu "); //Nu gro&szlig; trans.put("&nu;", " nu "); //nu klein trans.put("&Xi;", " Xi "); //Xi gro&szlig; trans.put("&xi;", " xi "); //xi klein trans.put("&Omicron;", " Omicron "); //Omicron gro&szlig; trans.put("&omicron;", " omicron "); //omicron klein trans.put("&Pi;", " Pi "); //Pi gro&szlig; trans.put("&pi;", " pi "); //pi klein trans.put("&Rho;", " Rho "); //Rho gro&szlig; trans.put("&rho;", " rho "); //rho klein trans.put("&Sigma;", " Sigma "); //Sigma gro&szlig; trans.put("&sigmaf;", " sigma "); //sigmaf klein trans.put("&sigma;", " sigma "); //sigma klein trans.put("&Tau;", " Tau "); //Tau gro&szlig; trans.put("&tau;", " tau "); //tau klein trans.put("&Upsilon;", " Ypsilon "); //Upsilon gro&szlig; trans.put("&upsilon;", " ypsilon "); //upsilon klein trans.put("&Phi;", " Phi "); //Phi gro&szlig; trans.put("&phi;", " phi "); //phi klein trans.put("&Chi;", " Chi "); //Chi gro&szlig; trans.put("&chi;", " chi "); //chi klein trans.put("&Psi;", " Psi "); //Psi gro&szlig; trans.put("&psi;", " psi "); //psi klein trans.put("&Omega;", " Omega "); //Omega gro&szlig; trans.put("&omega;", " omega "); //omega klein trans.put("&thetasym;", " theta "); //theta Symbol trans.put("&upsih;", " ypsilon "); //upsilon mit Haken trans.put("&piv;", " pi "); //pi Symbol trans.put("&forall;", " for all "); //f&uuml;r alle trans.put("&part;", " part of "); //teilweise trans.put("&exist;", " exists "); //existiert trans.put("&empty;", " null "); //leer trans.put("&nabla;", " nabla "); //nabla trans.put("&isin;", " element of "); //Element von trans.put("&notin;", " not element of "); //kein Element von trans.put("&ni;", " contains "); //enth&auml;lt als Element trans.put("&prod;", " product "); //Produkt trans.put("&sum;", " sum "); //Summe trans.put("&minus;", " minus "); //minus trans.put("&lowast;", " times "); //Asterisk trans.put("&radic;", " sqare root "); //Quadratwurzel trans.put("&prop;", " proportional to "); //proportional zu trans.put("&infin;", " unlimited "); //unendlich trans.put("&ang;", " angle "); //Winkel trans.put("&and;", " and "); //und trans.put("&or;", " or "); //oder trans.put("&cap;", " "); //Schnittpunkt trans.put("&cup;", " unity "); //Einheit trans.put("&int;", " integral "); //Integral trans.put("&there4;", " cause "); //deshalb trans.put("&sim;", " similar to "); //&auml;hnlich wie trans.put("&cong;", " equal "); //ann&auml;hernd gleich trans.put("&asymp;", " equal "); //beinahe gleich trans.put("&ne;", " not equal "); //ungleich trans.put("&equiv;", " identical "); //identisch mit trans.put("&le;", " smaller or equal than "); //kleiner gleich trans.put("&ge;", " greater or equal than "); //gr&ouml;&szlig;er gleich trans.put("&sub;", " subset of "); //Untermenge von trans.put("&sup;", " superset of "); //Obermenge von trans.put("&nsub;", " not subset of "); //keine Untermenge von trans.put("&sube;", ""); //Untermenge von oder gleich mit trans.put("&supe;", ""); //Obermenge von oder gleich mit trans.put("&oplus;", ""); //Direktsumme trans.put("&otimes;", ""); //Vektorprodukt trans.put("&perp;", ""); //senkrecht zu trans.put("&sdot;", ""); //Punkt-Operator trans.put("&loz;", ""); //Raute trans.put("&lceil;", ""); //links oben trans.put("&rceil;", ""); //rechts oben trans.put("&lfloor;", ""); //links unten trans.put("&rfloor;", ""); //rechts unten trans.put("&lang;", ""); //spitze Klammer links trans.put("&rang;", ""); //spitze Klammer rechts trans.put("&larr;", ""); //Pfeil links trans.put("&uarr;", ""); //Pfeil oben trans.put("&rarr;", ""); //Pfeil rechts trans.put("&darr;", ""); //Pfeil unten trans.put("&harr;", ""); //Pfeil links/rechts trans.put("&crarr;", ""); //Pfeil unten-Knick-links trans.put("&lArr;", ""); //Doppelpfeil links trans.put("&uArr;", ""); //Doppelpfeil oben trans.put("&rArr;", ""); //Doppelpfeil rechts trans.put("&dArr;", ""); //Doppelpfeil unten trans.put("&hArr;", ""); //Doppelpfeil links/rechts trans.put("&bull;", ""); //Bullet-Zeichen trans.put("&hellip;", ""); //Horizontale Ellipse trans.put("&prime;", ""); //Minutenzeichen trans.put("&oline;", ""); //&Uuml;berstrich trans.put("&frasl;", ""); //Bruchstrich trans.put("&weierp;", ""); //Weierstrass p trans.put("&image;", ""); //Zeichen f&uuml;r &quot;imagin&auml;r&quot; trans.put("&real;", ""); //Zeichen f&uuml;r &quot;real&quot; trans.put("&trade;", ""); //Trademark-Zeichen trans.put("&euro;", ""); //Euro-Zeichen trans.put("&alefsym;", ""); //Alef-Symbol trans.put("&spades;", ""); //Pik-Zeichen trans.put("&clubs;", ""); //Kreuz-Zeichen trans.put("&hearts;", ""); //Herz-Zeichen trans.put("&diams;", ""); //Karo-Zeichen trans.put("&ensp;", ""); //Leerzeichen Breite n trans.put("&emsp;", ""); //Leerzeichen Breite m trans.put("&thinsp;", ""); //Schmales Leerzeichen trans.put("&zwnj;", ""); //null breiter Nichtverbinder trans.put("&zwj;", ""); //null breiter Verbinder trans.put("&lrm;", ""); //links-nach-rechts-Zeichen trans.put("&rlm;", ""); //rechts-nach-links-Zeichen trans.put("&ndash;", ""); //Gedankenstrich Breite n trans.put("&mdash;", ""); //Gedankenstrich Breite m trans.put("&lsquo;", ""); //einfaches Anf&uuml;hrungszeichen links trans.put("&rsquo;", ""); //einfaches Anf&uuml;hrungszeichen rechts trans.put("&sbquo;", ""); //einfaches low-9-Zeichen trans.put("&ldquo;", ""); //doppeltes Anf&uuml;hrungszeichen links trans.put("&rdquo;", ""); //doppeltes Anf&uuml;hrungszeichen rechts trans.put("&bdquo;", ""); //doppeltes low-9-Zeichen rechts trans.put("&dagger;", ""); //Kreuz trans.put("&Dagger;", ""); //Doppelkreuz trans.put("&permil;", ""); //zu tausend trans.put("&lsaquo;", ""); //angewinkeltes einzelnes Anf.zeichen links trans.put("&rsaquo;", ""); //angewinkeltes einzelnes Anf.zeichen rechts } public htmlFilterAbstractScraper(TreeSet tags0, TreeSet tags1) { this.tags0 = tags0; this.tags1 = tags1; } public boolean isTag0(String tag) { return (tags0 != null) && (tags0.contains(tag)); } public boolean isTag1(String tag) { return (tags1 != null) && (tags1.contains(tag)); } //the 'missing' method that shall be implemented: public abstract void scrapeText(byte[] text); // the other methods must take into account to construct the return value correctly public void scrapeTag0(String tagname, Properties tagopts) { } public void scrapeTag1(String tagname, Properties tagopts, byte[] text) { } // string conversions private static String code_iso8859s(int c) { switch (c & 0xff) { // german umlaute and ligaturen case 0xc4: return "AE"; case 0xd6: return "OE"; case 0xdc: return "UE"; case 0xe4: return "ae"; case 0xf6: return "oe"; case 0xfc: return "ue"; case 0xdf: return "ss"; // accent on letters; i.e. french characters case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc5: return "A"; case 0xc6: return "AE"; case 0xc7: return "C"; case 0xc8: case 0xc9: case 0xca: return "E"; case 0xcc: case 0xcd: case 0xce: case 0xcf: return "I"; case 0xd0: return "D"; case 0xd1: return "N"; case 0xd2: case 0xd3: case 0xd4: case 0xd5: case 0xd8: return "O"; case 0xd7: return "x"; case 0xd9: case 0xda: case 0xdb: return "U"; case 0xdd: return "Y"; case 0xde: return "p"; case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe5: return "a"; case 0xe6: return "ae"; case 0xe7: return "c"; case 0xe8: case 0xe9: case 0xea: return "e"; case 0xec: case 0xed: case 0xee: case 0xef: return "i"; case 0xf0: return "d"; case 0xf1: return "n"; case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf8: return "o"; case 0xf7: return "%"; case 0xf9: case 0xfa: case 0xfb: return "u"; case 0xfd: case 0xff: return "y"; case 0xfe: return "p"; // special characters case 0xa4: return " euro "; default: return null; } } public static serverByteBuffer convertUmlaute(serverByteBuffer bb) { serverByteBuffer t = new serverByteBuffer(bb.length() + 20); int b0, b1, b2; String z; int i = 0; while (i < bb.length()) { b0 = bb.byteAt(i) & 0xff; // check utf-8 encoding if ((b0 < 128) || (i + 1 == bb.length())) { t.append(b0); i++; } else { b1 = bb.byteAt(i + 1) & 0xff; if (b1 > 0x3f) { z = code_iso8859s(b0); i++; } else if ((b0 > 0xbf) && (b0 < 0xe0)) { z = code_iso8859s(((b0 & 0x1f) << 0x6) | (b1 & 0x3f)); i += 2; } else { if (i + 2 >= bb.length()) { z = null; i++; } else { b2 = bb.byteAt(i + 2) & 0xff; if (b2 > 0x3f) { z = code_iso8859s(b0); i++; } else { z = code_iso8859s(((b0 & 0xf) << 0xc) | ((b1 & 0x3f) << 0x6) | (b2 & 0x3f)); i += 3; } } } if (z == null) t.append(b0); else t.append(z); } } return t; } private static byte[] transscript(byte[] code) { String t; try { // TODO: use the source encoding here to get the tag name from the byte array t = (String) trans.get(new String(code, "UTF-8")); } catch (UnsupportedEncodingException e) { t = null; } if (t == null) return new byte[0]; // TODO: use the source encoding to convert the string properly to the original document encoding return t.getBytes(); } protected static serverByteBuffer transscriptAll(serverByteBuffer bb) { int p0 = 0, p1; byte[] t; while ((p0 = bb.indexOf((byte) '&', p0)) >= 0) { p1 = bb.indexOf((byte) ';', p0); if (p1 >= 0) { t = transscript(bb.getBytes(p0, p1 + 1)); bb = new serverByteBuffer(bb.getBytes(0, p0), bb.length() + p0 - p1 + t.length).append(t).append(bb.getBytes(p1 + 1)); } else { bb = new serverByteBuffer(bb.getBytes(0, p0), bb.length()).append(bb.getBytes(p0 + 1)); } } t = null; return bb; } protected static serverByteBuffer stripAllTags(serverByteBuffer bb) { // TODO: trim() does not work with other charsets int p0 = 0, p1; while ((p0 = bb.indexOf(lb, p0)) >= 0) { p1 = bb.indexOf(rb, p0); if (p1 >= 0) { bb = new serverByteBuffer(bb.getBytes(0, p0), bb.length() + p0 - p1 + 1).trim().append((byte) 32).append(new serverByteBuffer(bb.getBytes(p1 + 1)).trim()); } else { bb = new serverByteBuffer(bb.getBytes(0, p0), bb.length()).trim().append(new serverByteBuffer(bb.getBytes(p0 + 1)).trim()); } } return bb.trim(); } public static serverByteBuffer stripAll(serverByteBuffer bb) { //return stripAllTags(s); return convertUmlaute(transscriptAll(stripAllTags(bb))); } public void close() { // free resources tags0 = null; tags1 = null; } public void finalize() { close(); } }
\ No newline at end of file
+// htmlFilterAbstractScraper.java // --------------------------- // (C) by Michael Peter Christen; [email protected] // first published on http://www.anomic.de // Frankfurt, Germany, 2004 // last major change: 18.02.2004 // // You agree that the Author(s) is (are) not responsible for cost, // loss of data or any harm that may be caused 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. // // THE SOFTWARE THAT FOLLOWS AS ART OF PROGRAMMING BELOW THIS SECTION // IS PUBLISHED UNDER THE GPL AS DOCUMENTED IN THE FILE gpl.txt ASIDE THIS // FILE AND AS IN http://www.gnu.org/licenses/gpl.txt // ANY CHANGES TO THIS FILE ACCORDING TO THE GPL CAN BE DONE TO THE // LINES THAT FOLLOWS THIS COPYRIGHT NOTICE HERE, BUT CHANGES MUST NOT // BE DONE ABOVE OR INSIDE THE COPYRIGHT NOTICE. A RE-DISTRIBUTION // MUST CONTAIN THE INTACT AND UNCHANGED COPYRIGHT NOTICE. // CONTRIBUTIONS AND CHANGES TO THE PROGRAM CODE SHOULD BE MARKED AS SUCH. package de.anomic.htmlFilter; import java.util.HashMap; import java.util.Properties; import java.util.TreeSet; import de.anomic.server.serverCharBuffer; public abstract class htmlFilterAbstractScraper implements htmlFilterScraper { public static final char lb = '<'; public static final char rb = '>'; public static final char sl = '/'; private TreeSet tags0; private TreeSet tags1; // define a translation table for html character codings private static HashMap trans = new HashMap(300); static { trans.put("&quot;", "\""); //Anf&uuml;hrungszeichen oben trans.put("&amp;", "&"); //Ampersand-Zeichen, kaufm&auml;nnisches Und trans.put("&lt;", "<"); //&ouml;ffnende spitze Klammer trans.put("&gt;", ">"); //schlie&szlig;ende spitze Klammer trans.put("&nbsp;", " "); //Erzwungenes Leerzeichen trans.put("&iexcl;", "!"); //umgekehrtes Ausrufezeichen trans.put("&cent;", " cent "); //Cent-Zeichen trans.put("&pound;", " pound "); //Pfund-Zeichen trans.put("&curren;", " currency "); //W&auml;hrungs-Zeichen trans.put("&yen;", " yen "); //Yen-Zeichen trans.put("&brvbar;", " "); //durchbrochener Strich trans.put("&sect;", " paragraph "); //Paragraph-Zeichen trans.put("&uml;", " "); //P&uuml;nktchen oben trans.put("&copy;", " copyright "); //Copyright-Zeichen trans.put("&ordf;", " "); //Ordinal-Zeichen weiblich trans.put("&laquo;", " "); //angewinkelte Anf&uuml;hrungszeichen links trans.put("&not;", " not "); //Verneinungs-Zeichen trans.put("&shy;", "-"); //kurzer Trennstrich trans.put("&reg;", " trademark "); //Registriermarke-Zeichen trans.put("&macr;", " "); //&Uuml;berstrich trans.put("&deg;", " degree "); //Grad-Zeichen trans.put("&plusmn;", " +/- "); //Plusminus-Zeichen trans.put("&sup2;", " square "); //Hoch-2-Zeichen trans.put("&sup3;", " 3 "); //Hoch-3-Zeichen trans.put("&acute;", " "); //Acute-Zeichen trans.put("&micro;", " micro "); //Mikro-Zeichen trans.put("&para;", " paragraph "); //Absatz-Zeichen trans.put("&middot;", " "); //Mittelpunkt trans.put("&cedil;", " "); //H&auml;kchen unten trans.put("&sup1;", " "); //Hoch-1-Zeichen trans.put("&ordm;", " degree "); //Ordinal-Zeichen m&auml;nnlich trans.put("&raquo;", " "); //angewinkelte Anf&uuml;hrungszeichen rechts trans.put("&frac14;", " quarter "); //ein Viertel trans.put("&frac12;", " half "); //ein Halb trans.put("&frac34;", " 3/4 "); //drei Viertel trans.put("&iquest;", "?"); //umgekehrtes Fragezeichen trans.put("&Agrave;", "A"); //A mit Accent grave trans.put("&Aacute;", "A"); //A mit Accent acute trans.put("&Acirc;", "A"); //A mit Circumflex trans.put("&Atilde;", "A"); //A mit Tilde trans.put("&Auml;", "Ae"); //A Umlaut trans.put("&Aring;", "A"); //A mit Ring trans.put("&AElig;", "A"); //A mit legiertem E trans.put("&Ccedil;", "C"); //C mit H&auml;kchen trans.put("&Egrave;", "E"); //E mit Accent grave trans.put("&Eacute;", "E"); //E mit Accent acute trans.put("&Ecirc;", "E"); //E mit Circumflex trans.put("&Euml;", "E"); //E Umlaut trans.put("&Igrave;", "I"); //I mit Accent grave trans.put("&Iacute;", "I"); //I mit Accent acute trans.put("&Icirc;", "I"); //I mit Circumflex trans.put("&Iuml;", "I"); //I Umlaut trans.put("&ETH;", "D"); //Eth (isl&auml;ndisch) trans.put("&Ntilde;", "N"); //N mit Tilde trans.put("&Ograve;", "O"); //O mit Accent grave trans.put("&Oacute;", "O"); //O mit Accent acute trans.put("&Ocirc;", "O"); //O mit Circumflex trans.put("&Otilde;", "O"); //O mit Tilde trans.put("&Ouml;", "Oe"); //O Umlaut trans.put("&times;", " times "); //Mal-Zeichen trans.put("&Oslash;", "O"); //O mit Schr&auml;gstrich trans.put("&Ugrave;", "U"); //U mit Accent grave trans.put("&Uacute;", "U"); //U mit Accent acute trans.put("&Ucirc;", "U"); //U mit Circumflex trans.put("&Uuml;", "Ue"); //U Umlaut trans.put("&Yacute;", "Y"); //Y mit Accent acute trans.put("&THORN;", "P"); //THORN (isl&auml;ndisch) trans.put("&szlig;", "ss"); //scharfes S trans.put("&agrave;", "a"); //a mit Accent grave trans.put("&aacute;", "a"); //a mit Accent acute trans.put("&acirc;", "a"); //a mit Circumflex trans.put("&atilde;", "a"); //a mit Tilde trans.put("&auml;", "ae"); //a Umlaut trans.put("&aring;", "a"); //a mit Ring trans.put("&aelig;", "a"); //a mit legiertem e trans.put("&ccedil;", "c"); //c mit H&auml;kchen trans.put("&egrave;", "e"); //e mit Accent grave trans.put("&eacute;", "e"); //e mit Accent acute trans.put("&ecirc;", "e"); //e mit Circumflex trans.put("&euml;", "e"); //e Umlaut trans.put("&igrave;", "i"); //i mit Accent grave trans.put("&iacute;", "i"); //i mit Accent acute trans.put("&icirc;", "i"); //i mit Circumflex trans.put("&iuml;", "i"); //i Umlaut trans.put("&eth;", "d"); //eth (isl&auml;ndisch) trans.put("&ntilde;", "n"); //n mit Tilde trans.put("&ograve;", "o"); //o mit Accent grave trans.put("&oacute;", "o"); //o mit Accent acute trans.put("&ocirc;", "o"); //o mit Circumflex trans.put("&otilde;", "o"); //o mit Tilde trans.put("&ouml;", "oe"); //o Umlaut trans.put("&divide;", "%"); //Divisions-Zeichen trans.put("&oslash;", "o"); //o mit Schr&auml;gstrich trans.put("&ugrave;", "u"); //u mit Accent grave trans.put("&uacute;", "u"); //u mit Accent acute trans.put("&ucirc;", "u"); //u mit Circumflex trans.put("&uuml;", "ue"); //u Umlaut trans.put("&yacute;", "y"); //y mit Accent acute trans.put("&thorn;", "p"); //thorn (isl&auml;ndisch) trans.put("&yuml;", "y"); //y Umlaut trans.put("&Alpha;", " Alpha "); //Alpha gro&szlig; trans.put("&alpha;", " alpha "); //alpha klein trans.put("&Beta;", " Beta "); //Beta gro&szlig; trans.put("&beta;", " beta "); //beta klein trans.put("&Gamma;", " Gamma "); //Gamma gro&szlig; trans.put("&gamma;", " gamma "); //gamma klein trans.put("&Delta;", " Delta "); //Delta gro&szlig; trans.put("&delta;", " delta "); //delta klein trans.put("&Epsilon;", " Epsilon "); //Epsilon gro&szlig; trans.put("&epsilon;", " epsilon "); //epsilon klein trans.put("&Zeta;", " Zeta "); //Zeta gro&szlig; trans.put("&zeta;", " zeta "); //zeta klein trans.put("&Eta;", " Eta "); //Eta gro&szlig; trans.put("&eta;", " eta "); //eta klein trans.put("&Theta;", " Theta "); //Theta gro&szlig; trans.put("&theta;", " theta "); //theta klein trans.put("&Iota;", " Iota "); //Iota gro&szlig; trans.put("&iota;", " iota "); //iota klein trans.put("&Kappa;", " Kappa "); //Kappa gro&szlig; trans.put("&kappa;", " kappa "); //kappa klein trans.put("&Lambda;", " Lambda "); //Lambda gro&szlig; trans.put("&lambda;", " lambda "); //lambda klein trans.put("&Mu;", " Mu "); //Mu gro&szlig; trans.put("&mu;", " mu "); //mu klein trans.put("&Nu;", " Nu "); //Nu gro&szlig; trans.put("&nu;", " nu "); //nu klein trans.put("&Xi;", " Xi "); //Xi gro&szlig; trans.put("&xi;", " xi "); //xi klein trans.put("&Omicron;", " Omicron "); //Omicron gro&szlig; trans.put("&omicron;", " omicron "); //omicron klein trans.put("&Pi;", " Pi "); //Pi gro&szlig; trans.put("&pi;", " pi "); //pi klein trans.put("&Rho;", " Rho "); //Rho gro&szlig; trans.put("&rho;", " rho "); //rho klein trans.put("&Sigma;", " Sigma "); //Sigma gro&szlig; trans.put("&sigmaf;", " sigma "); //sigmaf klein trans.put("&sigma;", " sigma "); //sigma klein trans.put("&Tau;", " Tau "); //Tau gro&szlig; trans.put("&tau;", " tau "); //tau klein trans.put("&Upsilon;", " Ypsilon "); //Upsilon gro&szlig; trans.put("&upsilon;", " ypsilon "); //upsilon klein trans.put("&Phi;", " Phi "); //Phi gro&szlig; trans.put("&phi;", " phi "); //phi klein trans.put("&Chi;", " Chi "); //Chi gro&szlig; trans.put("&chi;", " chi "); //chi klein trans.put("&Psi;", " Psi "); //Psi gro&szlig; trans.put("&psi;", " psi "); //psi klein trans.put("&Omega;", " Omega "); //Omega gro&szlig; trans.put("&omega;", " omega "); //omega klein trans.put("&thetasym;", " theta "); //theta Symbol trans.put("&upsih;", " ypsilon "); //upsilon mit Haken trans.put("&piv;", " pi "); //pi Symbol trans.put("&forall;", " for all "); //f&uuml;r alle trans.put("&part;", " part of "); //teilweise trans.put("&exist;", " exists "); //existiert trans.put("&empty;", " null "); //leer trans.put("&nabla;", " nabla "); //nabla trans.put("&isin;", " element of "); //Element von trans.put("&notin;", " not element of "); //kein Element von trans.put("&ni;", " contains "); //enth&auml;lt als Element trans.put("&prod;", " product "); //Produkt trans.put("&sum;", " sum "); //Summe trans.put("&minus;", " minus "); //minus trans.put("&lowast;", " times "); //Asterisk trans.put("&radic;", " sqare root "); //Quadratwurzel trans.put("&prop;", " proportional to "); //proportional zu trans.put("&infin;", " unlimited "); //unendlich trans.put("&ang;", " angle "); //Winkel trans.put("&and;", " and "); //und trans.put("&or;", " or "); //oder trans.put("&cap;", " "); //Schnittpunkt trans.put("&cup;", " unity "); //Einheit trans.put("&int;", " integral "); //Integral trans.put("&there4;", " cause "); //deshalb trans.put("&sim;", " similar to "); //&auml;hnlich wie trans.put("&cong;", " equal "); //ann&auml;hernd gleich trans.put("&asymp;", " equal "); //beinahe gleich trans.put("&ne;", " not equal "); //ungleich trans.put("&equiv;", " identical "); //identisch mit trans.put("&le;", " smaller or equal than "); //kleiner gleich trans.put("&ge;", " greater or equal than "); //gr&ouml;&szlig;er gleich trans.put("&sub;", " subset of "); //Untermenge von trans.put("&sup;", " superset of "); //Obermenge von trans.put("&nsub;", " not subset of "); //keine Untermenge von trans.put("&sube;", ""); //Untermenge von oder gleich mit trans.put("&supe;", ""); //Obermenge von oder gleich mit trans.put("&oplus;", ""); //Direktsumme trans.put("&otimes;", ""); //Vektorprodukt trans.put("&perp;", ""); //senkrecht zu trans.put("&sdot;", ""); //Punkt-Operator trans.put("&loz;", ""); //Raute trans.put("&lceil;", ""); //links oben trans.put("&rceil;", ""); //rechts oben trans.put("&lfloor;", ""); //links unten trans.put("&rfloor;", ""); //rechts unten trans.put("&lang;", ""); //spitze Klammer links trans.put("&rang;", ""); //spitze Klammer rechts trans.put("&larr;", ""); //Pfeil links trans.put("&uarr;", ""); //Pfeil oben trans.put("&rarr;", ""); //Pfeil rechts trans.put("&darr;", ""); //Pfeil unten trans.put("&harr;", ""); //Pfeil links/rechts trans.put("&crarr;", ""); //Pfeil unten-Knick-links trans.put("&lArr;", ""); //Doppelpfeil links trans.put("&uArr;", ""); //Doppelpfeil oben trans.put("&rArr;", ""); //Doppelpfeil rechts trans.put("&dArr;", ""); //Doppelpfeil unten trans.put("&hArr;", ""); //Doppelpfeil links/rechts trans.put("&bull;", ""); //Bullet-Zeichen trans.put("&hellip;", ""); //Horizontale Ellipse trans.put("&prime;", ""); //Minutenzeichen trans.put("&oline;", ""); //&Uuml;berstrich trans.put("&frasl;", ""); //Bruchstrich trans.put("&weierp;", ""); //Weierstrass p trans.put("&image;", ""); //Zeichen f&uuml;r &quot;imagin&auml;r&quot; trans.put("&real;", ""); //Zeichen f&uuml;r &quot;real&quot; trans.put("&trade;", ""); //Trademark-Zeichen trans.put("&euro;", ""); //Euro-Zeichen trans.put("&alefsym;", ""); //Alef-Symbol trans.put("&spades;", ""); //Pik-Zeichen trans.put("&clubs;", ""); //Kreuz-Zeichen trans.put("&hearts;", ""); //Herz-Zeichen trans.put("&diams;", ""); //Karo-Zeichen trans.put("&ensp;", ""); //Leerzeichen Breite n trans.put("&emsp;", ""); //Leerzeichen Breite m trans.put("&thinsp;", ""); //Schmales Leerzeichen trans.put("&zwnj;", ""); //null breiter Nichtverbinder trans.put("&zwj;", ""); //null breiter Verbinder trans.put("&lrm;", ""); //links-nach-rechts-Zeichen trans.put("&rlm;", ""); //rechts-nach-links-Zeichen trans.put("&ndash;", ""); //Gedankenstrich Breite n trans.put("&mdash;", ""); //Gedankenstrich Breite m trans.put("&lsquo;", ""); //einfaches Anf&uuml;hrungszeichen links trans.put("&rsquo;", ""); //einfaches Anf&uuml;hrungszeichen rechts trans.put("&sbquo;", ""); //einfaches low-9-Zeichen trans.put("&ldquo;", ""); //doppeltes Anf&uuml;hrungszeichen links trans.put("&rdquo;", ""); //doppeltes Anf&uuml;hrungszeichen rechts trans.put("&bdquo;", ""); //doppeltes low-9-Zeichen rechts trans.put("&dagger;", ""); //Kreuz trans.put("&Dagger;", ""); //Doppelkreuz trans.put("&permil;", ""); //zu tausend trans.put("&lsaquo;", ""); //angewinkeltes einzelnes Anf.zeichen links trans.put("&rsaquo;", ""); //angewinkeltes einzelnes Anf.zeichen rechts } public htmlFilterAbstractScraper(TreeSet tags0, TreeSet tags1) { this.tags0 = tags0; this.tags1 = tags1; } public boolean isTag0(String tag) { return (tags0 != null) && (tags0.contains(tag)); } public boolean isTag1(String tag) { return (tags1 != null) && (tags1.contains(tag)); } //the 'missing' method that shall be implemented: public abstract void scrapeText(char[] text); // the other methods must take into account to construct the return value correctly public abstract void scrapeTag0(String tagname, Properties tagopts); public abstract void scrapeTag1(String tagname, Properties tagopts, char[] text); // string conversions private static String code_iso8859s(char c) { switch (c) { // german umlaute and ligaturen case 0xc4: return "AE"; case 0xd6: return "OE"; case 0xdc: return "UE"; case 0xe4: return "ae"; case 0xf6: return "oe"; case 0xfc: return "ue"; case 0xdf: return "ss"; // accent on letters; i.e. french characters case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc5: return "A"; case 0xc6: return "AE"; case 0xc7: return "C"; case 0xc8: case 0xc9: case 0xca: return "E"; case 0xcc: case 0xcd: case 0xce: case 0xcf: return "I"; case 0xd0: return "D"; case 0xd1: return "N"; case 0xd2: case 0xd3: case 0xd4: case 0xd5: case 0xd8: return "O"; case 0xd7: return "x"; case 0xd9: case 0xda: case 0xdb: return "U"; case 0xdd: return "Y"; case 0xde: return "p"; case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe5: return "a"; case 0xe6: return "ae"; case 0xe7: return "c"; case 0xe8: case 0xe9: case 0xea: return "e"; case 0xec: case 0xed: case 0xee: case 0xef: return "i"; case 0xf0: return "d"; case 0xf1: return "n"; case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf8: return "o"; case 0xf7: return "%"; case 0xf9: case 0xfa: case 0xfb: return "u"; case 0xfd: case 0xff: return "y"; case 0xfe: return "p"; // special characters case 0xa4: return " euro "; default: return null; } } public static serverCharBuffer convertUmlaute(serverCharBuffer bb) { if (bb.length() == 0) return bb; serverCharBuffer t = new serverCharBuffer(bb.length() + 20); char c; for (int i = 0; i < bb.length(); i++) { c = bb.charAt(i); String z = code_iso8859s(c); if (z == null) t.append(c); else t.append(z); } return t; // serverByteBuffer t = new serverByteBuffer(bb.length() + 20); // int b0, b1, b2; // String z; // int i = 0; // while (i < bb.length()) { // b0 = bb.byteAt(i) & 0xff; // // check utf-8 encoding // if ((b0 < 128) || (i + 1 == bb.length())) { // t.append(b0); // i++; // } else { // b1 = bb.byteAt(i + 1) & 0xff; // if (b1 > 0x3f) { // z = code_iso8859s(b0); // i++; // } else if ((b0 > 0xbf) && (b0 < 0xe0)) { // z = code_iso8859s(((b0 & 0x1f) << 0x6) | (b1 & 0x3f)); // i += 2; // } else { // if (i + 2 >= bb.length()) { // z = null; // i++; // } else { // b2 = bb.byteAt(i + 2) & 0xff; // if (b2 > 0x3f) { // z = code_iso8859s(b0); // i++; // } else { // z = code_iso8859s(((b0 & 0xf) << 0xc) | ((b1 & 0x3f) << 0x6) | (b2 & 0x3f)); // i += 3; // } // } // } // if (z == null) t.append(b0); else t.append(z); // } // } // return t; } private static char[] transscript(char[] code) { String t = (String) trans.get(new String(code)); if (t == null) return new char[0]; return t.toCharArray(); } protected static serverCharBuffer transscriptAll(serverCharBuffer bb) { int p0 = 0, p1; char[] t; while ((p0 = bb.indexOf('&', p0)) >= 0) { p1 = bb.indexOf(';', p0); if (p1 >= 0) { t = transscript(bb.getChars(p0, p1 + 1)); bb = new serverCharBuffer(bb.getChars(0, p0), bb.length() + p0 - p1 + t.length).append(t).append(bb.getChars(p1 + 1)); } else { bb = new serverCharBuffer(bb.getChars(0, p0), bb.length()).append(bb.getChars(p0 + 1)); } } t = null; return bb; } protected static serverCharBuffer stripAllTags(serverCharBuffer bb) { int p0 = 0, p1; while ((p0 = bb.indexOf(lb, p0)) >= 0) { p1 = bb.indexOf(rb, p0); if (p1 >= 0) { bb = ((serverCharBuffer)new serverCharBuffer(bb.getChars(0, p0), bb.length() + p0 - p1 + 1).trim().append(32)).append(new serverCharBuffer(bb.getChars(p1 + 1)).trim()); } else { bb = new serverCharBuffer(bb.getChars(0, p0), bb.length()).trim().append(new serverCharBuffer(bb.getChars(p0 + 1)).trim()); } } return bb.trim(); } public static serverCharBuffer stripAll(serverCharBuffer bb) { //return stripAllTags(s); return convertUmlaute(transscriptAll(stripAllTags(bb))); } public void close() { // free resources tags0 = null; tags1 = null; } public void finalize() { close(); } }
\ No newline at end of file
diff --git a/source/de/anomic/htmlFilter/htmlFilterAbstractTransformer.java b/source/de/anomic/htmlFilter/htmlFilterAbstractTransformer.java
index fa8bcdc91..fcb4cb5f5 100644
--- a/source/de/anomic/htmlFilter/htmlFilterAbstractTransformer.java
+++ b/source/de/anomic/htmlFilter/htmlFilterAbstractTransformer.java
@@ -62,7 +62,7 @@ public abstract class htmlFilterAbstractTransformer implements htmlFilterTransfo
}
//the 'missing' method that shall be implemented:
- public abstract byte[] transformText(byte[] text);
+ public abstract char[] transformText(char[] text);
/* could be easily implemented as:
{
return text;
@@ -70,12 +70,12 @@ public abstract class htmlFilterAbstractTransformer implements htmlFilterTransfo
*/
// the other methods must take into account to construct the return value correctly
- public byte[] transformTag0(String tagname, Properties tagopts, byte quotechar) {
- return htmlFilterOutputStream.genTag0(tagname, tagopts, quotechar);
+ public char[] transformTag0(String tagname, Properties tagopts, char quotechar) {
+ return htmlFilterWriter.genTag0(tagname, tagopts, quotechar);
}
- public byte[] transformTag1(String tagname, Properties tagopts, byte[] text, byte quotechar) {
- return htmlFilterOutputStream.genTag1(tagname, tagopts, text, quotechar);
+ public char[] transformTag1(String tagname, Properties tagopts, char[] text, char quotechar) {
+ return htmlFilterWriter.genTag1(tagname, tagopts, text, quotechar);
}
public void close() {
diff --git a/source/de/anomic/htmlFilter/htmlFilterContentScraper.java b/source/de/anomic/htmlFilter/htmlFilterContentScraper.java
index 3ffd42eb1..b41d97ca6 100644
--- a/source/de/anomic/htmlFilter/htmlFilterContentScraper.java
+++ b/source/de/anomic/htmlFilter/htmlFilterContentScraper.java
@@ -43,9 +43,6 @@
package de.anomic.htmlFilter;
-import de.anomic.server.serverByteBuffer;
-import de.anomic.net.URL;
-
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
@@ -59,6 +56,9 @@ import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;
+import de.anomic.net.URL;
+import de.anomic.server.serverCharBuffer;
+
public class htmlFilterContentScraper extends htmlFilterAbstractScraper implements htmlFilterScraper {
// statics: for initialisation of the HTMLFilterAbstractScraper
@@ -95,10 +95,9 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
private String title;
//private String headline;
private List[] headlines;
- private serverByteBuffer content;
+ private serverCharBuffer content;
private URL root;
- private String charset = "UTF-8";
public htmlFilterContentScraper(URL root) {
// the root value here will not be used to load the resource.
@@ -111,27 +110,13 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
this.title = "";
this.headlines = new ArrayList[4];
for (int i = 0; i < 4; i++) headlines[i] = new ArrayList();
- this.content = new serverByteBuffer(1024);
- }
-
- public void setCharset(String charset) throws UnsupportedCharsetException {
- if (charset == null) return;
-
- // testing if charset exists
- Charset.forName(charset);
-
- // remember it
- this.charset = charset;
- }
-
- public String getCharset() {
- return this.charset;
+ this.content = new serverCharBuffer(1024);
}
- public void scrapeText(byte[] newtext) {
+ public void scrapeText(char[] newtext) {
// System.out.println("SCRAPE: " + new String(newtext));
- if ((content.length() != 0) && (content.byteAt(content.length() - 1) != 32)) content.append(32);
- content.append(super.stripAll(new serverByteBuffer(newtext, newtext.length + 1)).trim()).append(32);
+ if ((content.length() != 0) && (content.charAt(content.length() - 1) != 32)) content.append(32);
+ content.append(super.stripAll(new serverCharBuffer(newtext, newtext.length + 1)).trim()).append(32);
}
public static final String splitrex = " |/|\\(|\\)|-|\\:|_|\\.|,|\\?|!|'|" + '"';
@@ -188,31 +173,31 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
}
}
- public void scrapeTag1(String tagname, Properties tagopts, byte[] text) {
+ public void scrapeTag1(String tagname, Properties tagopts, char[] text) {
// System.out.println("ScrapeTag1: tagname=" + tagname + ", opts=" + tagopts.toString() + ", text=" + new String(text));
if ((tagname.equalsIgnoreCase("a")) && (text.length < 2048)) {
String href = tagopts.getProperty("href", "");
- if (href.length() > 0) anchors.put(absolutePath(href), super.stripAll(new serverByteBuffer(text)).trim().toString(this.charset));
+ if (href.length() > 0) anchors.put(absolutePath(href), super.stripAll(new serverCharBuffer(text)).trim().toString());
}
String h;
if ((tagname.equalsIgnoreCase("h1")) && (text.length < 1024)) {
- h = cleanLine(super.stripAll(new serverByteBuffer(text)).toString(this.charset));
+ h = cleanLine(super.stripAll(new serverCharBuffer(text)).toString());
if (h.length() > 0) headlines[0].add(h);
}
if ((tagname.equalsIgnoreCase("h2")) && (text.length < 1024)) {
- h = cleanLine(super.stripAll(new serverByteBuffer(text)).toString(this.charset));
+ h = cleanLine(super.stripAll(new serverCharBuffer(text)).toString());
if (h.length() > 0) headlines[1].add(h);
}
if ((tagname.equalsIgnoreCase("h3")) && (text.length < 1024)) {
- h = cleanLine(super.stripAll(new serverByteBuffer(text)).toString(this.charset));
+ h = cleanLine(super.stripAll(new serverCharBuffer(text)).toString());
if (h.length() > 0) headlines[2].add(h);
}
if ((tagname.equalsIgnoreCase("h4")) && (text.length < 1024)) {
- h = cleanLine(super.stripAll(new serverByteBuffer(text)).toString(this.charset));
+ h = cleanLine(super.stripAll(new serverCharBuffer(text)).toString());
if (h.length() > 0) headlines[3].add(h);
}
if ((tagname.equalsIgnoreCase("title")) && (text.length < 1024))
- title = cleanLine(super.stripAll(new serverByteBuffer(text)).toString(this.charset));
+ title = cleanLine(super.stripAll(new serverCharBuffer(text)).toString());
}
private static String cleanLine(String s) {
@@ -250,13 +235,9 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
// extract headline from content
if (content.length() > 80) {
- try {
- return cleanLine(new String(content.getBytes(), 0, 80,this.charset));
- } catch (UnsupportedEncodingException e) {
- return cleanLine(new String(content.getBytes(), 0, 80));
- }
+ return cleanLine(new String(content.getChars(), 0, 80));
}
- return cleanLine(content.trim().toString(this.charset));
+ return cleanLine(content.trim().toString());
}
public String[] getHeadlines(int i) {
@@ -267,7 +248,11 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
}
public byte[] getText() {
- return content.getBytes();
+ try {
+ return content.toString().getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ return content.toString().getBytes();
+ }
}
public Map getAnchors() {
@@ -359,7 +344,7 @@ public class htmlFilterContentScraper extends htmlFilterAbstractScraper implemen
System.out.println("ANCHORS :" + anchors.toString());
System.out.println("IMAGES :" + images.toString());
System.out.println("METAS :" + metas.toString());
- System.out.println("TEXT :" + new String(content.getBytes()));
+ System.out.println("TEXT :" + content.toString());
}
diff --git a/source/de/anomic/htmlFilter/htmlFilterContentTransformer.java b/source/de/anomic/htmlFilter/htmlFilterContentTransformer.java
index 838ad8e16..d39a0b9fc 100644
--- a/source/de/anomic/htmlFilter/htmlFilterContentTransformer.java
+++ b/source/de/anomic/htmlFilter/htmlFilterContentTransformer.java
@@ -46,7 +46,6 @@ package de.anomic.htmlFilter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
-import java.io.UnsupportedEncodingException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Locale;
@@ -55,6 +54,7 @@ import java.util.TreeSet;
import de.anomic.http.httpTemplate;
import de.anomic.server.serverByteBuffer;
+import de.anomic.server.serverCharBuffer;
public class htmlFilterContentTransformer extends htmlFilterAbstractTransformer implements htmlFilterTransformer {
@@ -111,25 +111,20 @@ public class htmlFilterContentTransformer extends htmlFilterAbstractTransformer
return (bluelist.size() == 0) && (!gettext);
}
- private static byte[] genBlueLetters(int length) {
- serverByteBuffer bb = new serverByteBuffer(" <FONT COLOR=#0000FF>".getBytes());
+ private static char[] genBlueLetters(int length) {
+ serverCharBuffer bb = new serverCharBuffer(" <FONT COLOR=#0000FF>".toCharArray());
length = length / 2;
if (length > 10) length = 7;
while (length-- > 0) {
- bb.append((byte) 'X');
+ bb.append('X');
}
- bb.append("</FONT> ".getBytes());
- return bb.getBytes();
+ bb.append("</FONT> ");
+ return bb.getChars();
}
- private boolean bluelistHit(byte[] text) {
+ private boolean bluelistHit(char[] text) {
if (text == null || bluelist == null) return false;
- String lc;
- try {
- lc = new String(text, "UTF-8").toLowerCase();
- } catch (UnsupportedEncodingException e) {
- lc = new String(text).toLowerCase();
- }
+ String lc = new String(text).toLowerCase();
for (int i = 0; i < bluelist.size(); i++) {
if (lc.indexOf((String) bluelist.get(i)) >= 0) return true;
}
@@ -158,65 +153,64 @@ public class htmlFilterContentTransformer extends htmlFilterAbstractTransformer
}
return result;
}
- public byte[] transformText(byte[] text) {
+ public char[] transformText(char[] text) {
if (gettext) {
- serverByteBuffer sbb = new serverByteBuffer(text);
- //if (sbb.length() > 0) System.out.println(" TEXT: " + sbb.toString());
- serverByteBuffer[] sbbs = httpTemplate.splitQuotations(sbb);
- sbb = new serverByteBuffer();
- for (int i = 0; i < sbbs.length; i++) {
- if (sbbs[i].isWhitespace(true)) {
- sbb.append(sbbs[i]);
- } else if ((sbbs[i].byteAt(0) == httpTemplate.hash) ||
- (sbbs[i].startsWith(httpTemplate.dpdpa))) {
- // this is a template or a part of a template
- sbb.append(sbbs[i]);
- } else {
- // this is a text fragment, generate gettext quotation
- int ws = sbbs[i].whitespaceStart(true);
- int we = sbbs[i].whitespaceEnd(true);
- sbb.append(sbbs[i].getBytes(0, ws));
- sbb.append('_');
- sbb.append('(');
- sbb.append(sbbs[i].getBytes(ws, we));
- sbb.append(')');
- sbb.append(sbbs[i].getBytes(we));
- }
- }
- //if (sbb.length() > 0) System.out.println("GETTEXT: " + sbb.toString());
- return sbb.getBytes();
+// serverCharBuffer sbb = new serverCharBuffer(text);
+// //if (sbb.length() > 0) System.out.println(" TEXT: " + sbb.toString());
+// serverCharBuffer[] sbbs = httpTemplate.splitQuotations(sbb);
+// sbb = new serverCharBuffer();
+// for (int i = 0; i < sbbs.length; i++) {
+// if (sbbs[i].isWhitespace(true)) {
+// sbb.append(sbbs[i]);
+// } else if ((sbbs[i].byteAt(0) == httpTemplate.hash) ||
+// (sbbs[i].startsWith(httpTemplate.dpdpa))) {
+// // this is a template or a part of a template
+// sbb.append(sbbs[i]);
+// } else {
+// // this is a text fragment, generate gettext quotation
+// int ws = sbbs[i].whitespaceStart(true);
+// int we = sbbs[i].whitespaceEnd(true);
+// sbb.append(sbbs[i].getBytes(0, ws));
+// sbb.append('_');
+// sbb.append('(');
+// sbb.append(sbbs[i].getBytes(ws, we));
+// sbb.append(')');
+// sbb.append(sbbs[i].getBytes(we));
+// }
+// }
+// //if (sbb.length() > 0) System.out.println("GETTEXT: " + sbb.toString());
+// return sbb.getChars();
}
if (bluelist != null) {
if (bluelistHit(text)) {
// System.out.println("FILTERHIT: " + text);
return genBlueLetters(text.length);
- } else {
- return text;
}
+ return text;
}
return text;
}
- public byte[] transformTag0(String tagname, Properties tagopts, byte quotechar) {
+ public char[] transformTag0(String tagname, Properties tagopts, char quotechar) {
if (tagname.equals("img")) {
// check bluelist
- if (bluelistHit(tagopts.getProperty("src","").getBytes())) return genBlueLetters(5);
- if (bluelistHit(tagopts.getProperty("alt","").getBytes())) return genBlueLetters(5);
+ if (bluelistHit(tagopts.getProperty("src","").toCharArray())) return genBlueLetters(5);
+ if (bluelistHit(tagopts.getProperty("alt","").toCharArray())) return genBlueLetters(5);
// replace image alternative name
- tagopts.setProperty("alt", new String(transformText(tagopts.getProperty("alt","").getBytes())));
+ tagopts.setProperty("alt", new String(transformText(tagopts.getProperty("alt","").toCharArray())));
}
if ((tagname.equals("input")) && (tagopts.getProperty("type").equals("submit"))) {
// rewrite button name
- tagopts.setProperty("value", new String(transformText(tagopts.getProperty("value","").getBytes())));
+ tagopts.setProperty("value", new String(transformText(tagopts.getProperty("value","").toCharArray())));
}
- return htmlFilterOutputStream.genTag0(tagname, tagopts, quotechar);
+ return htmlFilterWriter.genTag0(tagname, tagopts, quotechar);
}
- public byte[] transformTag1(String tagname, Properties tagopts, byte[] text, byte quotechar) {
- if (bluelistHit(tagopts.getProperty("href","").getBytes())) return genBlueLetters(text.length);
+ public char[] transformTag1(String tagname, Properties tagopts, char[] text, char quotechar) {
+ if (bluelistHit(tagopts.getProperty("href","").toCharArray())) return genBlueLetters(text.length);
if (bluelistHit(text)) return genBlueLetters(text.length);
- return htmlFilterOutputStream.genTag1(tagname, tagopts, text, quotechar);
+ return htmlFilterWriter.genTag1(tagname, tagopts, text, quotechar);
}
public void close() {
diff --git a/source/de/anomic/htmlFilter/htmlFilterScraper.java b/source/de/anomic/htmlFilter/htmlFilterScraper.java
index a4f494591..ad054233d 100644
--- a/source/de/anomic/htmlFilter/htmlFilterScraper.java
+++ b/source/de/anomic/htmlFilter/htmlFilterScraper.java
@@ -40,7 +40,6 @@
package de.anomic.htmlFilter;
-import java.nio.charset.UnsupportedCharsetException;
import java.util.Properties;
public interface htmlFilterScraper {
@@ -49,22 +48,11 @@ public interface htmlFilterScraper {
public boolean isTag1(String tag);
- public void scrapeText(byte[] text);
+ public void scrapeText(char[] text);
public void scrapeTag0(String tagname, Properties tagopts);
- public void scrapeTag1(String tagname, Properties tagopts, byte[] text);
+ public void scrapeTag1(String tagname, Properties tagopts, char[] text);
public void close();
-
- /**
- * @param charset the charset of the source document
- * @throws UnsupportedCharsetException
- */
- public void setCharset(String charset) throws UnsupportedCharsetException;
-
- /**
- * @return charset the charset of the source document
- */
- public String getCharset();
}
diff --git a/source/de/anomic/htmlFilter/htmlFilterTransformer.java b/source/de/anomic/htmlFilter/htmlFilterTransformer.java
index a255c255f..e28e8fd2d 100644
--- a/source/de/anomic/htmlFilter/htmlFilterTransformer.java
+++ b/source/de/anomic/htmlFilter/htmlFilterTransformer.java
@@ -65,13 +65,13 @@ public interface htmlFilterTransformer {
// method that is called with any text between tags
// the returned text replaces the given text
// if the text shall not be changed, it must be returned as called
- public byte[] transformText(byte[] text);
+ public char[] transformText(char[] text);
// method that is called when a body-less tag occurs
- public byte[] transformTag0(String tagname, Properties tagopts, byte quotechar);
+ public char[] transformTag0(String tagname, Properties tagopts, char quotechar);
// method that is called when a body-containing text occurs
- public byte[] transformTag1(String tagname, Properties tagopts, byte[] text, byte quotechar);
+ public char[] transformTag1(String tagname, Properties tagopts, char[] text, char quotechar);
public void close();
}
diff --git a/source/de/anomic/htmlFilter/htmlFilterOutputStream.java b/source/de/anomic/htmlFilter/htmlFilterWriter.java
similarity index 62%
rename from source/de/anomic/htmlFilter/htmlFilterOutputStream.java
rename to source/de/anomic/htmlFilter/htmlFilterWriter.java
index bf168739a..a232be49a 100644
--- a/source/de/anomic/htmlFilter/htmlFilterOutputStream.java
+++ b/source/de/anomic/htmlFilter/htmlFilterWriter.java
@@ -4,9 +4,9 @@
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004, 2005
//
-// $LastChangedDate$
-// $LastChangedRevision$
-// $LastChangedBy$
+// $LastChangedDate: 2006-09-15 17:01:25 +0200 (Fr, 15 Sep 2006) $
+// $LastChangedRevision: 2598 $
+// $LastChangedBy: theli $
//
// 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
@@ -50,34 +50,37 @@
package de.anomic.htmlFilter;
-import de.anomic.net.URL;
-
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
import java.io.UnsupportedEncodingException;
+import java.io.Writer;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Properties;
-import de.anomic.server.serverByteBuffer;
-public final class htmlFilterOutputStream extends OutputStream {
+import de.anomic.net.URL;
+import de.anomic.server.serverCharBuffer;
+
+public final class htmlFilterWriter extends Writer {
- public static final byte lb = (byte) '<';
- public static final byte rb = (byte) '>';
- public static final byte dash = (byte) '-';
- public static final byte excl = (byte) '!';
- public static final byte singlequote = (byte) 39;
- public static final byte doublequote = (byte) 34;
+ public static final char lb = '<';
+ public static final char rb = '>';
+ public static final char dash = '-';
+ public static final char excl = '!';
+ public static final char singlequote = '\'';
+ public static final char doublequote = '"';
- private OutputStream out;
- private serverByteBuffer buffer;
+ private OutputStream outStream;
+ private OutputStreamWriter out;
+ private serverCharBuffer buffer;
private String filterTag;
private Properties filterOpts;
- private serverByteBuffer filterCont;
+ private serverCharBuffer filterCont;
private htmlFilterScraper scraper;
private htmlFilterTransformer transformer;
private boolean inSingleQuote;
@@ -87,13 +90,17 @@ public final class htmlFilterOutputStream extends OutputStream {
private boolean binaryUnsuspect;
private boolean passbyIfBinarySuspect;
- public htmlFilterOutputStream(OutputStream out, htmlFilterScraper scraper,
- htmlFilterTransformer transformer,
- boolean passbyIfBinarySuspect) {
- this.out = out;
+ public htmlFilterWriter(
+ OutputStream outStream,
+ String outputStreamCharset,
+ htmlFilterScraper scraper,
+ htmlFilterTransformer transformer,
+ boolean passbyIfBinarySuspect
+ ) throws UnsupportedEncodingException {
+ this.outStream = outStream;
this.scraper = scraper;
this.transformer = transformer;
- this.buffer = new serverByteBuffer(1024);
+ this.buffer = new serverCharBuffer(1024);
this.filterTag = null;
this.filterOpts = null;
this.filterCont = null;
@@ -103,74 +110,77 @@ public final class htmlFilterOutputStream extends OutputStream {
this.inScript = false;
this.binaryUnsuspect = true;
this.passbyIfBinarySuspect = passbyIfBinarySuspect;
+
+ if (this.outStream != null) {
+ this.out = new OutputStreamWriter(this.outStream,(outputStreamCharset == null)?"UTF-8":outputStreamCharset);
+ }
}
- public static byte[] genTag0raw(String tagname, boolean opening, byte[] tagopts) {
- serverByteBuffer bb = new serverByteBuffer(tagname.length() + tagopts.length + 3);
- bb.append((byte) '<');
+ public static char[] genTag0raw(String tagname, boolean opening, char[] tagopts) {
+ serverCharBuffer bb = new serverCharBuffer(tagname.length() + tagopts.length + 3);
+ bb.append('<');
if (!opening) {
- bb.append((byte) '/');
+ bb.append('/');
}
- bb.append(tagname.getBytes());
+ bb.append(tagname);
if (tagopts.length > 0) {
// if (tagopts[0] == (byte) 32)
bb.append(tagopts);
// else bb.append((byte) 32).append(tagopts);
}
- bb.append((byte) '>');
- return bb.getBytes();
+ bb.append('>');
+ return bb.getChars();
}
- public static byte[] genTag1raw(String tagname, byte[] tagopts, byte[] text) {
- serverByteBuffer bb = new serverByteBuffer(2 * tagname.length() + tagopts.length + text.length + 5);
- bb.append((byte) '<').append(tagname.getBytes());
+ public static char[] genTag1raw(String tagname, char[] tagopts, char[] text) {
+ serverCharBuffer bb = new serverCharBuffer(2 * tagname.length() + tagopts.length + text.length + 5);
+ bb.append('<').append(tagname);
if (tagopts.length > 0) {
// if (tagopts[0] == (byte) 32)
bb.append(tagopts);
// else bb.append((byte) 32).append(tagopts);
}
- bb.append((byte) '>');
+ bb.append('>');
bb.append(text);
- bb.append((byte) '<').append((byte) '/').append(tagname.getBytes()).append((byte) '>');
- return bb.getBytes();
+ bb.append('<').append('/').append(tagname).append('>');
+ return bb.getChars();
}
- public static byte[] genTag0(String tagname, Properties tagopts, byte quotechar) {
- byte[] tagoptsx = (tagopts.size() == 0) ? null : genOpts(tagopts, quotechar);
- serverByteBuffer bb = new serverByteBuffer(tagname.length() + ((tagoptsx == null) ? 0 : (tagoptsx.length + 1)) + tagname.length() + 2).append((byte) '<').append(tagname.getBytes());
+ public static char[] genTag0(String tagname, Properties tagopts, char quotechar) {
+ char[] tagoptsx = (tagopts.size() == 0) ? null : genOpts(tagopts, quotechar);
+ serverCharBuffer bb = new serverCharBuffer(tagname.length() + ((tagoptsx == null) ? 0 : (tagoptsx.length + 1)) + tagname.length() + 2);
+ bb.append('<').append(tagname);
if (tagoptsx != null) {
- bb = bb.append((byte) 32).append(tagoptsx);
+ bb.append(32);
+ bb.append(tagoptsx);
}
- bb = bb.append((byte) '>');
- return bb.getBytes();
+ bb.append('>');
+ return bb.getChars();
}
- public static byte[] genTag1(String tagname, Properties tagopts, byte[] text, byte quotechar) {
- byte[] gt0 = genTag0(tagname, tagopts, quotechar);
- return new serverByteBuffer(gt0, gt0.length + text.length + tagname.length() + 3).append(text).append(("</" + tagname + ">").getBytes()).getBytes();
+ public static char[] genTag1(String tagname, Properties tagopts, char[] text, char quotechar) {
+ char[] gt0 = genTag0(tagname, tagopts, quotechar);
+ serverCharBuffer cb = new serverCharBuffer(gt0, gt0.length + text.length + tagname.length() + 3);
+ cb.append(text).append('<').append('/').append(tagname).append('>');
+ return cb.getChars();
}
// a helper method for pretty-printing of properties for html tags
- public static byte[] genOpts(Properties prop, byte quotechar) {
+ public static char[] genOpts(Properties prop, char quotechar) {
Enumeration e = prop.propertyNames();
- serverByteBuffer bb = new serverByteBuffer(prop.size() * 40);
+ serverCharBuffer bb = new serverCharBuffer(prop.size() * 40);
String key;
while (e.hasMoreElements()) {
key = (String) e.nextElement();
- bb = bb.append((byte) 32).append(key.getBytes()).append((byte) '=');
- bb = bb.append(quotechar);
- try {
- bb.append(prop.getProperty(key).getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e1) {
- bb.append(prop.getProperty(key).getBytes());
- }
+ bb.append(32).append(key).append('=').append(quotechar);
+ bb.append(prop.getProperty(key));
bb.append(quotechar);
}
- if (bb.length() > 0) return bb.getBytes(1);
- return bb.getBytes();
+ if (bb.length() > 0) return bb.getChars(1);
+ return bb.getChars();
}
- private byte[] filterTag(String tag, boolean opening, byte[] content, byte quotechar) {
+ private char[] filterTag(String tag, boolean opening, char[] content, char quotechar) {
// System.out.println("FILTER1: filterTag=" + ((filterTag == null) ? "null" : filterTag) + ", tag=" + tag + ", opening=" + ((opening) ? "true" : "false") + ", content=" + new String(content)); // debug
if (filterTag == null) {
// we are not collection tag text
@@ -185,18 +195,18 @@ public final class htmlFilterOutputStream extends OutputStream {
if (opening) {
if ((scraper != null) && (scraper.isTag0(tag))) {
// this single tag is collected at once here
- scraper.scrapeTag0(tag, new serverByteBuffer(content).propParser(scraper.getCharset()));
+ scraper.scrapeTag0(tag, new serverCharBuffer(content).propParser());
}
if ((transformer != null) && (transformer.isTag0(tag))) {
// this single tag is collected at once here
- return transformer.transformTag0(tag, new serverByteBuffer(content).propParser(scraper.getCharset()), quotechar);
+ return transformer.transformTag0(tag, new serverCharBuffer(content).propParser(), quotechar);
} else if (((scraper != null) && (scraper.isTag1(tag))) ||
((transformer != null) && (transformer.isTag1(tag)))) {
// ok, start collecting
filterTag = tag;
- filterOpts = new serverByteBuffer(content).propParser(scraper.getCharset());
- filterCont = new serverByteBuffer();
- return new byte[0];
+ filterOpts = new serverCharBuffer(content).propParser();
+ filterCont = new serverCharBuffer();
+ return new char[0];
} else {
// we ignore that thing and return it again
return genTag0raw(tag, true, content);
@@ -217,23 +227,23 @@ public final class htmlFilterOutputStream extends OutputStream {
} else {
filterCont.append(content);
}
- return new byte[0];
+ return new char[0];
}
// it's a tag! which one?
if ((opening) || (!(tag.equals(filterTag)))) {
// this tag is not our concern. just add it
filterCont.append(genTag0raw(tag, opening, content));
- return new byte[0];
+ return new char[0];
}
// it's our closing tag! return complete result.
- byte[] ret;
- if (scraper != null) scraper.scrapeTag1(filterTag, filterOpts, filterCont.getBytes());
+ char[] ret;
+ if (scraper != null) scraper.scrapeTag1(filterTag, filterOpts, filterCont.getChars());
if (transformer != null) {
- ret = transformer.transformTag1(filterTag, filterOpts, filterCont.getBytes(), quotechar);
+ ret = transformer.transformTag1(filterTag, filterOpts, filterCont.getChars(), quotechar);
} else {
- ret = genTag1(filterTag, filterOpts, filterCont.getBytes(), quotechar);
+ ret = genTag1(filterTag, filterOpts, filterCont.getChars(), quotechar);
}
filterTag = null;
filterOpts = null;
@@ -241,18 +251,18 @@ public final class htmlFilterOutputStream extends OutputStream {
return ret;
}
- private byte[] filterFinalize(byte quotechar) {
+ private char[] filterFinalize(char quotechar) {
if (filterTag == null) {
- return new byte[0];
+ return new char[0];
}
// it's our closing tag! return complete result.
- byte[] ret;
- if (scraper != null) scraper.scrapeTag1(filterTag, filterOpts, filterCont.getBytes());
+ char[] ret;
+ if (scraper != null) scraper.scrapeTag1(filterTag, filterOpts, filterCont.getChars());
if (transformer != null) {
- ret = transformer.transformTag1(filterTag, filterOpts, filterCont.getBytes(), quotechar);
+ ret = transformer.transformTag1(filterTag, filterOpts, filterCont.getChars(), quotechar);
} else {
- ret = genTag1(filterTag, filterOpts, filterCont.getBytes(), quotechar);
+ ret = genTag1(filterTag, filterOpts, filterCont.getChars(), quotechar);
}
filterTag = null;
filterOpts = null;
@@ -260,7 +270,7 @@ public final class htmlFilterOutputStream extends OutputStream {
return ret;
}
- private byte[] filterSentence(byte[] in, byte quotechar) {
+ private char[] filterSentence(char[] in, char quotechar) {
if (in.length == 0) return in;
// System.out.println("FILTER0: " + new String(in)); // debug
// scan the string and parse structure
@@ -272,16 +282,16 @@ public final class htmlFilterOutputStream extends OutputStream {
if (in[1] == '/') {
// a closing tag
tagend = tagEnd(in, 2);
- tag = new String(in, 2, tagend - 2); // TODO: bugfix needed for other charsets
- byte[] text = new byte[in.length - tagend - 1];
+ tag = new String(in, 2, tagend - 2);
+ char[] text = new char[in.length - tagend - 1];
System.arraycopy(in, tagend, text, 0, in.length - tagend - 1);
return filterTag(tag, false, text, quotechar);
}
// an opening tag
tagend = tagEnd(in, 1);
- tag = new String(in, 1, tagend - 1); // TODO: bugfix needed for other charsets
- byte[] text = new byte[in.length - tagend - 1];
+ tag = new String(in, 1, tagend - 1);
+ char[] text = new char[in.length - tagend - 1];
System.arraycopy(in, tagend, text, 0, in.length - tagend - 1);
return filterTag(tag, true, text, quotechar);
}
@@ -290,10 +300,10 @@ public final class htmlFilterOutputStream extends OutputStream {
return filterTag(null, true, in, quotechar);
}
- private static int tagEnd(byte[] tag, int start) {
+ private static int tagEnd(char[] tag, int start) {
char c;
for (int i = start; i < tag.length; i++) {
- c = (char) tag[i];
+ c = tag[i];
if (c != '!' && c != '-' &&
(c < '0' || c > '9') &&
(c < 'a' || c > 'z') &&
@@ -303,145 +313,141 @@ public final class htmlFilterOutputStream extends OutputStream {
return tag.length - 1;
}
- public void write(int b) throws IOException {
- write((byte) (b & 0xff));
- }
-
- private void write(byte b) throws IOException {
+ public void write(int c) throws IOException {
// System.out.println((char) b);
- if ((binaryUnsuspect) && (binaryHint(b))) {
+ if ((binaryUnsuspect) && (binaryHint((char)c))) {
binaryUnsuspect = false;
if (passbyIfBinarySuspect) finalize();
}
if (binaryUnsuspect || !passbyIfBinarySuspect) {
- byte[] filtered;
+ char[] filtered;
if (inSingleQuote) {
- buffer.append(b);
- if (b == singlequote) inSingleQuote = false;
+ buffer.append(c);
+ if (c == singlequote) inSingleQuote = false;
// check error cases
- if ((b == rb) && (buffer.byteAt(0) == lb)) {
+ if ((c == rb) && (buffer.charAt(0) == lb)) {
inSingleQuote = false;
// the tag ends here. after filtering: pass on
- filtered = filterSentence(buffer.getBytes(), singlequote);
+ filtered = filterSentence(buffer.getChars(), singlequote);
if (out != null) { out.write(filtered); }
// buffer = new serverByteBuffer();
buffer.reset();
}
} else if (inDoubleQuote) {
- buffer.append(b);
- if (b == doublequote) inDoubleQuote = false;
+ buffer.append(c);
+ if (c == doublequote) inDoubleQuote = false;
// check error cases
- if (b == rb && buffer.byteAt(0) == lb) {
+ if (c == rb && buffer.charAt(0) == lb) {
inDoubleQuote = false;
// the tag ends here. after filtering: pass on
- filtered = filterSentence(buffer.getBytes(), doublequote);
+ filtered = filterSentence(buffer.getChars(), doublequote);
if (out != null) out.write(filtered);
// buffer = new serverByteBuffer();
buffer.reset();
}
} else if (inComment) {
- buffer.append(b);
- if (b == rb &&
+ buffer.append(c);
+ if (c == rb &&
buffer.length() > 6 &&
- buffer.byteAt(buffer.length() - 3) == dash) {
+ buffer.charAt(buffer.length() - 3) == dash) {
// comment is at end
inComment = false;
- if (out != null) out.write(buffer.getBytes());
+ if (out != null) out.write(buffer.getChars());
// buffer = new serverByteBuffer();
buffer.reset();
}
} else if (inScript) {
- buffer.append(b);
+ buffer.append(c);
int bufferLength = buffer.length();
- if ((b == rb) && (bufferLength > 14) &&
- (buffer.byteAt(bufferLength - 8) == (byte) '/') &&
- (buffer.byteAt(bufferLength - 7) == (byte) 's') &&
- (buffer.byteAt(bufferLength - 6) == (byte) 'c') &&
- (buffer.byteAt(bufferLength - 5) == (byte) 'r') &&
- (buffer.byteAt(bufferLength - 4) == (byte) 'i') &&
- (buffer.byteAt(bufferLength - 3) == (byte) 'p') &&
- (buffer.byteAt(bufferLength - 2) == (byte) 't')) {
+ if ((c == rb) && (bufferLength > 14) &&
+ (buffer.charAt(bufferLength - 8) == '/') &&
+ (buffer.charAt(bufferLength - 7) == 's') &&
+ (buffer.charAt(bufferLength - 6) == 'c') &&
+ (buffer.charAt(bufferLength - 5) == 'r') &&
+ (buffer.charAt(bufferLength - 4) == 'i') &&
+ (buffer.charAt(bufferLength - 3) == 'p') &&
+ (buffer.charAt(bufferLength - 2) == 't')) {
// script is at end
inScript = false;
- if (out != null) out.write(buffer.getBytes());
+ if (out != null) out.write(buffer.getChars());
// buffer = new serverByteBuffer();
buffer.reset();
}
} else {
if (buffer.length() == 0) {
- if (b == rb) {
+ if (c == rb) {
// very strange error case; we just let it pass
- if (out != null) out.write(b);
+ if (out != null) out.write(c);
} else {
- buffer.append(b);
+ buffer.append(c);
}
- } else if (buffer.byteAt(0) == lb) {
- if (b == singlequote) inSingleQuote = true;
- if (b == doublequote) inDoubleQuote = true;
+ } else if (buffer.charAt(0) == lb) {
+ if (c == singlequote) inSingleQuote = true;
+ if (c == doublequote) inDoubleQuote = true;
// fill in tag text
- if ((buffer.length() == 3) && (buffer.byteAt(1) == excl) &&
- (buffer.byteAt(2) == dash) && (b == dash)) {
+ if ((buffer.length() == 3) && (buffer.charAt(1) == excl) &&
+ (buffer.charAt(2) == dash) && (c == dash)) {
// this is the start of a comment
inComment = true;
- buffer.append(b);
+ buffer.append(c);
} else if ((buffer.length() == 6) &&
- (buffer.byteAt(1) == (byte) 's') &&
- (buffer.byteAt(2) == (byte) 'c') &&
- (buffer.byteAt(3) == (byte) 'r') &&
- (buffer.byteAt(4) == (byte) 'i') &&
- (buffer.byteAt(5) == (byte) 'p') &&
- (b == (byte) 't')) {
+ (buffer.charAt(1) == 's') &&
+ (buffer.charAt(2) == 'c') &&
+ (buffer.charAt(3) == 'r') &&
+ (buffer.charAt(4) == 'i') &&
+ (buffer.charAt(5) == 'p') &&
+ (c == 't')) {
// this is the start of a comment
inScript = true;
- buffer.append(b);
- } else if (b == rb) {
- buffer.append(b);
+ buffer.append(c);
+ } else if (c == rb) {
+ buffer.append(c);
// the tag ends here. after filtering: pass on
- filtered = filterSentence(buffer.getBytes(), doublequote);
+ filtered = filterSentence(buffer.getChars(), doublequote);
if (out != null) out.write(filtered);
// buffer = new serverByteBuffer();
buffer.reset();
- } else if (b == lb) {
+ } else if (c == lb) {
// this is an error case
// we consider that there is one rb missing
if (buffer.length() > 0) {
- filtered = filterSentence(buffer.getBytes(), doublequote);
+ filtered = filterSentence(buffer.getChars(), doublequote);
if (out != null) out.write(filtered);
}
// buffer = new serverByteBuffer();
buffer.reset();
- buffer.append(b);
+ buffer.append(c);
} else {
- buffer.append(b);
+ buffer.append(c);
}
} else {
// fill in plain text
- if (b == lb) {
+ if (c == lb) {
// the text ends here
if (buffer.length() > 0) {
- filtered = filterSentence(buffer.getBytes(), doublequote);
+ filtered = filterSentence(buffer.getChars(), doublequote);
if (out != null) out.write(filtered);
}
// buffer = new serverByteBuffer();
buffer.reset();
- buffer.append(b);
+ buffer.append(c);
} else {
// simply append
- buffer.append(b);
+ buffer.append(c);
}
}
}
} else {
- out.write(b);
+ out.write(c);
}
}
- public void write(byte b[]) throws IOException {
- this.write(b, 0, b.length);
+ public void write(char b[]) throws IOException {
+ write(b, 0, b.length);
}
- public void write(byte b[], int off, int len) throws IOException {
+ public void write(char b[], int off, int len) throws IOException {
// System.out.println(new String(b, off, len));
if ((off | len | (b.length - (len + off)) | (off + len)) < 0) throw new IndexOutOfBoundsException();
for (int i = off ; i < (len - off) ; i++) this.write(b[i]);
@@ -462,15 +468,15 @@ public final class htmlFilterOutputStream extends OutputStream {
}
public void close() throws IOException {
- byte quotechar = (inSingleQuote) ? singlequote : doublequote;
+ char quotechar = (inSingleQuote) ? singlequote : doublequote;
if (buffer != null) {
if (buffer.length() > 0) {
- byte[] filtered = filterSentence(buffer.getBytes(), quotechar);
+ char[] filtered = filterSentence(buffer.getChars(), quotechar);
if (out != null) out.write(filtered);
}
buffer = null;
}
- byte[] finalized = filterFinalize(quotechar);
+ char[] finalized = filterFinalize(quotechar);
if (out != null) {
if (finalized != null) out.write(finalized);
out.flush();
@@ -483,10 +489,11 @@ public final class htmlFilterOutputStream extends OutputStream {
// if (transformer != null) {transformer.close(); transformer = null;}
}
- private static boolean binaryHint(byte b) {
- if (b < 0) return false;
- if (b > 31) return false;
- if ((b == 8) || (b == 9) || (b == 10) || (b == 13)) return false;
+ private static boolean binaryHint(char c) {
+// return Character.isDefined(c);
+ if (c < 0) return false;
+ if (c > 31) return false;
+ if ((c == 8) || (c == 9) || (c == 10) || (c == 13)) return false;
// return false;
// System.out.println("BINARY HINT: " + (int) b);
return true;
@@ -499,14 +506,16 @@ public final class htmlFilterOutputStream extends OutputStream {
public static void main(String[] args) {
// takes one argument: a file name
if (args.length != 1) return;
- byte[] buffer = new byte[512];
+ char[] buffer = new char[512];
try {
htmlFilterContentScraper scraper = new htmlFilterContentScraper(new URL("http://localhost:8080"));
- htmlFilterTransformer transformer = new htmlFilterContentTransformer();
+ htmlFilterTransformer transformer = new htmlFilterContentTransformer();
+ // TODO: this does not work at the moment
+ System.exit(0);
transformer.init("gettext");
- InputStream is = new FileInputStream(args[0]);
+ Reader is = new FileReader(args[0]);
FileOutputStream fos = new FileOutputStream(new File(args[0] + ".out"));
- OutputStream os = new htmlFilterOutputStream(fos, scraper, transformer, false);
+ Writer os = new htmlFilterWriter(fos, "UTF-8",scraper, transformer, false);
int i;
while ((i = is.read(buffer)) > 0) os.write(buffer, 0, i);
os.close();
diff --git a/source/de/anomic/http/httpc.java b/source/de/anomic/http/httpc.java
index acd86f72a..e9c7a4bbe 100644
--- a/source/de/anomic/http/httpc.java
+++ b/source/de/anomic/http/httpc.java
@@ -48,6 +48,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
+import java.io.Writer;
import java.lang.Math;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@@ -86,6 +87,7 @@ import de.anomic.kelondro.kelondroMScoreCluster;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.server.serverByteBuffer;
import de.anomic.server.serverCore;
+import de.anomic.server.serverFileUtils;
import de.anomic.server.serverObjects;
import de.anomic.server.logging.serverLog;
@@ -1586,65 +1588,65 @@ do upload
}
}
- /**
- * This method outputs the input stream to either an output socket or an
- * file or both. If the length of the input stream is given in the
- * header, exactly that lenght is written. Otherwise the stream is
- * written, till it is closed. If this instance is zipped, stream the
- * input stream through gzip to unzip it on the fly.
- *
- * @param procOS OutputStream where the stream is to be written. If null
- * no write happens.
- * @param bufferOS OutputStream where the stream is to be written too.
- * If null no write happens.
- * @param clientInput InputStream where the content is to be read from.
- * @throws IOException
- */
- public static void writeContentX(InputStream clientInput, boolean usegzip, long length, OutputStream procOS, OutputStream bufferOS) throws IOException {
- // we write length bytes, but if length == -1 (or < 0) then we
- // write until the input stream closes
- // procOS == null -> no write to procOS
- // file == null -> no write to file
- // If the Content-Encoding is gzip, we gunzip on-the-fly
- // and change the Content-Encoding and Content-Length attributes in the header
- byte[] buffer = new byte[2048];
- int l;
- long len = 0;
-
- // using the proper intput stream
- InputStream dis = (usegzip) ? (InputStream) new GZIPInputStream(clientInput) : (InputStream) clientInput;
-
- // we have three methods of reading: length-based, length-based gzip and connection-close-based
- try {
- if (length > 0) {
- // we read exactly 'length' bytes
- while ((len < length) && ((l = dis.read(buffer)) >= 0)) {
- if (procOS != null) procOS.write(buffer, 0, l);
- if (bufferOS != null) bufferOS.write(buffer, 0, l);
- len += l;
- }
- } else {
- // no content-length was given, thus we read until the connection closes
- while ((l = dis.read(buffer, 0, buffer.length)) >= 0) {
- if (procOS != null) procOS.write(buffer, 0, l);
- if (bufferOS != null) bufferOS.write(buffer, 0, l);
- }
- }
- } catch (java.net.SocketException e) {
- throw new IOException("Socket exception: " + e.getMessage());
- } catch (java.net.SocketTimeoutException e) {
- throw new IOException("Socket time-out: " + e.getMessage());
- } finally {
- // close the streams
- if (procOS != null) {
- if (procOS instanceof httpChunkedOutputStream)
- ((httpChunkedOutputStream)procOS).finish();
- procOS.flush();
- }
- if (bufferOS != null) bufferOS.flush();
- buffer = null;
- }
- }
+// /**
+// * This method outputs the input stream to either an output socket or an
+// * file or both. If the length of the input stream is given in the
+// * header, exactly that lenght is written. Otherwise the stream is
+// * written, till it is closed. If this instance is zipped, stream the
+// * input stream through gzip to unzip it on the fly.
+// *
+// * @param procOS OutputStream where the stream is to be written. If null
+// * no write happens.
+// * @param bufferOS OutputStream where the stream is to be written too.
+// * If null no write happens.
+// * @param clientInput InputStream where the content is to be read from.
+// * @throws IOException
+// */
+// private static void writeContentX(InputStream clientInput, boolean usegzip, long length, OutputStream procOS, OutputStream bufferOS) throws IOException {
+// // we write length bytes, but if length == -1 (or < 0) then we
+// // write until the input stream closes
+// // procOS == null -> no write to procOS
+// // file == null -> no write to file
+// // If the Content-Encoding is gzip, we gunzip on-the-fly
+// // and change the Content-Encoding and Content-Length attributes in the header
+// byte[] buffer = new byte[2048];
+// int l;
+// long len = 0;
+//
+// // using the proper intput stream
+// InputStream dis = (usegzip) ? (InputStream) new GZIPInputStream(clientInput) : (InputStream) clientInput;
+//
+// // we have three methods of reading: length-based, length-based gzip and connection-close-based
+// try {
+// if (length > 0) {
+// // we read exactly 'length' bytes
+// while ((len < length) && ((l = dis.read(buffer)) >= 0)) {
+// if (procOS != null) procOS.write(buffer, 0, l);
+// if (bufferOS != null) bufferOS.write(buffer, 0, l);
+// len += l;
+// }
+// } else {
+// // no content-length was given, thus we read until the connection closes
+// while ((l = dis.read(buffer, 0, buffer.length)) >= 0) {
+// if (procOS != null) procOS.write(buffer, 0, l);
+// if (bufferOS != null) bufferOS.write(buffer, 0, l);
+// }
+// }
+// } catch (java.net.SocketException e) {
+// throw new IOException("Socket exception: " + e.getMessage());
+// } catch (java.net.SocketTimeoutException e) {
+// throw new IOException("Socket time-out: " + e.getMessage());
+// } finally {
+// // close the streams
+// if (procOS != null) {
+// if (procOS instanceof httpChunkedOutputStream)
+// ((httpChunkedOutputStream)procOS).finish();
+// procOS.flush();
+// }
+// if (bufferOS != null) bufferOS.flush();
+// buffer = null;
+// }
+// }
/**
@@ -1799,18 +1801,40 @@ do upload
return ((this.status.charAt(0) == '2') || (this.status.charAt(0) == '3'));
}
+ /**
+ * Returns a {@link InputStream} to read the response body. If the response was encoded using <code>Content-Encoding: gzip</code>
+ * a {@link GZIPInputStream} is returned. If the <code>Content-Length</code> header was set,
+ * a {@link httpContentLengthInputStream} is returned which returns <code>-1</code> if the end of the
+ * response body was reached.
+ *
+ * @return
+ * @throws IOException
+ */
+ public InputStream getContentInputStream() throws IOException {
+ if (this.gzip) {
+ // use a gzip input stream for Content-Encoding: gzip
+ return new GZIPInputStream(httpc.this.clientInput);
+ } else if (this.responseHeader.contentLength() != -1) {
+ // use a httpContentLengthInputStream to read until the end of the response body is reached
+ return new httpContentLengthInputStream(httpc.this.clientInput,this.responseHeader.contentLength());
+ }
+ // no Content-Lenght was set. In this case we can read until EOF
+ return httpc.this.clientInput;
+ }
+
/**
* This method just output the found content into an byte-array and
* returns it.
*
- * @return
+ * @return
* @throws IOException
*/
public byte[] writeContent() throws IOException {
- int contentLength = (int) this.responseHeader.contentLength();
- serverByteBuffer sbb = new serverByteBuffer((contentLength==-1)?8192:contentLength);
- writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), null, sbb);
- return sbb.getBytes();
+// int contentLength = (int) this.responseHeader.contentLength();
+// serverByteBuffer sbb = new serverByteBuffer((contentLength==-1)?8192:contentLength);
+// writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), null, sbb);
+// return sbb.getBytes();
+ return serverFileUtils.read(this.getContentInputStream());
}
/**
@@ -1821,12 +1845,23 @@ do upload
* @return
* @throws IOException
*/
- public byte[] writeContent(OutputStream procOS) throws IOException {
+ public byte[] writeContent(Object procOS) throws IOException {
int contentLength = (int) this.responseHeader.contentLength();
serverByteBuffer sbb = new serverByteBuffer((contentLength==-1)?8192:contentLength);
- writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), procOS, sbb);
+
+ if (procOS instanceof OutputStream) {
+ //writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), procOS, sbb);
+ serverFileUtils.writeX(this.getContentInputStream(), (OutputStream)procOS, sbb);
+ } else if (procOS instanceof Writer) {
+ String charSet = this.responseHeader.getCharacterEncoding();
+ if (charSet == null) charSet = "UTF-8";
+ serverFileUtils.writeX(this.getContentInputStream(), charSet, (Writer)procOS, sbb, charSet);
+ } else {
+ throw new IllegalArgumentException("Invalid procOS object type '" + procOS.getClass().getName() + "'");
+ }
+
return sbb.getBytes();
- }
+ }
/**
* This method writes the input stream to either another output stream
@@ -1836,13 +1871,22 @@ do upload
* @param file
* @throws IOException
*/
- public void writeContent(OutputStream procOS, File file) throws IOException {
+ public void writeContent(Object procOS, File file) throws IOException {
// this writes the input stream to either another output stream or
// a file or both.
FileOutputStream bufferOS = null;
try {
if (file != null) bufferOS = new FileOutputStream(file);
- writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), procOS, bufferOS);
+ if (procOS instanceof OutputStream) {
+ serverFileUtils.writeX(this.getContentInputStream(), (OutputStream) procOS, bufferOS);
+ //writeContentX(httpc.this.clientInput, this.gzip, this.responseHeader.contentLength(), procOS, bufferOS);
+ } else if (procOS instanceof Writer) {
+ String charSet = this.responseHeader.getCharacterEncoding();
+ if (charSet == null) charSet = "UTF-8";
+ serverFileUtils.writeX(this.getContentInputStream(), charSet, (Writer)procOS, bufferOS, charSet);
+ } else {
+ throw new IllegalArgumentException("Invalid procOS object type '" + procOS.getClass().getName() + "'");
+ }
} finally {
if (bufferOS != null) {
bufferOS.close();
diff --git a/source/de/anomic/http/httpdProxyHandler.java b/source/de/anomic/http/httpdProxyHandler.java
index 4de724b49..3c7bcc3ed 100644
--- a/source/de/anomic/http/httpdProxyHandler.java
+++ b/source/de/anomic/http/httpdProxyHandler.java
@@ -70,6 +70,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
+import java.io.Writer;
import java.net.BindException;
import java.net.ConnectException;
import java.net.InetAddress;
@@ -77,7 +78,6 @@ import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.SocketTimeoutException;
-import de.anomic.net.URL;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;
@@ -89,9 +89,11 @@ import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
+
import de.anomic.htmlFilter.htmlFilterContentTransformer;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
import de.anomic.htmlFilter.htmlFilterTransformer;
+import de.anomic.htmlFilter.htmlFilterWriter;
+import de.anomic.net.URL;
import de.anomic.plasma.plasmaHTCache;
import de.anomic.plasma.plasmaParser;
import de.anomic.plasma.plasmaSwitchboard;
@@ -225,7 +227,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
// remoteProxyNoProxyPatterns = remoteProxyNoProxy.split(",");
// set timeout
- timeout = Integer.parseInt(switchboard.getConfig("clientTimeout", "10000"));
+ timeout = Integer.parseInt(switchboard.getConfig("proxy.clientTimeout", "10000"));
// create a htRootPath: system pages
if (htRootPath == null) {
@@ -502,7 +504,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
GZIPOutputStream gzippedOut = null;
httpChunkedOutputStream chunkedOut = null;
- OutputStream hfos = null;
+ Object hfos = null;
httpc remote = null;
httpc.response res = null;
@@ -562,7 +564,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
res.status.startsWith("304")) {
res.responseHeader.put(httpHeader.CONTENT_LENGTH,"0");
} else {
- if (httpVer.equals("HTTP/0.9") || httpVer.equals("HTTP/1.0")) {
+ if (httpVer.equals(httpHeader.HTTP_VERSION_0_9) || httpVer.equals(httpHeader.HTTP_VERSION_1_0)) {
conProp.setProperty(httpHeader.CONNECTION_PROP_PERSISTENT,"close");
} else {
chunkedOut = new httpChunkedOutputStream(respond);
@@ -609,7 +611,10 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
) {
// make a transformer
this.theLogger.logFine("create transformer for URL " + url);
- hfos = new htmlFilterOutputStream((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond), null, transformer, (ext.length() == 0));
+ //hfos = new htmlFilterOutputStream((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond), null, transformer, (ext.length() == 0));
+ String charSet = res.responseHeader.getCharacterEncoding();
+ if (charSet == null) charSet = "UTF-8";
+ hfos = new htmlFilterWriter((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond),charSet, null, transformer, (ext.length() == 0));
} else {
// simply pass through without parsing
this.theLogger.logFine("create passthrough for URL " + url + ", extension '" + ext + "', mime-type '" + res.responseHeader.mime() + "'");
@@ -660,7 +665,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
byte[] cacheArray = res.writeContent(hfos);
this.theLogger.logFine("writeContent of " + url + " produced cacheArray = " + ((cacheArray == null) ? "null" : ("size=" + cacheArray.length)));
- if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
+ if (hfos instanceof htmlFilterWriter) ((htmlFilterWriter) hfos).finalize();
if (sizeBeforeDelete == -1) {
// totally fresh file
@@ -686,7 +691,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
// write to file right here.
cacheFile.getParentFile().mkdirs();
res.writeContent(hfos, cacheFile);
- if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
+ if (hfos instanceof htmlFilterWriter) ((htmlFilterWriter) hfos).finalize();
this.theLogger.logFine("for write-file of " + url + ": contentLength = " + contentLength + ", sizeBeforeDelete = " + sizeBeforeDelete);
cacheManager.writeFileAnnouncement(cacheFile);
if (sizeBeforeDelete == -1) {
@@ -716,7 +721,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
" SupportetContent=" + isSupportedContent);
res.writeContent(hfos, null);
- if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
+ if (hfos instanceof htmlFilterWriter) ((htmlFilterWriter) hfos).finalize();
if (sizeBeforeDelete == -1) {
// no old file and no load. just data passing
//cacheEntry.status = plasmaHTCache.CACHE_PASSING;
@@ -760,7 +765,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
httpChunkedOutputStream chunkedOut = null;
GZIPOutputStream gzippedOut = null;
- OutputStream hfos = null;
+ Object hfos = null;
// we respond on the request by using the cache, the cache is fresh
try {
@@ -809,20 +814,28 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
httpd.sendRespondHeader(conProp,respond,httpVer,203,cachedResponseHeader);
//respondHeader(respond, "203 OK", cachedResponseHeader); // respond with 'non-authoritative'
+ // determine the content charset
+ String charSet = cachedResponseHeader.getCharacterEncoding();
+ if (charSet == null) charSet = "UTF-8";
+
// make a transformer
if ((!(transformer.isIdentityTransformer())) &&
((ext == null) || (!(plasmaParser.supportedRealtimeFileExtContains(url)))) &&
((cachedResponseHeader == null) || (plasmaParser.realtimeParsableMimeTypesContains(cachedResponseHeader.mime())))) {
- hfos = new htmlFilterOutputStream((chunkedOut != null) ? chunkedOut : respond, null, transformer, (ext.length() == 0));
+ hfos = new htmlFilterWriter((chunkedOut != null) ? chunkedOut : respond, charSet, null, transformer, (ext.length() == 0));
} else {
hfos = (gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond);
}
// send also the complete body now from the cache
// simply read the file and transfer to out socket
- serverFileUtils.copy(cacheFile,hfos);
+ if (hfos instanceof OutputStream) {
+ serverFileUtils.copy(cacheFile,(OutputStream)hfos);
+ } else if (hfos instanceof Writer) {
+ serverFileUtils.copy(cacheFile,charSet,(Writer)hfos);
+ }
- if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
+ if (hfos instanceof htmlFilterWriter) ((htmlFilterWriter) hfos).finalize();
if (gzippedOut != null) gzippedOut.finish();
if (chunkedOut != null) chunkedOut.finish();
}
diff --git a/source/de/anomic/plasma/cache/IResourceInfo.java b/source/de/anomic/plasma/cache/IResourceInfo.java
index 81cfc2491..efa22767c 100644
--- a/source/de/anomic/plasma/cache/IResourceInfo.java
+++ b/source/de/anomic/plasma/cache/IResourceInfo.java
@@ -86,7 +86,7 @@ public interface IResourceInfo {
* Returns the charset of the resource
* @return returns the name of the charset or <code>null</code> if unknown
*/
- public String getCharSet();
+ public String getCharacterEncoding();
/**
* Returns the modification date of the cached object
diff --git a/source/de/anomic/plasma/cache/ftp/ResourceInfo.java b/source/de/anomic/plasma/cache/ftp/ResourceInfo.java
index dfbc32ccd..89b86b357 100644
--- a/source/de/anomic/plasma/cache/ftp/ResourceInfo.java
+++ b/source/de/anomic/plasma/cache/ftp/ResourceInfo.java
@@ -161,7 +161,7 @@ public class ResourceInfo implements IResourceInfo {
return responseStatus != null && responseStatus.equalsIgnoreCase("OK");
}
- public String getCharSet() {
+ public String getCharacterEncoding() {
return null;
}
diff --git a/source/de/anomic/plasma/cache/http/ResourceInfo.java b/source/de/anomic/plasma/cache/http/ResourceInfo.java
index ec3768fd1..aeb52d3ab 100644
--- a/source/de/anomic/plasma/cache/http/ResourceInfo.java
+++ b/source/de/anomic/plasma/cache/http/ResourceInfo.java
@@ -111,13 +111,9 @@ public class ResourceInfo implements IResourceInfo {
return ((pos < 0) ? mimeType : mimeType.substring(0, pos));
}
- public String getCharSet() {
+ public String getCharacterEncoding() {
if (this.responseHeader == null) return null;
-
- String mimeType = this.responseHeader.mime();
-
- int pos = mimeType.indexOf(';');
- return ((pos < 0) ? null : mimeType.substring(pos));
+ return this.responseHeader.getCharacterEncoding();
}
/**
diff --git a/source/de/anomic/plasma/parser/rss/rssParser.java b/source/de/anomic/plasma/parser/rss/rssParser.java
index 05209cc75..299e3f865 100644
--- a/source/de/anomic/plasma/parser/rss/rssParser.java
+++ b/source/de/anomic/plasma/parser/rss/rssParser.java
@@ -45,8 +45,7 @@ package de.anomic.plasma.parser.rss;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
-import java.io.OutputStream;
-import de.anomic.net.URL;
+import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
@@ -58,12 +57,14 @@ import java.util.TreeSet;
import de.anomic.htmlFilter.htmlFilterAbstractScraper;
import de.anomic.htmlFilter.htmlFilterContentScraper;
import de.anomic.htmlFilter.htmlFilterImageEntry;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
+import de.anomic.htmlFilter.htmlFilterWriter;
+import de.anomic.net.URL;
import de.anomic.plasma.plasmaParserDocument;
import de.anomic.plasma.parser.AbstractParser;
import de.anomic.plasma.parser.Parser;
import de.anomic.plasma.parser.ParserException;
import de.anomic.server.serverByteBuffer;
+import de.anomic.server.serverCharBuffer;
import de.anomic.server.serverFileUtils;
import de.nava.informa.core.ChannelIF;
import de.nava.informa.core.ImageIF;
@@ -148,14 +149,14 @@ public class rssParser extends AbstractParser implements Parser {
anchors.put(itemURL.toString(),itemTitle);
if ((text.length() != 0) && (text.byteAt(text.length() - 1) != 32)) text.append((byte) 32);
- text.append(new serverByteBuffer(htmlFilterAbstractScraper.stripAll(new serverByteBuffer(itemDescr.getBytes("UTF-8")))).trim()).append((byte) ' '); // TODO: this does not work for utf-8
+ text.append(new serverCharBuffer(htmlFilterAbstractScraper.stripAll(new serverCharBuffer(itemDescr.toCharArray()))).trim()).append(' '); // TODO: this does not work for utf-8
String itemContent = item.getElementValue("content");
if ((itemContent != null) && (itemContent.length() > 0)) {
htmlFilterContentScraper scraper = new htmlFilterContentScraper(itemURL);
- OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
- serverFileUtils.copy(new ByteArrayInputStream(itemContent.getBytes("UTF-8")), os);
+ Writer writer = new htmlFilterWriter(null, null, scraper, null, false);
+ serverFileUtils.copy(new ByteArrayInputStream(itemContent.getBytes("UTF-8")), writer, "UTF-8");
String itemHeadline = scraper.getTitle();
if ((itemHeadline != null) && (itemHeadline.length() > 0)) {
diff --git a/source/de/anomic/plasma/plasmaCondenser.java b/source/de/anomic/plasma/plasmaCondenser.java
index 2da1d87cf..6ca5bfc63 100644
--- a/source/de/anomic/plasma/plasmaCondenser.java
+++ b/source/de/anomic/plasma/plasmaCondenser.java
@@ -47,7 +47,6 @@ package de.anomic.plasma;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
@@ -60,8 +59,6 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
-import de.anomic.htmlFilter.htmlFilterContentScraper;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
import de.anomic.kelondro.kelondroMSetTools;
public final class plasmaCondenser {
@@ -682,47 +679,47 @@ public final class plasmaCondenser {
}
public static void main(String[] args) {
- if ((args.length == 0) || (args.length > 3))
- System.out.println("wrong number of arguments: plasmaCondenser -text|-html <infile> <outfile>");
- else
- try {
- plasmaCondenser pc = null;
-
- // read and analyse file
- File file = new File(args[1]);
- InputStream textStream = null;
- if (args[0].equals("-text")) {
- // read a text file
- textStream = new FileInputStream(file);
- } else if (args[0].equals("-html")) {
- // read a html file
- htmlFilterContentScraper cs = new htmlFilterContentScraper(new de.anomic.net.URL("http://localhost/"));
- htmlFilterOutputStream fos = new htmlFilterOutputStream(null, cs, null, false);
- FileInputStream fis = new FileInputStream(file);
- byte[] buffer = new byte[512];
- int i;
- while ((i = fis.read(buffer)) > 0) fos.write(buffer, 0, i);
- fis.close();
- fos.close();
- // cs.print();
- // System.out.println("TEXT:" + new String(cs.getText()));
- textStream = new ByteArrayInputStream(cs.getText());
- } else {
- System.out.println("first argument must be either '-text' or '-html'");
- System.exit(-1);
- }
-
- // call condenser
- pc = new plasmaCondenser(textStream, 1, 0);
- textStream.close();
-
- // output result
- pc.writeMapToFile(new File(args[2]));
- pc.print();
- //System.out.println("ANALYSIS:" + pc.getAnalysis().toString());
- } catch (IOException e) {
- System.out.println("Problem with input file: " + e.getMessage());
- }
+// if ((args.length == 0) || (args.length > 3))
+// System.out.println("wrong number of arguments: plasmaCondenser -text|-html <infile> <outfile>");
+// else
+// try {
+// plasmaCondenser pc = null;
+//
+// // read and analyse file
+// File file = new File(args[1]);
+// InputStream textStream = null;
+// if (args[0].equals("-text")) {
+// // read a text file
+// textStream = new FileInputStream(file);
+// } else if (args[0].equals("-html")) {
+// // read a html file
+// htmlFilterContentScraper cs = new htmlFilterContentScraper(new de.anomic.net.URL("http://localhost/"));
+// htmlFilterOutputStream fos = new htmlFilterOutputStream(null, cs, null, false);
+// FileInputStream fis = new FileInputStream(file);
+// byte[] buffer = new byte[512];
+// int i;
+// while ((i = fis.read(buffer)) > 0) fos.write(buffer, 0, i);
+// fis.close();
+// fos.close();
+// // cs.print();
+// // System.out.println("TEXT:" + new String(cs.getText()));
+// textStream = new ByteArrayInputStream(cs.getText());
+// } else {
+// System.out.println("first argument must be either '-text' or '-html'");
+// System.exit(-1);
+// }
+//
+// // call condenser
+// pc = new plasmaCondenser(textStream, 1, 0);
+// textStream.close();
+//
+// // output result
+// pc.writeMapToFile(new File(args[2]));
+// pc.print();
+// //System.out.println("ANALYSIS:" + pc.getAnalysis().toString());
+// } catch (IOException e) {
+// System.out.println("Problem with input file: " + e.getMessage());
+// }
}
}
diff --git a/source/de/anomic/plasma/plasmaParser.java b/source/de/anomic/plasma/plasmaParser.java
index a92c7222d..02c360b38 100644
--- a/source/de/anomic/plasma/plasmaParser.java
+++ b/source/de/anomic/plasma/plasmaParser.java
@@ -50,7 +50,7 @@ import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
-import java.io.OutputStream;
+import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
@@ -66,19 +66,20 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import org.apache.commons.pool.KeyedPoolableObjectFactory;
+import org.apache.commons.pool.impl.GenericKeyedObjectPool;
+import org.apache.commons.pool.impl.GenericObjectPool;
+
import de.anomic.htmlFilter.htmlFilterContentScraper;
-import de.anomic.htmlFilter.htmlFilterOutputStream;
+import de.anomic.htmlFilter.htmlFilterWriter;
import de.anomic.http.httpc;
+import de.anomic.net.URL;
import de.anomic.plasma.parser.Parser;
import de.anomic.plasma.parser.ParserException;
import de.anomic.plasma.parser.ParserInfo;
import de.anomic.server.serverFileUtils;
import de.anomic.server.logging.serverLog;
-import org.apache.commons.pool.impl.GenericKeyedObjectPool;
-import org.apache.commons.pool.KeyedPoolableObjectFactory;
-import org.apache.commons.pool.impl.GenericObjectPool;
-
public final class plasmaParser {
public static final String PARSER_MODE_PROXY = "PROXY";
public static final String PARSER_MODE_CRAWLER = "CRAWLER";
@@ -482,13 +483,21 @@ public final class plasmaParser {
}
- public plasmaParserDocument parseSource(URL location, String mimeType, String charset, File sourceFile) throws InterruptedException {
+ public plasmaParserDocument parseSource(URL location, String mimeType, String documentCharset, File sourceFile) throws InterruptedException {
Parser theParser = null;
try {
+ // getting the mimetype of the document
mimeType = getRealMimeType(mimeType);
+
+ // getting the file extension of the document
String fileExt = getFileExt(location);
+ // getting the charset of the document
+ if (documentCharset == null)
+ // TODO: do a charset detection here ....
+ documentCharset = "ISO-8859-1";
+
if (this.theLogger.isFine())
this.theLogger.logFine("Parsing " + location + " with mimeType '" + mimeType +
"' and file extension '" + fileExt + "'.");
@@ -547,7 +556,7 @@ public final class plasmaParser {
// if a parser was found we use it ...
if (theParser != null) {
- return theParser.parse(location, mimeType,charset,sourceFile);
+ return theParser.parse(location, mimeType,documentCharset,sourceFile);
} else if (realtimeParsableMimeTypesContains(mimeType)) {
// ...otherwise we make a scraper and transformer
htmlFilterContentScraper scraper = new htmlFilterContentScraper(location);
@@ -561,11 +570,17 @@ public final class plasmaParser {
return null;
}
}*/
-
- OutputStream hfos = new htmlFilterOutputStream(null, scraper, null, false);
- serverFileUtils.copy(sourceFile, hfos);
- hfos.close();
- return transformScraper(location, mimeType, scraper);
+ htmlFilterWriter writer = new htmlFilterWriter(null,null,scraper,null,false);
+ serverFileUtils.copy(sourceFile, documentCharset, writer);
+ writer.close();
+ //OutputStream hfos = new htmlFilterOutputStream(null, scraper, null, false);
+ //serverFileUtils.copy(sourceFile, hfos);
+ //hfos.close();
+ if (writer.binarySuspect()) {
+ this.theLogger.logInfo("Binary data found in URL " + location);
+ return null;
+ }
+ return transformScraper(location, mimeType, documentCharset, scraper);
} else {
serverLog.logWarning("PARSER", "parseSource2: wrong mime type");
return null;
@@ -581,13 +596,13 @@ public final class plasmaParser {
}
}
- public plasmaParserDocument transformScraper(URL location, String mimeType, htmlFilterContentScraper scraper) {
+ public plasmaParserDocument transformScraper(URL location, String mimeType, String charSet, htmlFilterContentScraper scraper) {
try {
String[] sections = new String[scraper.getHeadlines(1).length + scraper.getHeadlines(2).length + scraper.getHeadlines(3).length + scraper.getHeadlines(4).length];
int p = 0;
for (int i = 1; i <= 4; i++) for (int j = 0; j < scraper.getHeadlines(i).length; j++) sections[p++] = scraper.getHeadlines(i)[j];
plasmaParserDocument ppd = new plasmaParserDocument(new URL(location.toNormalform()),
- mimeType, scraper.getCharset(), scraper.getKeywords(),
+ mimeType, charSet, scraper.getKeywords(),
scraper.getTitle(), scraper.getTitle(),
sections, scraper.getDescription(),
scraper.getText(), scraper.getAnchors(), scraper.getImages());
diff --git a/source/de/anomic/plasma/plasmaSearchQuery.java b/source/de/anomic/plasma/plasmaSearchQuery.java
index 1e65779e9..9d28c0ffb 100644
--- a/source/de/anomic/plasma/plasmaSearchQuery.java
+++ b/source/de/anomic/plasma/plasmaSearchQuery.java
@@ -51,6 +51,7 @@ import de.anomic.htmlFilter.htmlFilterAbstractScraper;
import de.anomic.index.indexEntryAttribute;
import de.anomic.kelondro.kelondroNaturalOrder;
import de.anomic.server.serverByteBuffer;
+import de.anomic.server.serverCharBuffer;
public final class plasmaSearchQuery {
@@ -136,7 +137,7 @@ public final class plasmaSearchQuery {
public static TreeSet cleanQuery(String words) {
// convert Umlaute
- words = htmlFilterAbstractScraper.convertUmlaute(new serverByteBuffer(words.getBytes())).toString();
+ words = htmlFilterAbstractScraper.convertUmlaute(new serverCharBuffer(words.toCharArray())).toString();
// remove funny symbols
final String seps = "' .,:/-&";
diff --git a/source/de/anomic/plasma/plasmaSnippetCache.java b/source/de/anomic/plasma/plasmaSnippetCache.java
index ff78a663a..60e4f3e60 100644
--- a/source/de/anomic/plasma/plasmaSnippetCache.java
+++ b/source/de/anomic/plasma/plasmaSnippetCache.java
@@ -413,7 +413,7 @@ public class plasmaSnippetCache {
return null;
}
if (plasmaParser.supportedMimeTypesContains(docInfo.getMimeType())) {
- return this.parser.parseSource(url, docInfo.getMimeType(), docInfo.getCharSet(), resource);
+ return this.parser.parseSource(url, docInfo.getMimeType(), docInfo.getCharacterEncoding(), resource);
}
return null;
} catch (InterruptedException e) {
diff --git a/source/de/anomic/plasma/plasmaSwitchboard.java b/source/de/anomic/plasma/plasmaSwitchboard.java
index e6e90ed42..dc557ff2b 100644
--- a/source/de/anomic/plasma/plasmaSwitchboard.java
+++ b/source/de/anomic/plasma/plasmaSwitchboard.java
@@ -1397,7 +1397,7 @@ public final class plasmaSwitchboard extends serverAbstractSwitch implements ser
// the mimetype of this entry
String mimeType = entry.getMimeType();
- String charset = entry.getCharSet();
+ String charset = entry.getCharacterEncoding();
// the parser logger
serverLog parserLogger = parser.getLogger();
diff --git a/source/de/anomic/plasma/plasmaSwitchboardQueue.java b/source/de/anomic/plasma/plasmaSwitchboardQueue.java
index ff77eb002..a75055aaa 100644
--- a/source/de/anomic/plasma/plasmaSwitchboardQueue.java
+++ b/source/de/anomic/plasma/plasmaSwitchboardQueue.java
@@ -320,9 +320,9 @@ public class plasmaSwitchboardQueue {
return (info == null) ? null : info.getMimeType();
}
- public String getCharSet() {
+ public String getCharacterEncoding() {
IResourceInfo info = this.getCachedObjectInfo();
- return (info == null) ? null : info.getCharSet();
+ return (info == null) ? null : info.getCharacterEncoding();
}
public Date getModificationDate() {
diff --git a/source/de/anomic/server/serverCharBuffer.java b/source/de/anomic/server/serverCharBuffer.java
index 1d8a37de0..7d9687a9d 100644
--- a/source/de/anomic/server/serverCharBuffer.java
+++ b/source/de/anomic/server/serverCharBuffer.java
@@ -50,7 +50,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
-public final class serverCharBuffer extends Writer {
+public final class serverCharBuffer /* extends Writer */ {
public static final char singlequote = '\'';
public static final char doublequote = '"';
@@ -164,12 +164,12 @@ public final class serverCharBuffer extends Writer {
length += le;
}
- public Writer append(char b) {
+ public serverCharBuffer append(char b) {
write(b);
return this;
}
- public Writer append(int i) {
+ public serverCharBuffer append(int i) {
write((char) (i));
return this;
}
@@ -198,11 +198,11 @@ public final class serverCharBuffer extends Writer {
return append(bb.buffer, bb.offset, bb.length);
}
- public serverCharBuffer append(Object o) {
- if (o instanceof String) return append((String) o);
- if (o instanceof char[]) return append((char[]) o);
- return null;
- }
+// public serverCharBuffer append(Object o) {
+// if (o instanceof String) return append((String) o);
+// if (o instanceof char[]) return append((char[]) o);
+// return null;
+// }
public char charAt(int pos) {
if (pos < 0) throw new IndexOutOfBoundsException();
diff --git a/source/de/anomic/server/serverFileUtils.java b/source/de/anomic/server/serverFileUtils.java
index 495ee7edb..22b531513 100644
--- a/source/de/anomic/server/serverFileUtils.java
+++ b/source/de/anomic/server/serverFileUtils.java
@@ -52,8 +52,12 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Writer;
import java.util.StringTokenizer;
+import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -93,6 +97,75 @@ public final class serverFileUtils {
return total;
}
+
+ public static void writeX(InputStream source, OutputStream procOS, OutputStream bufferOS) throws IOException {
+ byte[] buffer = new byte[2048];
+ int l;
+
+ while ((l = source.read(buffer, 0, buffer.length)) >= 0) {
+ if (procOS != null) procOS.write(buffer, 0, l);
+ if (bufferOS != null) bufferOS.write(buffer, 0, l);
+ }
+
+ // flush the streams
+ if (procOS != null) procOS.flush();
+ if (bufferOS != null) bufferOS.flush();
+ buffer = null;
+ }
+
+ public static void writeX(Reader source, Writer procOS, Writer bufferOS) throws IOException {
+ char[] buffer = new char[2048];
+ int l;
+
+ while ((l = source.read(buffer, 0, buffer.length)) >= 0) {
+ if (procOS != null) procOS.write(buffer, 0, l);
+ if (bufferOS != null) bufferOS.write(buffer, 0, l);
+ }
+
+ // flush the streams
+ if (procOS != null) procOS.flush();
+ if (bufferOS != null) bufferOS.flush();
+ buffer = null;
+ }
+
+ public static void writeX(InputStream source, String inputCharset, Writer procOS, OutputStream bufferOS, String outputCharset) throws IOException {
+ InputStreamReader sourceReader = new InputStreamReader(source,inputCharset);
+ OutputStreamWriter bufferOSWriter = new OutputStreamWriter(bufferOS,outputCharset);
+ writeX(sourceReader,procOS,bufferOSWriter);
+ }
+
+
+ public static int copy (File source, String inputCharset, Writer dest) throws IOException {
+ InputStream fis = null;
+ try {
+ fis = new FileInputStream(source);
+ return copy(fis, dest, inputCharset);
+ } finally {
+ if (fis != null) try { fis.close(); } catch (Exception e) {}
+ }
+ }
+
+ public static int copy (InputStream source, Writer dest, String inputCharset) throws IOException {
+ InputStreamReader reader = new InputStreamReader(source,inputCharset);
+ return copy(reader,dest);
+ }
+
+ public static int copy (String source, Writer dest) throws IOException {
+ dest.write(source);
+ dest.flush();
+ return source.length();
+ }
+
+ public static int copy (Reader source, Writer dest) throws IOException {
+ char[] buffer = new char[4096];
+ int count = 0;
+ int n = 0;
+ while (-1 != (n = source.read(buffer))) {
+ dest.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
/**
* Copies an InputStream to a File.
@@ -231,6 +304,10 @@ public final class serverFileUtils {
if (zipOut != null) try { zipOut.close(); } catch (Exception e) {}
}
}
+
+ public static void write(String source, Writer dest) throws IOException {
+ copy(source,dest);
+ }
public static void write(byte[] source, OutputStream dest) throws IOException {
copy(new ByteArrayInputStream(source), dest);
diff --git a/yacy.init b/yacy.init
index db8cb3090..51e681d5e 100644
--- a/yacy.init
+++ b/yacy.init
@@ -599,6 +599,9 @@ isTransparentProxy=false
# Specifies if yacy should use the http connection keep-alive feature
connectionKeepAliveSupport=true
+# Specifies the timeout the proxy sould use
+proxy.clientTimeout = 10000
+
# Specifies if the proxy should send the via header according to RFC
proxy.sendViaHeader=true