001: // htmlFilterContentTransformer.java
002: // ---------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: //
007: // $LastChangedDate: 2008-01-28 18:21:08 +0000 (Mo, 28 Jan 2008) $
008: // $LastChangedRevision: 4411 $
009: // $LastChangedBy: orbiter $
010: //
011: // This program is free software; you can redistribute it and/or modify
012: // it under the terms of the GNU General Public License as published by
013: // the Free Software Foundation; either version 2 of the License, or
014: // (at your option) any later version.
015: //
016: // This program is distributed in the hope that it will be useful,
017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: // GNU General Public License for more details.
020: //
021: // You should have received a copy of the GNU General Public License
022: // along with this program; if not, write to the Free Software
023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: //
025: // Using this software in any meaning (reading, learning, copying, compiling,
026: // running) means that you agree that the Author(s) is (are) not responsible
027: // for cost, loss of data or any harm that may be caused directly or indirectly
028: // by usage of this softare or this documentation. The usage of this software
029: // is on your own risk. The installation and usage (starting/running) of this
030: // software may allow other people or application to access your computer and
031: // any attached devices and is highly dependent on the configuration of the
032: // software which must be done by the user of the software; the author(s) is
033: // (are) also not responsible for proper configuration and usage of the
034: // software, even if provoked by documentation provided together with
035: // the software.
036: //
037: // Any changes to this file according to the GPL as documented in the file
038: // gpl.txt aside this file in the shipment you received can be done to the
039: // lines that follows this copyright notice here, but changes must not be
040: // done inside the copyright notive above. A re-distribution must contain
041: // the intact and unchanged copyright notice.
042: // Contributions and changes to the program code must be marked as such.
043:
044: package de.anomic.htmlFilter;
045:
046: import java.io.BufferedReader;
047: import java.io.File;
048: import java.io.FileReader;
049: import java.text.Collator;
050: import java.util.ArrayList;
051: import java.util.Locale;
052: import java.util.Properties;
053: import java.util.TreeSet;
054:
055: import de.anomic.http.httpTemplate;
056: import de.anomic.server.serverByteBuffer;
057: import de.anomic.server.serverCharBuffer;
058:
059: public class htmlFilterContentTransformer extends
060: htmlFilterAbstractTransformer implements htmlFilterTransformer {
061:
062: // statics: for initialisation of the HTMLFilterAbstractTransformer
063: private static TreeSet<String> linkTags0;
064: private static TreeSet<String> linkTags1;
065:
066: private static final Collator insensitiveCollator = Collator
067: .getInstance(Locale.US);
068: static {
069: insensitiveCollator.setStrength(Collator.SECONDARY);
070: insensitiveCollator.setDecomposition(Collator.NO_DECOMPOSITION);
071: }
072:
073: static {
074: linkTags0 = new TreeSet<String>(insensitiveCollator);
075: linkTags0.add("img");
076: linkTags0.add("input");
077:
078: linkTags1 = new TreeSet<String>(insensitiveCollator);
079: linkTags1.add("a");
080: }
081:
082: private ArrayList<String> bluelist = null;
083:
084: public htmlFilterContentTransformer() {
085: super (linkTags0, linkTags1);
086: }
087:
088: public void init(String initarg) {
089: if (bluelist == null) {
090: // here, the init arg is used to load a list of blue-listed words
091: bluelist = new ArrayList<String>();
092: File f = new File(initarg);
093: if (f.canRead()) {
094: try {
095: BufferedReader r = new BufferedReader(
096: new FileReader(f));
097: String s;
098: while ((s = r.readLine()) != null) {
099: if (!s.startsWith("#") && s.length() > 0)
100: bluelist.add(s.toLowerCase());
101: }
102: r.close();
103: } catch (Exception e) {
104: }
105: // if (bluelist.size() == 0) System.out.println("BLUELIST is empty");
106: }
107: }
108: }
109:
110: public boolean isIdentityTransformer() {
111: return (bluelist.size() == 0);
112: }
113:
114: private static char[] genBlueLetters(int length) {
115: serverCharBuffer bb = new serverCharBuffer(
116: " <FONT COLOR=#0000FF>".toCharArray());
117: length = length / 2;
118: if (length > 10)
119: length = 7;
120: while (length-- > 0) {
121: bb.append((int) 'X');
122: }
123: bb.append("</FONT> ");
124: return bb.getChars();
125: }
126:
127: private boolean bluelistHit(char[] text) {
128: if (text == null || bluelist == null)
129: return false;
130: String lc = new String(text).toLowerCase();
131: for (int i = 0; i < bluelist.size(); i++) {
132: if (lc.indexOf((String) bluelist.get(i)) >= 0)
133: return true;
134: }
135: return false;
136: }
137:
138: public ArrayList<String> getStrings(byte[] text) {
139: ArrayList<String> result = new ArrayList<String>();
140:
141: serverByteBuffer sbb = new serverByteBuffer(text);
142: serverByteBuffer[] sbbs = httpTemplate.splitQuotations(sbb);
143: //sbb = new serverByteBuffer();
144: for (int i = 0; i < sbbs.length; i++) {
145: if (sbbs[i].isWhitespace(true)) {
146: //sbb.append(sbbs[i]);
147: } else if ((sbbs[i].byteAt(0) == httpTemplate.hash)
148: || (sbbs[i].startsWith(httpTemplate.dpdpa))) {
149: // this is a template or a part of a template
150: //sbb.append(sbbs[i]);
151: } else {
152: // this is a text fragment, generate gettext quotation
153: int ws = sbbs[i].whitespaceStart(true);
154: int we = sbbs[i].whitespaceEnd(true);
155: result.add(new String(sbbs[i].getBytes(ws, we - ws)));
156: }
157: }
158: return result;
159: }
160:
161: public char[] transformText(char[] text) {
162: if (bluelist != null) {
163: if (bluelistHit(text)) {
164: // System.out.println("FILTERHIT: " + text);
165: return genBlueLetters(text.length);
166: }
167: return text;
168: }
169: return text;
170: }
171:
172: public char[] transformTag0(String tagname, Properties tagopts,
173: char quotechar) {
174: if (tagname.equals("img")) {
175: // check bluelist
176: if (bluelistHit(tagopts.getProperty("src", "")
177: .toCharArray()))
178: return genBlueLetters(5);
179: if (bluelistHit(tagopts.getProperty("alt", "")
180: .toCharArray()))
181: return genBlueLetters(5);
182:
183: // replace image alternative name
184: tagopts.setProperty("alt", new String(transformText(tagopts
185: .getProperty("alt", "").toCharArray())));
186: }
187: if ((tagname.equals("input"))
188: && (tagopts.getProperty("type").equals("submit"))) {
189: // rewrite button name
190: tagopts.setProperty("value", new String(
191: transformText(tagopts.getProperty("value", "")
192: .toCharArray())));
193: }
194: return htmlFilterWriter.genTag0(tagname, tagopts, quotechar);
195: }
196:
197: public char[] transformTag1(String tagname, Properties tagopts,
198: char[] text, char quotechar) {
199: if (bluelistHit(tagopts.getProperty("href", "").toCharArray()))
200: return genBlueLetters(text.length);
201: if (bluelistHit(text))
202: return genBlueLetters(text.length);
203: return htmlFilterWriter.genTag1(tagname, tagopts, text,
204: quotechar);
205: }
206:
207: public void close() {
208: // free resources
209: super.close();
210: }
211:
212: }
|