Java Properties Load from File loadPropertiesFile(File file, boolean critical)

Here you can find the source of loadPropertiesFile(File file, boolean critical)

Description

load Properties File

License

Apache License

Declaration

public static Properties loadPropertiesFile(File file, boolean critical) throws IOException 

Method Source Code


//package com.java2s;
/*//  w w  w.  ja  v a 2s.  c om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.util.Enumeration;

import java.util.Properties;
import java.util.StringTokenizer;

public class Main {
    private static final String INCLUDES_PROPERTY = "${includes}";
    private static final String OPTIONALS_PROPERTY = "${optionals}";

    public static Properties loadPropertiesFile(File file, boolean critical) throws IOException {
        // Read the properties file.
        Properties configProps = new Properties();
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            configProps.load(is);
        } catch (FileNotFoundException ex) {
            if (critical) {
                throw ex;
            }
        } catch (IOException ex) {
            System.err.println("Error loading properties from " + file);
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }

        loadIncludes(INCLUDES_PROPERTY, true, file, configProps);
        loadIncludes(OPTIONALS_PROPERTY, false, file, configProps);

        return configProps;
    }

    private static void loadIncludes(String propertyName, boolean mandatory, File file, Properties configProps)
            throws IOException {
        String includes = configProps.getProperty(propertyName);
        if (includes != null) {
            StringTokenizer st = new StringTokenizer(includes, "\" ", true);
            if (st.countTokens() > 0) {
                String location;
                do {
                    location = nextLocation(st);
                    if (location != null) {
                        File newLocation = new File(location);
                        if (!newLocation.isAbsolute()) {
                            newLocation = new File(file.getParent(), location);
                        }
                        Properties props = loadPropertiesFile(newLocation, true);
                        // included properties do NOT override current values
                        for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
                            String name = (String) e.nextElement();
                            if (!configProps.containsKey(name)) {
                                configProps.put(name, props.getProperty(name));
                            }
                        }
                    }
                } while (location != null);
            }
        }
        configProps.remove(propertyName);
    }

    private static String nextLocation(StringTokenizer st) {
        String retVal = null;

        if (st.countTokens() > 0) {
            String tokenList = "\" ";
            StringBuffer tokBuf = new StringBuffer(10);
            String tok;
            boolean inQuote = false;
            boolean tokStarted = false;
            boolean exit = false;
            while ((st.hasMoreTokens()) && (!exit)) {
                tok = st.nextToken(tokenList);
                if (tok.equals("\"")) {
                    inQuote = !inQuote;
                    if (inQuote) {
                        tokenList = "\"";
                    } else {
                        tokenList = "\" ";
                    }

                } else if (tok.equals(" ")) {
                    if (tokStarted) {
                        retVal = tokBuf.toString();
                        tokStarted = false;
                        tokBuf = new StringBuffer(10);
                        exit = true;
                    }
                } else {
                    tokStarted = true;
                    tokBuf.append(tok.trim());
                }
            }

            // Handle case where end of token stream and
            // still got data
            if ((!exit) && (tokStarted)) {
                retVal = tokBuf.toString();
            }
        }

        return retVal;
    }
}

Related

  1. loadProperties(String propertiesFile)
  2. loadProperties(String propertyFile)
  3. loadProperties(String propertyPath)
  4. loadProperties(String propsFile)
  5. loadProperties(String sFile)
  6. loadPropertiesFile(final File file)
  7. loadPropertiesFile(String fileName)
  8. loadPropertiesFile(String fileName)
  9. loadPropertiesFile(String filePath)