com.freedomotic.persistence.PayloadConverter Maven / Gradle / Ivy
/**
*
* Copyright (c) 2009-2014 Freedomotic team
* http://freedomotic.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Freedomotic; see the file COPYING. If not, see
* .
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.freedomotic.persistence;
import com.freedomotic.rules.Payload;
import com.freedomotic.rules.Statement;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
/**
*
* @author Enrico
*/
public class PayloadConverter
implements Converter {
/**
*
* @param o
* @param writer
* @param mc
*/
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
Payload payload = (Payload) o;
writer.startNode("payload");
Iterator it = payload.iterator();
while (it.hasNext()) {
Statement statement = it.next();
writer.startNode("com.freedomotic.reactions.Statement");
writer.startNode("logical");
writer.setValue(statement.getLogical());
writer.endNode(); //
writer.startNode("attribute");
writer.setValue(statement.getAttribute());
writer.endNode(); //
writer.startNode("operand");
writer.setValue(statement.getOperand());
writer.endNode(); //
writer.startNode("value");
writer.setValue(statement.getValue());
writer.endNode(); //
writer.endNode(); //
}
writer.endNode(); //
}
/**
*
* @param reader
* @param uc
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
Payload payload = new Payload();
reader.moveDown(); //goes down to
while (reader.hasMoreChildren()) { // are the childs of payload
reader.moveDown();
ArrayList statementValues = new ArrayList();
while (reader.hasMoreChildren()) { //childs of statement (logical, attribute, ...)
reader.moveDown();
statementValues.add(reader.getValue());
reader.moveUp();
}
payload.addStatement(statementValues.get(0),
statementValues.get(1),
statementValues.get(2),
statementValues.get(3));
reader.moveUp(); //next
} //no more (childs of payload)
reader.moveUp(); //goes up to the next
reader.moveUp(); //goes up to the next
return payload;
}
/**
*
* @param clazz
* @return
*/
@Override
public boolean canConvert(Class clazz) {
return clazz.equals(Payload.class);
}
private static final Logger LOG = Logger.getLogger(PayloadConverter.class.getName());
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy