AssetConfig.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.asset;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.util.Scanner;
  36. import java.util.logging.Level;
  37. import java.util.logging.Logger;
  38. /**
  39. * <code>AssetConfig</code> loads a config file to configure the asset manager.
  40. * <br/><br/>
  41. * The config file is specified with the following format:
  42. * <code>
  43. * "LOADER" <class> : (<extension> ",")* <extension>
  44. * "LOCATOR" <path> <class> : (<extension> ",")* <extension>
  45. * </code>
  46. *
  47. * @author Kirill Vainer
  48. */
  49. public class AssetConfig {
  50. private AssetManager manager;
  51. public AssetConfig(AssetManager manager){
  52. this.manager = manager;
  53. }
  54. public void loadText(InputStream in) throws IOException{
  55. Scanner scan = new Scanner(in);
  56. while (scan.hasNext()){
  57. String cmd = scan.next();
  58. if (cmd.equals("LOADER")){
  59. String loaderClass = scan.next();
  60. String colon = scan.next();
  61. if (!colon.equals(":")){
  62. throw new IOException("Expected ':', got '"+colon+"'");
  63. }
  64. String extensionsList = scan.nextLine();
  65. String[] extensions = extensionsList.split(",");
  66. for (int i = 0; i < extensions.length; i++){
  67. extensions[i] = extensions[i].trim();
  68. }
  69. Class clazz = acquireClass(loaderClass);
  70. if (clazz != null) {
  71. manager.registerLoader(clazz, extensions);
  72. } else {
  73. Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find loader {0}", loaderClass);
  74. }
  75. } else if (cmd.equals("LOCATOR")) {
  76. String rootPath = scan.next();
  77. String locatorClass = scan.nextLine().trim();
  78. Class clazz = acquireClass(locatorClass);
  79. if (clazz != null) {
  80. manager.registerLocator(rootPath, clazz);
  81. } else {
  82. Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find locator {0}", locatorClass);
  83. }
  84. } else {
  85. throw new IOException("Expected command, got '" + cmd + "'");
  86. }
  87. }
  88. }
  89. private Class acquireClass(String name) {
  90. try {
  91. Class clazz = Class.forName(name);
  92. return clazz;
  93. } catch (ClassNotFoundException ex) {
  94. return null;
  95. }
  96. }
  97. /*
  98. private static String readString(DataInput dataIn) throws IOException{
  99. int length = dataIn.readUnsignedShort();
  100. char[] chrs = new char[length];
  101. for (int i = 0; i < length; i++){
  102. chrs[i] = (char) dataIn.readUnsignedByte();
  103. }
  104. return String.valueOf(chrs);
  105. }
  106. public void loadBinary(DataInput dataIn) throws IOException{
  107. // read signature and version
  108. // how many locator entries?
  109. int locatorEntries = dataIn.readUnsignedShort();
  110. for (int i = 0; i < locatorEntries; i++){
  111. String locatorClazz = readString(dataIn);
  112. String rootPath = readString(dataIn);
  113. manager.registerLocator(rootPath, locatorClazz);
  114. }
  115. int loaderEntries = dataIn.readUnsignedShort();
  116. for (int i = 0; i < loaderEntries; i++){
  117. String loaderClazz = readString(dataIn);
  118. int numExtensions = dataIn.readUnsignedByte();
  119. String[] extensions = new String[numExtensions];
  120. for (int j = 0; j < numExtensions; j++){
  121. extensions[j] = readString(dataIn);
  122. }
  123. manager.registerLoader(loaderClazz, extensions);
  124. }
  125. }
  126. */
  127. }