Cod sursa(job #1361191)

Utilizator devLucian93Lucian Alexandru devLucian93 Data 25 februarie 2015 20:04:24
Problema Convertor Scor 70
Compilator java Status done
Runda rosedu_cdl_2015 Marime 3.99 kb
import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.PrintWriter;
 
public class Main {
 
    private static final byte CURLY_CLOSED = 2;
    private static final byte COMMA = 5;
    private static final byte COLON = 6;
    private static final byte STRING = 7;
    private static final byte NUMBER = 8;
    private static char token;
   
    private static StringBuilder result ;
    public static void main(String[] args)throws IOException
    {   boolean keys = true;
    	Pattern splitRegex = Pattern.compile("\\s*");
    	Pattern checkString = Pattern.compile("");
        Scanner reader = new Scanner(new FileInputStream("convertor.in"));
        PrintWriter writer = new PrintWriter("convertor.out");
        reader.useDelimiter(splitRegex);
        reader.skip("\\[");
        token = reader.next().charAt(0);
        result = new StringBuilder();
        
        while( reader.hasNext() ){
             
             while(keys){
                if(checkToken(token) == CURLY_CLOSED){
                
                    keys = false;
                    result.append(System.lineSeparator());
                    token = reader.next().charAt(0);
                    reader.close();
                    reader = new Scanner(new FileInputStream("convertor.in"));
                    reader.useDelimiter(splitRegex);
                    reader.skip("\\[");        
                     
                }
                if(checkToken(token) == STRING ){
               
                	 getString(reader, splitRegex, checkString);
                }
                if(checkToken(token) == COLON){
                     
                    while( (checkToken(token) != COMMA) && (checkToken(token) != CURLY_CLOSED) ) {
                        token = reader.next().charAt(0);
                         
                    }
                 
                }
                 
                if(checkToken(token) != CURLY_CLOSED){
                    token = reader.next().charAt(0);
                     
                     
                }
                 
            }
           
            token = reader.next().charAt(0);
      
            if(checkToken(token) == COLON){
                token = reader.next().charAt(0);
                getString(reader, splitRegex, checkString);
                if(checkToken(token) == NUMBER){
                    while(checkToken(token) == NUMBER){
                        result.append(token);
                        token = reader.next().charAt(0);
                   }
                    result.append(',');
                }
            }
             
            if(checkToken(token) == CURLY_CLOSED){
            	result.append(System.lineSeparator());
            }
        
   
        }
        result.trimToSize();
        writer.write(result.toString());
        writer.close();
        reader.close();
   
    }
    
    private static void getString(Scanner reader, Pattern splitRegex, Pattern checkString){
    	 if(checkToken(token) == STRING ){
         	token = reader.next().charAt(0);
         	reader.useDelimiter(checkString);
         	while(checkToken(token) != STRING){
         		
         			result.append(token);
         			token = reader.next().charAt(0);
         			
         	}
         	result.append(',');
         	reader.useDelimiter(splitRegex);
        
         }
    	
    }
     
    private static int checkToken(char token){
        switch (token) {
          
            case '}':
                return CURLY_CLOSED;
            case ',':
                return COMMA;
            case '"':
                return STRING;
            case '0': case '1': case '2': case '3': case '4':
            case '5': case '6': case '7': case '8': case '9':
            case '-':
                return NUMBER;
            case ':':
                return COLON;
            default: return 12;
        }
 
    }
     
     
}