/plugins/SuperAbbrevs/tags/jedit_43pre3-beta012/superabbrevs/SuperAbbrevsIO.java

#
Java | 152 lines | 118 code | 24 blank | 10 comment | 15 complexity | b57e4f42d6c1f63305fe062d31f47068 MD5 | raw file

✨ Summary
  1. package superabbrevs;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.net.*;
  5. import org.gjt.sp.jedit.MiscUtilities;
  6. import org.gjt.sp.jedit.jEdit;
  7. import org.gjt.sp.jedit.Mode;
  8. public class SuperAbbrevsIO {
  9. private static final String ABBREVS_DIR =
  10. MiscUtilities.constructPath(
  11. jEdit.getSettingsDirectory(),
  12. "SuperAbbrevs");
  13. private static final String MACRO_DIR =
  14. MiscUtilities.constructPath(MiscUtilities.constructPath(
  15. jEdit.getSettingsDirectory(),
  16. "macros"),
  17. "SuperAbbrevs");
  18. private static final String ABBREV_FUNCTIONS = "abbrev_functions.bsh";
  19. public static Hashtable readAbbrevs(String mode){
  20. File modeFile = getModeFile(mode);
  21. try{
  22. FileInputStream in = new FileInputStream(modeFile);
  23. ObjectInputStream s = new ObjectInputStream(in);
  24. return (Hashtable)s.readObject();
  25. }catch (FileNotFoundException e){
  26. //TODO log
  27. }catch (IOException e){
  28. //TODO log
  29. }catch (ClassNotFoundException e){
  30. //TODO log
  31. }
  32. return null;
  33. }
  34. public static void write(String mode,Hashtable abbrevs){
  35. File modeFile = getModeFile(mode);
  36. if (abbrevs != null && (!abbrevs.isEmpty() || modeFile.exists())){
  37. try{
  38. FileOutputStream out = new FileOutputStream(modeFile);
  39. ObjectOutputStream s = new ObjectOutputStream(out);
  40. s.writeObject(abbrevs);
  41. s.flush();
  42. }catch (FileNotFoundException e){
  43. //TODO log
  44. }catch (IOException e){
  45. //TODO log
  46. }
  47. }
  48. }
  49. private static File getModeFile(String mode){
  50. String configDir = jEdit.getSettingsDirectory();
  51. File modeDir =
  52. new File(MiscUtilities.constructPath(configDir,"SuperAbbrevs"));
  53. if (!modeDir.exists()){
  54. //TODO make defaults
  55. //make the SuperAbbrev settings dir
  56. modeDir.mkdir();
  57. }
  58. File modeFile =
  59. new File(MiscUtilities.constructPath(modeDir.toString(),mode));
  60. return modeFile;
  61. }
  62. private static void copy(URL url, File f) {
  63. try{
  64. InputStream in = url.openStream();
  65. // Create a new file output stream
  66. FileOutputStream out = new FileOutputStream(f);
  67. byte[] buf = new byte[1024];
  68. int i = 0;
  69. while((i=in.read(buf))!=-1) {
  70. out.write(buf, 0, i);
  71. }
  72. in.close();
  73. out.close();
  74. } catch (Exception e) {
  75. //TODO log
  76. System.out.println("WriteToFile: File error: "+e.getMessage());
  77. }
  78. }
  79. public static void removeOldMacros(){
  80. File macrosDir = new File(MACRO_DIR);
  81. if (macrosDir.exists()){
  82. File tabFile =
  83. new File(MiscUtilities.constructPath(MACRO_DIR,"tab.bsh"));
  84. tabFile.delete();
  85. File shiftTabFile =
  86. new File(MiscUtilities.constructPath(MACRO_DIR,"shift-tab.bsh"));
  87. shiftTabFile.delete();
  88. macrosDir.delete();
  89. }
  90. }
  91. public static void writeDefaultAbbrevs(){
  92. File abbrevsDir = new File(ABBREVS_DIR);
  93. if (!abbrevsDir.exists()){
  94. abbrevsDir.mkdir();
  95. }
  96. Mode[] modes = jEdit.getModes();
  97. for(int i = 0; i < modes.length; i++){
  98. String name = modes[i].getName();
  99. URL url = SuperAbbrevsIO.class.getClassLoader().getResource("abbrevs/"+name+".abbr");
  100. File abbrevsFile = new File(MiscUtilities.constructPath(ABBREVS_DIR,name));
  101. if (url != null && !abbrevsFile.exists()){
  102. copy(url,abbrevsFile);
  103. }
  104. }
  105. }
  106. public static void writeDefaultAbbrevFunctions(){
  107. // the abbrevs dir is created by the writeDefaultAbbrevs function
  108. File abbrevsDir = new File(ABBREVS_DIR);
  109. URL url = SuperAbbrevsIO.class.getClassLoader().getResource(ABBREV_FUNCTIONS);
  110. File abbrevFunctionsFile = new File(MiscUtilities.constructPath(ABBREVS_DIR,ABBREV_FUNCTIONS));
  111. if (url != null && !abbrevFunctionsFile.exists()){
  112. copy(url,abbrevFunctionsFile);
  113. }
  114. }
  115. public static String getGlobalFunctionPath(){
  116. return MiscUtilities.constructPath(ABBREVS_DIR,ABBREV_FUNCTIONS);
  117. }
  118. public static boolean abbrevsDirExists(){
  119. File abbrevsDir = new File(ABBREVS_DIR);
  120. return abbrevsDir.exists();
  121. }
  122. public static void createAbbrevsDir(){
  123. if (!abbrevsDirExists()){
  124. File abbrevsDir = new File(ABBREVS_DIR);
  125. abbrevsDir.mkdir();
  126. }
  127. }
  128. }