KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > SubstringFactory


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.transformers;
11
12 import org.mmbase.util.functions.*;
13 import java.io.Reader JavaDoc;
14 import java.io.Writer JavaDoc;
15
16
17 /**
18  * Factories {@link CharTransformer}'s which mirror the input, but only between 'from' and 'to'
19  * parameters. So, those transformers work like {@link java.lang.String#substring}.
20  * And as a bonus you can specify the parameter 'ellipsis' (the three dots at the end of the text)
21  * to use when a text has been 'substringed'...
22  *
23  * @author Michiel Meeuwissen
24  * @author André van Toly
25  * @since MMBase-1.8
26  * @version $Id: SubstringFactory.java,v 1.7 2006/03/07 14:01:03 michiel Exp $
27  */

28
29 public class SubstringFactory implements ParameterizedTransformerFactory {
30
31
32     protected final static Parameter[] PARAMS = {
33         new Parameter("from", Integer JavaDoc.class, new Integer JavaDoc(0)),
34         new Parameter("to" , Integer JavaDoc.class, new Integer JavaDoc(Integer.MAX_VALUE)),
35         new Parameter("ellipsis" , String JavaDoc.class, "")
36     };
37
38     public Transformer createTransformer(Parameters parameters) {
39         return new Substring( (Integer JavaDoc) parameters.get("from"),
40                               (Integer JavaDoc) parameters.get("to"),
41                               (String JavaDoc) parameters.get("ellipsis") );
42     }
43     public Parameters createParameters() {
44         return new Parameters(PARAMS);
45     }
46
47     protected class Substring extends ReaderTransformer {
48
49         private final int from;
50         private final int to;
51         private final String JavaDoc ellipsis;
52         Substring(Integer JavaDoc f, Integer JavaDoc t, String JavaDoc e) {
53             from = f.intValue(); to = t.intValue(); ellipsis = e;
54         }
55
56
57         // implementation, javadoc inherited
58
public Writer JavaDoc transform(Reader JavaDoc r, Writer JavaDoc w) {
59             if (from < 0 || to < 0) throw new UnsupportedOperationException JavaDoc("When using streams, it is not possible to use negative values.");
60             int current = 0;
61             try {
62                 while (true) {
63                     int c = r.read();
64                     if (c == -1) break;
65                     if (current >= from) {
66                         w.write(c);
67                     }
68                     current++;
69                     if (current >= to) {
70                         if (ellipsis != null && (! ellipsis.equals(""))) w.write(ellipsis);
71                         break;
72                     }
73                 }
74             } catch (java.io.IOException JavaDoc e) {
75                 throw new RuntimeException JavaDoc(e);
76             }
77             return w;
78         }
79
80         public String JavaDoc toString() {
81             return "SUBSTRING(" + from + "," + to + "," + ellipsis + ")";
82         }
83     }
84
85 }
86
Popular Tags