Advertisement
Guest User

SnippetGen

a guest
Nov 1st, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.lang.System;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.io.BufferedReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9.  
  10. public class SnippetGenerator
  11. {
  12.     // File to generate snippets from, reads line by line, a line could be SetPlayerHealth(playerid, Float:health), only supports functions currently
  13.     final static String RESOURCE_FILE = "resource.txt";
  14.     final static String OUTPUT_FILE = "output.json";
  15.  
  16.     public static void main(String[] args) throws FileNotFoundException, IOException
  17.     {
  18.         readFile();
  19.     }
  20.  
  21.     public static void readFile() throws FileNotFoundException, IOException
  22.     {
  23.         BufferedReader br = null;
  24.         try {
  25.             br = new BufferedReader(new FileReader(RESOURCE_FILE));
  26.             String line = br.readLine();
  27.             PrintWriter writer = new PrintWriter(OUTPUT_FILE, "UTF-8");
  28.  
  29.             while (line != null)
  30.             {
  31.                 print(line);
  32.                
  33.                 if (line.contains("(") && line.contains(")"))
  34.                 {
  35.                     int argStart = line.indexOf("(");
  36.                     int argEnd = line.indexOf(")");
  37.                     String name = line.substring(0, argStart);
  38.                     String args = line.substring(argStart+1, argEnd);
  39.                    
  40.                     String gen = "\"" + name + "\": {\n" +
  41.                     "\t\"prefix\": \"" + name + "\",\n" +
  42.                     "\t\"body\": \"" + name + "(";
  43.  
  44.                     int argLen = argEnd - argStart;
  45.  
  46.                     print("Name: " + name + " Argend-argstart = " + argLen);
  47.  
  48.                     if (argLen > 1)
  49.                     {
  50.                         String argArray[] = args.split(",");
  51.                         int argIndex = 0;
  52.  
  53.                         for (int i = 0; i < argArray.length; i++)
  54.                         {
  55.                             argIndex++;
  56.                             print(i + ": " + argArray[i]);
  57.                            
  58.                             gen += "${" + argIndex + ":" + argArray[i] + "}";
  59.                            
  60.                             if (i != argArray.length - 1)
  61.                             {
  62.                                 gen += ", ";
  63.                             }
  64.                             else
  65.                             {
  66.                                 gen += ")$0\"";
  67.                                 print("\n");
  68.                             }
  69.                         }
  70.                     }
  71.                     else
  72.                     {
  73.                         gen += ")$0\"";
  74.                     }
  75.                    
  76.                     gen += "\n},";
  77.                     print(gen);
  78.                     writer.println(gen);
  79.                 }
  80.                 else
  81.                 {
  82.                     print("Couldn't generate any snippet from the following line, ( ) are missing.");
  83.                 }
  84.  
  85.                 line = br.readLine();
  86.             }
  87.  
  88.             writer.close();
  89.         }
  90.         catch (FileNotFoundException e)
  91.         {
  92.             print("Couldn't find the file specified, using path: " + RESOURCE_FILE);
  93.         }
  94.         catch (IOException e)
  95.         {
  96.             e.printStackTrace();
  97.             print("Error: " + e.getMessage());
  98.         }
  99.         finally
  100.         {
  101.             if (br != null)
  102.             {
  103.                 br.close();
  104.                 print("BufferedReader successfully closed.");
  105.             }
  106.             else
  107.             {
  108.                 print("BufferedReader is null, will not call the close() function.");
  109.             }
  110.         }
  111.     }
  112.  
  113.     public static void print(String message)
  114.     {
  115.         Calendar cal = Calendar.getInstance();
  116.         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  117.         System.out.println("[SnippetGen " + sdf.format(cal.getTime()) + "] " + message);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement