01: /***
02: * jwma Java WebMail
03: * Copyright (c) 2000-2003 jwma team
04: *
05: * jwma is free software; you can distribute and use this source
06: * under the terms of the BSD-style license received along with
07: * the distribution.
08: ***/package dtw.webmail.util;
09:
10: import java.io.*;
11:
12: import org.apache.log4j.Logger;
13: import org.apache.oro.text.regex.*;
14: import net.wimpi.text.*;
15: import net.wimpi.text.std.*;
16:
17: public class EntityEncoder extends AbstractProcessor {
18:
19: //logging
20: private static Logger log = Logger.getLogger(EntityEncoder.class);
21:
22: private Pattern m_MatchPattern = null;
23: private ResourcePool m_ResourcePool = null;
24: private CharacterSubstitution m_CharSub = null;
25:
26: public EntityEncoder() {
27: try {
28: //prepare pattern
29: m_MatchPattern = new Perl5Compiler().compile(CHAR_PATTERN);
30: //Prepare Substitution
31: m_CharSub = new CharacterSubstitution();
32:
33: //get resourcepool
34: m_ResourcePool = ProcessingKernel.getReference()
35: .getResourcePool("patternmatchers");
36: } catch (Exception ex) {
37: throw new RuntimeException("Failed to construct processor.");
38: }
39: }//constructor
40:
41: public String getName() {
42: return "entityencoder";
43: }//getName
44:
45: public String process(String str) {
46:
47: //do not waste cycles on nulls or empty strings
48: if (str == null || str.equals("")) {
49: return "";
50: }
51:
52: ProcessingResource resource = m_ResourcePool.leaseResource();
53: PatternMatcher matcher = ((PatternMatchingResource) resource)
54: .getPatternMatcher();
55: try {
56:
57: return Util.substitute(matcher, m_MatchPattern, m_CharSub,
58: str, Util.SUBSTITUTE_ALL);
59: } finally {
60: m_ResourcePool.releaseResource(resource);
61: }
62: }//process
63:
64: public InputStream process(InputStream in) throws IOException {
65:
66: ByteArrayOutputStream bout = new ByteArrayOutputStream(in
67: .available());
68:
69: byte[] buffer = new byte[8192];
70: int amount = 0;
71: while ((amount = in.read(buffer)) >= 0) {
72: bout.write(buffer, 0, amount);
73: }
74: return new ByteArrayInputStream(process(bout.toString())
75: .getBytes());
76:
77: }//process
78:
79: private static final String CHAR_PATTERN = "([^\\n\\t !\\#\\$%\\'-;=?-~])";
80:
81: }//class EntityEncoder
|