1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-26 10:25:32 +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;
@ -1338,6 +1339,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.
* @param messageFileName - the name of the message xml file. Will look for it in this plugin's install folder. * @param messageFileName - the name of the message xml file. Will look for it in this plugin's install folder.

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) * Factory method for constructing a SystemUIMessageFile. If an error occurs when
{ * reading the message file DTD then that is logged.
super(messageFileName, defaultMessageFileLocation); * @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.
* @return SystemMessageFile (null if unable to load the file) * @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 SystemMessageFile loadMessageFile(Bundle bundle, public static final URL resolveBundleNameNL(Bundle bundle, String name) {
String fileName) URL result = null;
{ Stack candidates = new Stack();
SystemMessageFile mf = null; Locale locale = Locale.getDefault();
boolean ok = false; String language = locale.getLanguage();
try String country = locale.getCountry();
{ candidates.push("/" + name);
IPath path = new Path("$nl$/"+fileName); if (language.length() > 0) {
URL url = Platform.find(bundle, path); candidates.push("/" + language + "/" + name);
if (country.length() > 0) {
if (url!=null) { candidates.push("/" + language + "/" + country + "/" + name);
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());
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) while (!candidates.isEmpty() && result == null) {
{ String candidate = (String) candidates.pop();
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(getActiveWorkbenchShell()); result = bundle.getEntry(candidate);
mb.setText("Unexpected Error"); }
mb.setMessage("Unable to load message file " + fileName + " in " + bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME)); return result;
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 loadMessageFile(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 = resolveBundleNameNL(bundle, fileName);
url = Platform.resolve(url); if (url != null) {
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), bundle.getEntry("/").getPath()); // url = Platform.resolve(url);
ok = true; // URL temp = Platform.getBundle(RSEUIPlugin.PLUGIN_ID).getEntry("/");
} // temp = Platform.resolve(temp);
} 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;
}
} 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) {
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;
}
if (!ok) /**
{ * Parse the given message file into memory, into a SystemMessageFile
Shell s = getActiveWorkbenchShell(); * 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 (s == null) { if (!ok) {
Display d = Display.getCurrent(); 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();
}
}
if (d != null) { return mf;
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;
}
/** /**
* 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)
{ {