0

first question, please be kind!

I have the following situation:

I have a couple of classes with different datas, not really complicated but not simple either.

I need to get data from these classes, from objects of these classes, and print them in a specific format, which i still don't know exactly.

For example:

public class A{
    public int data1;
    public String data2;
}

public class B{
    public Map<String, A> map1;
} 

B obj;

I need to print obj on a pdf in a predefined format, like in a table.

My idea for now is to like derive an xml description of the pdf and read the xml to guide the pdf writing regarding to where put the text and with which font.

For example:

<document>
    <text id="val1.data1" x="10" y="10" />
    <text id="val1.data2" x="100" y ="10" />
    <text id="val2.data1" x="10" y="100" />
    <text id="val2.data2" x="100" y ="100" />
</document>

This solution, even if it allows me to decouple the document format from the code, doesn't allow me to decouple retrieving the data from the object, i still need to get the ids and manually do a switch like

if(id=="data1"){
    A.getData1();
}else if(id=="data2"){
    A.getData2();
}

A thing i would prefer to avoid.

Any suggestion about how could i design it? i'm already able to draw on pdf anything i like in any position and retrive data when i need, i'd just like to have everything more decoupled because the document template and the data in it could change anytime as far as i know.

Thanks for any suggestion!

bracco23
  • 2,181
  • 10
  • 28
  • 1
    please read [how do i compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy May 31 '16 at 09:56
  • I think that the easiest way would be to put `val1.data1` into a map, with the key `"val1.data1"`, so you could simply do a lookup by the `id` in the XML. – Andy Turner May 31 '16 at 09:58
  • The map is a good starting point, i could use that to decouple and let the class that actually does the render get the map representing the value to write instead of acting directly on the objects. Still I need to change the code if the template changes too much, but I don't know if that is avoidable after all. – bracco23 May 31 '16 at 19:12

2 Answers2

1

toString() method in java class , will be invoked when you try to print the object. so override the method in your class and return the formatted String ex:-

@override
public String toString() {
    return 
    "<text id=" +this.data1 + " x= " +this.x+ " y=" +this.y +"/>";
} 
Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • I need to do something different. I need to get like a description of a document, in the XML format described above, and some objects to get the data from, and create a document with that description and those data. – bracco23 May 31 '16 at 19:15
  • still possible to do that with above approach.use a for loop to generate the code. – Priyamal Jun 01 '16 at 03:33
0

A way of doing this if you do not mind a bit of extra memory used, would be to have a map from id=>Value inside A.

In the constructor of A, assign the values to the map. When getting the required value , you could just do map.get(id), potentially casting to the required format.

AndreiV
  • 99
  • 4