[Simple-support] Bug when read XML with ArrayList and HashMap created by simple
Brought to you by:
niallg
|
From: user <xx...@ra...> - 2011-06-07 12:38:57
|
There is a small class which creates default configuration and saves it
into file:
package videocontroldesk.params;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
@Root
public class VCDParams {
private static VCDParams instance;
private static final String filename = "VCDParams.xml";
private static final Logger logger =
Logger.getLogger(VCDParams.class.getName());
@Element
private int selectedLayoutIdx;
@ElementList
private List<LayoutParams> layouts;
@ElementMap
private Map<String, String> ipForCamera;
private VCDParams() {
selectedLayoutIdx = 0;
layouts = new ArrayList<LayoutParams>();
for (int i = 0; i < 4; ++i) {
layouts.add(new LayoutParams());
}
ipForCamera = new HashMap<String, String>();
ipForCamera.put("БК1", "192.168.0.101");
}
public int getLayoutsCount() {
return layouts.size();
}
public LayoutParams getLayout(int layoutIdx) {
return layouts.get(layoutIdx);
}
public static VCDParams getInstance() {
if (instance == null) {
try {
instance = read();
}
catch (Exception ex) {
instance = new VCDParams();
}
}
return instance;
}
private static VCDParams read() throws Exception {
Serializer reader = new Persister();
File f = new File(filename);
return reader.read(VCDParams.class, f);
}
public void save() {
Serializer saver = new Persister();
File f = new File(filename);
try {
saver.write(this, f);
} catch (Exception ex) {
logger.log(Level.WARNING, "Не могу сохранить файл {0}",
filename);
}
}
public static void main(String[] args) {
//VCDParams.getInstance().save();
VCDParams params = VCDParams.getInstance();
//String[] cams =
VCDParams.getInstance().getLayout(0).getAllCamIds();
}
}
If I uncomment "save()" method call I get the following XML:
<VCDParams>
<selectedLayoutIdx>0</selectedLayoutIdx>
<layouts class="java.util.ArrayList">
<layoutParams columnsCount="2" rowsCount="1">
<cell cellId="0">БК1</cell>
</layoutParams>
<layoutParams columnsCount="2" rowsCount="1">
<cell cellId="0">БК1</cell>
</layoutParams>
<layoutParams columnsCount="2" rowsCount="1">
<cell cellId="0">БК1</cell>
</layoutParams>
<layoutParams columnsCount="2" rowsCount="1">
<cell cellId="0">БК1</cell>
</layoutParams>
</layouts>
<ipForCamera class="java.util.HashMap">
<entry>
<string>БК1</string>
<string>192.168.0.101</string>
</entry>
</ipForCamera>
</VCDParams>
It looks OK. Then I comment "save()" and uncomment "
VCDParams.getInstance()" call.
When I set breakpoint right after this call I check the value of
layouts.size(). It return 8 instead of 4.
Please fix it.
Tested on 2.5.3 and 2.4.1.
|