Skip to main content
summaryrefslogtreecommitdiffstats
blob: db7ed2a06ae582d2c576016d71ceaa8824b9506a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.common.metadata.internal;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/**
 * Default implementation of StandardMetaDataSourceFileLocator that will locate standard metadata files
 * and resource bundles relative to the plugin that registered the files using the 
 * <code>org.eclipse.jst.jsf.common.StandardMetaDataFiles</code> ext-pt.
 *
 */
public class PluginRelativeStandardMetaDataSourceFileLocator extends StandardMetaDataSourceFileLocator{
	
	private ResourceBundle resourceBundle;

	/* 
	 * Returns InputStream of standard metadata file from plugin relative location.
	 * 	 
	 */
	public InputStream getInputStream() throws IOException {
		URL url = getURL();
		if (url != null) 
			return url.openStream();
		
        String  fileName = Path.fromOSString(fileInfo.getLocation()).toString() + " (Plugin: " + fileInfo.getBundleId()+")" ; //$NON-NLS-1$ //$NON-NLS-2$
        throw new FileNotFoundException("Metadata file not found: "+ fileName); //$NON-NLS-1$
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.common.metadata.internal.StandardMetaDataSourceFileLocator#getURL()
	 */
	public URL getURL() {
	    final Bundle bundle = Platform.getBundle(fileInfo.getBundleId());
	    if (bundle == null)
	    {
	        return null;
	    }
	    final IPath fromOSString = Path.fromOSString(fileInfo.getLocation());
		return FileLocator.find(bundle, fromOSString, null);
	}

	
	/* 
	 * Returns property resource bundle if it exists.  May return null.
	 * This implementation assumes that the basename of the bundle is the same 
	 * as the source file name and in the same directory.
	 * 
	 */
	public ResourceBundle getResourceBundle() throws IOException, MalformedURLException {
		if (resourceBundle == null){
			URL bundleURL = getStandardMetaDataSourceFileBasenameURL();
			if (bundleURL == null)
				return null;
			
			resourceBundle = ResourceBundleHelper.getResourceBundle(bundleURL);
		}
		return resourceBundle;
	}
	
	private URL getStandardMetaDataSourceFileBasenameURL()  {
		IPath annotationPath = Path.fromOSString(fileInfo.getLocation()); 
		IPath annotationFolder = annotationPath.removeLastSegments(1);
		IPath propertiesLocation = annotationPath.removeFirstSegments(annotationPath.segmentCount() - 1).removeFileExtension();
		// append location of properties file
		IPath propertiesFile = annotationFolder.append(propertiesLocation);
	
		// append .properties extension if needed
		if (propertiesFile.getFileExtension() == null)
			propertiesFile = propertiesFile.addFileExtension("properties"); //$NON-NLS-1$
		// create a URL out of the properties file location
		return FileLocator.find(Platform.getBundle(fileInfo.getBundleId()),
				propertiesFile, null);
	}

}

Back to the top