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

/bundles/plugins-trunk/Sessions/sessions/SessionFileConverter.java

#
Java | 132 lines | 77 code | 26 blank | 29 comment | 10 complexity | 7823a760f565ed07d0782f69ae2ad6cd MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * SessionFileConverter.java - convert session files from old format to XML
  3. * Copyright (c) 2001 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package sessions;
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FilenameFilter;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.util.StringTokenizer;
  28. import org.gjt.sp.jedit.MiscUtilities;
  29. import org.gjt.sp.util.Log;
  30. public class SessionFileConverter implements Runnable
  31. {
  32. private String sessionsDir;
  33. SessionFileConverter()
  34. {
  35. sessionsDir = SessionManager.getSessionsDir();
  36. }
  37. public void run()
  38. {
  39. String[] files = getOldSessionFiles();
  40. if(files.length == 0)
  41. return;
  42. for(int i = 0; i < files.length; ++i)
  43. convert(files[i]);
  44. }
  45. private String[] getOldSessionFiles()
  46. {
  47. return new File(sessionsDir).list(new FilenameFilter()
  48. {
  49. public boolean accept(File dir, String name)
  50. {
  51. return name.toLowerCase().endsWith(".session");
  52. }
  53. });
  54. }
  55. private void convert(String filename)
  56. {
  57. Log.log(Log.DEBUG, this, "converting to new session file format: " + filename);
  58. String absoluteFilename = MiscUtilities.constructPath(sessionsDir, filename);
  59. String name = filename.substring(0, filename.length() - 8);
  60. Session session = new Session(name);
  61. String line;
  62. try
  63. {
  64. File absoluteFile = new File(absoluteFilename);
  65. BufferedReader in = new BufferedReader(new FileReader(absoluteFile));
  66. while ((line = in.readLine()) != null)
  67. readOldSessionLine(line, session);
  68. in.close();
  69. session.saveXML();
  70. absoluteFile.delete();
  71. }
  72. catch (IOException io)
  73. {
  74. Log.log(Log.ERROR, this, "IOException during session convert: " + io);
  75. }
  76. }
  77. /**
  78. * Parse one line from an old-format session file.
  79. * @param line the line.
  80. * @param session where to add the line info to.
  81. */
  82. private void readOldSessionLine(String line, Session session)
  83. {
  84. // handle path:XXX for backwards compatibility
  85. // with jEdit 2.2 sessions
  86. if (line.startsWith("path:"))
  87. line = line.substring(5);
  88. boolean isCurrent = false;
  89. StringTokenizer st = new StringTokenizer(line, "\t");
  90. String path = st.nextToken();
  91. // ignore all tokens except for 'current' to maintain
  92. // compatibility with jEdit 2.2 sessions
  93. while(st.hasMoreTokens())
  94. {
  95. String token = st.nextToken();
  96. if (token.equals("current"))
  97. isCurrent = true;
  98. }
  99. if (path != null)
  100. {
  101. session.addFile(path);
  102. if(isCurrent)
  103. session.setCurrentFile(path);
  104. }
  105. }
  106. }