392 lines
14 KiB
Java
392 lines
14 KiB
Java
package javax.activation;
|
|
|
|
import com.sun.activation.registries.LogSupport;
|
|
import com.sun.activation.registries.MailcapFile;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
public class MailcapCommandMap extends CommandMap {
|
|
private static MailcapFile defDB = null;
|
|
|
|
private MailcapFile[] DB;
|
|
|
|
private static final int PROG = 0;
|
|
|
|
public MailcapCommandMap() {
|
|
ArrayList arrayList = new ArrayList(5);
|
|
MailcapFile mailcapFile = null;
|
|
arrayList.add(null);
|
|
LogSupport.log("MailcapCommandMap: load HOME");
|
|
try {
|
|
String str = System.getProperty("user.home");
|
|
if (str != null) {
|
|
String str1 = str + File.separator + ".mailcap";
|
|
mailcapFile = loadFile(str1);
|
|
if (mailcapFile != null)
|
|
arrayList.add(mailcapFile);
|
|
}
|
|
} catch (SecurityException e) {}
|
|
LogSupport.log("MailcapCommandMap: load SYS");
|
|
try {
|
|
String str = System.getProperty("java.home") + File.separator + "lib" + File.separator + "mailcap";
|
|
mailcapFile = loadFile(str);
|
|
if (mailcapFile != null)
|
|
arrayList.add(mailcapFile);
|
|
} catch (SecurityException e) {}
|
|
LogSupport.log("MailcapCommandMap: load JAR");
|
|
loadAllResources(arrayList, "META-INF/mailcap");
|
|
LogSupport.log("MailcapCommandMap: load DEF");
|
|
synchronized (MailcapCommandMap.class) {
|
|
if (defDB == null)
|
|
defDB = loadResource("/META-INF/mailcap.default");
|
|
}
|
|
if (defDB != null)
|
|
arrayList.add(defDB);
|
|
this.DB = new MailcapFile[arrayList.size()];
|
|
this.DB = (MailcapFile[])arrayList.toArray(this.DB);
|
|
}
|
|
|
|
private MailcapFile loadResource(String paramString) {
|
|
InputStream inputStream = null;
|
|
try {
|
|
inputStream = SecuritySupport.getResourceAsStream(getClass(), paramString);
|
|
if (inputStream != null) {
|
|
MailcapFile mailcapFile = new MailcapFile(inputStream);
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: successfully loaded mailcap file: " + paramString);
|
|
return mailcapFile;
|
|
}
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: not loading mailcap file: " + paramString);
|
|
} catch (IOException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: can't load " + paramString, e);
|
|
} catch (SecurityException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: can't load " + paramString, e);
|
|
} finally {
|
|
try {
|
|
if (inputStream != null)
|
|
inputStream.close();
|
|
} catch (IOException e) {}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void loadAllResources(List paramList, String paramString) {
|
|
boolean bool = false;
|
|
try {
|
|
URL[] arrayOfURL;
|
|
ClassLoader classLoader = null;
|
|
classLoader = SecuritySupport.getContextClassLoader();
|
|
if (classLoader == null)
|
|
classLoader = getClass().getClassLoader();
|
|
if (classLoader != null) {
|
|
arrayOfURL = SecuritySupport.getResources(classLoader, paramString);
|
|
} else {
|
|
arrayOfURL = SecuritySupport.getSystemResources(paramString);
|
|
}
|
|
if (arrayOfURL != null) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: getResources");
|
|
for (int i = 0; i < arrayOfURL.length; i++) {
|
|
URL uRL = arrayOfURL[i];
|
|
InputStream inputStream = null;
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: URL " + uRL);
|
|
try {
|
|
inputStream = SecuritySupport.openStream(uRL);
|
|
if (inputStream != null) {
|
|
paramList.add(new MailcapFile(inputStream));
|
|
bool = true;
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: successfully loaded mailcap file from URL: " + uRL);
|
|
} else if (LogSupport.isLoggable()) {
|
|
LogSupport.log("MailcapCommandMap: not loading mailcap file from URL: " + uRL);
|
|
}
|
|
} catch (IOException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: can't load " + uRL, e);
|
|
} catch (SecurityException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: can't load " + uRL, e);
|
|
} finally {
|
|
try {
|
|
if (inputStream != null)
|
|
inputStream.close();
|
|
} catch (IOException e) {}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: can't load " + paramString, e);
|
|
}
|
|
if (!bool) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: !anyLoaded");
|
|
MailcapFile mailcapFile = loadResource("/" + paramString);
|
|
if (mailcapFile != null)
|
|
paramList.add(mailcapFile);
|
|
}
|
|
}
|
|
|
|
private MailcapFile loadFile(String paramString) {
|
|
MailcapFile mailcapFile = null;
|
|
try {
|
|
mailcapFile = new MailcapFile(paramString);
|
|
} catch (IOException e) {}
|
|
return mailcapFile;
|
|
}
|
|
|
|
public MailcapCommandMap(String paramString) throws IOException {
|
|
this();
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: load PROG from " + paramString);
|
|
if (this.DB[0] == null)
|
|
this.DB[0] = new MailcapFile(paramString);
|
|
}
|
|
|
|
public MailcapCommandMap(InputStream paramInputStream) {
|
|
this();
|
|
LogSupport.log("MailcapCommandMap: load PROG");
|
|
if (this.DB[0] == null)
|
|
try {
|
|
this.DB[0] = new MailcapFile(paramInputStream);
|
|
} catch (IOException e) {}
|
|
}
|
|
|
|
public synchronized CommandInfo[] getPreferredCommands(String paramString) {
|
|
ArrayList arrayList = new ArrayList();
|
|
if (paramString != null)
|
|
paramString = paramString.toLowerCase(Locale.ENGLISH);
|
|
for (int j = 0; j < this.DB.length; j++) {
|
|
if (this.DB[j] != null) {
|
|
Map map = this.DB[j].getMailcapList(paramString);
|
|
if (map != null)
|
|
appendPrefCmdsToList(map, arrayList);
|
|
}
|
|
}
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
Map map = this.DB[i].getMailcapFallbackList(paramString);
|
|
if (map != null)
|
|
appendPrefCmdsToList(map, arrayList);
|
|
}
|
|
}
|
|
CommandInfo[] arrayOfCommandInfo = new CommandInfo[arrayList.size()];
|
|
arrayOfCommandInfo = (CommandInfo[])arrayList.toArray(arrayOfCommandInfo);
|
|
return arrayOfCommandInfo;
|
|
}
|
|
|
|
private void appendPrefCmdsToList(Map paramMap, List paramList) {
|
|
Iterator iterator = paramMap.keySet().iterator();
|
|
while (iterator.hasNext()) {
|
|
String str = (String)iterator.next();
|
|
if (!checkForVerb(paramList, str)) {
|
|
List list = (List)paramMap.get(str);
|
|
String str1 = (String)list.get(0);
|
|
paramList.add(new CommandInfo(str, str1));
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean checkForVerb(List paramList, String paramString) {
|
|
Iterator iterator = paramList.iterator();
|
|
while (iterator.hasNext()) {
|
|
String str = ((CommandInfo)iterator.next()).getCommandName();
|
|
if (str.equals(paramString))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public synchronized CommandInfo[] getAllCommands(String paramString) {
|
|
ArrayList arrayList = new ArrayList();
|
|
if (paramString != null)
|
|
paramString = paramString.toLowerCase(Locale.ENGLISH);
|
|
for (int j = 0; j < this.DB.length; j++) {
|
|
if (this.DB[j] != null) {
|
|
Map map = this.DB[j].getMailcapList(paramString);
|
|
if (map != null)
|
|
appendCmdsToList(map, arrayList);
|
|
}
|
|
}
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
Map map = this.DB[i].getMailcapFallbackList(paramString);
|
|
if (map != null)
|
|
appendCmdsToList(map, arrayList);
|
|
}
|
|
}
|
|
CommandInfo[] arrayOfCommandInfo = new CommandInfo[arrayList.size()];
|
|
arrayOfCommandInfo = (CommandInfo[])arrayList.toArray(arrayOfCommandInfo);
|
|
return arrayOfCommandInfo;
|
|
}
|
|
|
|
private void appendCmdsToList(Map paramMap, List paramList) {
|
|
Iterator iterator = paramMap.keySet().iterator();
|
|
while (iterator.hasNext()) {
|
|
String str = (String)iterator.next();
|
|
List list = (List)paramMap.get(str);
|
|
Iterator iterator1 = list.iterator();
|
|
while (iterator1.hasNext()) {
|
|
String str1 = (String)iterator1.next();
|
|
paramList.add(new CommandInfo(str, str1));
|
|
}
|
|
}
|
|
}
|
|
|
|
public synchronized CommandInfo getCommand(String paramString1, String paramString2) {
|
|
if (paramString1 != null)
|
|
paramString1 = paramString1.toLowerCase(Locale.ENGLISH);
|
|
for (int j = 0; j < this.DB.length; j++) {
|
|
if (this.DB[j] != null) {
|
|
Map map = this.DB[j].getMailcapList(paramString1);
|
|
if (map != null) {
|
|
List list = (List)map.get(paramString2);
|
|
if (list != null) {
|
|
String str = (String)list.get(0);
|
|
if (str != null)
|
|
return new CommandInfo(paramString2, str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
Map map = this.DB[i].getMailcapFallbackList(paramString1);
|
|
if (map != null) {
|
|
List list = (List)map.get(paramString2);
|
|
if (list != null) {
|
|
String str = (String)list.get(0);
|
|
if (str != null)
|
|
return new CommandInfo(paramString2, str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public synchronized void addMailcap(String paramString) {
|
|
LogSupport.log("MailcapCommandMap: add to PROG");
|
|
if (this.DB[0] == null)
|
|
this.DB[0] = new MailcapFile();
|
|
this.DB[0].appendToMailcap(paramString);
|
|
}
|
|
|
|
public synchronized DataContentHandler createDataContentHandler(String paramString) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("MailcapCommandMap: createDataContentHandler for " + paramString);
|
|
if (paramString != null)
|
|
paramString = paramString.toLowerCase(Locale.ENGLISH);
|
|
for (int j = 0; j < this.DB.length; j++) {
|
|
if (this.DB[j] != null) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log(" search DB #" + j);
|
|
Map map = this.DB[j].getMailcapList(paramString);
|
|
if (map != null) {
|
|
List list = (List)map.get("content-handler");
|
|
if (list != null) {
|
|
String str = (String)list.get(0);
|
|
DataContentHandler dataContentHandler = getDataContentHandler(str);
|
|
if (dataContentHandler != null)
|
|
return dataContentHandler;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log(" search fallback DB #" + i);
|
|
Map map = this.DB[i].getMailcapFallbackList(paramString);
|
|
if (map != null) {
|
|
List list = (List)map.get("content-handler");
|
|
if (list != null) {
|
|
String str = (String)list.get(0);
|
|
DataContentHandler dataContentHandler = getDataContentHandler(str);
|
|
if (dataContentHandler != null)
|
|
return dataContentHandler;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private DataContentHandler getDataContentHandler(String paramString) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log(" got content-handler");
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log(" class " + paramString);
|
|
try {
|
|
ClassLoader classLoader = null;
|
|
classLoader = SecuritySupport.getContextClassLoader();
|
|
if (classLoader == null)
|
|
classLoader = getClass().getClassLoader();
|
|
Class clazz = null;
|
|
try {
|
|
clazz = classLoader.loadClass(paramString);
|
|
} catch (Exception e) {
|
|
clazz = Class.forName(paramString);
|
|
}
|
|
if (clazz != null)
|
|
return (DataContentHandler)clazz.newInstance();
|
|
} catch (IllegalAccessException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("Can't load DCH " + paramString, e);
|
|
} catch (ClassNotFoundException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("Can't load DCH " + paramString, e);
|
|
} catch (InstantiationException e) {
|
|
if (LogSupport.isLoggable())
|
|
LogSupport.log("Can't load DCH " + paramString, e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public synchronized String[] getMimeTypes() {
|
|
ArrayList arrayList = new ArrayList();
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
String[] arrayOfString1 = this.DB[i].getMimeTypes();
|
|
if (arrayOfString1 != null)
|
|
for (int j = 0; j < arrayOfString1.length; j++) {
|
|
if (!arrayList.contains(arrayOfString1[j]))
|
|
arrayList.add(arrayOfString1[j]);
|
|
}
|
|
}
|
|
}
|
|
String[] arrayOfString = new String[arrayList.size()];
|
|
arrayOfString = (String[])arrayList.toArray(arrayOfString);
|
|
return arrayOfString;
|
|
}
|
|
|
|
public synchronized String[] getNativeCommands(String paramString) {
|
|
ArrayList arrayList = new ArrayList();
|
|
if (paramString != null)
|
|
paramString = paramString.toLowerCase(Locale.ENGLISH);
|
|
for (int i = 0; i < this.DB.length; i++) {
|
|
if (this.DB[i] != null) {
|
|
String[] arrayOfString1 = this.DB[i].getNativeCommands(paramString);
|
|
if (arrayOfString1 != null)
|
|
for (int j = 0; j < arrayOfString1.length; j++) {
|
|
if (!arrayList.contains(arrayOfString1[j]))
|
|
arrayList.add(arrayOfString1[j]);
|
|
}
|
|
}
|
|
}
|
|
String[] arrayOfString = new String[arrayList.size()];
|
|
arrayOfString = (String[])arrayList.toArray(arrayOfString);
|
|
return arrayOfString;
|
|
}
|
|
}
|