1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.pkg;
21
22 import java.util.Arrays;
23 import java.util.StringTokenizer;
24
25 /***
26 *
27 */
28 public class DefaultDependencyParser implements DependencyParser
29 {
30
31 private String dependsKey;
32 private String conflictsKey;
33
34 public DefaultDependencyParser(String dependsKey, String conflictsKey)
35 {
36 this.dependsKey = dependsKey;
37 this.conflictsKey = conflictsKey;
38 }
39
40 public DefaultDependencyParser()
41 {
42 this("Depends", "Conflicts");
43 }
44
45 public AbstractToken parseConflicts(PackageInfo info) throws ParseException
46 {
47 return parse(info, conflictsKey);
48 }
49
50 public AbstractToken parseDepends(PackageInfo info) throws ParseException
51 {
52 return parse(info, dependsKey);
53 }
54
55 public AbstractToken parse(PackageInfo info, String key)
56 throws ParseException
57 {
58 String value = info.getProperty(key);
59 return (value != null && value.trim().length() > 0)
60 ? parse(value)
61 : null;
62 }
63
64 public AbstractToken parse(String depends)
65 throws ParseException
66 {
67 StringTokenizer t = new StringTokenizer(depends, ",");
68 if (t.countTokens() > 1) {
69 AbstractToken[] tokens = new AbstractToken[t.countTokens()];
70 for (int i = 0; i < tokens.length; i++) {
71 tokens[i] = parseDepends(t.nextToken());
72 }
73 Arrays.sort(tokens);
74 return new CommaToken(tokens);
75 }
76 else {
77 return parseDepends(depends);
78 }
79 }
80
81 private AbstractToken parseDepends(String depends) throws ParseException
82 {
83 StringTokenizer t = new StringTokenizer(depends, "|");
84 if (t.countTokens() > 1) {
85 AbstractToken[] tokens = new AbstractToken[t.countTokens()];
86 for (int i = 0; i < tokens.length; i++) {
87 tokens[i] = parsePackageDepends(t.nextToken());
88 }
89 Arrays.sort(tokens);
90 return new PipeToken(tokens);
91 }
92 else {
93 return parsePackageDepends(depends);
94 }
95 }
96
97 private AbstractToken parsePackageDepends(String depends)
98 throws ParseException
99 {
100 String name = depends.trim();
101 if (name.length() == 0) {
102 throw new ParseException("Empty dependency, package expected");
103 }
104
105 String compareMode = null;
106 String version = null;
107
108 int index = name.indexOf("(");
109 if (index > 0) {
110 if (!name.endsWith(")")) {
111 throw new ParseException("Invalid version, expected ')'");
112 }
113 String s = name.substring(index + 1, name.length() - 1);
114 int i = 0;
115 while (s.charAt(i) == '<' || s.charAt(i) == '='
116 || s.charAt(i) == '>' || s.charAt(i) == ' ') {
117 i++;
118 }
119 compareMode = s.substring(0, i).trim();
120 version = s.substring(i, s.length()).trim();
121 name = name.substring(0, index).trim();
122 }
123
124 return new PackageToken(name, compareMode, version);
125 }
126
127 }
128