/androidsays/src/com/google/marvin/androidsays/Theme.java
Java | 118 lines | 74 code | 19 blank | 25 comment | 0 complexity | 0aee8759f4dccce99bd52ee548116ef6 MD5 | raw file
1/* 2 * Copyright (C) 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16package com.google.marvin.androidsays; 17 18import org.w3c.dom.Document; 19import org.w3c.dom.Node; 20import org.xml.sax.SAXException; 21 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.IOException; 25 26import javax.xml.parsers.DocumentBuilder; 27import javax.xml.parsers.DocumentBuilderFactory; 28import javax.xml.parsers.FactoryConfigurationError; 29import javax.xml.parsers.ParserConfigurationException; 30 31 32/** 33 * Reads a .androidsays theme file and creates a theme object that is used to 34 * apply that theme to the game interface. 35 * 36 * @author clchen@google.com (Charles L. Chen) 37 */ 38public class Theme { 39 public String backgroundImg; 40 41 public String touchedRedImg; 42 public String touchedGreenImg; 43 public String touchedBlueImg; 44 public String touchedYellowImg; 45 46 public String redImg; 47 public String greenImg; 48 public String blueImg; 49 public String yellowImg; 50 51 public String redSnd; 52 public String greenSnd; 53 public String blueSnd; 54 public String yellowSnd; 55 56 public Theme() { 57 // Empty constructor 58 } 59 60 public boolean loadTheme(String fileUriString) { 61 boolean loadedSuccessfully = false; 62 try { 63 FileInputStream fis = new FileInputStream(fileUriString); 64 65 DocumentBuilder docBuild = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 66 67 Document themeDoc = docBuild.parse(fis); 68 69 // Go through the nodes, for each known type, if it exists, then apply the 70 // skin. 71 Node tempNode = themeDoc.getElementsByTagName("background_img").item(0); 72 backgroundImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 73 74 tempNode = themeDoc.getElementsByTagName("touched_red_img").item(0); 75 touchedRedImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 76 tempNode = themeDoc.getElementsByTagName("touched_green_img").item(0); 77 touchedGreenImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 78 tempNode = themeDoc.getElementsByTagName("touched_blue_img").item(0); 79 touchedBlueImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 80 tempNode = themeDoc.getElementsByTagName("touched_yellow_img").item(0); 81 touchedYellowImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 82 83 tempNode = themeDoc.getElementsByTagName("red_img").item(0); 84 redImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 85 tempNode = themeDoc.getElementsByTagName("green_img").item(0); 86 greenImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 87 tempNode = themeDoc.getElementsByTagName("blue_img").item(0); 88 blueImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 89 tempNode = themeDoc.getElementsByTagName("yellow_img").item(0); 90 yellowImg = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 91 92 tempNode = themeDoc.getElementsByTagName("red_snd").item(0); 93 redSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 94 tempNode = themeDoc.getElementsByTagName("green_snd").item(0); 95 greenSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 96 tempNode = themeDoc.getElementsByTagName("blue_snd").item(0); 97 blueSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 98 tempNode = themeDoc.getElementsByTagName("yellow_snd").item(0); 99 yellowSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue(); 100 101 // This statement will not be reached if there were any exceptions 102 loadedSuccessfully = true; 103 } catch (FileNotFoundException e) { 104 e.printStackTrace(); 105 } catch (ParserConfigurationException e) { 106 e.printStackTrace(); 107 } catch (FactoryConfigurationError e) { 108 e.printStackTrace(); 109 } catch (SAXException e) { 110 e.printStackTrace(); 111 } catch (IOException e) { 112 e.printStackTrace(); 113 } 114 return loadedSuccessfully; 115 } 116 117 118}