org.kisoonlineapp.startup.StartupSqlReader.java Source code

Java tutorial

Introduction

Here is the source code for org.kisoonlineapp.startup.StartupSqlReader.java

Source

/*
 * Copyright 2016 berni.
 *
 * 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 org.kisoonlineapp.startup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author berni
 */
public class StartupSqlReader {

    private final Map<String, String> statementMap = new HashMap<>();

    public StartupSqlReader(InputStream is) throws IOException {
        readFromInputStream(is);
    }

    final void readFromInputStream(InputStream is) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try {
            String key = null;
            StringBuilder value = new StringBuilder();
            for (String line; (line = br.readLine()) != null;) {

                if (StringUtils.startsWithAny(line, new String[] { "#", "---" })) {
                    continue;
                }
                line = StringUtils.removeEnd(line, "\\");
                if (StringUtils.contains(line, '=')) {
                    if (key == null) {
                        String[] splitted = StringUtils.split(line, "=", 2);
                        if (splitted.length == 2) {
                            key = splitted[0];
                            value.append(splitted[1]);
                        } else if (splitted.length == 1) {
                            key = splitted[0];
                            value.append("");
                        }
                    } else {
                        value.append(line);
                    }
                } else if (StringUtils.isBlank(line)) {
                    if (key != null) {
                        this.statementMap.put(key, value.toString());
                        key = null;
                        value = new StringBuilder();
                    }
                } else {
                    value.append(line);
                }
            }
            if (key != null) {
                this.statementMap.put(key, value.toString());
            }

        } finally {
            br.close();
        }
    }

    String lookupStatementByName(String key) {
        String v = this.statementMap.get(key);
        return v;
    }

    Map<String, String> getMap() {
        return Collections.unmodifiableMap(statementMap);
    }
}