1 17 package org.eclipse.emf.ecore.plugin; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 33 import org.osgi.framework.BundleContext; 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 import org.eclipse.core.resources.IFile; 40 import org.eclipse.core.resources.IProject; 41 import org.eclipse.core.resources.IWorkspaceRoot; 42 import org.eclipse.core.resources.ResourcesPlugin; 43 import org.eclipse.core.runtime.Platform; 44 45 import org.eclipse.emf.common.EMFPlugin; 46 import org.eclipse.emf.common.util.ResourceLocator; 47 import org.eclipse.emf.common.util.URI; 48 import org.eclipse.emf.common.util.WrappedException; 49 50 51 55 public class EcorePlugin extends EMFPlugin 56 { 57 60 public static final EcorePlugin INSTANCE = new EcorePlugin(); 61 62 65 private EcorePlugin() 66 { 67 super(new ResourceLocator[] {}); 68 } 69 70 73 public ResourceLocator getPluginResourceLocator() 74 { 75 return plugin; 76 } 77 78 95 public static Map getPlatformResourceMap() 96 { 97 if (platformResourceMap == null) 98 { 99 platformResourceMap = new HashMap (); 100 } 101 return platformResourceMap; 102 } 103 104 127 public static URI resolvePlatformResourcePath(String platformResourcePath) 128 { 129 int index = platformResourcePath.indexOf("/", 1); 130 String rootContainerName = platformResourcePath.substring(1, index); 131 String relativeName = platformResourcePath.substring(index + 1); 132 URI rootContainerLocation = (URI)platformResourceMap.get(rootContainerName); 133 return 134 rootContainerLocation != null ? 135 URI.createURI(relativeName).resolve(rootContainerLocation) : 136 null; 137 } 138 139 160 public static String [] handlePlatformResourceOptions(String [] arguments) 161 { 162 getPlatformResourceMap(); 163 164 for (int i = 0; i < arguments.length; ++i) 165 { 166 if (arguments[i].equalsIgnoreCase("-platformResource")) 167 { 168 int start = i; 169 while (++i < arguments.length && !arguments[i].startsWith("-")) 170 { 171 String rootContainerName = arguments[i]; 172 if (++i < arguments.length) 173 { 174 String rootContainerLocation = arguments[i]; 175 176 URI uri; 180 File file = new File (rootContainerLocation); 181 if (file.isDirectory() || !file.exists() && file.getParent() != null && file.getParentFile().isDirectory()) 182 { 183 try 184 { 185 file = file.getCanonicalFile(); 186 } 187 catch (IOException exception) 188 { 189 throw new WrappedException(exception); 190 } 191 uri = URI.createFileURI(file.toString() + "/"); 192 } 193 else 194 { 195 uri = URI.createURI(rootContainerLocation); 196 } 197 198 platformResourceMap.put(rootContainerName, uri); 199 } 200 } 201 202 String [] remainingArguments = new String [arguments.length - (i - start)]; 203 System.arraycopy(arguments, 0, remainingArguments, 0, start); 204 System.arraycopy(arguments, i, remainingArguments, start, arguments.length - i); 205 return remainingArguments; 206 } 207 } 208 209 return arguments; 210 } 211 212 217 public static Map getEPackageNsURIToGenModelLocationMap() 218 { 219 if (ePackageNsURIToGenModelLocationMap == null) 220 { 221 ePackageNsURIToGenModelLocationMap = new HashMap (); 222 } 223 return ePackageNsURIToGenModelLocationMap; 224 } 225 226 237 public static Map computePlatformResourceToPlatformPluginMap(Collection uris) 238 { 239 Map result = new HashMap (); 240 IWorkspaceRoot root = getWorkspaceRoot(); 241 if (root != null) 242 { 243 for (Iterator i = uris.iterator(); i.hasNext(); ) 244 { 245 URI uri = (URI)i.next(); 246 if ("platform".equals(uri.scheme()) && uri.segmentCount() > 1 && "plugin".equals(uri.segment(0))) 247 { 248 String pluginID = uri.segment(1); 249 if (!root.getProject(pluginID).isOpen()) 250 { 251 result.put(URI.createPlatformResourceURI(pluginID + "/"), URI.createURI("platform:/plugin/" + pluginID + "/")); 252 } 253 } 254 } 255 } 256 return result; 257 } 258 259 private static Pattern bundleSymbolNamePattern; 260 private static byte [] NO_BYTES = new byte [0]; 261 262 272 public static Map computePlatformPluginToPlatformResourceMap() 273 { 274 Map result = new HashMap (); 275 IWorkspaceRoot root = getWorkspaceRoot(); 276 if (root != null) 277 { 278 IProject [] projects = root.getProjects(); 279 if (projects != null) 280 { 281 String pluginID = null; 282 283 class Handler extends DefaultHandler 284 { 285 public String pluginID; 286 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 287 { 288 if ("".equals(uri) && "plugin".equals(localName)) 289 { 290 pluginID = attributes.getValue("id"); 291 } 292 throw new SAXException ("Done"); 293 } 294 }; 295 Handler handler = new Handler (); 296 297 SAXParserFactory parserFactory= SAXParserFactory.newInstance(); 298 parserFactory.setNamespaceAware(true); 299 SAXParser parser = null; 300 301 try 302 { 303 parser = parserFactory.newSAXParser(); 304 } 305 catch (Exception exception) 306 { 307 INSTANCE.log(exception); 308 } 309 310 if (bundleSymbolNamePattern == null) 311 { 312 bundleSymbolNamePattern = Pattern.compile("^\\s*Bundle-SymbolicName\\s*:\\s*([^\\s;]*)\\s*(;.*)?$", Pattern.MULTILINE); 313 } 314 315 byte [] bytes = NO_BYTES; 316 317 for (int i = 0, size = projects.length; i < size; ++i) 318 { 319 IProject project = projects[i]; 320 if (project.isOpen()) 321 { 322 pluginID = null; 323 IFile manifest = project.getFile("META-INF/MANIFEST.MF"); 324 if (manifest.exists()) 325 { 326 try 327 { 328 InputStream inputStream = manifest.getContents(); 329 int available = inputStream.available(); 330 if (bytes.length < available) 331 { 332 bytes = new byte [available]; 333 } 334 inputStream.read(bytes); 335 String contents = new String (bytes, "UTF-8"); 336 Matcher matcher = bundleSymbolNamePattern.matcher(contents); 337 if (matcher.find()) 338 { 339 pluginID = matcher.group(1); 340 } 341 } 342 catch (Exception exception) 343 { 344 EcorePlugin.INSTANCE.log(exception); 345 } 346 } 347 else if (parser != null) 348 { 349 final IFile plugin = project.getFile("plugin.xml"); 350 if (plugin.exists()) 351 { 352 try 353 { 354 parser.parse(new InputSource (plugin.getContents()), handler); 355 } 356 catch (Exception exception) 357 { 358 if (handler.pluginID != null) 359 { 360 pluginID = handler.pluginID; 361 } 362 else 363 { 364 INSTANCE.log(exception); 365 } 366 } 367 } 368 } 369 370 if (pluginID != null) 371 { 372 URI platformPluginURI = URI.createURI("platform:/plugin/" + pluginID + "/"); 373 URI platformResourceURI = URI.createPlatformResourceURI(project.getName() + "/"); 374 result.put(platformPluginURI, platformResourceURI); 375 } 376 } 377 } 378 } 379 } 380 381 return result; 382 } 383 384 401 public static Map computePlatformURIMap() 402 { 403 Map result = new HashMap (); 404 result.putAll(computePlatformPluginToPlatformResourceMap()); 405 result.putAll(computePlatformResourceToPlatformPluginMap(new HashSet (EcorePlugin.getEPackageNsURIToGenModelLocationMap().values()))); 406 return result; 407 } 408 409 413 private static Map platformResourceMap; 414 415 419 private static Map ePackageNsURIToGenModelLocationMap; 420 421 425 static public class Implementation extends EclipsePlugin 426 { 427 430 public Implementation() 431 { 432 super(); 433 plugin = this; 434 } 435 436 493 public void start(BundleContext context) throws Exception 494 { 495 super.start(context); 496 497 if (System.getProperty("org.eclipse.emf.ecore.plugin.EcorePlugin.doNotLoadResourcesPlugin") == null && 498 Platform.getBundle("org.eclipse.core.resources") != null) 499 { 500 workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 501 } 502 503 new GeneratedPackageRegistryReader(getEPackageNsURIToGenModelLocationMap()).readRegistry(); 504 new ExtensionParserRegistryReader().readRegistry(); 505 new ProtocolParserRegistryReader().readRegistry(); 506 new URIMappingRegistryReader().readRegistry(); 507 } 508 } 509 510 514 public static Implementation getPlugin() 515 { 516 return plugin; 517 } 518 519 522 private static Implementation plugin; 523 524 528 private static IWorkspaceRoot workspaceRoot; 529 530 534 public static IWorkspaceRoot getWorkspaceRoot() 535 { 536 return workspaceRoot; 537 } 538 539 static final String GENERATED_PACKAGE_PPID = "generated_package"; 540 static final String EXTENSION_PARSER_PPID = "extension_parser"; 541 static final String PROTOCOL_PARSER_PPID = "protocol_parser"; 542 static final String SCHEME_PARSER_PPID = "scheme_parser"; 543 static final String URI_MAPPING_PPID = "uri_mapping"; 544 } | Popular Tags |