Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SplitterFactory |
|
| 1.7142857142857142;1.714 | ||||
SplitterFactory$1 |
|
| 1.7142857142857142;1.714 | ||||
SplitterFactory$Splitter |
|
| 1.7142857142857142;1.714 |
1 | package org.jtheque.primary.utils.web.analyzers.generic.transform; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import org.jdom.Element; | |
20 | import org.jtheque.core.utils.file.XMLException; | |
21 | import org.jtheque.core.utils.file.XMLReader; | |
22 | import org.jtheque.primary.utils.web.analyzers.generic.Factory; | |
23 | import org.jtheque.primary.utils.web.analyzers.generic.value.ValueGetter; | |
24 | import org.jtheque.primary.utils.web.analyzers.generic.value.ValueGetterFactory; | |
25 | import org.jtheque.utils.StringUtils; | |
26 | ||
27 | /** | |
28 | * @author Baptiste Wicht | |
29 | */ | |
30 | 0 | final class SplitterFactory implements Factory<Transformer> { |
31 | @Override | |
32 | public boolean canFactor(Element element, XMLReader reader){ | |
33 | 0 | return "splitter".equals(element.getName()); |
34 | } | |
35 | ||
36 | @Override | |
37 | public Transformer factor(Element n, XMLReader reader) throws XMLException{ | |
38 | 0 | Splitter splitter = new Splitter(); |
39 | ||
40 | 0 | splitter.setSplitter(reader.readString("split", n)); |
41 | 0 | splitter.setSplitReplace(reader.readString("new", n)); |
42 | 0 | splitter.setGetter(ValueGetterFactory.getValueGetter(n, reader)); |
43 | ||
44 | 0 | return splitter; |
45 | } | |
46 | ||
47 | /** | |
48 | * A Transformer who split the value and rebuild the value in a different String. | |
49 | * | |
50 | * @author Baptiste Wicht | |
51 | */ | |
52 | 0 | private static final class Splitter implements Transformer { |
53 | private String splitter; | |
54 | private String splitReplace; | |
55 | private ValueGetter getter; | |
56 | ||
57 | @Override | |
58 | public String transform(String value){ | |
59 | 0 | if (!StringUtils.isEmpty(value)){ |
60 | 0 | StringBuilder builder = new StringBuilder(50); |
61 | ||
62 | 0 | String[] temp = value.split(splitter); |
63 | ||
64 | 0 | boolean first = true; |
65 | ||
66 | 0 | for (String v : temp){ |
67 | 0 | if (StringUtils.isEmpty(v)){ |
68 | 0 | continue; |
69 | } | |
70 | ||
71 | 0 | String real = getter.getValue(v); |
72 | ||
73 | 0 | if (first){ |
74 | 0 | first = false; |
75 | } else { | |
76 | 0 | builder.append(splitReplace); |
77 | } | |
78 | ||
79 | 0 | builder.append(real); |
80 | } | |
81 | ||
82 | 0 | return builder.toString(); |
83 | } | |
84 | ||
85 | 0 | return value; |
86 | } | |
87 | ||
88 | /** | |
89 | * Set the String with which we split the value. | |
90 | * | |
91 | * @param splitter The split string. | |
92 | */ | |
93 | public void setSplitter(String splitter){ | |
94 | 0 | this.splitter = splitter; |
95 | 0 | } |
96 | ||
97 | /** | |
98 | * Set the new separator. | |
99 | * | |
100 | * @param splitReplace The new separator. | |
101 | */ | |
102 | public void setSplitReplace(String splitReplace){ | |
103 | 0 | this.splitReplace = splitReplace; |
104 | 0 | } |
105 | ||
106 | /** | |
107 | * Set the ValueGetter to take the value. | |
108 | * | |
109 | * @param getter The value getter. | |
110 | */ | |
111 | public void setGetter(ValueGetter getter){ | |
112 | 0 | this.getter = getter; |
113 | 0 | } |
114 | ||
115 | @Override | |
116 | public String toString(){ | |
117 | 0 | return "Splitter{" + |
118 | "splitter='" + splitter + '\'' + | |
119 | ", splitReplace='" + splitReplace + '\'' + | |
120 | ", getter=" + getter + | |
121 | '}'; | |
122 | } | |
123 | } | |
124 | } |