Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultDependency.java @ 40487

History | View | Annotate | Download (3.98 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
import org.gvsig.tools.packageutils.StringWithAlias;
10

    
11
public class DefaultDependency implements Dependency {
12

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

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

    
23
        public DefaultDependency(PackageInfo packageInfo) {
24
                this();
25
                this.code = packageInfo.getCode();
26
                this.type = "required";
27
                this.op = ">=";
28
                this.version = packageInfo.getVersion();
29
        }
30

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

    
38
        public Dependency parse(String dependency) {
39
                // Parse a string with the dependency specification
40
                // (required|suggested|recommended|conflict)[:] code (=|>|<|>=|<=)
41
                // version
42
                if (dependency == null) {
43
                        this.clear();
44
                        return this;
45
                }
46
                dependency = dependency.trim();
47
                if (dependency.equals("")) {
48
                        this.clear();
49
                        return this;
50
                }
51
                
52
                String s = dependency;
53
                /*
54
                 * Remove special characters, so
55
                 * dependency description in pom.xml is not
56
                 * so fragile
57
                 */
58
        s = s.replace('\n', ' ');
59
        s = s.replace('\t', ' ');
60
        s = s.replace('\r', ' ');
61
        s = s.replace(':', ' ');
62
                
63
        /*
64
         * Added loop because replaceAll is not
65
         * exhaustive in this case
66
         */
67
        while (s.indexOf("  ") != -1) {
68
            s = s.replaceAll("  ", " ");
69
        }
70
                
71
                String[] x = s.split(" ");
72
                if (x.length != 4) {
73
                        throw new InvalidDependencyFormatException(dependency);
74
                }
75
                
76
                this.type = x[0];
77
                this.code = x[1];
78
                this.op = x[2];
79
                this.version.parse(x[3]);
80
                return this;
81
        }
82

    
83
        private class InvalidDependencyFormatException extends BaseRuntimeException {
84

    
85
                private static final long serialVersionUID = 2856837862117653856L;
86

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

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

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

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

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

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

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

    
113
        public boolean match(String type, String code, Version version) {
114
                if (!this.type.equalsIgnoreCase(type)) {
115
                        return false;
116
                }
117
                if (!this.code.equalsIgnoreCase(code)) {
118
                        return false;
119
                }
120
                return version.check(this.op, this.version);
121
        }
122

    
123
        public boolean match(String type, StringWithAlias code, Version version) {
124
                if (!this.type.equalsIgnoreCase(type)) {
125
                        return false;
126
                }
127
                if (!code.equalsIgnoreCase(this.code)) {
128
                        return false;
129
                }
130
                return version.check(this.op, this.version);
131
        }
132

    
133
        @Override
134
        public String toString() {
135
                return MessageFormat.format("{0}: {1} {2} {3}", this.type, this.code,
136
                                this.op, this.version.toString());
137
        }
138

    
139
        @Override
140
        public Object clone() throws CloneNotSupportedException {
141
                DefaultDependency x = (DefaultDependency) super.clone();
142
                x.version = (Version) this.version.clone();
143
                return x;
144
        }
145

    
146
        @Override
147
        public boolean equals(Object obj) {
148
                Dependency other;
149
                try {
150
                        other = (Dependency) obj;
151
                } catch (Exception ex) {
152
                        return false;
153
                }
154
                if (!this.code.equalsIgnoreCase(other.getCode())) {
155
                        return false;
156
                }
157
                if (!this.type.equalsIgnoreCase(other.getType())) {
158
                        return false;
159
                }
160
                if (!this.op.equalsIgnoreCase(other.getOp())) {
161
                        return false;
162
                }
163
                if (!this.version.equals(other.getVersion())) {
164
                        return false;
165
                }
166
                return true;
167
        }
168
        
169
        /* (non-Javadoc)
170
         * @see java.lang.Object#hashCode()
171
         */
172
        @Override
173
        public int hashCode() {
174
            return version.hashCode() + code.hashCode()
175
                + type.hashCode() + op.hashCode();
176
        }
177
}