SwmmParser.java [src/parsers] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package parsers;

import java.io.File;
import java.io.IOException;
import SwmmObjects.Infiltration;
import SwmmObjects.Outfall;
import SwmmObjects.RainGage;
import SwmmObjects.Subarea;
import SwmmObjects.Subcatchment;
import SwmmObjects.SwmmData;
import SwmmObjects.SwmmFile;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Lucas Yaege
 */
public class SwmmParser
{

    public static SwmmData Parse( String inFile )
    {
        SwmmData swmmData = new SwmmData();
        String[] inFileLines = inFile.split( "\n" );
        String curSection = "";

        for ( int i = 0; i < inFileLines.length; i++ )
        {
            String line = inFileLines[i];

            //Change sections as we encounter them.
            if ( line.length() != 0 && line.charAt( 0 ) == '[' ) //No parsing needs to be done on this line, just change which section we're on.
            {
                curSection = line;
            }
            else if ( line.length() != 0 && line.charAt( 0 ) != ';' )//Ignore empty lines and comments
            {
                switch ( curSection ) //Take the action that's appropriate for our context.
                {
                    case "[OPTIONS]":
                        swmmData.options.setOption( line );
                        break;
                    case "[TITLE]":
                        swmmData.title.concat( line );
                        break;
                    case "[FILES]":
                        SwmmFile file = new SwmmFile( line );
                        swmmData.files.add( file );
                        break;
                    case "[RAINGAGES]":
                        RainGage rg = new RainGage( line );
                        swmmData.rainGages.add( rg );
                        break;
                    case "[EVAPORATION]":
                        swmmData.evaporation.parse( line );
                        break;
                    case "[SUBCATCHMENTS]":
                        Subcatchment sc = new Subcatchment( line );
                        swmmData.subcatchments.add( sc );
                        break;
                    case "[SUBAREAS]":
                        Subarea sa = new Subarea( line );
                        swmmData.subareas.add( sa );
                        break;
                    case "[INFILTRATION]":
                        Infiltration in = new Infiltration( line );
                        swmmData.infiltrations.add( in );
                        break;
                    case "[OUTFALLS]":
                        Outfall o = new Outfall( line );
                        swmmData.outfalls.add( o );
                        break;
                    case "[REPORT]":
                        swmmData.reportOptions.setReportOption( line );
                        break;
                }
            }
        }
        
        return swmmData;
    }

    public static SwmmData Parse( File inFile ) throws IOException
    {
        String data = FileUtils.readFileToString( inFile, "UTF-8" );
        return Parse( data );
    }

}