1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-26 02:15:31 +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; 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 * Constructor
* @param messageFileName - name of xml file which will contain the messages * @param messageFileName - name of xml file which will contain the messages
@ -137,7 +120,8 @@ public class SystemMessageFile implements ErrorHandler
this.dtdInputStream = dtdStream; this.dtdInputStream = dtdStream;
if (msgFile == null) 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); msgfList.add(msgFile);
//scanForDuplicates(); // don't keep this for production. Too expensive //scanForDuplicates(); // don't keep this for production. Too expensive
} }

View file

@ -17,6 +17,7 @@
package org.eclipse.rse.ui; package org.eclipse.rse.ui;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URL;
import java.util.Vector; import java.util.Vector;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
@ -1337,6 +1338,14 @@ public class RSEUIPlugin extends SystemBasePlugin
} }
return showPrefPageActions; 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. * Load a message file for this plugin.

View file

@ -15,26 +15,58 @@
********************************************************************************/ ********************************************************************************/
package org.eclipse.rse.ui.messages; 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.IndicatorException;
import org.eclipse.rse.services.clientserver.messages.SystemMessage; import org.eclipse.rse.services.clientserver.messages.SystemMessage;
import org.eclipse.rse.services.clientserver.messages.SystemMessageFile; import org.eclipse.rse.services.clientserver.messages.SystemMessageFile;
import org.eclipse.rse.ui.RSEUIPlugin;
/** /**
* @author dmcknigh * A SystemUIMessageFile extends SystemMessageFile and makes it more compatible
* * with Eclipse
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/ */
public class SystemUIMessageFile extends SystemMessageFile public class SystemUIMessageFile extends SystemMessageFile {
{
public SystemUIMessageFile(String messageFileName,
String defaultMessageFileLocation)
{
super(messageFileName, defaultMessageFileLocation);
}
/**
* 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 * Override this to provide different extended SystemMessage implementation
*
* @param componentAbbr * @param componentAbbr
* @param subComponentAbbr * @param subComponentAbbr
* @param msgNumber * @param msgNumber
@ -44,9 +76,10 @@ public class SystemUIMessageFile extends SystemMessageFile
* @return The SystemMessage for the given message information * @return The SystemMessage for the given message information
* @throws IndicatorException * @throws IndicatorException
*/ */
protected SystemMessage loadSystemMessage(String componentAbbr, String subComponentAbbr, String msgNumber, char msgIndicator, protected SystemMessage loadSystemMessage(String componentAbbr,
String msgL1, String msgL2) throws IndicatorException String subComponentAbbr, String msgNumber, char msgIndicator,
{ String msgL1, String msgL2) throws IndicatorException {
return new SystemUIMessage(componentAbbr, subComponentAbbr, msgNumber, msgIndicator, msgL1, msgL2); 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.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Stack;
import java.util.Vector;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot; 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.rse.ui.messages.SystemUIMessageFile;
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow; 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. * Resolves the bundle relative name to its URL inside a bundle if the resource
* @param descriptor - the descriptor for this plugin * named by that name exists. Returns null if the resources does not exist.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension. * 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) * @return SystemMessageFile (null if unable to load the file)
*/ */
public static final SystemMessageFile loadMessageFile(Bundle bundle, public static final SystemMessageFile loadMessageFile(Bundle bundle,
String fileName) String fileName) {
{ SystemMessageFile mf = null;
SystemMessageFile mf = null; boolean ok = false;
boolean ok = false; try {
try URL url = resolveBundleNameNL(bundle, fileName);
{ if (url != null) {
IPath path = new Path("$nl$/"+fileName); // url = Platform.resolve(url);
URL url = Platform.find(bundle, path); // URL temp = Platform.getBundle(RSEUIPlugin.PLUGIN_ID).getEntry("/");
// temp = Platform.resolve(temp);
if (url!=null) { // url = Platform.resolve(url);
url = Platform.resolve(url); InputStream messageFileStream = url.openStream();
URL temp = Platform.getBundle(RSEUIPlugin.PLUGIN_ID).getEntry("/"); mf = SystemUIMessageFile.getMessageFile(fileName, messageFileStream);
temp = Platform.resolve(temp); messageFileStream.close();
url = Platform.resolve(url);
mf = new SystemUIMessageFile(url.getPath(), temp.getFile());
ok = true; ok = true;
} }
} catch (Throwable t) } catch (Throwable t) {
{ logError("Error loading message file "
logError("Error loading message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME), t); + fileName
ok = false; // DY + " in "
+ bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
} }
if (!ok) if (!ok) {
{ MessageBox mb = new MessageBox(getActiveWorkbenchShell());
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(getActiveWorkbenchShell()); mb.setText("Unexpected Error");
mb.setText("Unexpected Error"); mb.setMessage("Unable to load message file "
mb.setMessage("Unable to load message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME)); + fileName
mb.open(); + " in "
} + bundle.getHeaders().get(
return mf; org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
return mf;
} }
/** /**
* Parse the given message file into memory, into a SystemMessageFile object. * Parse the given message file into memory, into a SystemMessageFile
* @param descriptor - the descriptor for this plugin * object.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension. *
* @return SystemMessageFile (null if unable to load the file) * @param descriptor -
*/ * the descriptor for this plugin
public static final SystemMessageFile loadDefaultMessageFile(Bundle bundle, * @param fileName -
String fileName) * unqualified name of the .xml message file, inluding the .xml
{ * extension.
SystemMessageFile mf = null; * @return SystemMessageFile (null if unable to load the file)
boolean ok = false; */
try public static final SystemMessageFile loadDefaultMessageFile(Bundle bundle,
{ String fileName) {
IPath path = new Path(fileName); SystemMessageFile mf = null;
URL url = Platform.find(bundle, path); boolean ok = false;
//URL url = new URL(descriptor.getInstallURL(), fileName); try {
if (url!=null) { URL url = bundle.getEntry(fileName);
url = Platform.resolve(url); // IPath path = new Path(fileName);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), bundle.getEntry("/").getPath()); // URL url = Platform.find(bundle, path);
ok = true; // URL url = new URL(descriptor.getInstallURL(), fileName);
} if (url != null) {
} catch (Throwable t) // url = Platform.resolve(url);
{ InputStream messageFileStream = url.openStream();
logError("Error loading message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME), t); mf = SystemUIMessageFile.getMessageFile(fileName, messageFileStream);
ok = false; // DY messageFileStream.close();
} ok = true;
}
if (!ok) } catch (Throwable t) {
{ logError("Error loading message file "
Shell s = getActiveWorkbenchShell(); + fileName
+ " in "
if (s == null) { + bundle.getHeaders().get(
Display d = Display.getCurrent(); org.osgi.framework.Constants.BUNDLE_NAME), t);
ok = false; // DY
if (d != null) { }
s = d.getActiveShell();
} if (!ok) {
else { Shell s = getActiveWorkbenchShell();
d = Display.getDefault(); if (s == null) {
Display d = Display.getCurrent();
if (d != null) { if (d != null) {
s = d.getActiveShell(); 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)); if (s != null) {
mb.open(); MessageBox mb = new MessageBox(s);
} mb.setText("Unexpected Error");
} mb.setMessage("Unable to load message file "
+ fileName
return mf; + " in "
} + bundle.getHeaders().get(
org.osgi.framework.Constants.BUNDLE_NAME));
mb.open();
}
}
return mf;
}
/** /**
* Retrieve a message from a message file. * 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 * @param msgFile -
* message's component abbreviation, subcomponent abbreviation, and message ID as declared * the system message file containing the message.
* in the message xml file. * @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) public static SystemMessage getMessage(SystemMessageFile msgFile, String msgId)
{ {