JSF Custom component? How to add Method expression support? - java

i want add Method Expression support on to my custom component.
i've seen that i must write a tagHandler.
i've seen this post :
How to add Method Expression to a custom JSF component
I don't understand what are the parameters "void.class" and "moveEvent" of the methodRule method.
This is my customComponent code :
public class Link extends UIOutput{
private StringBuilder style = new StringBuilder();
private String id = "";
private Boolean active = false;
private String alt = "";
private String href = "";
private String hreflang = "";
private String rel = "";
private String target = "";
private String download = "";
private String media = "";
private String type = "";
public Boolean isActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getHreflang() {
return hreflang;
}
public void setHreflang(String hreflang) {
this.hreflang = hreflang;
}
public String getRel() {
return rel;
}
public void setRel(String rel) {
this.rel = rel;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getDownload() {
return download;
}
public void setDownload(String download) {
this.download = download;
}
public String getMedia() {
return media;
}
public void setMedia(String media) {
this.media = media;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
#Override
public void encodeBegin(FacesContext context)throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = getClientId(context);
if("".equals(id) || id == null){
id = clientId;
}
Object styleClass = (getAttributes().get("styleClass") != null) ? (Boolean)getAttributes().get("styleClass") : "";
writer.startElement("li", this);
if(active){
style.append(" active ");
}
if(!"".equals(styleClass)){
style.append(styleClass);
}
writer.writeAttribute("id", id, null);
writer.writeAttribute("class", style.toString().trim(), null);
writer.startElement("a", this);
writer.writeAttribute("href", href, null);
if(!"".equals(alt)) { writer.writeAttribute("alt", alt, null); }
if(!"".equals(hreflang)) { writer.writeAttribute("hreflang", hreflang, null); }
System.out.println("rel = " + rel);
if(!"".equals(rel)) { writer.writeAttribute("rel", rel, null); }
if(!"".equals(target)) { writer.writeAttribute("target", target, null); }
if(!"".equals(download)) { writer.writeAttribute("download", download, null); }
if(!"".equals(media)) { writer.writeAttribute("media", media, null); }
if(!"".equals(type)) { writer.writeAttribute("type", type, null); }
for (UIComponent child : getChildren()) {
child.encodeAll(context);
}
}
#Override
public Object saveState(FacesContext context) {
Object values[] = new Object[10];
values[0] = super.saveState(context);
values[1] = id;
values[2] = style;
values[3] = active;
values[4] = alt;
values[5] = href;
values[6] = hreflang;
values[7] = rel;
values[8] = target;
values[9] = download;
values[10] = media;
values[110] = type;
return ((Object) (values));
}
#Override
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[])state;
super.restoreState(context, values[0]);
id = (String)values[1];
style.insert(0, values[2]);
active = (Boolean)values[3];
alt = (String)values[4];
href = (String)values[5];
hreflang = (String)values[6];
rel = (String)values[7];
target = (String)values[8];
download = (String)values[9];
media = (String)values[10];
type = (String)values[11];
}
}

Related

Rooms database save and retrieve object using converter

