Java Properties Load from File loadErrorMap(InputStream resourceStream)

Here you can find the source of loadErrorMap(InputStream resourceStream)

Description

Loads the mapping from error codes to error messages from a properties file.

License

Apache License

Parameter

Parameter Description

Return

the map that maps error codes (integers) to error message templates (strings).

Declaration

public static Map<Integer, String> loadErrorMap(InputStream resourceStream) throws IOException 

Method Source Code

//package com.java2s;
/*//from   www  .  ja v a 2s .co 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.IOException;
import java.io.InputStream;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Main {
    private static final String COMMA = ",";

    /**
     * Loads the mapping from error codes to error messages from a properties file.
     * The key of properties is in the form of comma-separated error codes and the value is the corresponding
     * error message template.
     *
     * Example entries in the properties file:
     * 4002=Extension Conflict between %1$s and %2$s both extensions extend %3$s
     * 2,1002 = Type mismatch: function %1$s expects its %2$s input parameter to be type %3$s
     *
     * @param resourceStream,
     *            the input stream of the properties file
     * @return the map that maps error codes (integers) to error message templates (strings).
     */
    public static Map<Integer, String> loadErrorMap(InputStream resourceStream) throws IOException {
        Properties prop = new Properties();
        Map<Integer, String> errorMessageMap = new HashMap<>();
        prop.load(resourceStream);
        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
            String key = (String) entry.getKey();
            String msg = (String) entry.getValue();
            if (key.contains(COMMA)) {
                String[] codes = key.split(COMMA);
                for (String code : codes) {
                    errorMessageMap.put(Integer.parseInt(code), msg);
                }
            } else {
                errorMessageMap.put(Integer.parseInt(key), msg);
            }
        }
        return errorMessageMap;
    }
}

Related

  1. loadDefault(String _file_path)
  2. loadDefaultConfiguration()
  3. loadDefaultProps(Properties deployProps)
  4. LoadDeployedObjects(String outputDirectory)
  5. loadEnv(final String configFilePath)
  6. loadEscaping(Properties prop, InputStream stream)
  7. loadFixedCommits()
  8. loadFixture(String fixtureId)
  9. loadGradleResource(String fileName)