/androidsays/src/com/google/marvin/androidsays/Theme.java

http://eyes-free.googlecode.com/ · 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. */
  16. package com.google.marvin.androidsays;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Node;
  19. import org.xml.sax.SAXException;
  20. import java.io.FileInputStream;
  21. import java.io.FileNotFoundException;
  22. import java.io.IOException;
  23. import javax.xml.parsers.DocumentBuilder;
  24. import javax.xml.parsers.DocumentBuilderFactory;
  25. import javax.xml.parsers.FactoryConfigurationError;
  26. import javax.xml.parsers.ParserConfigurationException;
  27. /**
  28. * Reads a .androidsays theme file and creates a theme object that is used to
  29. * apply that theme to the game interface.
  30. *
  31. * @author clchen@google.com (Charles L. Chen)
  32. */
  33. public class Theme {
  34. public String backgroundImg;
  35. public String touchedRedImg;
  36. public String touchedGreenImg;
  37. public String touchedBlueImg;
  38. public String touchedYellowImg;
  39. public String redImg;
  40. public String greenImg;
  41. public String blueImg;
  42. public String yellowImg;
  43. public String redSnd;
  44. public String greenSnd;
  45. public String blueSnd;
  46. public String yellowSnd;
  47. public Theme() {
  48. // Empty constructor
  49. }
  50. public boolean loadTheme(String fileUriString) {
  51. boolean loadedSuccessfully = false;
  52. try {
  53. FileInputStream fis = new FileInputStream(fileUriString);
  54. DocumentBuilder docBuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  55. Document themeDoc = docBuild.parse(fis);
  56. // Go through the nodes, for each known type, if it exists, then apply the
  57. // skin.
  58. Node tempNode = themeDoc.getElementsByTagName("background_img").item(0);
  59. backgroundImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  60. tempNode = themeDoc.getElementsByTagName("touched_red_img").item(0);
  61. touchedRedImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  62. tempNode = themeDoc.getElementsByTagName("touched_green_img").item(0);
  63. touchedGreenImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  64. tempNode = themeDoc.getElementsByTagName("touched_blue_img").item(0);
  65. touchedBlueImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  66. tempNode = themeDoc.getElementsByTagName("touched_yellow_img").item(0);
  67. touchedYellowImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  68. tempNode = themeDoc.getElementsByTagName("red_img").item(0);
  69. redImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  70. tempNode = themeDoc.getElementsByTagName("green_img").item(0);
  71. greenImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  72. tempNode = themeDoc.getElementsByTagName("blue_img").item(0);
  73. blueImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  74. tempNode = themeDoc.getElementsByTagName("yellow_img").item(0);
  75. yellowImg = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  76. tempNode = themeDoc.getElementsByTagName("red_snd").item(0);
  77. redSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  78. tempNode = themeDoc.getElementsByTagName("green_snd").item(0);
  79. greenSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  80. tempNode = themeDoc.getElementsByTagName("blue_snd").item(0);
  81. blueSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  82. tempNode = themeDoc.getElementsByTagName("yellow_snd").item(0);
  83. yellowSnd = tempNode.getAttributes().getNamedItem("src").getNodeValue();
  84. // This statement will not be reached if there were any exceptions
  85. loadedSuccessfully = true;
  86. } catch (FileNotFoundException e) {
  87. e.printStackTrace();
  88. } catch (ParserConfigurationException e) {
  89. e.printStackTrace();
  90. } catch (FactoryConfigurationError e) {
  91. e.printStackTrace();
  92. } catch (SAXException e) {
  93. e.printStackTrace();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. return loadedSuccessfully;
  98. }
  99. }