1 6 package fr.jayasoft.ivy.xml; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.URL ; 12 import java.text.ParseException ; 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.DefaultHandler ; 24 25 import fr.jayasoft.ivy.ConflictManager; 26 import fr.jayasoft.ivy.Ivy; 27 import fr.jayasoft.ivy.LatestStrategy; 28 import fr.jayasoft.ivy.ModuleId; 29 import fr.jayasoft.ivy.circular.CircularDependencyStrategy; 30 import fr.jayasoft.ivy.matcher.PatternMatcher; 31 import fr.jayasoft.ivy.status.StatusManager; 32 import fr.jayasoft.ivy.url.URLHandlerRegistry; 33 import fr.jayasoft.ivy.util.Configurator; 34 import fr.jayasoft.ivy.util.Message; 35 36 40 public class XmlIvyConfigurationParser extends DefaultHandler { 41 private Configurator _configurator; 42 private List _configuratorTags = Arrays.asList(new String [] {"resolvers", "namespaces", "parsers", "latest-strategies", "conflict-managers", "outputters", "version-matchers", "statuses", "circular-dependency-strategies", "triggers"}); 43 44 private Ivy _ivy; 45 46 private String _defaultResolver; 47 private String _defaultCM; 48 private String _defaultLatest; 49 private String _defaultCircular; 50 private String _currentConfiguratorTag; 51 52 public XmlIvyConfigurationParser(Ivy ivy) { 53 _ivy = ivy; 54 } 55 56 public void parse(URL configuration) throws ParseException , IOException { 57 _configurator = new Configurator(); 58 Map typeDefs = _ivy.getTypeDefs(); 60 for (Iterator iter = typeDefs.keySet().iterator(); iter.hasNext();) { 61 String name = (String ) iter.next(); 62 _configurator.typeDef(name, (Class )typeDefs.get(name)); 63 } 64 65 doParse(configuration); 66 } 67 68 private void doParse(URL configuration) throws IOException , ParseException { 69 InputStream stream = null; 70 try { 71 stream = URLHandlerRegistry.getDefault().openStream(configuration); 72 SAXParserFactory.newInstance().newSAXParser().parse( 73 stream, 74 this); 75 } catch (IOException e) { 76 throw e; 77 } catch (Exception e) { 78 ParseException pe = new ParseException ("failed to configure with "+configuration+": "+e.getMessage(), 0); 79 pe.initCause(e); 80 throw pe; 81 } finally { 82 if (stream != null) { 83 try { 84 stream.close(); 85 } catch (IOException e) { 86 } 87 } 88 } 89 } 90 91 private void parse(Configurator configurator, URL configuration) throws IOException , ParseException { 92 _configurator = configurator; 93 doParse(configuration); 94 } 95 96 public void startElement(String uri, String localName, String qName, Attributes att) throws SAXException { 97 Map attributes = new HashMap (); 99 for (int i=0; i<att.getLength(); i++) { 100 attributes.put(att.getQName(i), att.getValue(i)); 101 } 102 103 try { 104 if (_configurator.getCurrent() != null) { 105 if ("macrodef".equals(_currentConfiguratorTag) && _configurator.getTypeDef(qName) != null) { 106 String name = (String )attributes.get("name"); 107 if (name == null) { 108 attributes.put("name", "@{name}"); 109 } else if (_configurator.isTopLevelMacroRecord() && name.indexOf("@{name}") != -1) { 110 attributes.put("name", name); 111 } else { 112 attributes.put("name", "@{name}-"+name); 113 } 114 } 115 if (attributes.get("ref") != null) { 116 if (attributes.size() != 1) { 117 throw new IllegalArgumentException ("ref attribute should be the only one ! found "+attributes.size()+" in "+qName); 118 } 119 String name = (String )attributes.get("ref"); 120 Object child = null; 121 if ("resolvers".equals(_currentConfiguratorTag)) { 122 child = _ivy.getResolver(name); 123 if (child == null) { 124 throw new IllegalArgumentException ("unknown resolver "+name+": resolver should be defined before being referenced"); 125 } 126 } else if ("latest-strategies".equals(_currentConfiguratorTag)) { 127 child = _ivy.getLatestStrategy(name); 128 if (child == null) { 129 throw new IllegalArgumentException ("unknown latest strategy "+name+": latest strategy should be defined before being referenced"); 130 } 131 } else if ("conflict-managers".equals(_currentConfiguratorTag)) { 132 child = _ivy.getConflictManager(name); 133 if (child == null) { 134 throw new IllegalArgumentException ("unknown conflict manager "+name+": conflict manager should be defined before being referenced"); 135 } 136 } 137 if (child == null) { 138 throw new IllegalArgumentException ("bad reference "+name); 139 } 140 _configurator.addChild(qName, child); 141 } else { 142 _configurator.startCreateChild(qName); 143 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 144 String attName = (String )iter.next(); 145 _configurator.setAttribute(attName, _ivy.substitute((String )attributes.get(attName))); 146 } 147 } 148 } else if ("classpath".equals(qName)) { 149 String urlStr = _ivy.substitute((String )attributes.get("url")); 150 URL url = null; 151 if (urlStr == null) { 152 String file = _ivy.substitute((String )attributes.get("file")); 153 if (file == null) { 154 throw new IllegalArgumentException ("either url or file should be given for classpath element"); 155 } else { 156 url = new File (file).toURL(); 157 } 158 } else { 159 url = new URL (urlStr); 160 } 161 _ivy.addClasspathURL(url); 162 } else if ("typedef".equals(qName)) { 163 String name = _ivy.substitute((String )attributes.get("name")); 164 String className = _ivy.substitute((String )attributes.get("classname")); 165 Class clazz = _ivy.typeDef(name, className); 166 _configurator.typeDef(name, clazz); 167 } else if ("property".equals(qName)) { 168 String name = _ivy.substitute((String )attributes.get("name")); 169 String value = _ivy.substitute((String )attributes.get("value")); 170 String override = _ivy.substitute((String )attributes.get("override")); 171 if (name == null) { 172 throw new IllegalArgumentException ("missing attribute name on property tag"); 173 } 174 if (value == null) { 175 throw new IllegalArgumentException ("missing attribute value on property tag"); 176 } 177 _ivy.setVariable(name, value, override == null ? true : Boolean.valueOf(override).booleanValue()); 178 } else if ("properties".equals(qName)) { 179 String propFilePath = _ivy.substitute((String )attributes.get("file")); 180 String override = _ivy.substitute((String )attributes.get("override")); 181 try { 182 Message.verbose("loading properties: "+propFilePath); 183 _ivy.loadProperties(new File (propFilePath), override == null ? true : Boolean.valueOf(override).booleanValue()); 184 } catch (Exception fileEx) { 185 Message.verbose("failed to load properties as file: trying as url: "+propFilePath); 186 try { 187 _ivy.loadProperties(new URL (propFilePath), override == null ? true : Boolean.valueOf(override).booleanValue()); 188 } catch (Exception urlEx) { 189 throw new IllegalArgumentException ("unable to load properties from "+propFilePath+". Tried both as an url and a file, with no success. File exception: "+fileEx+". URL exception: "+urlEx); 190 } 191 } 192 } else if ("include".equals(qName)) { 193 Map variables = new HashMap (_ivy.getVariables()); 194 try { 195 String propFilePath = _ivy.substitute((String )attributes.get("file")); 196 URL ivyconfURL = null; 197 if (propFilePath == null) { 198 propFilePath = _ivy.substitute((String )attributes.get("url")); 199 if (propFilePath == null) { 200 Message.error("bad include tag: specify file or url to include"); 201 return; 202 } else { 203 Message.verbose("including url: "+propFilePath); 204 ivyconfURL = new URL (propFilePath); 205 _ivy.setConfigurationVariables(ivyconfURL); 206 } 207 } else { 208 File incFile = new File (propFilePath); 209 if (!incFile.exists()) { 210 Message.error("impossible to include "+incFile+": file does not exist"); 211 return; 212 } else { 213 Message.verbose("including file: "+propFilePath); 214 _ivy.setConfigurationVariables(incFile); 215 ivyconfURL = incFile.toURL(); 216 } 217 } 218 new XmlIvyConfigurationParser(_ivy).parse(_configurator, ivyconfURL); 219 } finally { 220 _ivy.setVariables(variables); 221 } 222 } else if ("conf".equals(qName)) { 223 String cache = (String )attributes.get("defaultCache"); 224 if (cache != null) { 225 _ivy.setDefaultCache(new File (_ivy.substitute(cache))); 226 } 227 String defaultBranch = (String )attributes.get("defaultBranch"); 228 if (defaultBranch != null) { 229 _ivy.setDefaultBranch(_ivy.substitute(defaultBranch)); 230 } 231 String validate = (String )attributes.get("validate"); 232 if (validate != null) { 233 _ivy.setValidate(Boolean.valueOf(_ivy.substitute(validate)).booleanValue()); 234 } 235 String up2d = (String )attributes.get("checkUpToDate"); 236 if (up2d != null) { 237 _ivy.setCheckUpToDate(Boolean.valueOf(_ivy.substitute(up2d)).booleanValue()); 238 } 239 String cacheIvyPattern = (String )attributes.get("cacheIvyPattern"); 240 if (cacheIvyPattern != null) { 241 _ivy.setCacheIvyPattern(_ivy.substitute(cacheIvyPattern)); 242 } 243 String cacheArtPattern = (String )attributes.get("cacheArtifactPattern"); 244 if (cacheArtPattern != null) { 245 _ivy.setCacheArtifactPattern(_ivy.substitute(cacheArtPattern)); 246 } 247 String useRemoteConfig = (String )attributes.get("useRemoteConfig"); 248 if (useRemoteConfig != null) { 249 _ivy.setUseRemoteConfig(Boolean.valueOf(_ivy.substitute(useRemoteConfig)).booleanValue()); 250 } 251 252 _defaultResolver = (String )attributes.get("defaultResolver"); 254 _defaultCM = (String )attributes.get("defaultConflictManager"); 255 _defaultLatest = (String )attributes.get("defaultLatestStrategy"); 256 _defaultCircular = (String )attributes.get("circularDependencyStrategy"); 257 258 } else if ("version-matchers".equals(qName)) { 259 _currentConfiguratorTag = qName; 260 _configurator.setRoot(_ivy); 261 if ("true".equals(_ivy.substitute((String )attributes.get("usedefaults")))) { 262 _ivy.configureDefaultVersionMatcher(); 263 } 264 } else if ("statuses".equals(qName)) { 265 _currentConfiguratorTag = qName; 266 StatusManager m = new StatusManager(); 267 String defaultStatus = _ivy.substitute((String )attributes.get("default")); 268 if (defaultStatus != null) { 269 m.setDefaultStatus(defaultStatus); 270 } 271 _ivy.setStatusManager(m); 272 _configurator.setRoot(m); 273 } else if (_configuratorTags.contains(qName)) { 274 _currentConfiguratorTag = qName; 275 _configurator.setRoot(_ivy); 276 } else if ("macrodef".equals(qName)) { 277 _currentConfiguratorTag = qName; 278 Configurator.MacroDef macrodef = _configurator.startMacroDef((String )attributes.get("name")); 279 macrodef.addAttribute("name", null); 280 } else if ("module".equals(qName)) { 281 String organisation = _ivy.substitute((String )attributes.get("organisation")); 282 String module = _ivy.substitute((String )attributes.get("name")); 283 String resolver = _ivy.substitute((String )attributes.get("resolver")); 284 String branch = _ivy.substitute((String )attributes.get("branch")); 285 String cm = _ivy.substitute((String )attributes.get("conflict-manager")); 286 String matcher = _ivy.substitute((String )attributes.get("matcher")); 287 matcher = matcher == null ? PatternMatcher.EXACT_OR_REGEXP : matcher; 288 _ivy.addModuleConfiguration(new ModuleId(organisation, module), _ivy.getMatcher(matcher), resolver, branch, cm); 289 } 290 } catch (Exception ex) { 291 throw new SAXException ("problem in config file: "+ex.getMessage(), ex); 292 } 293 } 294 295 public void endElement(String uri, String localName, String qName) throws SAXException { 296 if (_configurator.getCurrent() != null) { 297 if (_configuratorTags.contains(qName) && _configurator.getDepth() == 1) { 298 _configurator.clear(); 299 _currentConfiguratorTag = null; 300 } else if ("macrodef".equals(qName) && _configurator.getDepth() == 1) { 301 _configurator.endMacroDef(); 302 _currentConfiguratorTag = null; 303 } else { 304 _configurator.endCreateChild(); 305 } 306 } 307 } 308 309 public void endDocument() throws SAXException { 310 if (_defaultResolver != null) { 311 _ivy.setDefaultResolver(_ivy.substitute(_defaultResolver)); 312 } 313 if (_defaultCM != null) { 314 ConflictManager conflictManager = _ivy.getConflictManager(_ivy.substitute(_defaultCM)); 315 if (conflictManager == null) { 316 throw new IllegalArgumentException ("unknown conflict manager "+_ivy.substitute(_defaultCM)); 317 } 318 _ivy.setDefaultConflictManager(conflictManager); 319 } 320 if (_defaultLatest != null) { 321 LatestStrategy latestStrategy = _ivy.getLatestStrategy(_ivy.substitute(_defaultLatest)); 322 if (latestStrategy == null) { 323 throw new IllegalArgumentException ("unknown latest strategy " + _ivy.substitute(_defaultLatest)); 324 } 325 _ivy.setDefaultLatestStrategy(latestStrategy); 326 } 327 if (_defaultCircular != null) { 328 CircularDependencyStrategy strategy = _ivy.getCircularDependencyStrategy(_ivy.substitute(_defaultCircular)); 329 if (strategy == null) { 330 throw new IllegalArgumentException ("unknown circular dependency strategy " + _ivy.substitute(_defaultCircular)); 331 } 332 _ivy.setCircularDependencyStrategy(strategy); 333 } 334 } 335 } 336 | Popular Tags |