Cod sursa(job #1346044)

Utilizator valentin_radValentin Radulescu valentin_rad Data 17 februarie 2015 23:49:14
Problema Convertor Scor 30
Compilator java Status done
Runda rosedu_cdl_2015 Marime 3.18 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;

class Main {
  
  public static StringBuilder buffer = new StringBuilder();
  public static boolean parseKeyList = true;
  public static PrintWriter writer;

  public static void main(String[] args) throws FileNotFoundException {
    long start = System.currentTimeMillis();
    String line;
    writer = new PrintWriter("convertor.out");
    
    try (BufferedReader br = new BufferedReader(new FileReader("convertor.in"))) {
      while((line = br.readLine()) != null) {
        parseString(line);
      }
      br.close();
    } catch (Exception e) {
      System.out.println("Something went wrong!");
      System.out.println(e.toString());
    }
    
    writer.close();
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }
  
  public static void parseString(String line) {
    int objectStart, objectEnd;
    
    buffer.append(line);
    
    while(containsJsonObject(buffer)) {
      objectStart = buffer.indexOf("{");
      objectEnd = buffer.indexOf("}");
      parseObject(
        buffer.substring(objectStart + 1, objectEnd),
        parseKeyList
      );
      buffer.delete(0, objectEnd + 1);
      parseKeyList = false;
    }
  } 
  
  public static boolean containsJsonObject(StringBuilder str) {
    return (str.indexOf("{") != -1) && (str.indexOf("}") != -1);
  }
  
  public static void parseObject(String jsonObject, boolean parseKeyList) {
    char ch;
    
    // print out the csv header
    if (parseKeyList) {
      int keyEndQuote, keyStartQuote;
      
      for(int i = 0; i < jsonObject.length(); i++) {
        ch = jsonObject.charAt(i);
        
        if (ch == ':') {
          keyEndQuote = jsonObject.lastIndexOf('"', i);
          keyStartQuote = jsonObject.lastIndexOf('"', keyEndQuote - 1);
          writer.print(jsonObject.substring(keyStartQuote + 1, keyEndQuote).concat(","));
        }
      }
      writer.println();
    }
    
    int pairSeparator = -1, 
        keyStartQuote = -1,
        keyEndQuote = -1,
        valueStartQuote = -1,
        valueEndQuote = -1;
    for(int i = 0; i < jsonObject.length(); i++) { 
      ch = jsonObject.charAt(i);
      
      if (ch == ',' || i == (jsonObject.length() - 1)) {
        // string value found
        if(valueStartQuote != -1 && valueEndQuote != -1) {
          writer.print(jsonObject.substring(valueStartQuote + 1, valueEndQuote).concat(","));
        } else { 
        // numeric value found
          writer.print(jsonObject.substring(pairSeparator + 1, i + 1).trim().concat(","));
        }
        // reset positions
        pairSeparator = -1; 
        keyStartQuote = -1;
        keyEndQuote = -1;
        valueStartQuote = -1;
        valueEndQuote = -1;
      } else if (ch == ':') {
        pairSeparator = i;
      } else if (ch == '"') {
        // record positions of key/value quotes
        if(keyStartQuote == -1) {
          keyStartQuote = i;
        } else if (keyEndQuote == -1) {
          keyEndQuote = i;
        } else if (valueStartQuote == -1) {
          valueStartQuote = i;
        } else {
          valueEndQuote = i;
        }
      }
    }
    writer.println();
  }
}