Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleValueGetterFactory |
|
| 1.4285714285714286;1.429 | ||||
SimpleValueGetterFactory$1 |
|
| 1.4285714285714286;1.429 | ||||
SimpleValueGetterFactory$SimpleValueGetter |
|
| 1.4285714285714286;1.429 |
1 | package org.jtheque.primary.utils.web.analyzers.generic.value; | |
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.position.Position; | |
24 | import org.jtheque.primary.utils.web.analyzers.generic.position.PositionFactory; | |
25 | ||
26 | /** | |
27 | * @author Baptiste Wicht | |
28 | */ | |
29 | 0 | final class SimpleValueGetterFactory implements Factory<ValueGetter> { |
30 | @Override | |
31 | public boolean canFactor(Element element, XMLReader reader) throws XMLException{ | |
32 | 0 | return !reader.getNodes("value", element).isEmpty(); |
33 | } | |
34 | ||
35 | @Override | |
36 | public ValueGetter factor(Element element, XMLReader reader) throws XMLException{ | |
37 | 0 | SimpleValueGetter simpleGetter = new SimpleValueGetter(); |
38 | ||
39 | 0 | simpleGetter.setEnd(getPosition(element, "end", reader)); |
40 | 0 | simpleGetter.setStart(getPosition(element, "start", reader)); |
41 | ||
42 | 0 | return simpleGetter; |
43 | } | |
44 | ||
45 | /** | |
46 | * Return the position contained in a specific location of a specific node. | |
47 | * | |
48 | * @param currentNode The node to search in. | |
49 | * @param location The location to search in. | |
50 | * @param reader The XML reader. | |
51 | * | |
52 | * @return The Position or null if we doesn't found one. | |
53 | * | |
54 | * @throws XMLException If an errors occurs during the parse of the XML Elements. | |
55 | */ | |
56 | private static Position getPosition(Object currentNode, String location, XMLReader reader) throws XMLException{ | |
57 | 0 | Object positionNode = reader.getNode("value/" + location, currentNode); |
58 | ||
59 | 0 | if (positionNode != null){ |
60 | 0 | Element node = reader.getNode("*", positionNode); |
61 | ||
62 | 0 | if (node != null){ |
63 | 0 | return PositionFactory.getPosition(node, reader); |
64 | } | |
65 | } | |
66 | ||
67 | 0 | return null; |
68 | } | |
69 | ||
70 | /** | |
71 | * A simple value getter. It seems a getter who takes a value from a start position to an end | |
72 | * position. | |
73 | * | |
74 | * @author Baptiste Wicht | |
75 | */ | |
76 | 0 | private static final class SimpleValueGetter implements ValueGetter { |
77 | private Position start; | |
78 | private Position end; | |
79 | ||
80 | /** | |
81 | * Set the start position. | |
82 | * | |
83 | * @param start The start position. | |
84 | */ | |
85 | public void setStart(Position start){ | |
86 | 0 | this.start = start; |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Set the end position. | |
91 | * | |
92 | * @param end The end position. | |
93 | */ | |
94 | public void setEnd(Position end){ | |
95 | 0 | this.end = end; |
96 | 0 | } |
97 | ||
98 | @Override | |
99 | public String getValue(String line){ | |
100 | 0 | return line.substring(start.intValue(line), end.intValue(line)); |
101 | } | |
102 | ||
103 | @Override | |
104 | public String toString(){ | |
105 | 0 | return "SimpleValueGetter{" + |
106 | "start=" + start + | |
107 | ", end=" + end + | |
108 | '}'; | |
109 | } | |
110 | } | |
111 | } |