1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-02 22:55:26 +02:00

Fixing message file access so it works with jarred plugins.

This commit is contained in:
David Dykstal 2006-04-27 22:29:28 +00:00
parent 7f5e7f3b3a
commit ddb85dadad
4 changed files with 202 additions and 123 deletions

View file

@ -106,24 +106,7 @@ public class SystemMessageFile implements ErrorHandler
return xmlDocument;
}
}
/**
* Constructor
* @param messageFileName - name of xml file which will contain the messages
*/
public SystemMessageFile (String messageFileName, String defaultMessageFileLocation)
{
this.defaultMsgFileLocation = defaultMessageFileLocation;
// have we already loaded this message file?
msgFile = getFromCache(messageFileName);
// now, we haven't. Load it now.
if (msgFile == null)
{
msgFile=new MessageFileInfo(messageFileName.toUpperCase(), messageFileName, loadAndParseXMLFile(messageFileName));
msgfList.add(msgFile);
//scanForDuplicates(); // don't keep this for production. Too expensive
}
}
/**
* Constructor
* @param messageFileName - name of xml file which will contain the messages
@ -137,7 +120,8 @@ public class SystemMessageFile implements ErrorHandler
this.dtdInputStream = dtdStream;
if (msgFile == null)
{
msgFile=new MessageFileInfo(messageFileName.toUpperCase(), messageFileName, loadAndParseXMLFile(messageFile));
Document doc = loadAndParseXMLFile(messageFile);
msgFile=new MessageFileInfo(messageFileName.toUpperCase(), messageFileName, doc);
msgfList.add(msgFile);
//scanForDuplicates(); // don't keep this for production. Too expensive
}

View file

@ -17,6 +17,7 @@
package org.eclipse.rse.ui;
import java.net.InetAddress;
import java.net.URL;
import java.util.Vector;
import org.eclipse.core.resources.IProject;
@ -1337,6 +1338,14 @@ public class RSEUIPlugin extends SystemBasePlugin
}
return showPrefPageActions;
}
/**
* @return The URL to the message file DTD. Null if it is not found.
*/
public URL getMessageFileDTD() {
URL result = getBundle().getEntry("/messageFile.dtd");
return result;
}
/**
* Load a message file for this plugin.

View file

@ -15,26 +15,58 @@
********************************************************************************/
package org.eclipse.rse.ui.messages;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.eclipse.rse.services.clientserver.messages.IndicatorException;
import org.eclipse.rse.services.clientserver.messages.SystemMessage;
import org.eclipse.rse.services.clientserver.messages.SystemMessageFile;
import org.eclipse.rse.ui.RSEUIPlugin;
/**
* @author dmcknigh
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
* A SystemUIMessageFile extends SystemMessageFile and makes it more compatible
* with Eclipse
*/
public class SystemUIMessageFile extends SystemMessageFile
{
public SystemUIMessageFile(String messageFileName,
String defaultMessageFileLocation)
{
super(messageFileName, defaultMessageFileLocation);
}
public class SystemUIMessageFile extends SystemMessageFile {
/**
* Factory method for constructing a SystemUIMessageFile. If an error occurs when
* reading the message file DTD then that is logged.
* @param messageFileName The "registered" name of the message file. Used to determine
* if the message file has been loaded.
* @param messageFileStream The stream containing the message file. It is the
* caller's responsibility to close this stream.
* @return The message file that was constructed.
*/
public static SystemUIMessageFile getMessageFile(String messageFileName,
InputStream messageFileStream) {
SystemUIMessageFile result = null;
URL dtdURL = RSEUIPlugin.getDefault().getMessageFileDTD();
if (dtdURL != null) {
try {
InputStream dtdStream = dtdURL.openStream();
result = new SystemUIMessageFile(messageFileName,
messageFileStream, dtdStream);
dtdStream.close();
} catch (IOException e) {
RSEUIPlugin.logError("Could not open message file DTD.", e);
}
} else {
RSEUIPlugin.logError("Could not find mesage file DTD.");
}
return result;
}
private SystemUIMessageFile(String messageFileName,
InputStream messageFileStream, InputStream dtdStream) {
super(messageFileName, messageFileStream, dtdStream);
}
/**
* Override this to provide different extended SystemMessage implementation
*
* @param componentAbbr
* @param subComponentAbbr
* @param msgNumber
@ -44,9 +76,10 @@ public class SystemUIMessageFile extends SystemMessageFile
* @return The SystemMessage for the given message information
* @throws IndicatorException
*/
protected SystemMessage loadSystemMessage(String componentAbbr, String subComponentAbbr, String msgNumber, char msgIndicator,
String msgL1, String msgL2) throws IndicatorException
{
return new SystemUIMessage(componentAbbr, subComponentAbbr, msgNumber, msgIndicator, msgL1, msgL2);
protected SystemMessage loadSystemMessage(String componentAbbr,
String subComponentAbbr, String msgNumber, char msgIndicator,
String msgL1, String msgL2) throws IndicatorException {
return new SystemUIMessage(componentAbbr, subComponentAbbr, msgNumber,
msgIndicator, msgL1, msgL2);
}
}

View file

@ -20,9 +20,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Stack;
import java.util.Vector;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
@ -40,6 +44,7 @@ import org.eclipse.rse.ui.RSEUIPlugin;
import org.eclipse.rse.ui.messages.SystemUIMessageFile;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
@ -354,107 +359,155 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
// ------------------
/**
* Parse the given message file into memory, into a SystemMessageFile object.
* @param descriptor - the descriptor for this plugin
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* Resolves the bundle relative name to its URL inside a bundle if the resource
* named by that name exists. Returns null if the resources does not exist.
* Looks for the resource in NL directories as well.
* @param bundle The bundle in which to look for the resource
* @param name The name of the resource
* @return The resource URL or null.
*/
public static final URL resolveBundleNameNL(Bundle bundle, String name) {
URL result = null;
Stack candidates = new Stack();
Locale locale = Locale.getDefault();
String language = locale.getLanguage();
String country = locale.getCountry();
candidates.push("/" + name);
if (language.length() > 0) {
candidates.push("/" + language + "/" + name);
if (country.length() > 0) {
candidates.push("/" + language + "/" + country + "/" + name);
}
}
while (!candidates.isEmpty() && result == null) {
String candidate = (String) candidates.pop();
result = bundle.getEntry(candidate);
}
return result;
}
/**
* Parse the given message file into memory, into a SystemMessageFile
* object.
*
* @param descriptor -
* the descriptor for this plugin
* @param fileName -
* unqualified name of the .xml message file, inluding the .xml
* extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public static final SystemMessageFile loadMessageFile(Bundle bundle,
String fileName)
{
SystemMessageFile mf = null;
boolean ok = false;
try
{
IPath path = new Path("$nl$/"+fileName);
URL url = Platform.find(bundle, path);
if (url!=null) {
url = Platform.resolve(url);
URL temp = Platform.getBundle(RSEUIPlugin.PLUGIN_ID).getEntry("/");
temp = Platform.resolve(temp);
url = Platform.resolve(url);
mf = new SystemUIMessageFile(url.getPath(), temp.getFile());
String fileName) {
SystemMessageFile mf = null;
boolean ok = false;
try {
URL url = resolveBundleNameNL(bundle, fileName);
if (url != null) {
// url = Platform.resolve(url);
// URL temp = Platform.getBundle(RSEUIPlugin.PLUGIN_ID).getEntry("/");
// temp = Platform.resolve(temp);
// url = Platform.resolve(url);
InputStream messageFileStream = url.openStream();
mf = SystemUIMessageFile.getMessageFile(fileName, messageFileStream);
messageFileStream.close();
ok = true;
}
} catch (Throwable t)
{
logError("Error loading message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
} catch (Throwable t) {
logError("Error loading message file "
+ fileName
+ " in "
+ bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
}
if (!ok)
{
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(getActiveWorkbenchShell());
mb.setText("Unexpected Error");
mb.setMessage("Unable to load message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
return mf;
if (!ok) {
MessageBox mb = new MessageBox(getActiveWorkbenchShell());
mb.setText("Unexpected Error");
mb.setMessage("Unable to load message file "
+ fileName
+ " in "
+ bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
return mf;
}
/**
* Parse the given message file into memory, into a SystemMessageFile object.
* @param descriptor - the descriptor for this plugin
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public static final SystemMessageFile loadDefaultMessageFile(Bundle bundle,
String fileName)
{
SystemMessageFile mf = null;
boolean ok = false;
try
{
IPath path = new Path(fileName);
URL url = Platform.find(bundle, path);
//URL url = new URL(descriptor.getInstallURL(), fileName);
if (url!=null) {
url = Platform.resolve(url);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), bundle.getEntry("/").getPath());
ok = true;
}
} catch (Throwable t)
{
logError("Error loading message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
}
if (!ok)
{
Shell s = getActiveWorkbenchShell();
if (s == null) {
Display d = Display.getCurrent();
if (d != null) {
s = d.getActiveShell();
}
else {
d = Display.getDefault();
if (d != null) {
s = d.getActiveShell();
}
}
}
if (s != null) {
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(s);
mb.setText("Unexpected Error");
mb.setMessage("Unable to load message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
}
return mf;
}
* Parse the given message file into memory, into a SystemMessageFile
* object.
*
* @param descriptor -
* the descriptor for this plugin
* @param fileName -
* unqualified name of the .xml message file, inluding the .xml
* extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public static final SystemMessageFile loadDefaultMessageFile(Bundle bundle,
String fileName) {
SystemMessageFile mf = null;
boolean ok = false;
try {
URL url = bundle.getEntry(fileName);
// IPath path = new Path(fileName);
// URL url = Platform.find(bundle, path);
// URL url = new URL(descriptor.getInstallURL(), fileName);
if (url != null) {
// url = Platform.resolve(url);
InputStream messageFileStream = url.openStream();
mf = SystemUIMessageFile.getMessageFile(fileName, messageFileStream);
messageFileStream.close();
ok = true;
}
} catch (Throwable t) {
logError("Error loading message file "
+ fileName
+ " in "
+ bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
}
if (!ok) {
Shell s = getActiveWorkbenchShell();
if (s == null) {
Display d = Display.getCurrent();
if (d != null) {
s = d.getActiveShell();
} else {
d = Display.getDefault();
if (d != null) {
s = d.getActiveShell();
}
}
}
if (s != null) {
MessageBox mb = new MessageBox(s);
mb.setText("Unexpected Error");
mb.setMessage("Unable to load message file "
+ fileName
+ " in "
+ bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
}
return mf;
}
/**
* Retrieve a message from a message file.
* @param msgFile - the system message file containing the message.
* @param msgId - the ID of the message to retrieve. This is the concatenation of the
* message's component abbreviation, subcomponent abbreviation, and message ID as declared
* in the message xml file.
*
* @param msgFile -
* the system message file containing the message.
* @param msgId -
* the ID of the message to retrieve. This is the concatenation
* of the message's component abbreviation, subcomponent
* abbreviation, and message ID as declared in the message xml
* file.
*/
public static SystemMessage getMessage(SystemMessageFile msgFile, String msgId)
{