OSDN Git Service

* XMLImporter can now read version numbers from XML J3O
authorshadowislord <shadowislord@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Sat, 25 Jun 2011 23:49:18 +0000 (23:49 +0000)
committershadowislord <shadowislord@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Sat, 25 Jun 2011 23:49:18 +0000 (23:49 +0000)
git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@7736 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

engine/src/core-plugins/com/jme3/export/binary/BinaryInputCapsule.java
engine/src/core/com/jme3/export/SavableClassUtil.java
engine/src/xml/com/jme3/export/xml/DOMInputCapsule.java
engine/src/xml/com/jme3/export/xml/DOMOutputCapsule.java

index a7e0e2e..b0b66ef 100644 (file)
@@ -34,6 +34,7 @@ package com.jme3.export.binary;
 
 import com.jme3.export.InputCapsule;
 import com.jme3.export.Savable;
+import com.jme3.export.SavableClassUtil;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.IntMap;
 import java.io.IOException;
@@ -258,23 +259,8 @@ final class BinaryInputCapsule implements InputCapsule {
     }
     
     public int getSavableVersion(Class<? extends Savable> desiredClass){
-        Class thisClass = savable.getClass();
-        int count = 0;
-        while (thisClass != null && thisClass != desiredClass){
-            thisClass = thisClass.getSuperclass();
-            count ++;
-        }
-        if (thisClass == null){
-            throw new IllegalArgumentException(savable.getClass().getName() + 
-                                               " does not extend " + 
-                                               desiredClass.getName() + "!");
-        }else if (count > cObj.classHierarchyVersions.length){
-            throw new IllegalArgumentException(savable.getClass().getName() + 
-                                               " cannot access version of " +
-                                               desiredClass.getName() + 
-                                               " because it doesn't implement Savable");
-        }
-        return cObj.classHierarchyVersions[count];
+        return SavableClassUtil.getSavedSavableVersion(savable, desiredClass, 
+                                            cObj.classHierarchyVersions);
     }
 
     public BitSet readBitSet(String name, BitSet defVal) throws IOException {
index a4c9c96..6b29daf 100644 (file)
@@ -124,6 +124,26 @@ public class SavableClassUtil {
         }
     }
     
+    public static int getSavedSavableVersion(Object savable, Class<? extends Savable> desiredClass, int[] versions){
+        Class thisClass = savable.getClass();
+        int count = 0;
+        while (thisClass != null && thisClass != desiredClass){
+            thisClass = thisClass.getSuperclass();
+            count ++;
+        }
+        if (thisClass == null){
+            throw new IllegalArgumentException(savable.getClass().getName() + 
+                                               " does not extend " + 
+                                               desiredClass.getName() + "!");
+        }else if (count > versions.length){
+            throw new IllegalArgumentException(savable.getClass().getName() + 
+                                               " cannot access version of " +
+                                               desiredClass.getName() + 
+                                               " because it doesn't implement Savable");
+        }
+        return versions[count];
+    }
+    
     /**
      * fromName creates a new Savable from the provided class name. First registered modules
      * are checked to handle special cases, if the modules do not handle the class name, the
index 97e9fdf..03678aa 100644 (file)
@@ -71,6 +71,9 @@ public class DOMInputCapsule implements InputCapsule {
     private XMLImporter importer;
     private boolean isAtRoot = true;
     private Map<String, Savable> referencedSavables = new HashMap<String, Savable>();
+    
+    private int[] classHierarchyVersions;
+    private Savable savable;
 
     public DOMInputCapsule(Document doc, XMLImporter importer) {
         this.doc = doc;
@@ -78,8 +81,13 @@ public class DOMInputCapsule implements InputCapsule {
         currentElem = doc.getDocumentElement();
     }
 
-    public int getSavableVersion(Class<? extends Savable> clazz) {
-        return 0; // TODO: figure this out ...
+    public int getSavableVersion(Class<? extends Savable> desiredClass) {
+        if (classHierarchyVersions != null){
+            return SavableClassUtil.getSavedSavableVersion(savable, desiredClass, 
+                                                        classHierarchyVersions);
+        }else{
+            return 0;
+        }
     }
     
     private static String decodeString(String s) {
@@ -978,10 +986,25 @@ public class DOMInputCapsule implements InputCapsule {
                 className = currentElem.getAttribute("class");
             }
             tmp = SavableClassUtil.fromName(className, null);
+            
+            
+            String versionsStr = currentElem.getAttribute("savable_versions");
+            if (versionsStr != null && !versionsStr.equals("")){
+                String[] versionStr = versionsStr.split(",");
+                classHierarchyVersions = new int[versionStr.length];
+                for (int i = 0; i < classHierarchyVersions.length; i++){
+                    classHierarchyVersions[i] = Integer.parseInt(versionStr[i].trim());
+                }
+            }else{
+                classHierarchyVersions = null;
+            }
+            
             String refID = currentElem.getAttribute("reference_ID");
             if (refID.length() < 1) refID = currentElem.getAttribute("id");
             if (refID.length() > 0) referencedSavables.put(refID, tmp);
             if (tmp != null) {
+                // Allows reading versions from this savable
+                savable = tmp;
                 tmp.read(importer);
                 ret = tmp;
             }
index 20ff3eb..a4eddec 100644 (file)
@@ -495,7 +495,7 @@ public class DOMOutputCapsule implements OutputCapsule {
                     sb.append(", ");
                 }
             }
-            el.setAttribute("savable_version", sb.toString());
+            el.setAttribute("savable_versions", sb.toString());
             
             writtenSavables.put(object, el);
             object.write(exporter);