View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.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      //--- Constant(s) ---
32  
33      //--- Data field(s) ---
34  
35  	private String dependsKey;
36  	private String conflictsKey;
37  
38      //--- Constructor(s) ---
39  
40  	public DefaultDependencyParser(String dependsKey, String conflictsKey)
41  	{
42  		this.dependsKey = dependsKey;
43  		this.conflictsKey = conflictsKey;
44      }
45  
46  	public DefaultDependencyParser()
47  	{
48  		this("Depends", "Conflicts");
49      }
50  
51      //--- Method(s) ---
52  
53  	public AbstractToken parseConflicts(PackageInfo info) throws ParseException
54  	{
55  		return parse(info, conflictsKey);
56  	}
57  
58  	public AbstractToken parseDepends(PackageInfo info) throws ParseException
59  	{
60  		return parse(info, dependsKey);
61  	}
62  
63  	public AbstractToken parse(PackageInfo info, String key)
64  		throws ParseException
65  	{
66  		String value = info.getProperty(key);
67  		return (value != null && value.trim().length() > 0) 
68  			? parse(value) 
69  			: null;
70  	}
71  
72  	public AbstractToken parse(String depends)
73  		throws ParseException
74  	{
75  		StringTokenizer t = new StringTokenizer(depends, ",");
76  		if (t.countTokens() > 1) {
77  			AbstractToken[] tokens = new AbstractToken[t.countTokens()];
78  			for (int i = 0; i < tokens.length; i++) {
79  				tokens[i] = parseDepends(t.nextToken());
80  			}
81  			Arrays.sort(tokens);
82  			return new CommaToken(tokens);
83  		}
84  		else {
85  			return parseDepends(depends);
86  		}
87  	}
88   
89  	private AbstractToken parseDepends(String depends) throws ParseException
90  	{
91  		StringTokenizer t = new StringTokenizer(depends, "|");
92  		if (t.countTokens() > 1) {
93  			AbstractToken[] tokens = new AbstractToken[t.countTokens()];
94  			for (int i = 0; i < tokens.length; i++) {
95  				tokens[i] = parsePackageDepends(t.nextToken());
96  			}
97  			Arrays.sort(tokens);
98  			return new PipeToken(tokens);
99  		}
100 		else {
101 			return parsePackageDepends(depends);
102 		}
103 	}
104 	
105 	private AbstractToken parsePackageDepends(String depends) 
106 		throws ParseException
107 	{
108 		String name = depends.trim();
109 		if (name.length() == 0) {
110 			throw new ParseException("Empty dependency, package expected");
111 		}
112 
113 		String compareMode = null;
114 		String version = null;		
115 
116 		int index = name.indexOf("(");
117 		if (index > 0) {
118 			if (!name.endsWith(")")) {
119 				throw new ParseException("Invalid version, expected ')'");
120 			}
121 			String s = name.substring(index + 1, name.length() - 1);
122 			int i = 0;
123 			while (s.charAt(i) == '<' || s.charAt(i) == '=' 
124 				   || s.charAt(i) == '>' || s.charAt(i) == ' ') {
125 				i++;
126 			}
127 			compareMode = s.substring(0, i).trim();
128 			version = s.substring(i, s.length()).trim();
129 			name = name.substring(0, index).trim();
130 		}
131 
132 		return new PackageToken(name, compareMode, version);
133 	}
134 
135 }
136