when i was trying to save an object using rooms database, data insertion was successful. while retrieving data, a value inside the object, which was an object, was retrieved as array. Converting the array back to object is not working. Other string, and integer values inside the root object was retrieved properly.
Saved object :
[[F#6c173ab
Retrieved object:
[[0.0066505815,
0.00605235, 0.0044598486, -0.011793762, 0.0069282507, -0.059397057, -0.058320906, 0.17123938, -0.019021105, -0.1037456, 0.0018062548, -0.005069571, 0.0035884415, 0.0017127255, 0.004238736, -0.058450952, -0.007079399, -0.010851874, 9.1687456E-4, 0.013117686, 0.26496187, -0.056337364, 0.054294083, 0.0023271786, 0.19248551, -0.031670928, 0.004658082, -0.042408537, -0.08871324, -0.13611381, 0.0048175915, 0.060110737, -0.05661096, -0.004599247, 0.02567487, -0.14492176, 0.41225138, 0.037969578, -0.0060184584, 0.16407079, -0.007605861, -0.004265492, -0.0011223844, 0.006605887, -0.008077469, 0.0015253898, -0.10189122, 0.050602783, 0.0058309403, 0.057144053, 0.07865115, -0.0022470118, 0.005851626, -0.001395651, -0.061264377, -0.007627005, -0.1531251, -8.3948084E-4, 0.02980912, -0.002113619, -0.0018161217, -0.10022795, 0.12291724, 0.07126657, 1.6772193E-4, 0.022098381, 0.0026675903, 0.03227209, 0.011782837, 0.006000733, 0.009471676, 0.036371186, -0.23033214, -0.002258695, 0.039965842, -0.00845738, 0.0025282402, 6.5926113E-4, 0.03005817, 0.032968543, 0.003514709, 0.03701494, -0.0047574765, 0.0067672534, 0.08913908, 0.0014652971, 0.009258712, -0.03202012, -0.024361957, -0.10464151, -0.033407286, -0.0027610548, -0.00944446, -0.0024300436, -0.0232218, 0.0094572455, 0.018079767, 0.23599343, -0.0063236416, 0.034273528, 5.9618044E-4, 0.010737699, 0.008162582, 0.0025107688, 0.007046912, 0.0026148462, -6.691977E-4, 0.0018003252, -0.017509019, 0.0071865367, 0.17709938, -0.0027926106, 0.0041772844, 0.24446069, 0.0035891477, 0.090311974, -0.005678741, 0.002527207, -0.043642007, 0.013362809, -0.10237697, 0.004665849, 0.029806657, 8.510543E-4, 0.008034529, 0.0037261287, 0.0016963077, -0.0065462743, 0.0014083247, -0.09163139, -7.1395264E-4, 0.0034422423, 2.2180262E-4, -0.050950024, 0.052165035, -6.5167714E-4, 0.09660987, 0.059556033, 0.0011484445, 0.0048205433, 0.016215535, -0.0074894307, -4.8624526E-4, 0.09030299, -0.031675506, 0.035420638, 0.00863125, 0.0062305178, -2.9943243E-4, 0.017759249, -0.007217323, -0.17156895, -0.08075601, 0.013375583, -0.0057125236, 0.0019819727, 0.0045942706, -9.6630485E-4, -0.0056799883, -0.001163379, 0.026658272, 0.0045382543, 0.0053815856, -0.0019246427, -0.0020221453, -0.0036204413, -0.016369255, -0.07884872, 0.0012176841, -0.0077565406, 0.08828778, -0.18202521, 0.0031140386, -0.013568612, 0.03727499, 0.0029328368, -0.009444142, 0.061891776, -0.0021317177, 7.041842E-5, -0.06919885, -0.022822084, -0.009733031, 0.0038881213, 0.2372824, 0.15371381, -0.0267819, -0.048720658, 0.029161548, -0.16049597, 0.023316571, 0.0020684062]]
This is the type converter i used
#TypeConverter
public static String fromRecognition(SimilarityClassifier.Recognition recognition) {
if (recognition == null) {
return (null);
}
Gson gson = new Gson();
Type type = new TypeToken<SimilarityClassifier.Recognition>() {
}.getType();
String json = gson.toJson(recognition, type);
return json;
}
#TypeConverter
public static SimilarityClassifier.Recognition toRecognition(String recognitionString) {
if (recognitionString == null) {
return (null);
}
Gson gson = new Gson();
Type type = new TypeToken<SimilarityClassifier.Recognition>() {
}.getType();
SimilarityClassifier.Recognition recognition = gson.fromJson(recognitionString, type);
return recognition;
}
column in data base is given as
#ColumnInfo(name = "record")
#TypeConverters(Converters.class)
private SimilarityClassifier.Recognition record;
public SimilarityClassifier.Recognition getRecord() {
return record;
}
public void setRecord(SimilarityClassifier.Recognition record) {
this.record = record;
}
similarityclassifier.Recognition is given below
public class Recognition {
private final String id;
private final String title;
private final Float distance;
private Object extra;
private RectF location;
private Integer color;
private Bitmap crop;
public Recognition(
final String id, final String title, final Float distance, final RectF location) {
this.id = id;
this.title = title;
this.distance = distance;
this.location = location;
this.color = null;
this.extra = null;
this.crop = null;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public Float getDistance() {
return distance;
}
public RectF getLocation() {
return new RectF(location);
}
public Bitmap getCrop() {
return this.crop;
}
public Integer getColor() {
return this.color;
}
public Object getExtra() {
return this.extra;
}
public void setExtra(Object extra) {
this.extra = extra;
}
public void setColor(Integer color) {
this.color = color;
}
public void setLocation(RectF location) {
this.location = location;
}
public void setCrop(Bitmap crop) {
this.crop = crop;
}
#Override
public String toString() {
String resultString = "";
if (id != null) {
resultString += "[" + id + "] ";
}
if (title != null) {
resultString += title + " ";
}
if (distance != null) {
resultString += String.format("(%.1f%%) ", distance * 100.0f);
}
if (location != null) {
resultString += location + " ";
}
return resultString.trim();
}
}
If anyone else encountered this issue, please help

Firebase : Serializing arrays is not supported

I am trying to put an object to my firebase database but I am getting the following error saying Array serialization is not supported and I should use Lists instead.
I am a newbie here please help me
Here is the DataPojo object I am trying to put into firebase
public class DataPojo {
private String msg1;
private String msg2;
private String msg3;
private String msg4;
private String msg5;
private String msg6;
private String msg7;
private String msg8;
private String msg9;
private String msg10;
private String msg11;
private String name;
public DataPojo(){}
public DataPojo(String msg1, String msg2, String msg3, String msg4, String msg5, String msg6, String msg7, String msg8, String msg9, String msg10, String msg11, String name) {
this.msg1 = msg1;
this.msg2 = msg2;
this.msg3 = msg3;
this.msg4 = msg4;
this.msg5 = msg5;
this.msg6 = msg6;
this.msg7 = msg7;
this.msg8 = msg8;
this.msg9 = msg9;
this.msg10 = msg10;
this.msg11 = msg11;
this.name = name;
}
public String getMsg9() {
return msg9;
}
public void setMsg9(String msg9) {
this.msg9 = msg9;
}
public String getMsg10() {
return msg10;
}
public void setMsg10(String msg10) {
this.msg10 = msg10;
}
public void setMsg11(String msg11) {
this.msg11 = msg11;
}
public String getMsg11() {
return msg11;
}
public String getMsg1() {
return msg1;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
public String getMsg2() {
return msg2;
}
public void setMsg2(String msg2) {
this.msg2 = msg2;
}
public String getMsg3() {
return msg3;
}
public void setMsg3(String msg3) {
this.msg3 = msg3;
}
public String getMsg4() {
return msg4;
}
public void setMsg4(String msg4) {
this.msg4 = msg4;
}
public String getMsg5() {
return msg5;
}
public void setMsg5(String msg5) {
this.msg5 = msg5;
}
public String getMsg6() {
return msg6;
}
public void setMsg6(String msg6) {
this.msg6 = msg6;
}
public String getMsg7() {
return msg7;
}
public void setMsg7(String msg7) {
this.msg7 = msg7;
}
public String getMsg8() {
return msg8;
}
public void setMsg8(String msg8) {
this.msg8 = msg8;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getMessages() {
String[] messages = new String[11];
messages[0] = msg1;
messages[1] = msg2;
messages[2] = msg3;
messages[3] = msg4;
messages[4] = msg5;
messages[5] = msg6;
messages[6] = msg7;
messages[7] = msg8;
messages[8] = msg9;
messages[9] = msg10;
messages[10] = msg11;
return messages;
}
}
Why is it giving me that error? is it because of the getMessages() method? I do not have an array field declared though.

Handle deep nested complex xml with FreeMarker

This is a part of a complex xml template which i put here for the question :
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>${MsgId}</MsgId>
<CreDtTm>${CreDtTm}</CreDtTm>
<NbOfTxs>${NbOfTxs}</NbOfTxs>
<a> ${val1}
<b>
${val2}
</b>
</a>
<CtrlSum>${CtrlSum}</CtrlSum>
</GrpHdr>
<PmtInf>
<PmtInfId>${PmtInfId}</PmtInfId>
<PmtMtd>${PmtMtd}</PmtMtd>
</PmtInf>
<#list persons as person>
</#list>
</CstmrDrctDbtInitn>
</Document>
I have been using FreeMarker for the previous month and until now the xml models have been easy
Searching over the web on how to approach this template , should i create matching java classes (100 of them ? )... should i use Map? like shown here .
I don't have a clue how to do it ... how to apply FreeMarker on this template ?
So here is a solution for any future users . Firstly i created 3 models which i am calling like below :
Map<String, Object> data = new HashMap<>();
//================= Example Creating HeaderVo //=================
HeaderVo header = new HeaderVo("la","la",5,5,"la","la");
data.put("header", header);
//================= Example Creating MiddleVo //=================
MiddleVo middleVo = new MiddleVo("la","la",5,"la","la","la","la");
data.put("middle", middleVo);
//================= Example Creating internal items =================
InternalVo v1 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","EUR");
InternalVo v2 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","DOLLARS");
InternalVo v3 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","LAT");
//List parsing
List<InternalVo> internalVos = new ArrayList<>();
internalVos.add(v1);
internalVos.add(v2);
internalVos.add(v3);
data.put("vos", internalVos);
//================= //================= //================= //=================
final String message = this.templateManager.composeStringFromTemplate(data, "bankfile.ftl");
So below is the bankfile.ftl which represents the complex xml :
```
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02">
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>${header.msgId}</MsgId>
<CreDtTm>${header.creDtTm}</CreDtTm>
<NbOfTxs>${header.nbOfTxs}</NbOfTxs>
<CtrlSum>${header.ctrlSum}</CtrlSum>
<InitgPty>
<Nm>${header.nm}</Nm>
<Id>
<OrgId>
<Othr>
<Id>${header.id}</Id>
</Othr>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtTpInf>
<SvcLvl>
<Cd>${middle.svcLvlCD}</Cd>
</SvcLvl>
<LclInstrm>
<Cd>${middle.lclInstrmCD}</Cd>
</LclInstrm>
<SeqTp>${middle.seqTp}</SeqTp>
</PmtTpInf>
<ReqdColltnDt>${middle.reqdColltnDt}</ReqdColltnDt>
<Cdtr>
<Nm>${middle.nm}</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>${middle.iBAN}</IBAN>
</Id>
</CdtrAcct>
<CdtrAgt>
<FinInstnId>
<BIC>${middle.bIC}</BIC>
</FinInstnId>
</CdtrAgt>
<#list vos as vo>
<DrctDbtTxInf> <PmtId> <EndToEndId>${vo.endToEndId}</EndToEndId> </PmtId> <InstdAmt Ccy="${vo.ccy}">${vo.instdAmt}</InstdAmt> <DrctDbtTx> <MndtRltdInf> <MndtId>${vo.mndtId}</MndtId> <DtOfSgntr>${vo.dtOfSgntr}</DtOfSgntr> <AmdmntInd>${vo.amdmntInd?c}</AmdmntInd> </MndtRltdInf> </DrctDbtTx> <DbtrAgt> <FinInstnId> <BIC>${vo.bIC}</BIC> </FinInstnId> </DbtrAgt> <Dbtr> <Nm>${vo.nm}</Nm> </Dbtr> <DbtrAcct> <Id> <IBAN>${vo.iBAN}</IBAN> </Id> </DbtrAcct> </DrctDbtTxInf>
</#list>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>
```
And finally here are the 3 models used for the FTL ( sorry for them being huge , i just wanted to so how complex it can get :) ):
HeaderVo
public class HeaderVo {
private String msgId;
private String creDtTm;
private int nbOfTxs;
private int ctrlSum;
private String nm;
private String id;
public HeaderVo() {
super();
}
public HeaderVo(String msgId, String creDtTm, int nbOfTxs, int ctrlSum, String nm, String id) {
super();
this.msgId = msgId;
this.creDtTm = creDtTm;
this.nbOfTxs = nbOfTxs;
this.ctrlSum = ctrlSum;
this.nm = nm;
this.id = id;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getCreDtTm() {
return creDtTm;
}
public void setCreDtTm(String creDtTm) {
this.creDtTm = creDtTm;
}
public int getNbOfTxs() {
return nbOfTxs;
}
public void setNbOfTxs(int nbOfTxs) {
this.nbOfTxs = nbOfTxs;
}
public int getCtrlSum() {
return ctrlSum;
}
public void setCtrlSum(int ctrlSum) {
this.ctrlSum = ctrlSum;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MiddleVo
public class MiddleVo {
private String svcLvlCD;
private String lclInstrmCD;
private int seqTp;
private String reqdColltnDt;
private String nm;
private String iBAN;
private String bIC;
public MiddleVo() {
super();
}
public MiddleVo(String svcLvlCD, String lclInstrmCD, int seqTp, String reqdColltnDt, String nm, String iBAN,
String bIC) {
super();
this.svcLvlCD = svcLvlCD;
this.lclInstrmCD = lclInstrmCD;
this.seqTp = seqTp;
this.reqdColltnDt = reqdColltnDt;
this.nm = nm;
this.iBAN = iBAN;
this.bIC = bIC;
}
public String getSvcLvlCD() {
return svcLvlCD;
}
public void setSvcLvlCD(String svcLvlCD) {
this.svcLvlCD = svcLvlCD;
}
public String getLclInstrmCD() {
return lclInstrmCD;
}
public void setLclInstrmCD(String lclInstrmCD) {
this.lclInstrmCD = lclInstrmCD;
}
public int getSeqTp() {
return seqTp;
}
public void setSeqTp(int seqTp) {
this.seqTp = seqTp;
}
public String getReqdColltnDt() {
return reqdColltnDt;
}
public void setReqdColltnDt(String reqdColltnDt) {
this.reqdColltnDt = reqdColltnDt;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getiBAN() {
return iBAN;
}
public void setiBAN(String iBAN) {
this.iBAN = iBAN;
}
public String getbIC() {
return bIC;
}
public void setbIC(String bIC) {
this.bIC = bIC;
}
}
InternalVo
public class InternalVo {
private int endToEndId;
private int instdAmt;
private int mndtId;
private String dtOfSgntr;
private boolean amdmntInd;
private String bIC;
private String nm;
private String iBAN;
private String ccy;
public InternalVo() {
super();
}
public InternalVo(int endToEndId, int instdAmt, int mndtId, String dtOfSgntr, boolean amdmntInd, String bIC, String nm,
String iBAN,String ccy) {
super();
this.endToEndId = endToEndId;
this.instdAmt = instdAmt;
this.mndtId = mndtId;
this.dtOfSgntr = dtOfSgntr;
this.amdmntInd = amdmntInd;
this.bIC = bIC;
this.nm = nm;
this.iBAN = iBAN;
this.ccy = ccy;
}
public int getEndToEndId() {
return endToEndId;
}
public void setEndToEndId(int endToEndId) {
this.endToEndId = endToEndId;
}
public int getInstdAmt() {
return instdAmt;
}
public void setInstdAmt(int instdAmt) {
this.instdAmt = instdAmt;
}
public int getMndtId() {
return mndtId;
}
public void setMndtId(int mndtId) {
this.mndtId = mndtId;
}
public String getDtOfSgntr() {
return dtOfSgntr;
}
public void setDtOfSgntr(String dtOfSgntr) {
this.dtOfSgntr = dtOfSgntr;
}
public boolean isAmdmntInd() {
return amdmntInd;
}
public void setAmdmntInd(boolean amdmntInd) {
this.amdmntInd = amdmntInd;
}
public String getbIC() {
return bIC;
}
public void setbIC(String bIC) {
this.bIC = bIC;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getiBAN() {
return iBAN;
}
public void setiBAN(String iBAN) {
this.iBAN = iBAN;
}
public String getCcy() {
return ccy;
}
public void setCcy(String ccy) {
this.ccy = ccy;
}

Java - Why stackoverflow error in my JSON method?

I need your help to understand why in my method the object allocation returns Stack Overflow error.
Here is the method:
public String toJson(){
JSONObject json;
json = new JSONObject(this); //error happens here...
return json.toString();
}
Here all code from this class:
package com.neocloud.model;
import java.io.Serializable;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.dao.GroupDAO;
import com.neocloud.model.dao.SubscriptionDAO;
public class Subscription implements Serializable{
private static final long serialVersionUID = -3901714994276495605L;
private long id;
private Device dispositivo;
private Group grupo;
private String subscription;
public Subscription(){
clear();
}
public void clear(){
id = 0;
subscription = "";
grupo = new Group();
dispositivo = new Device();
}
public String toJson(){
JSONObject json;
json = new JSONObject(this); //error happens here...
return json.toString();
}
public String registerDB(boolean registrarSNS){
GroupDAO gDAO = new GroupDAO();
DeviceDAO dDAO = new DeviceDAO();
gDAO.selectById(this.grupo);
dDAO.selectById(this.dispositivo);
if (registrarSNS)
registerSNS(this.dispositivo.getEndPointArn(), this.grupo.getTopicArn());
SubscriptionDAO sDAO = new SubscriptionDAO();
return sDAO.insert(this);
}
public String registerSNS(String endpointArn, String topicArn){
String resposta;
AwsSns sns = AwsSns.getInstance();
resposta = ""+sns.addSubscriptionToTopic(endpointArn, topicArn);
setSubscription(resposta);
return resposta;
}
public String delete(){
AwsSns sns = AwsSns.getInstance();
SubscriptionDAO sDAO = new SubscriptionDAO();
sDAO.selectById(this);
sns.deleteSubscriptionFromTopic(this.subscription);
return sDAO.delete(this);
}
public long getId() {
return id;
}
public void setId(long id) {
if (this.id != id)
this.id = id;
}
public String getSubscription() {
return subscription;
}
public void setSubscription(String subscription) {
if (subscription != null){
if (!this.subscription.equals(subscription))
this.subscription = subscription;
}else
this.subscription = new String();
}
public Device getDispositivo() {
return dispositivo;
}
public void setDispositivo(Device dispositivo) {
if (dispositivo != null){
if (dispositivo.getId() != this.dispositivo.getId())
this.dispositivo = dispositivo;
}else
this.dispositivo = new Device();
}
public Group getGrupo() {
return grupo;
}
public void setGrupo(Group grupo) {
if (grupo != null){
if (grupo.getId() != this.grupo.getId())
this.grupo = grupo;
}else
this.grupo = new Group();
}
}
Here is the Device class:
package com.neocloud.model;
import java.io.Serializable;
import java.util.ArrayList;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.enums.EEnvironment;
import com.neocloud.model.enums.EProduct;
import com.neocloud.model.enums.ESystem;
public class Device implements Serializable{
private static final long serialVersionUID = 8515474788917476721L;
private long id;
private String development;
private String SSL = "tls://gateway.push.apple.com:2195";
private String feedback = "tls://feedback.push.apple.com:2196";
private String sandboxSSL = "tls://gateway.sandbox.push.apple.com:2195";
private String sandboxFeedback = "tls://feedback.sandbox.push.apple.com:2196";
private String message;
private String endPointArn;
private String deviceToken;
private EProduct appID;
private String appVersion;
private String deviceUID;
private String deviceName;
private String deviceModel;
private String deviceVersion;
private boolean pushBadge;
private boolean pushAlert;
private boolean pushSound;
private ESystem system;
private EEnvironment environment;
private ArrayList<Subscription> subscriptions;
private String deviceUser;
public Device(){
clear();
}
public String deleteToken(){
/*
*
* terminar aqui ainda...
*
DeviceDAO dDAO = new DeviceDAO();
AwsSns sns = AwsSns.getInstance();
sns.deleteDeviceEndpoint(getEndPointArn());
ArrayList<String> subscriptions = dDAO.getSubscriptionsFromEndpointArn(this);
for (int i=0; i<subscriptions.size();i++){
sns.deleteSubscriptionFromTopic(subscriptions.get(i));
}
return dDAO.delete(this);
*/ return "";
}
public String updateToken(){
if ((system == ESystem.IOS) && (deviceToken.length() != 64))
return "deviceTokenlen64";
AwsSns sns = AwsSns.getInstance();
String jsonResposta = ""+sns.updateDeviceEndpoint(getEndPointArn(), deviceToken);
DeviceDAO dDAO = new DeviceDAO();
dDAO.update(this);
return jsonResposta;
}
public String registerDevice(){
if (appVersion.length() == 0)
return "applen0";
if (deviceUID.length() > 40)
return "deviceUID40";
if ((system == ESystem.IOS) && (deviceToken.length() != 64))
return "deviceTokenlen64";
if (deviceName.length() == 0)
return "deviceNamelen0";
if (deviceModel.length() == 0)
return "deviceModellen0";
if (deviceVersion.length() == 0)
return "deviceVersionlen0";
AwsSns sns = AwsSns.getInstance();
String jsonResposta = null;
switch (getAppID()) {
case MODULE: {
if (system == ESystem.IOS)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEIOS);
else if (system == ESystem.ANDROID)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEANDROID);
break;
}
case HOSTPRO: {
if (system == ESystem.IOS)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROIOS);
else if (system == ESystem.ANDROID)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROANDROID);
break;
}
case CONNEXOON: {
if (system == ESystem.IOS)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONIOS);
else if (system == ESystem.ANDROID)
jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONANDROID);
break;
}
case TAHOMA: {
break;
}
case MINIBOX: {
break;
}
case NONE: {
}
default: {
}
}
if (jsonResposta != null){
JSONObject json;
try {
jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
jsonResposta = jsonResposta.replace("{EndPointArn:", "{\"EndPointArn\":");
json = new JSONObject(jsonResposta);
setEndPointArn(json.getString("EndpointArn"));
} catch (JSONException e) {
e.printStackTrace();
}
}
Subscription subscription;
switch (getAppID()){
case MODULE: {
subscription = new Subscription();
subscription.getGrupo().setId(19);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEGERAL);
subscriptions.add(subscription);
if (system == ESystem.IOS){
subscription = new Subscription();
subscription.getGrupo().setId(25);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEIOS);
subscriptions.add(subscription);
}
else if (system == ESystem.ANDROID){
subscription = new Subscription();
subscription.getGrupo().setId(22);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEANDROID);
subscriptions.add(subscription);
}
break;
}
case HOSTPRO: {
subscription = new Subscription();
subscription.getGrupo().setId(10);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROGERAL);
subscriptions.add(subscription);
if (system == ESystem.IOS){
subscription = new Subscription();
subscription.getGrupo().setId(16);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROIOS);
subscriptions.add(subscription);
}
else if (system == ESystem.ANDROID){
subscription = new Subscription();
subscription.getGrupo().setId(13);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROANDROID);
subscriptions.add(subscription);
}
break;
}
case CONNEXOON: {
subscription = new Subscription();
subscription.getGrupo().setId(1);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONGERAL);
subscriptions.add(subscription);
if (system == ESystem.IOS){
subscription = new Subscription();
subscription.getGrupo().setId(7);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONIOS);
subscriptions.add(subscription);
}
else if (system == ESystem.ANDROID){
subscription = new Subscription();
subscription.getGrupo().setId(4);
subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONANDROID);
subscriptions.add(subscription);
}
break;
}
case TAHOMA: {
break;
}
case MINIBOX: {
break;
}
case NONE: {
}
default: {
}
}
DeviceDAO dDAO = new DeviceDAO();
return dDAO.insert(this);
}
public void clear(){
development = "";
message = "";
endPointArn = "";
deviceToken = "";
appID = EProduct.NONE;
appVersion = "";
deviceUID = "";
deviceName = "";
deviceModel = "";
deviceVersion = "";
pushBadge = false;
pushAlert = false;
pushSound = false;
system = ESystem.NONE;
environment = EEnvironment.HOMOLOGATION;
subscriptions = new ArrayList<Subscription>();
deviceUser = "";
}
public String toJson(){
JSONObject json;
json = new JSONObject(this);
return json.toString();
}
public String getDevelopment() {
return development;
}
public void setDevelopment(String development) {
if (development != null){
if (!development.equals(this.development))
this.development = development;
}
this.development = new String();
}
public String getSSL() {
return SSL;
}
public void setSSL(String sSL) {
if (sSL != null){
if (!sSL.equals(this.SSL))
this.SSL = sSL;
}
else
this.SSL = new String();
}
public String getFeedback() {
return feedback;
}
public void setFeedback(String feedback) {
if (feedback != null){
if (!feedback.equals(this.feedback))
this.feedback = feedback;
}
this.feedback = new String();
}
public String getSandboxSSL() {
return sandboxSSL;
}
public void setSandboxSSL(String sandboxSSL) {
if (sandboxSSL != null){
if (!sandboxSSL.equals(this.sandboxSSL))
this.sandboxSSL = sandboxSSL;
}else
this.sandboxSSL = new String();
}
public String getSandboxFeedback() {
return sandboxFeedback;
}
public void setSandboxFeedback(String sandboxFeedback) {
if (sandboxFeedback != null){
if (!sandboxFeedback.equals(this.sandboxFeedback))
this.sandboxFeedback = sandboxFeedback;
}else
this.sandboxFeedback = new String();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if (message != null){
if (!message.equals(this.message))
this.message = message;
}else
this.message = new String();
}
public String getEndPointArn() {
return endPointArn;
}
public void setEndPointArn(String createPlatformEndpointResult) {
if (createPlatformEndpointResult != null){
if (!createPlatformEndpointResult.equals(this.endPointArn))
this.endPointArn = createPlatformEndpointResult;
}else
this.endPointArn = new String();
}
public String getDeviceToken() {
return deviceToken;
}
public void setDeviceToken(String deviceToken) {
if (deviceToken != null){
if (!deviceToken.equals(this.deviceToken))
this.deviceToken = deviceToken;
}else
this.deviceToken = new String();
}
public EProduct getAppID() {
return appID ;
}
public void setAppID(EProduct appID){
if (appID != this.appID)
this.appID = appID;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
if (appVersion != null){
if (!appVersion.equals(this.appVersion))
this.appVersion = appVersion;
}else
this.appVersion = new String();
}
public String getDeviceUID() {
return deviceUID;
}
public void setDeviceUID(String deviceUID) {
if (deviceUID != null){
if (!deviceUID.equals(this.deviceUID))
this.deviceUID = deviceUID;
}else
this.deviceUID = new String();
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
if (deviceName != null){
if (!deviceName.equals(this.deviceName))
this.deviceName = deviceName;
}else
this.deviceName = new String();
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
if (deviceModel != null){
if (!deviceModel.equals(this.deviceModel))
this.deviceModel = deviceModel;
}else
this.deviceModel = new String();
}
public String getDeviceVersion() {
return deviceVersion;
}
public void setDeviceVersion(String deviceVersion) {
if (deviceVersion != null){
if (!deviceVersion.equals(this.deviceVersion))
this.deviceVersion = deviceVersion;
}else
this.deviceVersion = new String();
}
public boolean isPushBadge() {
return pushBadge;
}
public void setPushBadge(boolean pushBadge) {
if (pushBadge != this.pushBadge)
this.pushBadge = pushBadge;
}
public boolean isPushAlert() {
return pushAlert;
}
public void setPushAlert(boolean pushAlert) {
if (pushAlert != this.pushAlert)
this.pushAlert = pushAlert;
}
public boolean isPushSound() {
return pushSound;
}
public void setPushSound(boolean pushSound) {
if (this.pushSound != pushSound)
this.pushSound = pushSound;
}
public ESystem getSystem() {
return system;
}
public void setTipoSmartphone(ESystem system) {
if (system != this.system)
this.system = system;
}
public EEnvironment getEnvironment() {
return environment;
}
public void setEnvironment(EEnvironment environment) {
if (environment != this.environment)
this.environment = environment;
}
public long getId() {
return id;
}
public void setId(long id) {
if (this.id != id)
this.id = id;
}
public void setSystem(ESystem system) {
if (system != this.system)
this.system = system;
}
public ArrayList<Subscription> getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(ArrayList<Subscription> subscriptions) {
if (subscriptions != null)
this.subscriptions = subscriptions;
else
this.subscriptions = new ArrayList<Subscription>();
}
public String getDeviceUser() {
return deviceUser;
}
public void setDeviceUser(String deviceUser) {
if (deviceUser != null){
if (!deviceUser.equals(this.deviceUser))
this.deviceUser = deviceUser;
}else
this.deviceUser = new String();
}
}
Here is the Group class:
package com.neocloud.model;
import java.io.Serializable;
import java.util.ArrayList;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.GroupDAO;
public class Group implements Serializable{
private static final long serialVersionUID = -5032857327241801763L;
private long id;
private String nome;
private String topicArn;
public Group(){
this.clear();
}
public void clear(){
this.id = 0;
this.nome = new String();
this.topicArn = new String();
}
public String registerGrupo(){
AwsSns snsClient = AwsSns.getInstance();
String topic = ""+snsClient.createTopic(this.nome);
JSONObject json;
String jsonResposta = topic;
try {
jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
jsonResposta = jsonResposta.replace("{TopicArn:", "{\"TopicArn\":");
json = new JSONObject(jsonResposta);
setTopicArn(json.getString("TopicArn"));
} catch (JSONException e) {
e.printStackTrace();
}
GroupDAO grupoDAO = new GroupDAO();
return grupoDAO.insert(this);
}
public String deleteGrupo(){
AwsSns snsClient = AwsSns.getInstance();
GroupDAO grupoDAO = new GroupDAO();
ArrayList<Group> grupos = grupoDAO.select(this);
if (grupos.size() > 0){
for (int i = 0; i<grupos.size(); i++){
this.topicArn = grupos.get(i).getTopicArn();
this.nome = grupos.get(i).getNome();
this.id = grupos.get(i).getId();
snsClient.deleteTopic(this.topicArn);
}
return grupoDAO.delete(this);
}
return "-1";
}
public String toJson(){
JSONObject json;
json = new JSONObject(this);
return json.toString();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTopicArn() {
return topicArn;
}
public void setTopicArn(String topicArn) {
this.topicArn = topicArn;
}
}
Here is the Tomcat log:
Exception in thread "http-bio-8080-exec-5"
java.lang.StackOverflowError at
java.lang.reflect.Executable.(Unknown Source) at
java.lang.reflect.Method.(Unknown Source) at
java.lang.reflect.Method.copy(Unknown Source) at
java.lang.reflect.ReflectAccess.copyMethod(Unknown Source) at
sun.reflect.ReflectionFactory.copyMethod(Unknown Source) at
java.lang.Class.copyMethods(Unknown Source) at
java.lang.Class.getMethods(Unknown Source) at
com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:930)
at com.amazonaws.util.json.JSONObject.(JSONObject.java:285) at
com.amazonaws.util.json.JSONObject.wrap(JSONObject.java:1540) at
com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:960)
Thanks for all help!
Maybe a circular reference in your object.
json = new JSONObject(this);
You're trying to JSONify this
id = 0;
subscription = "";
grupo = new Group();
dispositivo = new Device();
And i think that somehow, it cant JSONify Group and Device. Could you show us the code for those classes ?
UPDATE : In your Subscription.clear() method, you affect a new Group() & a new Device(). When creating a new Device(), the method Device.clear() get called and create a new ArrayList<Subscription>(), that'll call the method Subscription.clear(), that'll affect a new Group() & a new Device() and so on. You're infinitely looping, that's why you get the StackOverflow error. It's weird that you're subscription is made of a Group that is made of an ArrayList of subscription who are also mades of Groups etc.
Maybe you have a circular reference...
Object a = X;
Object c = Y;
a.prop = Y;
c.prop2 = X;
then, your JSON(a) is
{
prop : {
prop2 : {
prop : Y (Y have prop2 again)
/*infinite..*/
}
}
}

