KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > parser > AbstractModuleDescriptorParser


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.parser;
8
9 import java.io.IOException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.text.ParseException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.xml.sax.SAXException JavaDoc;
19 import org.xml.sax.SAXParseException JavaDoc;
20 import org.xml.sax.helpers.DefaultHandler JavaDoc;
21
22 import fr.jayasoft.ivy.Configuration;
23 import fr.jayasoft.ivy.DefaultDependencyDescriptor;
24 import fr.jayasoft.ivy.DefaultModuleDescriptor;
25 import fr.jayasoft.ivy.DependencyDescriptor;
26 import fr.jayasoft.ivy.Ivy;
27 import fr.jayasoft.ivy.ModuleDescriptor;
28 import fr.jayasoft.ivy.ModuleRevisionId;
29 import fr.jayasoft.ivy.repository.Resource;
30 import fr.jayasoft.ivy.repository.url.URLResource;
31 import fr.jayasoft.ivy.util.Message;
32
33 public abstract class AbstractModuleDescriptorParser implements ModuleDescriptorParser {
34     public ModuleDescriptor parseDescriptor(Ivy ivy, URL JavaDoc descriptorURL, boolean validate) throws ParseException JavaDoc, IOException JavaDoc {
35         return parseDescriptor(ivy, descriptorURL, new URLResource(descriptorURL), validate);
36     }
37     
38     protected abstract static class AbstractParser extends DefaultHandler JavaDoc {
39         private static final String JavaDoc DEFAULT_CONF_MAPPING = "*->*";
40         private String JavaDoc _defaultConf; // used only as defaultconf, not used for
41
// guesssing right side part of a mapping
42
private String JavaDoc _defaultConfMapping; // same as default conf but is used
43
// for guesssing right side part of a mapping
44
private DefaultDependencyDescriptor _defaultConfMappingDescriptor;
45         private Resource _res;
46         private List JavaDoc _errors = new ArrayList JavaDoc();
47         protected DefaultModuleDescriptor _md;
48         private ModuleDescriptorParser _parser;
49         
50         protected AbstractParser(ModuleDescriptorParser parser) {
51             _parser = parser;
52         }
53         
54         public ModuleDescriptorParser getModuleDescriptorParser() {
55             return _parser;
56         }
57         
58         protected void checkErrors() throws ParseException JavaDoc {
59             if (!_errors.isEmpty()) {
60                 throw new ParseException JavaDoc(_errors.toString(), 0);
61             }
62         }
63         
64         protected void setResource(Resource res) {
65             _res = res; // used for log and date only
66
_md = new DefaultModuleDescriptor(_parser, res);
67             _md.setLastModified(getLastModified());
68         }
69         
70         protected Resource getResource() {
71             return _res;
72         }
73         
74         protected String JavaDoc getDefaultConfMapping() {
75             return _defaultConfMapping;
76         }
77         
78         protected void setDefaultConfMapping(String JavaDoc defaultConf) {
79             _defaultConfMapping = defaultConf;
80         }
81         
82         
83         protected void parseDepsConfs(String JavaDoc confs, DefaultDependencyDescriptor dd) {
84             parseDepsConfs(confs, dd, _defaultConfMapping != null);
85         }
86         protected void parseDepsConfs(String JavaDoc confs, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande) {
87             parseDepsConfs(confs, dd, useDefaultMappingToGuessRightOperande, true);
88         }
89         protected void parseDepsConfs(String JavaDoc confs, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande, boolean evaluateConditions) {
90             if (confs == null) {
91                 return;
92             }
93             
94             String JavaDoc[] conf = confs.split(";");
95             parseDepsConfs(conf, dd, useDefaultMappingToGuessRightOperande, evaluateConditions);
96         }
97         protected void parseDepsConfs(String JavaDoc[] conf, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande) {
98             parseDepsConfs(conf, dd, useDefaultMappingToGuessRightOperande, true);
99         }
100         protected void parseDepsConfs(String JavaDoc[] conf, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande, boolean evaluateConditions) {
101             replaceConfigurationWildcards(_md);
102             for (int i = 0; i < conf.length; i++) {
103                 String JavaDoc[] ops = conf[i].split("->");
104                 if (ops.length == 1) {
105                     String JavaDoc[] modConfs = ops[0].split(",");
106                     if (!useDefaultMappingToGuessRightOperande) {
107                         for (int j = 0; j < modConfs.length; j++) {
108                             dd.addDependencyConfiguration(modConfs[j].trim(), modConfs[j].trim());
109                         }
110                     } else {
111                         for (int j = 0; j < modConfs.length; j++) {
112                             String JavaDoc[] depConfs = getDefaultConfMappingDescriptor().getDependencyConfigurations(modConfs[j]);
113                             if (depConfs.length > 0) {
114                                 for (int k = 0; k < depConfs.length; k++) {
115                                     String JavaDoc mappedDependency = evaluateConditions ? evaluateCondition(depConfs[k].trim(), dd): depConfs[k].trim();
116                                     if (mappedDependency != null) {
117                                         dd.addDependencyConfiguration(modConfs[j].trim(), mappedDependency);
118                                     }
119                                 }
120                             } else {
121                                 // no default mapping found for this configuration, map configuration to itself
122
dd.addDependencyConfiguration(modConfs[j].trim(), modConfs[j].trim());
123                             }
124                         }
125                     }
126                 } else if (ops.length == 2) {
127                     String JavaDoc[] modConfs = ops[0].split(",");
128                     String JavaDoc[] depConfs = ops[1].split(",");
129                     for (int j = 0; j < modConfs.length; j++) {
130                         for (int k = 0; k < depConfs.length; k++) {
131                             String JavaDoc mappedDependency = evaluateConditions ? evaluateCondition(depConfs[k].trim(), dd): depConfs[k].trim();
132                             if (mappedDependency != null) {
133                                 dd.addDependencyConfiguration(modConfs[j].trim(), mappedDependency);
134                             }
135                         }
136                     }
137                 } else {
138                     addError("invalid conf "+conf[i]+" for "+dd.getDependencyRevisionId());
139                 }
140             }
141             
142             if (_md.isMappingOverride()) {
143                 addExtendingConfigurations(conf, dd, useDefaultMappingToGuessRightOperande);
144             }
145         }
146         /**
147          * Evaluate the optional condition in the given configuration, like "[org=MYORG]confX".
148          * If the condition evaluates to true, the configuration is returned, if the condition
149          * evaluatate to false, null is returned.
150          * If there are no conditions, the configuration itself is returned.
151          *
152          * @param conf the configuration to evaluate
153          * @param dd the dependencydescriptor to which the configuration will be added
154          * @return the evaluated condition
155          */

156         private String JavaDoc evaluateCondition(String JavaDoc conf, DefaultDependencyDescriptor dd) {
157             if (conf.charAt(0) != '[') {
158                 return conf;
159             }
160             
161             int endConditionIndex = conf.indexOf(']');
162             if (endConditionIndex == -1) {
163                 addError("invalid conf " + conf + " for " + dd.getDependencyRevisionId());
164                 return null;
165             }
166             
167             String JavaDoc condition = conf.substring(1, endConditionIndex);
168             
169             int notEqualIndex = condition.indexOf("!=");
170             if (notEqualIndex == -1) {
171                 int equalIndex = condition.indexOf('=');
172                 if (equalIndex == -1) {
173                     addError("invalid conf " + conf + " for " + dd.getDependencyRevisionId());
174                     return null;
175                 }
176                 
177                 String JavaDoc leftOp = condition.substring(0, equalIndex).trim();
178                 String JavaDoc rightOp = condition.substring(equalIndex + 1).trim();
179                 
180                 // allow organisation synonyms, like 'org' or 'organization'
181
if (leftOp.equals("org") || leftOp.equals("organization")) {
182                     leftOp = "organisation";
183                 }
184                 
185                 String JavaDoc attrValue = dd.getAttribute(leftOp);
186                 if (!rightOp.equals(attrValue)) {
187                     return null;
188                 }
189             } else {
190                 String JavaDoc leftOp = condition.substring(0, notEqualIndex).trim();
191                 String JavaDoc rightOp = condition.substring(notEqualIndex + 2).trim();
192                 
193                 // allow organisation synonyms, like 'org' or 'organization'
194
if (leftOp.equals("org") || leftOp.equals("organization")) {
195                     leftOp = "organisation";
196                 }
197                 
198                 String JavaDoc attrValue = dd.getAttribute(leftOp);
199                 if (rightOp.equals(attrValue)) {
200                     return null;
201                 }
202             }
203             
204             return conf.substring(endConditionIndex + 1);
205         }
206
207         private void addExtendingConfigurations(String JavaDoc[] confs, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande) {
208             for (int i = 0; i < confs.length; i++) {
209                 addExtendingConfigurations(confs[i], dd, useDefaultMappingToGuessRightOperande);
210             }
211         }
212         private void addExtendingConfigurations(String JavaDoc conf, DefaultDependencyDescriptor dd, boolean useDefaultMappingToGuessRightOperande) {
213             Set JavaDoc configsToAdd = new HashSet JavaDoc();
214             Configuration[] configs = _md.getConfigurations();
215             for (int i = 0; i < configs.length; i++) {
216                 String JavaDoc[] ext = configs[i].getExtends();
217                 for (int j = 0; j < ext.length; j++) {
218                     if (conf.equals(ext[j])) {
219                         String JavaDoc configName = configs[i].getName();
220 // if (getDefaultConfMappingDescriptor().getDependencyConfigurations(configName).length > 0) {
221
configsToAdd.add(configName);
222 // } else {
223
addExtendingConfigurations(configName, dd, useDefaultMappingToGuessRightOperande);
224 // }
225
}
226                 }
227             }
228             
229             String JavaDoc[] confs = (String JavaDoc[]) configsToAdd.toArray(new String JavaDoc[configsToAdd.size()]);
230             parseDepsConfs(confs, dd, useDefaultMappingToGuessRightOperande);
231         }
232         
233         protected DependencyDescriptor getDefaultConfMappingDescriptor() {
234             if (_defaultConfMappingDescriptor == null) {
235                 _defaultConfMappingDescriptor = new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("", "", ""), false);
236                 parseDepsConfs(_defaultConfMapping, _defaultConfMappingDescriptor, false, false);
237             }
238             return _defaultConfMappingDescriptor;
239         }
240         
241         protected void addError(String JavaDoc msg) {
242             if (_res != null) {
243                 _errors.add(msg+" in "+_res+"\n");
244             } else {
245                 _errors.add(msg+"\n");
246             }
247         }
248         public void warning(SAXParseException JavaDoc ex) {
249             Message.warn("xml parsing: " +
250                     getLocationString(ex)+": "+
251                     ex.getMessage());
252         }
253         
254         public void error(SAXParseException JavaDoc ex) {
255             addError("xml parsing: " +
256                     getLocationString(ex)+": "+
257                     ex.getMessage());
258         }
259         
260         public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
261             addError("[Fatal Error] "+
262                     getLocationString(ex)+": "+
263                     ex.getMessage());
264         }
265         
266         /** Returns a string of the location. */
267         private String JavaDoc getLocationString(SAXParseException JavaDoc ex) {
268             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
269             
270             String JavaDoc systemId = ex.getSystemId();
271             if (systemId != null) {
272                 int index = systemId.lastIndexOf('/');
273                 if (index != -1)
274                     systemId = systemId.substring(index + 1);
275                 str.append(systemId);
276             } else if (getResource() != null) {
277                 str.append(getResource().toString());
278             }
279             str.append(':');
280             str.append(ex.getLineNumber());
281             str.append(':');
282             str.append(ex.getColumnNumber());
283             
284             return str.toString();
285             
286         } // getLocationString(SAXParseException):String
287

288         protected String JavaDoc getDefaultConf() {
289             return _defaultConfMapping != null ? _defaultConfMapping : (_defaultConf != null ? _defaultConf : DEFAULT_CONF_MAPPING);
290         }
291         protected void setDefaultConf(String JavaDoc defaultConf) {
292             _defaultConf = defaultConf;
293         }
294         public ModuleDescriptor getModuleDescriptor() throws ParseException JavaDoc {
295             checkErrors();
296             return _md;
297         }
298         protected Date JavaDoc getDefaultPubDate() {
299             return new Date JavaDoc(_md.getLastModified());
300         }
301         protected long getLastModified() {
302             long last = getResource().getLastModified();
303             if (last > 0) {
304                 return last;
305             } else {
306                 Message.debug("impossible to get date for "+getResource()+": using 'now'");
307                 return System.currentTimeMillis();
308             }
309         }
310         
311         private void replaceConfigurationWildcards(ModuleDescriptor md) {
312             Configuration[] configs = md.getConfigurations();
313             for (int i = 0; i < configs.length; i++) {
314                 configs[i].replaceWildcards(md);
315             }
316         }
317
318     }
319 }
320
Popular Tags