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

History | View | Annotate | Download (5.07 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
import org.gvsig.tools.packageutils.impl.DefaultStringWithAlias;
34

    
35
public class DefaultDependency implements Dependency {
36

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

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

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

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

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

    
107
        private class InvalidDependencyFormatException extends BaseRuntimeException {
108

    
109
                private static final long serialVersionUID = 2856837862117653856L;
110

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

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

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

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

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

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

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

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

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

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

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

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