PageRenderTime 87ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/pooka-3.0-080505/net/suberic/pooka/FullMailcapCommandMap.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 454 lines | 408 code | 13 blank | 33 comment | 16 complexity | 86955dde14699face4a3aa378dbb73a6 MD5 | raw file
  1. package net.suberic.pooka;
  2. import javax.activation.*;
  3. import java.util.*;
  4. import java.io.*;
  5. /**
  6. * FullMailcapCommandMap accepts both x-java-* type mailcap entries
  7. * as well as standard, external-to-java entries.
  8. *
  9. * It uses the following resources as the sources:
  10. *
  11. * The file .mailcap (or mailcap.txt) in the user's home directory.
  12. * The file <java.home>/lib/mailcap.
  13. * The file or resource named META-INF/mailcap.
  14. * The file or resource named META-INF/mailcap.default
  15. * (usually found only in the activation.jar file).
  16. */
  17. public class FullMailcapCommandMap extends MailcapCommandMap {
  18. private Vector mailcapMaps;
  19. private static String externalLauncher = "net.suberic.pooka.ExternalLauncher";
  20. private File sourceFile = null;;
  21. /**
  22. * Creates a FullMailcapCommandMap.
  23. */
  24. public FullMailcapCommandMap() {
  25. initializeMailcap(null);
  26. }
  27. /**
  28. * Create a FullMailcapCommandMap that uses the file represented by
  29. * localMailcap as the top-most entry in the mailcap list.
  30. */
  31. public FullMailcapCommandMap(String localMailcap) throws java.io.IOException {
  32. initializeMailcap(localMailcap);
  33. }
  34. /**
  35. * Return the DataContentHandler for the specified MIME type.
  36. */
  37. public DataContentHandler createDataContentHandler(java.lang.String mimeType) {
  38. CommandInfo[] allCmds = getAllCommands(mimeType);
  39. DataContentHandler returnValue = null;
  40. if (allCmds != null) {
  41. for (int i = 0; i < allCmds.length; i++) {
  42. CommandInfo current = allCmds[i];
  43. if (current != null) {
  44. String name = current.getCommandName();
  45. if (name != null && name.equalsIgnoreCase("content-handler")) {
  46. try {
  47. String className = current.getCommandClass();
  48. if (returnValue == null)
  49. returnValue = (DataContentHandler) Class.forName(className).newInstance();
  50. } catch (Exception e) {
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if (returnValue == null)
  57. return super.createDataContentHandler(mimeType);
  58. else
  59. return returnValue;
  60. }
  61. /**
  62. * this adds the following files/resources, in order:
  63. *
  64. * The file localMailcap.
  65. * The file .mailcap (or mailcap.txt) in the user's home directory.
  66. * The file <java.home>/lib/mailcap.
  67. * The file or resource named META-INF/mailcap.
  68. * The file or resource named META-INF/mailcap.default
  69. * (usually found only in the activation.jar file).
  70. */
  71. public void initializeMailcap(String localMailcap) {
  72. mailcapMaps = new Vector();
  73. InputStream is;
  74. try {
  75. if (localMailcap != null) {
  76. sourceFile = new File(localMailcap);
  77. addMailcapFile(localMailcap);
  78. }
  79. } catch (IOException ioe) {
  80. // if it doesn't exist, it's ok.
  81. }
  82. try {
  83. if (System.getProperty("file.separator").equals("\\")) {
  84. if (sourceFile == null)
  85. sourceFile = new File(System.getProperty("user.home") + "\\mailcap.txt");
  86. addMailcapFile(System.getProperty("user.home") + "\\mailcap.txt");
  87. } else {
  88. if (sourceFile == null)
  89. sourceFile = new File(System.getProperty("user.home") + System.getProperty("file.separator") + ".mailcap");
  90. addMailcapFile(System.getProperty("user.home") + System.getProperty("file.separator") + ".mailcap");
  91. }
  92. } catch (IOException ioe) {
  93. // if it doesn't exist, it's ok.
  94. }
  95. try {
  96. addMailcapFile(System.getProperty("java.home") + System.getProperty("file.separator") + "lib" + System.getProperty("file.separator") + "mailcap");
  97. } catch (IOException ioe) {
  98. // if it doesn't exist, it's ok.
  99. }
  100. is = new Object().getClass().getResourceAsStream("/META-INF/mailcap");
  101. if (is != null)
  102. addMailcapFile(is);
  103. is = new Object().getClass().getResourceAsStream("/META-INF/mailcap.default");
  104. if (is != null)
  105. addMailcapFile(is);
  106. }
  107. /**
  108. * Gets all of the commands associated with the given mimeType.
  109. */
  110. public CommandInfo[] getAllCommands(java.lang.String mimeType) {
  111. /*
  112. * this just goes through the list of mailcap objects stored
  113. * and calls getAllCommands() on each.
  114. */
  115. Vector foundCommands = new Vector();
  116. for (int i = 0; i < mailcapMaps.size() ; i++) {
  117. MailcapMap mc = (MailcapMap)mailcapMaps.elementAt(i);
  118. CommandInfo[] cis = mc.getAllCommands(mimeType);
  119. if (cis != null)
  120. for (int j = 0; j < cis.length; j++) {
  121. foundCommands.add(cis[j]);
  122. }
  123. }
  124. if (foundCommands.size() == 0)
  125. return null;
  126. CommandInfo[] returnValue = new CommandInfo[foundCommands.size()];
  127. for (int k = 0; k < foundCommands.size(); k++)
  128. returnValue[k] = (CommandInfo)foundCommands.elementAt(k);
  129. return returnValue;
  130. }
  131. public CommandInfo getCommand(java.lang.String mimeType, java.lang.String cmdName) {
  132. for (int i = mailcapMaps.size(); i >= 0; i--) {
  133. MailcapMap mc = (MailcapMap)mailcapMaps.elementAt(i);
  134. CommandInfo cis = mc.getSpecificCommand(mimeType, cmdName);
  135. if (cis != null)
  136. return cis;
  137. cis = mc.getGenericCommand(mimeType, cmdName);
  138. if (cis != null)
  139. return cis;
  140. }
  141. return null;
  142. }
  143. public CommandInfo[] getPreferredCommands(java.lang.String mimeType) {
  144. return getAllCommands(mimeType);
  145. }
  146. public void addMailcap(java.lang.String mail_cap) {
  147. MailcapMap mc = (MailcapMap)mailcapMaps.firstElement();
  148. mc.addMailcapEntry(mail_cap, true);
  149. if (sourceFile != null)
  150. writeEntryToSourceFile(mail_cap);
  151. }
  152. /**
  153. * This writes the entry to the mailcap file. Note that it actually
  154. * ends up overwriting _all_ entries for that particular mime type.
  155. */
  156. private synchronized void writeEntryToSourceFile(String mail_cap) {
  157. if (sourceFile != null) {
  158. int semicolonIndex = mail_cap.indexOf(';');
  159. if (semicolonIndex > -1) {
  160. String mimeType = mail_cap.substring(0, semicolonIndex);
  161. try {
  162. if (!sourceFile.exists())
  163. sourceFile.createNewFile();
  164. File outputFile = sourceFile.createTempFile(sourceFile.getName(), ".tmp", sourceFile.getParentFile());
  165. BufferedReader readSourceFile = new BufferedReader(new FileReader(sourceFile));
  166. BufferedWriter writeSourceFile = new BufferedWriter(new FileWriter(outputFile));
  167. String currentLine = readSourceFile.readLine();
  168. while (currentLine != null) {
  169. int equalsLoc = currentLine.indexOf(';');
  170. if (equalsLoc != -1) {
  171. String key = currentLine.substring(0, equalsLoc);
  172. if (!mimeType.equalsIgnoreCase(key)) {
  173. writeSourceFile.write(currentLine);
  174. writeSourceFile.newLine();
  175. }
  176. }
  177. currentLine = readSourceFile.readLine();
  178. }
  179. writeSourceFile.write(mail_cap);
  180. writeSourceFile.newLine();
  181. readSourceFile.close();
  182. writeSourceFile.flush();
  183. writeSourceFile.close();
  184. // if you don't delete the .old file first, then the
  185. // rename fails under Windows.
  186. String oldSourceName = sourceFile.getAbsolutePath() + ".old";
  187. File oldSource = new File (oldSourceName);
  188. if (oldSource.exists())
  189. oldSource.delete();
  190. String fileName = new String(sourceFile.getAbsolutePath());
  191. sourceFile.renameTo(oldSource);
  192. outputFile.renameTo(new File(fileName));
  193. oldSource.delete();
  194. } catch (Exception e) {
  195. System.out.println("caugh exception while writing source file: " + e);
  196. e.printStackTrace();
  197. }
  198. }
  199. }
  200. }
  201. public void addMailcapFile(java.lang.String mailcapFileName) throws IOException {
  202. FileInputStream fis = new FileInputStream(mailcapFileName);
  203. addMailcapFile(fis);
  204. }
  205. public void addMailcapFile(InputStream is) {
  206. InputStreamReader isr = new InputStreamReader(is);
  207. BufferedReader reader = new BufferedReader(isr);
  208. MailcapMap map = new MailcapMap();
  209. try {
  210. String nextLine = reader.readLine();
  211. while (nextLine != null) {
  212. if (!(nextLine.startsWith("#")))
  213. map.addMailcapEntry(nextLine);
  214. nextLine=reader.readLine();
  215. }
  216. mailcapMaps.add(map);
  217. } catch (Exception e) {
  218. System.out.println("an error happened." + e.getMessage());
  219. e.printStackTrace();
  220. } finally {
  221. try {
  222. reader.close();
  223. } catch (IOException ioe) {
  224. }
  225. try {
  226. isr.close();
  227. } catch (IOException ioe) {
  228. }
  229. }
  230. }
  231. public static String getExternalLauncher() {
  232. return externalLauncher;
  233. }
  234. public static void setExternalLauncher(String newVal) {
  235. externalLauncher = newVal;
  236. }
  237. private class MailcapMap {
  238. // these are Hashtables of Vectors. the Vectors should all be
  239. // CommandInfos.
  240. private Hashtable specificMap;
  241. private Hashtable genericMap;
  242. MailcapMap() {
  243. specificMap=new Hashtable();
  244. genericMap=new Hashtable();
  245. }
  246. CommandInfo[] getAllCommands(String mimeType) {
  247. Vector foundCommands = new Vector();
  248. Vector v = (Vector)specificMap.get(mimeType);
  249. if (v != null) {
  250. for (int i = 0; i < v.size(); i++)
  251. foundCommands.add(v.elementAt(i));
  252. }
  253. v = (Vector)genericMap.get(getGenericMimeType(mimeType));
  254. if (v != null)
  255. for (int j = 0; j < v.size(); j++)
  256. foundCommands.add(v.elementAt(j));
  257. if (foundCommands.size() == 0)
  258. return null;
  259. CommandInfo[] commandsAsArray = new CommandInfo[foundCommands.size()];
  260. for (int i = 0; i < foundCommands.size(); i++)
  261. commandsAsArray[i] = (CommandInfo)foundCommands.elementAt(i);
  262. return commandsAsArray;
  263. }
  264. CommandInfo getSpecificCommand(String mimeType, String verb) {
  265. return getCommandFromVector((Vector)specificMap.get(mimeType), verb);
  266. }
  267. CommandInfo getGenericCommand(String mimeType, String verb) {
  268. return getCommandFromVector((Vector)genericMap.get(getGenericMimeType(mimeType)), verb);
  269. }
  270. CommandInfo getCommandFromVector(Vector v, String verb) {
  271. if (v == null)
  272. return null;
  273. CommandInfo ci;
  274. for (int i = 0; i < v.size(); i++) {
  275. ci = (CommandInfo)v.elementAt(i);
  276. if (matches(ci, verb))
  277. return ci;
  278. }
  279. return null;
  280. }
  281. public void addMailcapEntry(String mailcapLine) {
  282. addMailcapEntry(mailcapLine, false);
  283. }
  284. synchronized void addMailcapEntry(String mailcapLine, boolean prepend) {
  285. String mimeType;
  286. String command;
  287. String verb;
  288. StringTokenizer tokens = new StringTokenizer(mailcapLine, ";");
  289. if (tokens.hasMoreTokens()) {
  290. mimeType = stripWhiteSpace(new StringBuffer(tokens.nextToken()));
  291. Hashtable addTable;
  292. int i = mimeType.indexOf('/');
  293. if (( i != -1 ) && (i != mimeType.length()-1) && (mimeType.charAt(i+1) == '*')) {
  294. addTable = genericMap;
  295. mimeType = mimeType.substring(0, i-1);
  296. } else {
  297. addTable = specificMap;
  298. }
  299. while (tokens.hasMoreTokens()) {
  300. String[] verbClass = parseCommand(stripWhiteSpace(new StringBuffer(tokens.nextToken())));
  301. if (verbClass != null) {
  302. Vector v = (Vector)addTable.get(mimeType);
  303. if (v != null) {
  304. if (prepend) {
  305. v.add(0, new CommandInfo(verbClass[0], verbClass[1]));
  306. } else {
  307. v.add(new CommandInfo(verbClass[0], verbClass[1]));
  308. }
  309. } else {
  310. v = new Vector();
  311. v.add(new CommandInfo(verbClass[0], verbClass[1]));
  312. addTable.put(mimeType, v);
  313. }
  314. }
  315. } // while
  316. } // if tokens.hasMoreTokens()
  317. }
  318. /**
  319. *
  320. */
  321. String getGenericMimeType(String original) {
  322. int slash = original.indexOf('/');
  323. if (slash == -1)
  324. return original;
  325. else
  326. return original.substring(0, slash);
  327. }
  328. boolean matches (CommandInfo ci, String verb) {
  329. return (ci.getCommandName().equals(verb));
  330. }
  331. String stripWhiteSpace(StringBuffer sb) {
  332. int i = 0;
  333. if (sb.charAt(i) == ' ' || sb.charAt(i) == '\t') {
  334. while (i < sb.length() && (sb.charAt(i) == ' ' || sb.charAt(i) == '\t'))
  335. i++;
  336. sb.delete(0, i);
  337. }
  338. i = sb.length();
  339. if (i > 0 && (sb.charAt(i-1) == ' ' || sb.charAt(i-1) == '\t')) {
  340. while (i > 0 && sb.charAt(i-1) == ' ' || sb.charAt(i-1) == '\t')
  341. i--;
  342. sb.delete(i, sb.length());
  343. }
  344. return sb.toString();
  345. }
  346. /**
  347. * returns a 2 dimensional String, where 0 = the command
  348. * verb, and 1 = the classname.
  349. *
  350. * if no x-java- is found, the whole string is taken to
  351. * be the verb, and the class defaults to
  352. * FullMailcapCommandMap.externalLauncher.
  353. */
  354. String[] parseCommand(String command) {
  355. if (command == null || command.length() < 1)
  356. return null;
  357. if (! command.startsWith("x-java-"))
  358. return new String[] {command, FullMailcapCommandMap.getExternalLauncher()};
  359. else {
  360. int equalsIndex = command.indexOf('=');
  361. if (equalsIndex < 8 || equalsIndex == command.length()-1)
  362. return null;
  363. else return new String[] {command.substring(7,equalsIndex), command.substring(equalsIndex+1)};
  364. }
  365. }
  366. }
  367. }