root / tags / v2_0_0_Build_2047 / extensions / org.gvsig.installer / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultDependency.java @ 38358
History | View | Annotate | Download (3.68 KB)
1 | 35979 | jjdelcerro | package org.gvsig.installer.lib.impl; |
---|---|---|---|
2 | |||
3 | import java.text.MessageFormat; |
||
4 | |||
5 | import org.gvsig.installer.lib.api.Dependency; |
||
6 | import org.gvsig.installer.lib.api.PackageInfo; |
||
7 | import org.gvsig.installer.lib.api.Version; |
||
8 | 36114 | nfrancisco | import org.gvsig.tools.exception.BaseRuntimeException; |
9 | 35979 | jjdelcerro | |
10 | public class DefaultDependency implements Dependency { |
||
11 | |||
12 | 37599 | nfrancisco | private String type; |
13 | private String code; |
||
14 | private String op; |
||
15 | private Version version;
|
||
16 | 35979 | jjdelcerro | |
17 | 37599 | nfrancisco | public DefaultDependency() {
|
18 | super();
|
||
19 | clear(); |
||
20 | } |
||
21 | 35979 | jjdelcerro | |
22 | 37599 | nfrancisco | public DefaultDependency(PackageInfo packageInfo) {
|
23 | this();
|
||
24 | this.code = packageInfo.getCode();
|
||
25 | this.type = "required"; |
||
26 | this.op = ">="; |
||
27 | this.version = packageInfo.getVersion();
|
||
28 | } |
||
29 | 35979 | jjdelcerro | |
30 | 37599 | nfrancisco | public void clear() { |
31 | this.type = "required"; |
||
32 | this.code = "unknow"; |
||
33 | this.op = "="; |
||
34 | this.version = new DefaultVersion(); |
||
35 | } |
||
36 | 35979 | jjdelcerro | |
37 | 37599 | nfrancisco | public Dependency parse(String dependency) { |
38 | // Parse a string with the dependency specification
|
||
39 | // (required|suggested|recommended|conflict)[:] code (=|>|<|>=|<=)
|
||
40 | // version
|
||
41 | if (dependency == null) { |
||
42 | this.clear();
|
||
43 | return this; |
||
44 | } |
||
45 | dependency = dependency.trim(); |
||
46 | if (dependency.equals("")) { |
||
47 | this.clear();
|
||
48 | return this; |
||
49 | } |
||
50 | 38244 | jldominguez | |
51 | String s = dependency;
|
||
52 | 38245 | jldominguez | /*
|
53 | * Remove special characters, so
|
||
54 | * dependency description in pom.xml is not
|
||
55 | * so fragile
|
||
56 | */
|
||
57 | 38244 | jldominguez | s = s.replace('\n', ' '); |
58 | s = s.replace('\t', ' '); |
||
59 | s = s.replace('\r', ' '); |
||
60 | s = s.replace(':', ' '); |
||
61 | |||
62 | 38245 | jldominguez | /*
|
63 | * Added loop because replaceAll is not
|
||
64 | * exhaustive in this case
|
||
65 | */
|
||
66 | 38244 | jldominguez | while (s.indexOf(" ") != -1) { |
67 | s = s.replaceAll(" ", " "); |
||
68 | } |
||
69 | |||
70 | 37599 | nfrancisco | String[] x = s.split(" "); |
71 | if (x.length != 4) { |
||
72 | throw new InvalidDependencyFormatException(dependency); |
||
73 | } |
||
74 | 38244 | jldominguez | |
75 | 37599 | nfrancisco | this.type = x[0]; |
76 | this.code = x[1]; |
||
77 | this.op = x[2]; |
||
78 | this.version.parse(x[3]); |
||
79 | return this; |
||
80 | } |
||
81 | 35979 | jjdelcerro | |
82 | 37599 | nfrancisco | private class InvalidDependencyFormatException extends BaseRuntimeException { |
83 | 35979 | jjdelcerro | |
84 | 37599 | nfrancisco | private static final long serialVersionUID = 2856837862117653856L; |
85 | 36114 | nfrancisco | |
86 | 37599 | nfrancisco | private static final String message = "Error parsing dependecy '%(dependency)s'"; |
87 | 36114 | nfrancisco | |
88 | 37599 | nfrancisco | private static final String KEY = "_Error_parsing_dependecy_XdependecyX"; |
89 | 36114 | nfrancisco | |
90 | 37599 | nfrancisco | public InvalidDependencyFormatException(String dependency) { |
91 | super(message, null, KEY, serialVersionUID); |
||
92 | setValue("dependency", dependency);
|
||
93 | } |
||
94 | } |
||
95 | 36114 | nfrancisco | |
96 | 37599 | nfrancisco | public String getType() { |
97 | return this.type; |
||
98 | } |
||
99 | 36114 | nfrancisco | |
100 | 37599 | nfrancisco | public String getCode() { |
101 | return this.code; |
||
102 | } |
||
103 | 36114 | nfrancisco | |
104 | 37599 | nfrancisco | public String getOp() { |
105 | return this.op; |
||
106 | } |
||
107 | 36114 | nfrancisco | |
108 | 37599 | nfrancisco | public Version getVersion() {
|
109 | return this.version; |
||
110 | } |
||
111 | 36114 | nfrancisco | |
112 | 37599 | nfrancisco | public boolean match(String type, String code, Version version) { |
113 | if (!this.type.equalsIgnoreCase(type)) { |
||
114 | return false; |
||
115 | } |
||
116 | if (!this.code.equalsIgnoreCase(code)) { |
||
117 | return false; |
||
118 | } |
||
119 | return version.check(this.op, this.version); |
||
120 | } |
||
121 | 36114 | nfrancisco | |
122 | 37599 | nfrancisco | @Override
|
123 | public String toString() { |
||
124 | return MessageFormat.format("{0}: {1} {2} {3}", this.type, this.code, |
||
125 | this.op, this.version.toString()); |
||
126 | } |
||
127 | 36114 | nfrancisco | |
128 | 37599 | nfrancisco | @Override
|
129 | public Object clone() throws CloneNotSupportedException { |
||
130 | DefaultDependency x = (DefaultDependency) super.clone();
|
||
131 | x.version = (Version) this.version.clone();
|
||
132 | return x;
|
||
133 | } |
||
134 | 36114 | nfrancisco | |
135 | 37599 | nfrancisco | @Override
|
136 | public boolean equals(Object obj) { |
||
137 | Dependency other; |
||
138 | try {
|
||
139 | other = (Dependency) obj; |
||
140 | } catch (Exception ex) { |
||
141 | return false; |
||
142 | } |
||
143 | if (!this.code.equalsIgnoreCase(other.getCode())) { |
||
144 | return false; |
||
145 | } |
||
146 | if (!this.type.equalsIgnoreCase(other.getType())) { |
||
147 | return false; |
||
148 | } |
||
149 | if (!this.op.equalsIgnoreCase(other.getOp())) { |
||
150 | return false; |
||
151 | } |
||
152 | if (!this.version.equals(other.getVersion())) { |
||
153 | return false; |
||
154 | } |
||
155 | return true; |
||
156 | } |
||
157 | 38244 | jldominguez | |
158 | /* (non-Javadoc)
|
||
159 | * @see java.lang.Object#hashCode()
|
||
160 | */
|
||
161 | @Override
|
||
162 | public int hashCode() { |
||
163 | return version.hashCode() + code.hashCode()
|
||
164 | + type.hashCode() + op.hashCode(); |
||
165 | } |
||
166 | 35979 | jjdelcerro | } |