Cod sursa(job #1343927)

Utilizator valentin_radValentin Radulescu valentin_rad Data 16 februarie 2015 01:33:55
Problema Convertor Scor 70
Compilator java Status done
Runda rosedu_cdl_2015 Marime 3.46 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;

class Main {
  
  public static String buffer = "";
  public static boolean parseKeyList = true;
  public static PrintWriter writer;

  public static void main(String[] args) throws FileNotFoundException {
    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();
  }
  
  public static void parseString(String line) {
    buffer = buffer.concat(line);
    
    while(containsJsonObject(buffer)) {
      String jsonObject = buffer.substring(getObjectStart(buffer) + 1, getObjectEnd(buffer));
      parseObject(jsonObject, parseKeyList);
      buffer = buffer.substring(getObjectEnd(buffer) + 1, buffer.length());
      parseKeyList = false;
    }
  } 
  
  public static boolean containsJsonObject(String str) {
    int objectStart = str.indexOf('{');
    int objectEnd = str.indexOf('}');
    
    return (objectStart != -1) && (objectEnd != -1);
  }

  public static int getObjectStart(String str) {
   return str.indexOf('{'); 
  }

  public static int getObjectEnd(String str) {
    return str.indexOf('}');
  }
  
  public static String cleanValue(String value) {
    return value.trim().replace("\"", "");
  }
  
  public static void parseObject(String jsonObject, boolean parseKeyList) {
    char ch;
    String csvLine = "";
    String value;
    String csvHeader = "";
    
    jsonObject  = jsonObject.trim();
    
    for(int i = 0; i < jsonObject.length(); i++) {
      ch = jsonObject.charAt(i);
      
      if(ch == '"') {
        int keyEndQuote = jsonObject.indexOf('"', i + 1),
            valueSeparator = jsonObject.indexOf(':', keyEndQuote),
            nextQuote = jsonObject.indexOf('"', valueSeparator),
            lastQuote = jsonObject.indexOf('"', nextQuote + 1),
            nextComma = jsonObject.indexOf(',', valueSeparator);
        
        // build key list
        if(parseKeyList) {
          csvHeader = csvHeader.concat(jsonObject.substring(i + 1, keyEndQuote)).concat(",");
        }
        
        // build value list
        if(nextComma == -1 || nextQuote == -1 || lastQuote == -1) { // end of object
          value = jsonObject.substring(valueSeparator + 1, jsonObject.length());
          csvLine = csvLine.concat(cleanValue(value)).concat(",");
          break;
        } else if(nextComma > nextQuote && nextComma < lastQuote) { // comma in value
          value = jsonObject.substring(nextQuote + 1, lastQuote);
          csvLine = csvLine.concat(cleanValue(value)).concat(",");
          // skip to next key
          i = lastQuote;
        } else if(nextComma > nextQuote && nextComma > lastQuote) { // string value
          value = jsonObject.substring(nextQuote + 1, lastQuote );
          csvLine = csvLine.concat(cleanValue(value)).concat(",");
          // skip to next key
          i = nextComma;
        } else { // numeric value
          value = jsonObject.substring(valueSeparator + 1, nextComma);
          csvLine = csvLine.concat(cleanValue(value)).concat(",");
          i = nextComma;
        }
      }
    }
    if(parseKeyList) {
      writer.println(csvHeader);
    }
    writer.println(csvLine);
  }
}