From: SVN by r. <sv...@ca...> - 2008-08-19 20:30:47
|
Author: roy Date: 2008-08-19 22:30:37 +0200 (Tue, 19 Aug 2008) New Revision: 291 Added: src/main/java/nl/improved/sqlclient/history/ src/main/java/nl/improved/sqlclient/history/HistoryPersister.java src/main/java/nl/improved/sqlclient/history/exception/ src/main/java/nl/improved/sqlclient/history/exception/CouldNotLoadHistoryException.java src/main/java/nl/improved/sqlclient/history/exception/CouldNotSaveHistoryException.java src/main/java/nl/improved/sqlclient/history/scrapbook.jpage Log: history persister classes created by Alarc?\195?\179n Vladimir <vla...@ya...> small adjustments to make it compatible with the current svn version made by me (roy van der kuil) Added: src/main/java/nl/improved/sqlclient/history/HistoryPersister.java =================================================================== --- src/main/java/nl/improved/sqlclient/history/HistoryPersister.java 2008-08-19 11:23:25 UTC (rev 290) +++ src/main/java/nl/improved/sqlclient/history/HistoryPersister.java 2008-08-19 20:30:37 UTC (rev 291) @@ -0,0 +1,209 @@ +/* + * Copyright 2008 Alarcón Vladimir <vla...@ya...> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.improved.sqlclient.history; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import nl.improved.sqlclient.AbstractSQLShellWindow; +import nl.improved.sqlclient.SQLCommand; +import nl.improved.sqlclient.SQLShell; +import nl.improved.sqlclient.history.exception.CouldNotLoadHistoryException; +import nl.improved.sqlclient.history.exception.CouldNotSaveHistoryException; + +/** + * Class for handling the persistance of the command history. + */ +public class HistoryPersister { + + /** + * Base path file name. + */ + private final static String BASE_PATH = "history"; + + /** + * Default empty command. + */ + private final static SQLCommand EMPTY_COMMAND = new SQLCommand(); + + /** + * I use "Half Width white circle" as an encoded new line. + */ + private static char ENCODED_NEW_LINE = '\u0009'; + + /** + * Load command history into the sqlshell. + * @param shell the shell to load the history into + * @param key the key to load the history for (connection identifier) + * @param firstLineWith + * @param nextLineWidth + */ + public static int loadHistory(final AbstractSQLShellWindow shell, final String key, + final int firstLineWidth, final int nextLineWidth) + throws CouldNotLoadHistoryException { + + List<SQLCommand> history = shell.getCommandHistory(); + history.clear(); + + log("loading history..."); + String fileName = buildHistoryFileName(key); + File f = new File(fileName); + log("loading history... file=" + f.getAbsolutePath()); + try { + BufferedReader r = new BufferedReader(new FileReader(fileName)); + log("ok 1"); + String line = null; + int tot = 0; + while ((line = r.readLine()) != null) { + if (line.trim().length() != 0) { + tot++; + log("ok tot=" + tot + " line=" + line); + SQLCommand sc = parseCommand(line, firstLineWidth, nextLineWidth); + history.add(sc); + } + } + log("ok 2"); + r.close(); + shell.setCommandIndex(history.size() - 1); + return tot; + } catch (IOException e) { + history.add(EMPTY_COMMAND); + shell.setCommandIndex(0); + throw new CouldNotLoadHistoryException(e); + } + } + + /** + * Save command history from sqlshell. + * @param shell the shell to save the history from + * @param key the key to store the history for (connection identifier) + * @param excludeLastCommand + */ + public static void saveHistory(final AbstractSQLShellWindow shell, final String key, + final boolean excludeLastCommand) throws CouldNotSaveHistoryException { + List<SQLCommand> history = shell.getCommandHistory(); + int commandIndex = shell.getCommandIndex(); + + log("saving history... (" + history.size() + " commands)"); + + File dir = new File(buildHistoryDir()); + if (!dir.exists()) { + dir.mkdirs(); + } + String fileName = buildHistoryFileName(key); + try { + File f = new File(fileName); + log("file=" + f.getAbsolutePath()); + BufferedWriter w = new BufferedWriter(new FileWriter(f)); + int tot = 0; + for (Iterator it = history.iterator(); it.hasNext();) { + SQLCommand c = (SQLCommand) it.next(); + if (excludeLastCommand && it.hasNext()) { + tot++; + w.write(renderCommand(c)); + w.write("\n"); + } + } + w.close(); + log("history size saved: " + tot); + } catch (IOException e) { + throw new CouldNotSaveHistoryException(e); + } + } + + + private static String renderCommand(final SQLCommand c) { + + // First, I filter: I only leave non-blank lines. + List<String> cleanLines = new ArrayList<String>(); + for (CharSequence line : c.getLines()) { + if (line != null) { + String tLine = line.toString().trim(); + if (tLine.length() > 0) { + cleanLines.add(tLine); + } + } + } + + // Then, I store the lines, adding an encoded newline. + StringBuffer sb = new StringBuffer(); + for (Iterator it = cleanLines.iterator(); it.hasNext();) { + String line = (String) it.next(); + sb.append(line); + if (it.hasNext()) { + sb.append(ENCODED_NEW_LINE); + } + } + + return sb.toString(); + } + + private static SQLCommand parseCommand(final String s, + final int firstLineWidth, final int nextLineWidth) { + SQLCommand c = new SQLCommand(); + List<StringBuffer> lines = c.getEditableLines(); // TODO change to not editable lines + + String[] chunks = s.split("" + ENCODED_NEW_LINE); + for (int i = 0; i < chunks.length; i++) { + log("chunk " + i + "='" + chunks[i] + "'"); + int width = (i == 0) ? firstLineWidth : nextLineWidth; + fit(chunks[i], lines, width); + } + return c; + } + + private static void fit(final String s, final List<StringBuffer> lines, + final int width) { + int pos = 0; + while ((s.length() - pos) > width) { + int sep = s.lastIndexOf(" ", pos + width); + if (sep > pos) { + log("*** SECTION adding chunk '" + s.substring(pos, sep) + "' pos=" + + pos + " sep=" + sep); + lines.add(new StringBuffer(s.substring(pos, sep))); + pos = sep + 1; + } else { + sep = pos + width; + log("*** NO-SEP adding chunk '" + s.substring(pos, sep) + "' pos=" + + pos + " sep=" + sep); + lines.add(new StringBuffer(s.substring(pos, sep))); + pos = sep; + } + } + log("*** END adding chunk '" + s.substring(pos) + "' pos=" + pos + + " length=" + s.length()); + lines.add(new StringBuffer(s.substring(pos))); + } + + private static String buildHistoryDir() { + return BASE_PATH; + } + + private static String buildHistoryFileName(final String key) { + return BASE_PATH + "/" + key + ".history"; + } + + private static void log(final String txt) { +// System.out.println(txt); + } +} Added: src/main/java/nl/improved/sqlclient/history/exception/CouldNotLoadHistoryException.java =================================================================== --- src/main/java/nl/improved/sqlclient/history/exception/CouldNotLoadHistoryException.java 2008-08-19 11:23:25 UTC (rev 290) +++ src/main/java/nl/improved/sqlclient/history/exception/CouldNotLoadHistoryException.java 2008-08-19 20:30:37 UTC (rev 291) @@ -0,0 +1,55 @@ +/* + * Copyright 2008 Alarcón Vladimir <vla...@ya...> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.improved.sqlclient.history.exception; + +/** + * Exception thrown when the command history could not be loaded. + */ +public class CouldNotLoadHistoryException extends Exception { + + /** + * Default constructor. + */ + public CouldNotLoadHistoryException() { + super(); + } + + /** + * Constructor. + * @param message the reason this exception whas thrown + * @param cause the root cause of this exception + */ + public CouldNotLoadHistoryException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor. + * @param message the reason this exception whas thrown + */ + public CouldNotLoadHistoryException(String message) { + super(message); + } + + /** + * Constructor. + * @param cause the root cause of this exception + */ + public CouldNotLoadHistoryException(Throwable cause) { + super(cause); + } + +} Added: src/main/java/nl/improved/sqlclient/history/exception/CouldNotSaveHistoryException.java =================================================================== --- src/main/java/nl/improved/sqlclient/history/exception/CouldNotSaveHistoryException.java 2008-08-19 11:23:25 UTC (rev 290) +++ src/main/java/nl/improved/sqlclient/history/exception/CouldNotSaveHistoryException.java 2008-08-19 20:30:37 UTC (rev 291) @@ -0,0 +1,55 @@ +/* + * Copyright 2008 Alarcón Vladimir <vla...@ya...> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.improved.sqlclient.history.exception; + +/** + * Exception thrown when the command history could not be saved. + */ +public class CouldNotSaveHistoryException extends Exception { + + /** + * Default constructor. + */ + public CouldNotSaveHistoryException() { + super(); + } + + /** + * Constructor. + * @param message the reason this exception whas thrown + * @param cause the root cause of this exception + */ + public CouldNotSaveHistoryException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor. + * @param message the reason this exception whas thrown + */ + public CouldNotSaveHistoryException(String message) { + super(message); + } + + /** + * Constructor. + * @param cause the root cause of this exception + */ + public CouldNotSaveHistoryException(Throwable cause) { + super(cause); + } + +} Added: src/main/java/nl/improved/sqlclient/history/scrapbook.jpage =================================================================== --- src/main/java/nl/improved/sqlclient/history/scrapbook.jpage 2008-08-19 11:23:25 UTC (rev 290) +++ src/main/java/nl/improved/sqlclient/history/scrapbook.jpage 2008-08-19 20:30:37 UTC (rev 291) @@ -0,0 +1,13 @@ + +java.io.BufferedReader r = new java.io.BufferedReader(new java.io.FileReader("history/one.properties")); + System.out.println("ok 1"); + String line = null; + int tot = 0; + while ((line = r.readLine()) != null) { + if (line.trim().length() != 0) { + tot++; + System.out.println("ok tot="+tot); + } + } + + \ No newline at end of file |