Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultDependency.java @ 38753

History | View | Annotate | Download (3.68 KB)

1
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
import org.gvsig.tools.exception.BaseRuntimeException;
9

    
10
public class DefaultDependency implements Dependency {
11

    
12
        private String type;
13
        private String code;
14
        private String op;
15
        private Version version;
16

    
17
        public DefaultDependency() {
18
                super();
19
                clear();
20
        }
21

    
22
        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

    
30
        public void clear() {
31
                this.type = "required";
32
                this.code = "unknow";
33
                this.op = "=";
34
                this.version = new DefaultVersion();
35
        }
36

    
37
        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
                
51
                String s = dependency;
52
                /*
53
                 * Remove special characters, so
54
                 * dependency description in pom.xml is not
55
                 * so fragile
56
                 */
57
        s = s.replace('\n', ' ');
58
        s = s.replace('\t', ' ');
59
        s = s.replace('\r', ' ');
60
        s = s.replace(':', ' ');
61
                
62
        /*
63
         * Added loop because replaceAll is not
64
         * exhaustive in this case
65
         */
66
        while (s.indexOf("  ") != -1) {
67
            s = s.replaceAll("  ", " ");
68
        }
69
                
70
                String[] x = s.split(" ");
71
                if (x.length != 4) {
72
                        throw new InvalidDependencyFormatException(dependency);
73
                }
74
                
75
                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

    
82
        private class InvalidDependencyFormatException extends BaseRuntimeException {
83

    
84
                private static final long serialVersionUID = 2856837862117653856L;
85

    
86
                private static final String message = "Error parsing dependecy '%(dependency)s'";
87

    
88
                private static final String KEY = "_Error_parsing_dependecy_XdependecyX";
89

    
90
                public InvalidDependencyFormatException(String dependency) {
91
                        super(message, null, KEY, serialVersionUID);
92
                        setValue("dependency", dependency);
93
                }
94
        }
95

    
96
        public String getType() {
97
                return this.type;
98
        }
99

    
100
        public String getCode() {
101
                return this.code;
102
        }
103

    
104
        public String getOp() {
105
                return this.op;
106
        }
107

    
108
        public Version getVersion() {
109
                return this.version;
110
        }
111

    
112
        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

    
122
        @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

    
128
        @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

    
135
        @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
        
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
}