- package osm.jp.postgis;
- import java.io.*;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import jp.co.areaweb.tools.database.*;
-
- public class Do_sqlfiles {
- static String sqlFileName = "busstop.sql";
-
- /**
- * メイン
- *
- * java -cp .:SelectBusstop.jar:hayashi_0225.jar:postgresql-9.4.1212.jar osm.jp.SelectBusstop
- *
- *
- * @throws IOException
- * @throws SQLException
- * @throws ClassNotFoundException
- * @throws FileNotFoundException
- * @throws TransformerException
- * @throws SAXException
- * @throws ParserConfigurationException */
- public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException
- {
- for (int i=0; i < args.length; i++) {
- if (args[i].equals("-update")) {
- sqlFileName = "update.sql";
- }
- }
-
- Connection con = DatabaseTool.openDb("postgis");
-
- try {
- /**
- * 都道府県ごとのGMLディレクトリの処理
- */
- File dir = new File(".");
- File[] dirs = dir.listFiles();
- for (File iDir : dirs) {
- if (checkGMLdir(iDir)) {
- System.out.println(iDir.getName());
-
- File[] files = iDir.listFiles();
- for (File iFile : files) {
- if (checkFile(iFile)) {
- System.out.println(iFile.getAbsoluteFile());
- Do_sqlfiles.sqlExecute(con, iFile);
- }
- }
- }
- }
- }
- finally {
- DatabaseTool.closeDb(con);
- }
- }
-
- /**
- * sqlFileを実行する
- * @param conn データベースコネクション
- * @param sqlFile 実行するSQLファイル
- * @throws SQLException SQL実行エラー
- * @throws IOException
- */
- public static void sqlExecute(Connection conn, File sqlFile) throws SQLException, IOException {
- // ローカルデータベース内の情報をPostGIS用の「busstop.sql」に出力する
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sqlFile), "UTF-8"));
-
- // 既存のreadLine()では\rまたは\n、および\r\nにて改行とみなしている。
- String strLine = null;
- while ((strLine = br.readLine()) != null) { // null=ファイルの最後
- sqlExecute(conn, strLine);
- }
- sqlExecute(conn, "commit;");
- br.close();
- }
-
- /**
- * sqlStrを実行する
- * @param conn データベースコネクション
- * @param sqlStr 実行するSQL文
- * @throws SQLException SQL実行エラー
- */
- public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {
- if ((sqlStr != null) && (!sqlStr.trim().equals(""))) {
- System.out.println(sqlStr);
- Statement stat = conn.createStatement();
- stat.execute(sqlStr);
- stat.close();
- }
- }
-
- /**
- * 数値地図情報のデータファイルかどうかを見極める
- * @param f
- * @return
- */
- static boolean checkGMLdir(File f) {
- if (!f.isDirectory()) {
- return false;
- }
- String name = f.getName();
- if (!name.startsWith(GML_DIR_PREFIX)) {
- return false;
- }
- if (!name.toUpperCase().endsWith(GML_DIR_PRIFIX)) {
- return false;
- }
- return true;
- }
-
- public static final String GML_DIR_PREFIX = "P11-10_";
- public static final String GML_DIR_PRIFIX = "_GML";
-
- /**
- * SQLファイルかどうかを見極める
- * @param f
- * @return
- */
- static boolean checkFile(File f) {
- String name = f.getName();
- if (name.equals(sqlFileName)) {
- return true;
- }
- return false;
- }
-
- }