Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple-XML and class attribute issues when dealing with multiple projects  (Read 276 times)
0 Members and 1 Guest are viewing this topic.
Offline Muteking

Senior Newbie





« Posted 2014-01-12 10:07:03 »

Complex problem here, since it involves also Java for Android. I'll ask anyway.

I'm trying to use SimpleXML to serialize/deserialize data that must be used by a main program (running on Android platform) and an editor (running on a Desktop platform).

The editor extends a ParentClass (contained in a "common libraries" project) with a ChildClass to give it more functionality (such as graphic properties and so on).

I would like to not save any reference to ChildClass in the XML files, to avoid circular dependencies problems between projects.

This is a summary of how the projects are structured:
First project(EDITOR)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
// I project (DESKTOP EDITOR, requires COMMON CLASSES to build)

@Element(name = "Tile2", type = Tile2.class, required = false)
public class ChildClass extends ParentClass {
    BufferedImage thumbnailToDisplay = null;
    // Additional methods and variables that are not annotated,
    // since they don't have to go into the XML, like these ones
    public void generateThumb() {
        getGraphics(super.imageName);
    }

    public BufferedImage getGraphics(String imgName) {
        // various graphic routines
    }
}

public class EditorWindow(Container contentToModify) {
    private mCont;
    // Constructor
    public EditorWindow(Container contentToModify) {
        this.mCont = contentToModify;
        this.initialize();
    }
    // Various methods
    // Must access all the elements in contentToModify
    // as if their type is ChildClass
    public void initialize() {
        for (int i = 0; i < this.mCont.getItems().size(); i++)
            ChildClass tmp = (ChildClass)this.mCont.getItems().get(i);
            tmp.generateThumb();
    }
 }

 // This is the main class, that starts the editor GUI, initializes stuffand so on
 public class Gui() {
    Container mainContainer;
    EditorWindow edWin;

    public static main() {
        mainContainer = Xml_IO.XmlLoad(new File("XmlPath/filename"));
        edWin = new EditorWindow(mainContainer);

        // Stuff that could modify the content of mainContainer
        File file = saveFile.getSelectedFile();
        XML_IO.XmlSave(file, this.mainContainer);
    }
 }


Second project (common libraries)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
// II project (COMMON CLASSES)
@Root(strict=false)
public class ParentClass {
    @Attribute
    float size;
    @Attribute
    float position;
    @Element
    String imageName;
}
@Root(name = "Container", strict=false)
public class Container {
    @ElementList(type = parentClass.class, required = false,inline=true, entry="container")
    private ArrayList<ParentClass> mItems = new ArrayList<ParentClass>();

    public ArrayList<XItem> getItems() {
        return mItems;
    }

}

public class Xml_IO {
    // Deserializer method (could be moved to I project(DESKTOP EDITOR), if needed)
    public static Container XmlLoad(File inputXmlFile) {
        Container tmpContainer = new Container();
        Serializer serializer = new Persister();

        try {
            tmpContainer = serializer.read(Container.class, inputXmlFile);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return tmpContainer;
    }

    // Serializer method (could be moved to I project(DESKTOP EDITOR), if needed)
    public static void XmlSave(File outputXmlFile, Container containerToSave) {
        Format format = new Format(
                "<?xml version=\"1.0\" encoding= \"UTF-8\" ?>");
        Serializer serializer = new Persister(format);

        try {
            serializer.write(containerToSave, outputXmlFile);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


Third project (Android application)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// III project (ANDROID MAIN PROGRAM, requires COMMON CLASSES to build, must NOT require EDITOR to build) 
public class Activity extends CustomActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Custom serializer
        Container contentToRead = new Container();
        Serializer serializer = new Persister();

        try {
            contentToRead = serializer.read(Container.class, this.getResources().openRawResource(R.raw.xml_resource), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


This is instead an example of the XML generated with current setup:
1  
2  
3  
4  
5  
6  
7  
8  
9  
<?xml version="1.0" encoding= "UTF-8" ?>
<Container>
    <ParentClass class="project1.ChildClass" size="1.0" position="2.5">
       <mImage>image1</mImage>
    </ParentClass>
    <ParentClass class="project1.ChildClass" size="2.0" position="1.5">
       <mImage>image2</mImage>
    </ParentClass>
</Container>


The I/O on the editor works well, but if I try to deserialize the XML on Android, it fails with the following error: java.lang.ClassNotFoundException: Didn't find class "project1.ChildClass" on path: DexPathList[dexElements=[zip file "/mnt/asec/projectname/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/projectname/lib, /vendor/lib, /system/lib]]

This is what I tried so far:

    1.I tried the @ElementListUnion annotation, as suggested here, but it does not remove the class attribute.
    2.tried to inline the list, as suggested here, but the class attribute did not disappear
    3.tried to understand how to implement a Converter, as suggested here but I cannot figure out how to solve the problems of circular dependencies it would introduce
    4.tried to manually remove the class="project1.ChildClass" attributes: the Android application in this case works, but the editor stops importing XML files, since it would not be possible to cast the ParentClass into a ChildClass. Even if I would solve that issue, it would still save the XML file with the "class" attribute

What I would like is to serialize XML files from the editor without any reference to the ChildClass (so the Android application could read them), but to deserialize the ParentClass items as ChildClass into the editor.
Offline Danny02
« Reply #1 - Posted 2014-01-12 11:13:14 »

remember the golden words: "composition over inheritance"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!