/bundles/plugins-trunk/SuperAbbrevs/superabbrevs/SuperAbbrevsIO.java

# · Java · 181 lines · 144 code · 29 blank · 8 comment · 13 complexity · accd8a9a0cb942f7d9f09507692b6b28 MD5 · raw file

  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. import org.gjt.sp.util.Log;
  9. public class SuperAbbrevsIO {
  10. private static final String ABBREVS_DIR =
  11. MiscUtilities.constructPath(
  12. jEdit.getSettingsDirectory(),
  13. "SuperAbbrevs");
  14. private static final String MACRO_DIR =
  15. MiscUtilities.constructPath(MiscUtilities.constructPath(
  16. jEdit.getSettingsDirectory(),
  17. "macros"),
  18. "SuperAbbrevs");
  19. private static final String RESOURCE_DIR = "resources";
  20. public static final String ABBREVS_FUNCTION_FILE = "abbrev_functions.bsh";
  21. public static final String TEMPLATE_GENERATION_FUNCTION_FILE =
  22. "template_generation_functions.bsh";
  23. public static final String VARIABLES_FILE = "global.variables";
  24. public static Hashtable readModeFile(String name){
  25. return readObjectFile(getModeFile(name));
  26. }
  27. public static void writeModeFile(String mode,Hashtable data){
  28. File modeFile = getModeFile(mode);
  29. if (data != null && (!data.isEmpty() || modeFile.exists())){
  30. writeObjectFile(modeFile,data);
  31. }
  32. }
  33. private static File getModeFile(String name){
  34. File modeDir = new File(ABBREVS_DIR);
  35. if (!modeDir.exists()){
  36. //make the SuperAbbrev settings dir
  37. modeDir.mkdir();
  38. }
  39. File modeFile =
  40. new File(MiscUtilities.constructPath(modeDir.toString(),name));
  41. return modeFile;
  42. }
  43. private static void copy(URL url, File f) {
  44. try{
  45. InputStream in = url.openStream();
  46. // Create a new file output stream
  47. FileOutputStream out = new FileOutputStream(f);
  48. byte[] buf = new byte[1024];
  49. int i = 0;
  50. while((i=in.read(buf))!=-1) {
  51. out.write(buf, 0, i);
  52. }
  53. in.close();
  54. out.close();
  55. } catch (Exception e) {
  56. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  57. }
  58. }
  59. public static void removeOldMacros(){
  60. File macrosDir = new File(MACRO_DIR);
  61. if (macrosDir.exists()){
  62. File tabFile =
  63. new File(MiscUtilities.constructPath(MACRO_DIR,"tab.bsh"));
  64. tabFile.delete();
  65. File shiftTabFile =
  66. new File(MiscUtilities.constructPath(MACRO_DIR,"shift-tab.bsh"));
  67. shiftTabFile.delete();
  68. macrosDir.delete();
  69. }
  70. }
  71. private static void copyFileFromResourceDir(String filename, boolean override) {
  72. URL url = getResource(RESOURCE_DIR+"/"+filename);
  73. String path = MiscUtilities.constructPath(ABBREVS_DIR,filename);
  74. File file = new File(path);
  75. if (url != null && (override || !file.exists())){
  76. copy(url,file);
  77. }
  78. }
  79. private static void copyFileFromResourceDir(String filename) {
  80. copyFileFromResourceDir(filename,false);
  81. }
  82. public static void writeDefaultAbbrevs(){
  83. Mode[] modes = jEdit.getModes();
  84. for(int i = 0; i < modes.length; i++){
  85. String name = modes[i].getName();
  86. // would be nicer if I knew how to iterate the files in the resource
  87. // directory
  88. copyFileFromResourceDir(name);
  89. }
  90. }
  91. public static void writeDefaultVariables() {
  92. copyFileFromResourceDir(VARIABLES_FILE);
  93. }
  94. public static void writeDefaultAbbrevFunctions(){
  95. copyFileFromResourceDir(ABBREVS_FUNCTION_FILE);
  96. }
  97. public static void writeDefaultTemplateGenerationFunctions(){
  98. copyFileFromResourceDir(TEMPLATE_GENERATION_FUNCTION_FILE,true);
  99. }
  100. public static String getAbbrevsFunctionPath(){
  101. return MiscUtilities.constructPath(ABBREVS_DIR,
  102. TEMPLATE_GENERATION_FUNCTION_FILE);
  103. }
  104. public static String getTemplateGenerationFunctionPath(){
  105. return MiscUtilities.constructPath(ABBREVS_DIR,ABBREVS_FUNCTION_FILE);
  106. }
  107. /**
  108. * Method getResource(String filename)
  109. * Get at resource from the jar file
  110. */
  111. private static URL getResource(String filename) {
  112. return SuperAbbrevsIO.class.getClassLoader().getResource(filename);
  113. }
  114. public static boolean abbrevsDirExists(){
  115. File abbrevsDir = new File(ABBREVS_DIR);
  116. return abbrevsDir.exists();
  117. }
  118. public static void createAbbrevsDir(){
  119. if (!abbrevsDirExists()){
  120. File abbrevsDir = new File(ABBREVS_DIR);
  121. abbrevsDir.mkdir();
  122. }
  123. }
  124. public static Hashtable readObjectFile(File file) {
  125. if (file.exists()){
  126. try{
  127. FileInputStream in = new FileInputStream(file);
  128. ObjectInputStream s = new ObjectInputStream(in);
  129. return (Hashtable)s.readObject();
  130. } catch (FileNotFoundException e){
  131. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  132. } catch (IOException e){
  133. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  134. } catch (ClassNotFoundException e){
  135. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  136. }
  137. }
  138. return null;
  139. }
  140. public static void writeObjectFile(File file, Object data) {
  141. try{
  142. FileOutputStream out = new FileOutputStream(file);
  143. ObjectOutputStream s = new ObjectOutputStream(out);
  144. s.writeObject(data);
  145. s.flush();
  146. }catch (FileNotFoundException e){
  147. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  148. }catch (IOException e){
  149. Log.log(Log.ERROR, SuperAbbrevsIO.class, e);
  150. }
  151. }
  152. }