android


How to annotation order field when using XStream on Android?


I need to generate XML from java objects on Android. XML nodes must be in
definite sequence. Here is what I got(These fields as according to alphabetical order):
<soap:Envelope xmlns:cwmp="urn:dslforum-org:cwmp-1-0"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<cwmp:Inform>
<CurrentTime>2013-06-07 07:11:20.561 UTC</CurrentTime>
<DeviceId>
<OUI>002615</OUI>
<Manufacturer>Conexant</Manufacturer>
<ProductClass>ADSL2+ IAD</ProductClass>
<SerialNumber>00261559a496</SerialNumber>
</DeviceId>
<Event soapenc:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<CommandKey></CommandKey>
<EventCode>0 BOOTSTRAP</EventCode>
</EventStruct>
</Event>
<ParameterList soapenc:arrayType="cwmp:ParameterValueStruct[1]">
<ParameterValueStruct>
<name>InternetGatewayDevice.DeviceInfo.SpecVersion</name>
<value>1.0</value>
</ParameterValueStruct>
</ParameterList>
<MaxEnvelopes>1</MaxEnvelopes>
<RetryCount>0</RetryCount>
</cwmp:Inform>
</soap:Body>
<soap:Header>
<ID soap:mustUnderstand="1">
<string>00001</string>
</ID>
</soap:Header>
</soap:Envelope>
Here is what I want it to be:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<ID soap:mustUnderstand="1">
<string>00001</string>
</ID>
</soap:Header>
<soap:Body>
<cwmp:Inform>
<DeviceId>
<Manufacturer>Conexant</Manufacturer>
<OUI>002615</OUI>
<ProductClass>ADSL2+ IAD</ProductClass>
<SerialNumber>00261559a496</SerialNumber>
</DeviceId>
<Event soapenc:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<EventCode>0 BOOTSTRAP</EventCode>
<CommandKey></CommandKey>
</EventStruct>
</Event>
<MaxEnvelopes>0</MaxEnvelopes>
<CurrentTime>2013-06-07 07:16:54.128 UTC</CurrentTime>
<RetryCount>0</RetryCount>
<ParameterList soapenc:arrayType="cwmp:ParameterValueStruct[1]">
<ParameterValueStruct>
<name>InternetGatewayDevice.DeviceInfo.SpecVersion</name>
<value>1.0</value>
</ParameterValueStruct>
</ParameterList>
</cwmp:Inform>
</soap:Body>
</soap:Envelope>
I've investigated this problem more. And I found that Xstream handles fields in
wrong order on Android. Xstream handles fields well in Java project.
#XStreamAlias("cwmp:Inform")
public class Inform {
#XStreamAlias("DeviceId")
private DeviceIdStruct deviceId;
#XStreamAlias("Event")
private EventList event;
#XStreamAlias("MaxEnvelopes")
private int maxEnvelopes;
#XStreamAlias("CurrentTime")
private Date currentTime;
#XStreamAlias("RetryCount")
private int retryCount;
#XStreamAlias("ParameterList")
private ParameterValueList parameterList;
}
Here is my answer.
I researched the issue and concluded that Android returns fields in alphabetical order of field types.
You can inherit FieldKeySorter to set order.
First you need create annotation that will define fields order:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface XMLSequence {
String[] value();
}
And example using:
#XMLSequence({
"accountIds",
"addresses",
"birthDate",
"contact",
"name",
"status",
})
Then I've inherited FieldKeySorter :
public class SequenceFieldKeySorter implements FieldKeySorter {
#Override
public Map sort(final Class type, final Map keyedByFieldKey) {
Annotation sequence = type.getAnnotation(XMLSequence.class);
if (sequence != null) {
final String[] fieldsOrder = ((XMLSequence) sequence).value();
Map result = new OrderRetainingMap();
Set<Map.Entry<FieldKey, Field>> fields = keyedByFieldKey.entrySet();
for (String fieldName : fieldsOrder) {
if (fieldName != null) {
for (Map.Entry<FieldKey, Field> fieldEntry : fields) {
if
(fieldName.equals(fieldEntry.getKey().getFieldName())) {
result.put(fieldEntry.getKey(),
fieldEntry.getValue());
}
}
}
}
return result;
} else {
return keyedByFieldKey;
}
}
}
And finally go:
XStream x = new XStream(new PureJavaReflectionProvider(
new FieldDictionary(new SequenceFieldKeySorter())));
You can implement this interface FieldKeySorter for the classes you want to control the order.
You can write an annotation by yourself, which does not only extend the target class but also implement FieldKeySorter at runtime by reflection. Finally replace the instance to control the order.

Related Links

Android - sharing activities/code between different applications
Enabling Camera LED alone without Camera.open in Android?
Paint.setAlpha() sums up, if called several times
How to refresh listview by hitting back button - Android
How to pass data from Unity scene to Android Activity
CLEAR_TOP + start new activity. Hierarchy issue
Android Google GCM - Is it possible to display alert when device is in sleep mode with security enabled?
Error with viewpager
Is there an Answer for EditText cutt off inside TableLayout?
Unable to tweet using Twitter-OAuth in android
phonegap database error in iPad table not found but working in android
Web Page not available on Gingerbread
Collapsing margins in Android layouts
Android Gallery bad image overlapping in CoverFlow
Is this possible to get notification for all emails that arrive on an Android phone?
Fast scroll in Robotium

Categories

HOME
django
grok
api
flex
themes
requirements
shiro
appcelerator
web-hosting
code-coverage
crystal-lang
special-characters
performancepoint
owin
mautic
angular-dart
stellar.js
docx
scsi
firefox-addon
nim
nodemcu
charles
asciidoctor-pdf
poedit
react-toolbox
roundup
ckfinder
expressionengine3
siri
jquery-multiselect
genexus-sd
iis-express
deltaspike
classcastexception
pdw-file-browser
google-earth-engine
amazon-ses
password-protection
odroid
p4merge
urlsession
visa
dcmtk
naming
congestion-control
apollostack
monkey-testing
pom.xml
dynamic-linq
builder
openrasta
globalize
reportservice2010
fclose
google-cse
pair-programming
amq
stdmap
subnet
oracleclient
payload
rsa-archer-grc
ziparchive
dup
php-socket
createprocess
realsense
design-principles
concurrentmodification
toran-proxy
rate-limiting
rubaxa-sortable
iced-coffeescript
linked-tables
shopizer
code-documentation
qtwebengine
ghcjs
punycode
mintty
parallel-testing
xmltodict
eclipse-kepler
gtm-oauth2
rolify
kogrid
diem
mantle
server-administration
paginator
subproject
jdb
luabind
filemerge
ie-compatibility-mode
pos-for-.net
wordprocessingml
perl5.12
int64

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App