PageRenderTime 118ms CodeModel.GetById 107ms app.highlight 8ms RepoModel.GetById 1ms 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
  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
 22
 23package sessions;
 24
 25
 26import java.io.BufferedReader;
 27import java.io.File;
 28import java.io.FilenameFilter;
 29import java.io.FileReader;
 30import java.io.IOException;
 31import java.util.StringTokenizer;
 32import org.gjt.sp.jedit.MiscUtilities;
 33import org.gjt.sp.util.Log;
 34
 35
 36public class SessionFileConverter implements Runnable
 37{
 38
 39	private String sessionsDir;
 40
 41
 42	SessionFileConverter()
 43	{
 44		sessionsDir = SessionManager.getSessionsDir();
 45	}
 46
 47
 48	public void run()
 49	{
 50		String[] files = getOldSessionFiles();
 51		if(files.length == 0)
 52			return;
 53
 54		for(int i = 0; i < files.length; ++i)
 55			convert(files[i]);
 56	}
 57
 58
 59	private String[] getOldSessionFiles()
 60	{
 61		return new File(sessionsDir).list(new FilenameFilter()
 62		{
 63			public boolean accept(File dir, String name)
 64			{
 65				return name.toLowerCase().endsWith(".session");
 66			}
 67		});
 68	}
 69
 70
 71	private void convert(String filename)
 72	{
 73		Log.log(Log.DEBUG, this, "converting to new session file format: " + filename);
 74
 75		String absoluteFilename = MiscUtilities.constructPath(sessionsDir, filename);
 76		String name = filename.substring(0, filename.length() - 8);
 77		Session session = new Session(name);
 78		String line;
 79
 80		try
 81		{
 82			File absoluteFile = new File(absoluteFilename);
 83			BufferedReader in = new BufferedReader(new FileReader(absoluteFile));
 84			while ((line = in.readLine()) != null)
 85				readOldSessionLine(line, session);
 86			in.close();
 87			session.saveXML();
 88			absoluteFile.delete();
 89		}
 90		catch (IOException io)
 91		{
 92			Log.log(Log.ERROR, this, "IOException during session convert: " + io);
 93		}
 94	}
 95
 96
 97	/**
 98	 * Parse one line from an old-format session file.
 99	 * @param line  the line.
100	 * @param session  where to add the line info to.
101	 */
102	private void readOldSessionLine(String line, Session session)
103	{
104		// handle path:XXX for backwards compatibility
105		// with jEdit 2.2 sessions
106		if (line.startsWith("path:"))
107			line = line.substring(5);
108
109		boolean isCurrent = false;
110		StringTokenizer st = new StringTokenizer(line, "\t");
111		String path = st.nextToken();
112
113		// ignore all tokens except for 'current' to maintain
114		// compatibility with jEdit 2.2 sessions
115		while(st.hasMoreTokens())
116		{
117			String token = st.nextToken();
118			if (token.equals("current"))
119				isCurrent = true;
120		}
121
122		if (path != null)
123		{
124			session.addFile(path);
125			if(isCurrent)
126				session.setCurrentFile(path);
127		}
128	}
129
130
131}
132