Android SAX XML Parser Access Enclosure Tag URL Attribute

I have created an XML parser that contains a detail view. I am reading in an RSS feed that contains an enclosure tag. I am trying to get the URL of the video file from the enclosure tag.
This is what the enclosure tag looks like:
<enclosure url="http://video.calvaryccm.com/main/podcastmp4/MB66-03.mp4" type="video/mp4"/>
Every time I try to retrieve it the parser returns null. What should I do?
This is the part that displays the results from the parser that seems to be causing the problem:
// TESTING HERE!
String enclosure = b.getString("enclosure");
// What is getting displayed
theTitle = b.getString("title").trim();
theDate = newDateStr;
theStory = b.getString("description") + "\n\nView in full website:\n" + b.getString("link")+ "\n\nDownload teaching:\n" + enclosure;
This is the RSSHandler class:
public class RSSHandler extends DefaultHandler
{
RSSFeed _feed;
RSSItem _item;
String _lastElementName = "";
boolean bFoundChannel = false;
final int RSS_TITLE = 1;
final int RSS_LINK = 2;
final int RSS_DESCRIPTION = 3;
final int RSS_ENCLOSURE = 4;
final int RSS_PUBDATE = 5;
int depth = 0;
int currentstate = 0;
/*
* Constructor
*/
RSSHandler()
{
}
/*
* getFeed - this returns the feed when all of the parsing is complete
*/
RSSFeed getFeed()
{
return _feed;
}
public void startDocument() throws SAXException
{
// Initialize RSSFeed object - this will hold parsed contents
_feed = new RSSFeed();
// Initialize the RSSItem object - we will use this as a crutch to grab the info from the channel
// because the channel and items have very similar entries..
_item = new RSSItem();
}
public void endDocument() throws SAXException
{
}
public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
{
depth++;
if (localName.equals("channel"))
{
currentstate = 0;
return;
}
if (localName.equals("image"))
{
// Record feed data - Temporarily stored it in the item
_feed.setTitle(_item.getTitle());
_feed.setPubDate(_item.getPubDate());
}
if (localName.equals("item"))
{
// create a new item
_item = new RSSItem();
return;
}
if (localName.equals("title"))
{
currentstate = RSS_TITLE;
return;
}
if (localName.equals("description"))
{
currentstate = RSS_DESCRIPTION;
return;
}
if (localName.equals("link"))
{
currentstate = RSS_LINK;
return;
}
if (localName.equals("enclosure"))
{
currentstate = RSS_ENCLOSURE;
return;
}
if (localName.equals("pubDate"))
{
currentstate = RSS_PUBDATE;
return;
}
// If I don't explicitly handle the element and to make sure I don't wind up erroneously
// storing a newline or other fake data into one of our existing elements
currentstate = 0;
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
depth--;
if (localName.equals("item"))
{
// Add the item to the list!
_feed.addItem(_item);
return;
}
}
public void characters(char ch[], int start, int length)
{
String theString = new String(ch,start,length);
Log.i("RSSReader","characters[" + theString + "]");
switch (currentstate)
{
case RSS_TITLE:
_item.setTitle(theString);
currentstate = 0;
break;
case RSS_LINK:
_item.setLink(theString);
currentstate = 0;
break;
case RSS_DESCRIPTION:
_item.setDescription(theString);
currentstate = 0;
break;
case RSS_ENCLOSURE:
_item.setEnclosure(theString);
currentstate = 0;
break;
case RSS_PUBDATE:
_item.setPubDate(theString);
currentstate = 0;
break;
default:
return;
}
}
}
This is the RSSFeed class:
public class RSSFeed
{
private String _title = null;
private String _pubdate = null;
private String _enclosure = null;
private int _itemcount = 0;
private List<RSSItem> _itemlist;
RSSFeed()
{
_itemlist = new Vector<RSSItem>(0);
}
int addItem(RSSItem item)
{
_itemlist.add(item);
_itemcount++;
return _itemcount;
}
RSSItem getItem(int location)
{
return _itemlist.get(location);
}
List<RSSItem> getAllItems()
{
return _itemlist;
}
int getItemCount()
{
return _itemcount;
}
void setTitle(String title)
{
_title = title;
}
void setPubDate(String pubdate)
{
_pubdate = pubdate;
}
void setEnclosure(String enclosure)
{
_enclosure = enclosure;
}
String getTitle()
{
return _title;
}
String getPubDate()
{
return _pubdate;
}
String getEnclosure()
{
return _enclosure;
}
}
This is the RSSItem class:
public class RSSItem
{
private String _title = null;
private String _description = null;
private String _link = null;
private String _enclosure = null;
private String _pubdate = null;
RSSItem()
{
}
void setTitle(String title)
{
_title = title;
}
void setDescription(String description)
{
_description = description;
}
void setLink(String link)
{
_link = link;
}
void setEnclosure(String enclosure)
{
_enclosure = enclosure;
}
void setPubDate(String pubdate)
{
_pubdate = pubdate;
}
String getTitle()
{
return _title;
}
String getDescription()
{
return _description;
}
String getLink()
{
return _link;
}
String getEnclosure()
{
return _enclosure;
}
String getPubDate()
{
return _pubdate;
}
public String toString()
{
// limit how much text we display
if (_title.length() > 42)
{
return _title.substring(0, 42) + "...";
}
return _title;
}
}
The url tag is an attribute so to access it you can call attributes.getValue("url"); in startElement when the localName is "enclosure".

Categories

Resources