1 12 package org.eclipse.update.internal.search; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.update.core.ISite; 28 import org.eclipse.update.core.Utilities; 29 import org.eclipse.update.internal.core.Messages; 30 import org.eclipse.update.internal.core.URLEncoder; 31 import org.eclipse.update.internal.core.UpdateManagerUtils; 32 import org.eclipse.update.internal.core.connection.ConnectionFactory; 33 import org.eclipse.update.internal.core.connection.IResponse; 34 import org.eclipse.update.search.IUpdateSiteAdapter; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.NamedNodeMap ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 49 50 public class UpdatePolicy { 51 private static final String TAG_POLICY = "update-policy"; private static final String TAG_URL_MAP = "url-map"; private static final String ATT_URL = "url"; private static final String ATT_PATTERN = "pattern"; private static final String ATT_TYPE = "url-type"; private static final String ATT_TYPE_VALUE_UPDATE = "update"; private static final String ATT_TYPE_VALUE_DISCOVERY = "discovery"; 60 private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 61 62 private static class MapSite implements IUpdateSiteAdapter { 63 private URL url; 64 public MapSite(URL url) { 65 this.url = url; 66 } 67 public String getLabel() { 68 if (url == null) { 69 return ""; } 71 return url.toString(); 72 } 73 public URL getURL() { 74 return url; 75 } 76 } 77 78 private static class UpdateMapEntry { 79 private IUpdateSiteAdapter site; 80 private String pattern; 81 82 public UpdateMapEntry(String pattern, URL url) { 83 this.pattern = pattern; 84 this.site = new MapSite(url); 85 } 86 public IUpdateSiteAdapter getSite() { 87 return site; 88 } 89 public boolean matches(String id) { 90 return id.startsWith(pattern); 91 } 92 public String getPattern() { 93 return pattern; 94 } 95 } 96 97 private ArrayList entries; 98 private ArrayList discoveryEntries; 99 private IUpdateSiteAdapter defaultSite; 100 private IUpdateSiteAdapter defaultDiscoverySite; 101 private boolean loaded = false; 102 private boolean fallbackAllowed = true; 103 104 public UpdatePolicy() { 105 entries = new ArrayList (); 106 discoveryEntries = new ArrayList (); 107 } 108 109 public void load(URL mapFile, IProgressMonitor monitor) 110 throws CoreException { 111 InputStream policyStream = null; 112 try { 113 IResponse response = ConnectionFactory.get(mapFile); 114 UpdateManagerUtils.checkConnectionResult(response, mapFile); 115 policyStream = response.getInputStream(monitor); 116 if (policyStream == null) 118 return; 119 120 documentBuilderFactory.setNamespaceAware(true); 121 DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); 122 Document doc = parser.parse(new InputSource (policyStream)); 123 124 processUpdatePolicy(doc); 125 loaded = true; 126 } catch (IOException e) { 127 throw Utilities.newCoreException( 128 NLS.bind(Messages.SiteURLFactory_UnableToAccessSiteStream, (new String [] { mapFile == null ? "" : mapFile.toExternalForm() })), ISite.SITE_ACCESS_EXCEPTION, 130 e); 131 } catch (SAXException e) { 132 throw Utilities.newCoreException( 133 Messages.UpdatePolicy_parsePolicy, 134 0, 135 e); 136 137 } catch(ParserConfigurationException e) { 138 throw Utilities.newCoreException( 139 Messages.UpdatePolicy_parsePolicy, 140 0, 141 e); 142 } finally { 143 if (policyStream != null) { 144 try { 145 policyStream.close(); 146 } catch (IOException e) { 147 } 148 } 149 } 150 } 151 152 public boolean isLoaded() { 153 return loaded; 154 } 155 156 164 public IUpdateSiteAdapter getMappedSite(String id) { 165 UpdateMapEntry lastEntry = null; 166 for (int i = 0; i < entries.size(); i++) { 167 UpdateMapEntry entry = (UpdateMapEntry) entries.get(i); 168 if (entry.matches(id)) { 169 if (lastEntry == null) 170 lastEntry = entry; 171 else { 172 String pattern = entry.getPattern(); 177 String lastPattern = lastEntry.getPattern(); 178 if (pattern.length() > lastPattern.length()) 179 lastEntry = entry; 180 } 181 } 182 } 183 if (lastEntry != null) 184 return lastEntry.getSite(); 185 else 186 return defaultSite; 187 } 188 189 197 public IUpdateSiteAdapter getMappedDiscoverySite(String id) { 198 UpdateMapEntry lastEntry = null; 199 for (int i = 0; i < discoveryEntries.size(); i++) { 200 UpdateMapEntry entry = (UpdateMapEntry) discoveryEntries.get(i); 201 if (entry.matches(id)) { 202 if (lastEntry == null) 203 lastEntry = entry; 204 else { 205 String pattern = entry.getPattern(); 210 String lastPattern = lastEntry.getPattern(); 211 if (pattern.length() > lastPattern.length()) 212 lastEntry = entry; 213 } 214 } 215 } 216 if (lastEntry != null) 217 return lastEntry.getSite(); 218 else 219 return defaultDiscoverySite; 220 } 221 222 public boolean isFallbackAllowed() { 223 return fallbackAllowed; 224 } 225 226 private void reset() { 227 if (!entries.isEmpty()) 228 entries.clear(); 229 if (!discoveryEntries.isEmpty()) 230 discoveryEntries.clear(); 231 } 232 233 private void processUpdatePolicy(Document document) throws CoreException { 234 Node root = document.getDocumentElement(); 235 reset(); 236 237 if (root.getNodeName().equals(TAG_POLICY)==false) 238 throwCoreException("'"+TAG_POLICY+Messages.UpdatePolicy_policyExpected, null); 240 NodeList nodes = root.getChildNodes(); 241 242 for (int i=0; i<nodes.getLength(); i++) { 243 Node child = nodes.item(i); 244 if (child.getNodeType() != Node.ELEMENT_NODE) 245 continue; 246 String tag = child.getNodeName(); 247 if (tag.equals(TAG_URL_MAP)) 248 processMapNode(child); 249 } 250 } 251 private void processMapNode(Node node) throws CoreException { 252 String pattern = getAttribute(node, ATT_PATTERN); 253 String urlName = getAttribute(node, ATT_URL); 254 String type = getAttribute(node, ATT_TYPE); 255 256 assertNotNull(ATT_PATTERN, pattern); 257 assertNotNull(ATT_URL, urlName); 258 259 if (urlName.trim().length() == 0) { 261 addUpdateEntry(pattern, null, type); 262 return; 263 } 264 265 try { 266 URL url = new URL (urlName); 267 URL resolvedURL = URLEncoder.encode(url); 268 addUpdateEntry(pattern, resolvedURL, type); 269 } catch (MalformedURLException e) { 270 throwCoreException(Messages.UpdatePolicy_invalidURL+urlName, null); 271 } 272 } 273 274 private void assertNotNull(String name, String value) throws CoreException { 275 if (value==null) 276 throwCoreException(name+Messages.UpdatePolicy_nameNoNull, null); 277 } 278 279 private String getAttribute(Node node, String name) { 280 NamedNodeMap attMap = node.getAttributes(); 281 Node att = attMap.getNamedItem(name); 282 if (att==null) return null; 283 return att.getNodeValue(); 284 } 285 286 private void addUpdateEntry(String pattern, URL url, String type) { 287 if (pattern.equalsIgnoreCase("*")) { if (type == null) 289 defaultSite = new MapSite(url); 290 else if (type.equals(ATT_TYPE_VALUE_UPDATE)) 291 defaultSite = new MapSite(url); 292 else if (type.equals(ATT_TYPE_VALUE_DISCOVERY)) 293 defaultDiscoverySite = new MapSite(url); 294 else { 295 defaultSite = new MapSite(url); 296 defaultDiscoverySite = new MapSite(url); 297 } 298 } else { 299 if (type == null ) 300 entries.add(new UpdateMapEntry(pattern, url)); 301 else if (type.equals(ATT_TYPE_VALUE_UPDATE)) 302 entries.add(new UpdateMapEntry(pattern, url)); 303 else if (type.equals(ATT_TYPE_VALUE_DISCOVERY)) 304 discoveryEntries.add(new UpdateMapEntry(pattern, url)); 305 else { 306 entries.add(new UpdateMapEntry(pattern, url)); 307 discoveryEntries.add(new UpdateMapEntry(pattern, url)); 308 } 309 } 310 } 311 312 private void throwCoreException(String message, Throwable e) throws CoreException { 313 String fullMessage = Messages.UpdatePolicy_UpdatePolicy+message; 314 throw Utilities.newCoreException(fullMessage, 0, e); 315 } 316 } 317 | Popular Tags |