Java Properties Load from File loadIncludes(String propertyName, boolean mandatory, File file, Properties configProps)

Here you can find the source of loadIncludes(String propertyName, boolean mandatory, File file, Properties configProps)

Description

load Includes

License

Apache License

Declaration

private static void loadIncludes(String propertyName, boolean mandatory, File file, Properties configProps)
            throws IOException 

Method Source Code


//package com.java2s;
/*/*  www .  j  a v  a 2s.c  o  m*/
 * 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}";

    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;
    }

    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;
    }
}

Related

  1. loadEscaping(Properties prop, InputStream stream)
  2. loadFixedCommits()
  3. loadFixture(String fixtureId)
  4. loadGradleResource(String fileName)
  5. loadHeader(String fileName)
  6. loadInputAsString(InputStream is)
  7. loadInstallProps()
  8. loadJZ(File file)
  9. loadKeys(String propertiesFileName, InputStream in)