All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.almis.awe.developer.util.LocaleUtil Maven / Gradle / Ivy

There is a newer version: 4.9.4
Show newest version
package com.almis.awe.developer.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utilities related to locale files
 */
@Slf4j
public final class LocaleUtil {

  // Static variables
  private static String extensionXML;
  private static String localeFileName;

  /**
   * Hide the constructor
   */
  private LocaleUtil() {
  }

  /**
   * Initialize Utility class
   *
   * @param springEnvironment Spring environment
   */
  public static void init(Environment springEnvironment) {
    Environment environment = springEnvironment;
    extensionXML = environment.getProperty("extensions.xml", ".xml");
    localeFileName = environment.getProperty("application.files.locale", "Locale-");
  }

  /**
   * Given a path, obtains the list of languages whose locale file is defined
   *
   * @param path Path
   * @return Language list
   */
  public static List getLanguageList(String path) {
    ArrayList languages = new ArrayList<>();

    try {
      File folder = new File(path);
      if (folder.exists() && folder.isDirectory()) {

        String patternString = localeFileName + "([a-zA-Z]+)" + extensionXML;
        final Pattern pattern = Pattern.compile(patternString);
        String[] files = folder.list((File dir, String name) -> pattern.matcher(name).matches());
        for (String file : files) {
          Matcher matcher = pattern.matcher(file);
          if (matcher.find()) {
            languages.add(matcher.group(1));
          }
        }
      }

    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }

    return languages;
  }

  /**
   * Print a XML header into a fileoutputstream
   *
   * @param out    Output Stream
   * @param doc    Document name
   * @param des    Document description
   * @param addHdg
   * @throws IOException
   */
  public static void printHeader(Writer out, String doc, String des, boolean addHdg) throws IOException {
    String strOut = "";
    strOut += "\n";
    if (addHdg) {
      strOut += "\n\n";
    }
    // Add SVN id property to be expanded when comitting a file
    strOut += "\n\n";

    out.write(strOut);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy