📄 textcache.java
字号:
/* Copyright (c) 2001-2002, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.io.IOException;import java.io.File;import java.sql.SQLException;/** * Handles operations on a DatabaseFile object and uses signle * TextDdatbaseRowInput and TextDatabaseRowOutput objects to read and write * rows of data to the file in text table format. * * @author sqlbob@users (RMP) * @version 1.7.0 */class TextCache extends org.hsqldb.Cache { public static final String NL = System.getProperty("line.separator"); private String fs; private String vs; private String lvs; private DatabaseRowOutputInterface out; protected boolean readOnly; protected TextDatabaseRowInput in; protected boolean ignoreFirst; protected String ignoredFirst = NL; private class TextSource { private String source; TextSource(String s) { source = s; } String getAttr(String id, String ret) { id = ";" + id + "="; int len = id.length(); int start; int end; if ((start = source.indexOf(id)) != -1) { start += len; if ((end = source.indexOf(";", start)) == -1) { end = source.length(); } ret = source.substring(start, end); source = source.substring(0, start - len) + source.substring(end); } return (ret); } } /** * TextCache constructor declaration <P> * * The cache constructor sets up the initial parameters of the cache * object, setting the name used for the file, etc. * * @param name of database file * @param propPrefix prefix for relevant properties * @param props Description of the Parameter * @exception SQLException Description of the Exception */ TextCache(String name, String propPrefix, HsqlDatabaseProperties props) throws SQLException { super("", props); TextSource textSource = new TextSource(name); //-- Get separators: fs = translateSep(textSource.getAttr("fs", null)); vs = textSource.getAttr("vs", fs); if (vs != fs) { vs = translateSep(vs); } String lvs = textSource.getAttr("lvs", fs); if (lvs != fs) { lvs = translateSep(lvs); } if (fs == null) { fs = translateSep(props.getProperty(propPrefix + "fs"), true); if (fs == null) { fs = ","; } } if (vs == null) { vs = props.getProperty(propPrefix + "vs", fs); if (vs != fs) { vs = translateSep(vs, true); } } if (lvs == null) { lvs = props.getProperty(propPrefix + "lvs", fs); if (lvs != fs) { lvs = translateSep(lvs, true); } } //-- Get boolean settings: String skipFirst = textSource.getAttr("ignore_first", null); if (skipFirst == null) { skipFirst = props.getProperty(propPrefix + "ignore_first", "false"); } ignoreFirst = skipFirst.equals("true"); String quoted = textSource.getAttr("quoted", null); if (quoted == null) { quoted = props.getProperty(propPrefix + "quoted", "true"); } String emptyIsNull = textSource.getAttr("empty_is_null", null); if (emptyIsNull == null) { emptyIsNull = props.getProperty(propPrefix + "empty_is_null", "true"); } // Get file name sName = textSource.source; if (sName.endsWith(";")) { sName = sName.substring(0, sName.length() - 1); } try { if (quoted.equals("true")) { in = new QuotedTextDatabaseRowInput( fs, vs, lvs, emptyIsNull.equals("true")); out = new QuotedTextDatabaseRowOutput(fs, vs, lvs); } else { in = new TextDatabaseRowInput(fs, vs, lvs, emptyIsNull.equals("true")); out = new TextDatabaseRowOutput(fs, vs, lvs); } } catch (IOException e) { throw (Trace.error(Trace.FILE_IO_ERROR, "invalid separator(s):" + e)); } } private String translateSep(String sep) { return (translateSep(sep, false)); } private String translateSep(String sep, boolean isProperty) { if (sep == null) { return (null); } int next = 0; if ((next = sep.indexOf('\\')) != -1) { int start = 0; char sepArray[] = sep.toCharArray(); char ch = 0; int len = sep.length(); StringBuffer realSep = new StringBuffer(len); do { realSep.append(sepArray, start, next - start); start = ++next; if (next >= len) { realSep.append('\\'); break; } if (!isProperty) { ch = sepArray[next]; } if (ch == 'n') { realSep.append('\n'); start++; } else if (ch == 'r') { realSep.append('\r'); start++; } else if (ch == 't') { realSep.append('\t'); start++; } else if (ch == '\\') { realSep.append('\\'); start++; } else if (ch == 'u') { start++; realSep.append( (char) Integer.parseInt( sep.substring(start, start + 4), 16)); start += 4; } else if (sep.startsWith("semi", next)) { realSep.append(';'); start += 4; } else if (sep.startsWith("space", next)) { realSep.append(' '); start += 5; } else if (sep.startsWith("quote", next)) { realSep.append('\"'); start += 5; } else if (sep.startsWith("apos", next)) { realSep.append('\''); start += 4; } else { realSep.append('\\'); realSep.append(sepArray[next]); start++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -