SparseArrayの続き(よくよく考えると・・・)

分割されるのはPathなんだから

  • Pathはいくつも持ってても良いし
  • 分割されたPath同士は無関係であるのだから(重なりは考えられるけど、それを切っていく間は重なりは発生し得ない)

なので、多分これでOK。


/**
* フェーズ情報、パス情報を読み込む
*
* @param resources
* @param iPhase
* 面数
* @return 面情報 面情報は下記で表せる。

* SparseArray(

* (1, Map(

*  ("mapName", "phase 01"),

*  ("map", "map08"),

*  ("start", Point(10,10)),

*  ("paths", SparseArray(

*   (1, SparseArray(

*    (10, Point(0,0)),

*    (20, Point(0,199)),

*    (30, Point(199,199)),

*    (40, Point(199,0)),

*    (50, Point(0,0)),

*   ),

*   (2, SparseArray(

*    (10, Point(49,49)),

*    (20, Point(49,149)),

*    (30, Point(149,149)),

*    (40, Point(149,0)),

*    (50, Point(49,49))

*   )

*  )

* )

*/
public static SparseArray loadPathXML(Resources res, int iPhase) {
// depth が何を表すか
final int S_PHASE = 2;
final int S_PHASE_INFORMATION = 3;
final int S_PATHS = 4;
final int S_PATH = 5;

// 階層構造
SparseArray phaseArray = new SparseArray();

try {
XmlPullParser parser = res.getXml(R.xml.paths);
try {
parser.setInput(res.openRawResource(R.xml.paths), null);
} catch (NotFoundException e) {
Log.e("XML_ERROR 1", e.getMessage());
} catch (XmlPullParserException e) {
Log.e("XML_ERROR 2", e.getMessage());
}

// 階層構造の深さ
int depth = 0;

// mPhaseMap は以下から構成される
int phaseNumber = 0;
Map phaseInformation = null;

// phaseInformation は以下から構成される
String phaseMapName = null;
String phaseMap = null;
String start = null;
String paths = null;

// paths は以下から構成される
int pathsNumber = 0;
SparseArray pathsInformation = null;

// pathInformation は以下から構成される
int path = 0;
SparseArray pathInformation = null;

loop: for (int e = parser.getEventType(); e != XmlPullParser.END_DOCUMENT; e = parser
.next()) {
// keyword = new Keyword();

if (e == XmlPullParser.START_DOCUMENT) {
// ドキュメント開始
Log.d("XML:DOC", "START");
} else if (e == XmlPullParser.TEXT) {
// エレメントに囲まれている部分
String text = parser.getText();
Log.d("XML:TEXT", "depth=" + depth + " text=" + text);
switch (depth) {
case S_PHASE: {
break;
}
case S_PHASE_INFORMATION: {
if (phaseMapName != null) {
phaseInformation.put(phaseMapName, text);
phaseMapName = null;
} else if (phaseMap != null) {
phaseInformation.put(phaseMap, text);
phaseMap = null;
} else if (start != null) {
ArrayList arr = Utils.split(",", text);
int x = Integer.valueOf(arr.get(0));
int y = Integer.valueOf(arr.get(1));
phaseInformation.put(start, new Point(x, y));
start = null;
} else if (paths != null) {
phaseInformation.put(paths, text);
paths = null;
}
break;
}
case S_PATHS: {
pathsInformation.put(pathsNumber, text);
break;
}
case S_PATH: {
ArrayList arr = Utils.split(",", text);
int x = Integer.valueOf(arr.get(0));
int y = Integer.valueOf(arr.get(1));
pathInformation.put(path, new Point(x, y));
break;
}
}
} else if (e == XmlPullParser.START_TAG) {
// エレメントの開始
depth++;
Log.d("XML:ELEM", "START " + depth);
// エレメント名
Log.d("XML:ELEM_NAME", parser.getName());

for (int a = 0; a < parser.getAttributeCount(); a++) {
// 属性情報
Log.d("XML:ELEM_ATTR", parser.getAttributeName(a)
+ " = " + parser.getAttributeValue(a));
switch (depth) {
case S_PHASE: {
if (parser.getName().equals("item")
&& parser.getAttributeName(a)
.equals("name")) {
phaseNumber = Integer.valueOf(parser
.getAttributeValue(a));
phaseInformation = new HashMap();
}
break;
}
case S_PHASE_INFORMATION: {
if (parser.getAttributeValue(a).equals("mapName")) {
phaseMapName = parser.getAttributeValue(a);
} else if (parser.getAttributeValue(a)
.equals("map")) {
phaseMap = parser.getAttributeValue(a);
} else if (parser.getAttributeValue(a).equals(
"start")) {
start = parser.getAttributeValue(a);
} else if (parser.getAttributeValue(a).equals(
"paths")) {
paths = parser.getAttributeValue(a);
pathsInformation = new SparseArray();
}
break;
}
case S_PATHS: {
pathsNumber = Integer.valueOf(parser.getAttributeValue(a));
pathInformation = new SparseArray();
break;
}
case S_PATH: {
// parser.getAttributeName(a); // name
path = Integer.valueOf(parser.getAttributeValue(a));
break;
}
}
}
} else if (e == XmlPullParser.END_TAG) {
// エレメントの終了
Log.d("XML:ELEM", "END " + depth);
switch (depth) {
case S_PHASE: {
if (phaseNumber == iPhase) {
phaseArray.put(phaseNumber, phaseInformation);
break loop;
}
phaseInformation = null;
break;
}
case S_PHASE_INFORMATION: {
if (paths != null) {
phaseInformation.put(paths, pathsInformation);
paths = null;
pathsInformation = null;
}
break;
}
case S_PATHS: {
pathsInformation.put(pathsNumber, pathInformation);
pathsNumber = 0;
pathInformation = null;
break;
}
case S_PATH: {
break;
}
}
depth--;
}
}
} catch (NotFoundException e) {
Log.e("XML", e.getClass().getName() + ": " + e.getMessage());
} catch (XmlPullParserException e) {
Log.d("XML", e.getClass().getName() + ": " + e.getMessage());
for (StackTraceElement s : e.getStackTrace()) {
Log.d("TRACE", s.toString());
}
} catch (IOException e) {
Log.d("XML", e.getClass().getName() + ": " + e.getMessage());
}

return phaseArray;
}