Package l2p.extensions.multilang

Source Code of l2p.extensions.multilang.ASCIIBuilder

package l2p.extensions.multilang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;

import javolution.text.TextBuilder;
import l2p.Config;

/**
* @Author: Death
* @Date: 25/9/2007
* @Time: 18:07:24
* <p/>
* Переводит класс properties из UTF-8 в ASCII
*/
public class ASCIIBuilder
{
  private static final String targetDir = Config.DATAPACK_ROOT + "/data/localization/ascii";
  private static final String encoding = "latin1";

  public static void createPropASCII(File f)
  {
    HashMap<String, String> map = new HashMap<String, String>();
    try
    {
      LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
      TextBuilder tb = new TextBuilder();
      String s;
      while((s = lnr.readLine()) != null)
      {
        tb.append(s);
        if(!tb.toString().endsWith("\\"))
        {
          String buf = tb.toString().replace("\\", "");
          String[] q = new String[2];
          int index = buf.indexOf("=");
          q[0] = buf.substring(0, index);
          q[1] = buf.substring(index + 1);
          map.put(q[0], q[1]);
          tb.clear();
        }
      }
      lnr.close();
    }
    catch(Exception e)
    {
      e.printStackTrace(System.err);
    }
    for(String s : map.keySet())
    {
      map.put(s, convertString(map.get(s)));
    }
    File dir = new File(targetDir);
    if(dir.exists())
    {
      dir.delete();
    }
    dir.mkdir();
    File target = new File(targetDir + "/" + f.getName());
    if(!target.exists())
    {
      try
      {
        target.createNewFile();
      }
      catch(IOException e)
      {
        e.printStackTrace(System.err);
      }
    }
    try
    {
      // Изврат, т.к. properties.store() работает криво.
      FileOutputStream fos = new FileOutputStream(target);
      for(String q : map.keySet())
      {
        fos.write(q.getBytes(encoding));
        fos.write("=".getBytes(encoding));
        fos.write(map.get(q).getBytes(encoding));
        fos.write("\n".getBytes(encoding));
      }
      fos.close();
    }
    catch(IOException e)
    {
      e.printStackTrace(System.err);
    }
  }

  private static String convertString(String s)
  {
    TextBuilder tb = new TextBuilder();
    for(char c : s.toCharArray())
    {
      if(c > 127) // Первые 128 символов в ASCII пишем по человечески
      {
        tb.append("\\u");
        String hex = Integer.toHexString(c);
        int lenght = hex.length();
        for(int i = lenght; i < 4; i++)
        {
          tb.append("0");
        }
        tb.append(hex);
      }
      else
      {
        tb.append(c);
      }
    }
    return tb.toString();
  }
}
TOP

Related Classes of l2p.extensions.multilang.ASCIIBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.