project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Scheme.java
branchhedgeroid
changeset 5465 b7e009722465
parent 5463 83c53a80f7ff
child 5467 88e25840f532
equal deleted inserted replaced
5463:83c53a80f7ff 5465:b7e009722465
     1 package org.hedgewars.mobile;
       
     2 
       
     3 import java.io.BufferedOutputStream;
       
     4 import java.io.BufferedReader;
       
     5 import java.io.File;
       
     6 import java.io.FileNotFoundException;
       
     7 import java.io.FileOutputStream;
       
     8 import java.io.FileReader;
       
     9 import java.io.IOException;
       
    10 import java.io.InputStream;
       
    11 import java.util.ArrayList;
       
    12 import java.util.Arrays;
       
    13 
       
    14 import org.xmlpull.v1.XmlPullParser;
       
    15 import org.xmlpull.v1.XmlPullParserException;
       
    16 import org.xmlpull.v1.XmlPullParserFactory;
       
    17 
       
    18 import android.content.Context;
       
    19 import android.content.res.TypedArray;
       
    20 
       
    21 public class Scheme {
       
    22 
       
    23 	public static final String DIRECTORY_SCHEME = "schemes";
       
    24 
       
    25 	
       
    26 	private String name;
       
    27 	private ArrayList<Integer> basic;
       
    28 	private ArrayList<Boolean> gamemod;
       
    29 
       
    30 	public Scheme(String _name, ArrayList<Integer> _basic, ArrayList<Boolean> _gamemod){
       
    31 		name = _name;
       
    32 		basic = _basic;
       
    33 		gamemod = _gamemod;
       
    34 	}
       
    35 
       
    36 	public String toString(){
       
    37 		return name;
       
    38 	}
       
    39 	
       
    40 	
       
    41 	public static final int STATE_START = 0;
       
    42 	public static final int STATE_ROOT = 1;
       
    43 	public static final int STATE_NAME = 2;
       
    44 	public static final int STATE_BASICFLAGS = 3;
       
    45 	public static final int STATE_GAMEMOD = 4;
       
    46 	public static final int STATE_BASICFLAG_INTEGER = 5;
       
    47 	public static final int STATE_GAMEMOD_TRUE = 6;
       
    48 	public static final int STATE_GAMEMOD_FALSE = 7;
       
    49 	
       
    50 	public static Scheme[] getSchemes(Context c) throws IllegalArgumentException{
       
    51 		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_SCHEME + '/';
       
    52 		String[] files = new File(dir).list();
       
    53 		if(files == null) files = new String[]{};
       
    54 		Arrays.sort(files);
       
    55 		
       
    56 		ArrayList<Scheme> schemes = new ArrayList<Scheme>();
       
    57 
       
    58 		try {
       
    59 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
    60 			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
       
    61 			
       
    62 			for(String file : files){
       
    63 				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
       
    64 				xmlPuller.setInput(br);
       
    65 				String name = null;
       
    66 				ArrayList<Integer> basic = new ArrayList<Integer>();
       
    67 				ArrayList<Boolean> gamemod = new ArrayList<Boolean>();
       
    68 				int eventType = xmlPuller.getEventType();
       
    69 				int state = STATE_START;
       
    70 				while(eventType != XmlPullParser.END_DOCUMENT){
       
    71 					switch(state){
       
    72 					case STATE_START:
       
    73 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("scheme")) state = STATE_ROOT;
       
    74 						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
       
    75 						break;
       
    76 					case STATE_ROOT:
       
    77 						if(eventType == XmlPullParser.START_TAG){
       
    78 							if(xmlPuller.getName().equals("basicflags")) state = STATE_BASICFLAGS;
       
    79 							else if(xmlPuller.getName().toLowerCase().equals("gamemod")) state = STATE_GAMEMOD;
       
    80 							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
       
    81 							else throwException(file, eventType);
       
    82 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
       
    83 						else throwException(xmlPuller.getText(), eventType);
       
    84 						break;
       
    85 					case STATE_BASICFLAGS:
       
    86 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("integer")) state = STATE_BASICFLAG_INTEGER;
       
    87 						else if(eventType == XmlPullParser.END_TAG)	state = STATE_ROOT;
       
    88 						else throwException(file, eventType);
       
    89 						break;
       
    90 					case STATE_GAMEMOD:
       
    91 						if(eventType == XmlPullParser.START_TAG){
       
    92 							if(xmlPuller.getName().toLowerCase().equals("true")) state = STATE_GAMEMOD_TRUE;
       
    93 							else if(xmlPuller.getName().toLowerCase().equals("false")) state = STATE_GAMEMOD_FALSE;
       
    94 							else throwException(file, eventType);
       
    95 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
    96 						else throwException(file, eventType);
       
    97 						break;
       
    98 					case STATE_NAME:
       
    99 						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
       
   100 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   101 						else throwException(file, eventType);
       
   102 						break;
       
   103 					case STATE_BASICFLAG_INTEGER:
       
   104 						if(eventType == XmlPullParser.TEXT) basic.add(Integer.parseInt(xmlPuller.getText().trim()));
       
   105 						else if(eventType == XmlPullParser.END_TAG) state = STATE_BASICFLAGS;
       
   106 						else throwException(file, eventType);
       
   107 						break;
       
   108 					case STATE_GAMEMOD_FALSE:
       
   109 						if(eventType == XmlPullParser.TEXT) gamemod.add(false);
       
   110 						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
       
   111 						else throwException(file, eventType);
       
   112 						break;
       
   113 					case STATE_GAMEMOD_TRUE:
       
   114 						if(eventType == XmlPullParser.TEXT) gamemod.add(true);
       
   115 						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
       
   116 						else throwException(file, eventType);
       
   117 						break;
       
   118 					}
       
   119 					eventType = xmlPuller.next();
       
   120 					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
       
   121 						eventType = xmlPuller.next();
       
   122 					}
       
   123 				}//end while(eventtype != END_DOCUMENT
       
   124 				schemes.add(new Scheme(name, basic, gamemod));
       
   125 			}//end for(string file : files
       
   126 			Scheme[] ret = new Scheme[schemes.size()];
       
   127 			schemes.toArray(ret);
       
   128 			return ret;
       
   129 		} catch (XmlPullParserException e) {
       
   130 			e.printStackTrace();
       
   131 		} catch (FileNotFoundException e) {
       
   132 			e.printStackTrace();
       
   133 		} catch (IOException e) {
       
   134 			e.printStackTrace();
       
   135 		}
       
   136 		return new Scheme[]{};//TODO handle correctly
       
   137 	}
       
   138 	
       
   139 	private static void throwException(String file, int eventType){
       
   140 		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
       
   141 	}
       
   142 
       
   143 	
       
   144 }