1 21 22 package com.izforge.izpack.util; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import net.n3.nanoxml.XMLElement; 29 30 39 public class OsConstraint implements java.io.Serializable 40 { 41 42 45 private static final long serialVersionUID = 3762248660406450488L; 46 47 48 private String family; 49 50 51 private String name; 52 53 54 private String version; 55 56 57 private String arch; 58 59 67 public OsConstraint(String family, String name, String version, String arch) 68 { 69 this.family = family != null ? family.toLowerCase() : null; 70 this.name = name != null ? name.toLowerCase() : null; 71 this.version = version != null ? version.toLowerCase() : null; 72 this.arch = arch != null ? arch.toLowerCase() : null; 73 } 74 75 80 public boolean matchCurrentSystem() 81 { 82 boolean match = true; 83 String osName = System.getProperty("os.name").toLowerCase(); 84 85 if (arch != null && arch.length() != 0) 86 { 87 match = System.getProperty("os.arch").toLowerCase().equals(arch); 88 } 89 if (match && version != null && version.length() != 0) 90 { 91 match = System.getProperty("os.version").toLowerCase().equals(version); 92 } 93 if (match && name != null && name.length() != 0) 94 { 95 match = osName.equals(name); 96 } 97 if (match && family != null) 98 { 99 if ("windows".equals(family)) 100 { 101 match = OsVersion.IS_WINDOWS; 102 } 103 else if ("mac".equals(family) || "osx".equals(family)) 104 { 105 match = OsVersion.IS_OSX; 106 } 107 else if ("unix".equals(family)) 108 { 109 match = OsVersion.IS_UNIX; 110 } 111 } 112 113 return match && (family != null || name != null || version != null || arch != null); 114 } 115 116 122 static public List getOsList(XMLElement element) 123 { 124 ArrayList osList = new ArrayList (); 126 Iterator osIterator = element.getChildrenNamed("os").iterator(); 127 while (osIterator.hasNext()) 128 { 129 XMLElement os = (XMLElement) osIterator.next(); 130 osList.add(new OsConstraint(os.getAttribute("family", null), os.getAttribute("name", 131 null), os.getAttribute("version", null), os.getAttribute("arch", null))); 132 } 133 134 String osattr = element.getAttribute("os"); 136 if (osattr != null && osattr.length() > 0) 137 { 138 osList.add(new OsConstraint(osattr, null, null, null)); 140 } 141 142 return osList; 143 } 144 145 153 public static boolean oneMatchesCurrentSystem(List constraint_list) 154 { 155 if (constraint_list == null) return true; 156 157 Iterator constraint_it = constraint_list.iterator(); 158 159 if (!constraint_it.hasNext()) return true; 161 162 while (constraint_it.hasNext()) 163 { 164 OsConstraint osc = (OsConstraint) constraint_it.next(); 165 166 Debug.trace("checking if os constraints " + osc + " match current OS"); 167 168 if (osc.matchCurrentSystem()) 170 { 171 Debug.trace("matched current OS."); 172 return true; } 174 175 } 176 177 Debug.trace("no match with current OS!"); 178 return false; 180 } 181 182 190 public static boolean oneMatchesCurrentSystem(XMLElement el) 191 { 192 return oneMatchesCurrentSystem(getOsList(el)); 193 } 194 195 public void setFamily(String f) 196 { 197 family = f.toLowerCase(); 198 } 199 200 public String getFamily() 201 { 202 return family; 203 } 204 205 public void setName(String n) 206 { 207 name = n.toLowerCase(); 208 } 209 210 public String getName() 211 { 212 return name; 213 } 214 215 public void setVersion(String v) 216 { 217 version = v.toLowerCase(); 218 } 219 220 public String getVersion() 221 { 222 return version; 223 } 224 225 public void setArch(String a) 226 { 227 arch = a.toLowerCase(); 228 } 229 230 public String getArch() 231 { 232 return arch; 233 } 234 235 public String toString() 236 { 237 StringBuffer retval = new StringBuffer (); 238 retval.append("[Os "); 239 retval.append(" family ").append(family); 240 retval.append(" name ").append(name); 241 retval.append(" version ").append(version); 242 retval.append(" arch ").append(arch); 243 retval.append(" ]"); 244 return retval.toString(); 245 } 246 247 } 248 | Popular Tags |