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

History | View | Annotate | Download (4.92 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.installer.lib.impl;
25

    
26
import java.text.MessageFormat;
27

    
28
import org.gvsig.installer.lib.api.Dependency;
29
import org.gvsig.installer.lib.api.PackageInfo;
30
import org.gvsig.installer.lib.api.Version;
31
import org.gvsig.tools.exception.BaseRuntimeException;
32
import org.gvsig.tools.packageutils.StringWithAlias;
33

    
34
public class DefaultDependency implements Dependency {
35

    
36
        private String type;
37
        private String code;
38
        private String op;
39
        private Version version;
40

    
41
        public DefaultDependency() {
42
                super();
43
                clear();
44
        }
45

    
46
        public DefaultDependency(PackageInfo packageInfo) {
47
                this();
48
                this.code = packageInfo.getCode();
49
                this.type = "required";
50
                this.op = ">=";
51
                this.version = packageInfo.getVersion();
52
        }
53

    
54
        public void clear() {
55
                this.type = "required";
56
                this.code = "unknow";
57
                this.op = "=";
58
                this.version = new DefaultVersion();
59
        }
60

    
61
        public Dependency parse(String dependency) {
62
                // Parse a string with the dependency specification
63
                // (required|suggested|recommended|conflict)[:] code (=|>|<|>=|<=)
64
                // version
65
                if (dependency == null) {
66
                        this.clear();
67
                        return this;
68
                }
69
                dependency = dependency.trim();
70
                if (dependency.equals("")) {
71
                        this.clear();
72
                        return this;
73
                }
74
                
75
                String s = dependency;
76
                /*
77
                 * Remove special characters, so
78
                 * dependency description in pom.xml is not
79
                 * so fragile
80
                 */
81
        s = s.replace('\n', ' ');
82
        s = s.replace('\t', ' ');
83
        s = s.replace('\r', ' ');
84
        s = s.replace(':', ' ');
85
                
86
        /*
87
         * Added loop because replaceAll is not
88
         * exhaustive in this case
89
         */
90
        while (s.indexOf("  ") != -1) {
91
            s = s.replaceAll("  ", " ");
92
        }
93
                
94
                String[] x = s.split(" ");
95
                if (x.length != 4) {
96
                        throw new InvalidDependencyFormatException(dependency);
97
                }
98
                
99
                this.type = x[0];
100
                this.code = x[1];
101
                this.op = x[2];
102
                this.version.parse(x[3]);
103
                return this;
104
        }
105

    
106
        private class InvalidDependencyFormatException extends BaseRuntimeException {
107

    
108
                private static final long serialVersionUID = 2856837862117653856L;
109

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

    
112
                private static final String KEY = "_Error_parsing_dependecy_XdependecyX";
113

    
114
                public InvalidDependencyFormatException(String dependency) {
115
                        super(message, null, KEY, serialVersionUID);
116
                        setValue("dependency", dependency);
117
                }
118
        }
119

    
120
        public String getType() {
121
                return this.type;
122
        }
123

    
124
        public String getCode() {
125
                return this.code;
126
        }
127

    
128
        public String getOp() {
129
                return this.op;
130
        }
131

    
132
        public Version getVersion() {
133
                return this.version;
134
        }
135

    
136
        public boolean match(String type, String code, Version version) {
137
                if (!this.type.equalsIgnoreCase(type)) {
138
                        return false;
139
                }
140
                if (!this.code.equalsIgnoreCase(code)) {
141
                        return false;
142
                }
143
                return version.check(this.op, this.version);
144
        }
145

    
146
        public boolean match(String type, StringWithAlias code, Version version) {
147
                if (!this.type.equalsIgnoreCase(type)) {
148
                        return false;
149
                }
150
                if (!code.equalsIgnoreCase(this.code)) {
151
                        return false;
152
                }
153
                return version.check(this.op, this.version);
154
        }
155

    
156
        @Override
157
        public String toString() {
158
                return MessageFormat.format("{0}: {1} {2} {3}", this.type, this.code,
159
                                this.op, this.version.toString());
160
        }
161

    
162
        @Override
163
        public Object clone() throws CloneNotSupportedException {
164
                DefaultDependency x = (DefaultDependency) super.clone();
165
                x.version = (Version) this.version.clone();
166
                return x;
167
        }
168

    
169
        @Override
170
        public boolean equals(Object obj) {
171
                Dependency other;
172
                try {
173
                        other = (Dependency) obj;
174
                } catch (Exception ex) {
175
                        return false;
176
                }
177
                if (!this.code.equalsIgnoreCase(other.getCode())) {
178
                        return false;
179
                }
180
                if (!this.type.equalsIgnoreCase(other.getType())) {
181
                        return false;
182
                }
183
                if (!this.op.equalsIgnoreCase(other.getOp())) {
184
                        return false;
185
                }
186
                if (!this.version.equals(other.getVersion())) {
187
                        return false;
188
                }
189
                return true;
190
        }
191
        
192
        /* (non-Javadoc)
193
         * @see java.lang.Object#hashCode()
194
         */
195
        @Override
196
        public int hashCode() {
197
            return version.hashCode() + code.hashCode()
198
                + type.hashCode() + op.hashCode();
199
        }
200
}