package jadex.xml.tutorial.example11;
import jadex.commons.SUtil;
import jadex.xml.AccessInfo;
import jadex.xml.MappingInfo;
import jadex.xml.ObjectInfo;
import jadex.xml.SubobjectInfo;
import jadex.xml.TypeInfo;
import jadex.xml.XMLInfo;
import jadex.xml.bean.BeanAccessInfo;
import jadex.xml.bean.BeanObjectReaderHandler;
import jadex.xml.bean.BeanObjectWriterHandler;
import jadex.xml.reader.Reader;
import jadex.xml.writer.Writer;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Main class to execute tutorial lesson.
*/
public class Main
{
/**
* Main method for using the xml reader/writer.
*/
public static void main(String[] args) throws Exception
{
// Using collections. Here a number of invoice items are stored in a list.
// Here it is shown how a map can be used as data container.
// As part of the access info a Java-bean specific extra info
// is used, the BeanAccessInfo(). It allows to specify access via
// store/fetch fields or methods and also via a mapname and key access help
// (field/method).
// Writing the map as collection is here sufficient because the map keys
// are also part of the elements in the map.
// new SubobjectInfo(new AccessInfo("item", "items"), null, true)
// There is currently no special support for writing maps, because
// they can be preprocessed in Java before writing.
// Create minimal type infos for types that need to be mapped
Set typeinfos = new HashSet();
typeinfos.add(new TypeInfo(new XMLInfo("invoice"), new ObjectInfo(InvoiceMap.class),
new MappingInfo(null, new SubobjectInfo[]{
new SubobjectInfo(new AccessInfo("item", "item", null, null, new BeanAccessInfo(null, null, "item",
Invoice.class.getMethod("getKey", new Class[0]))))
})));
typeinfos.add(new TypeInfo(new XMLInfo("item"), new ObjectInfo(Invoice.class),
new MappingInfo(null, new SubobjectInfo[]{
new SubobjectInfo(new AccessInfo("product-key", "key"))
})));
// Create an xml reader with standard bean object reader and the
// custom typeinfos
Reader xmlreader = new Reader(new BeanObjectReaderHandler(typeinfos));
InputStream is = SUtil.getResource("jadex/xml/tutorial/example11/data.xml", null);
Object object = xmlreader.read(is, null, null);
is.close();
typeinfos = new HashSet();
typeinfos.add(new TypeInfo(new XMLInfo("invoice"), new ObjectInfo(InvoiceMap.class),
new MappingInfo(null, new SubobjectInfo[]{
new SubobjectInfo(new AccessInfo("item", "items"), null, true)
})));
typeinfos.add(new TypeInfo(new XMLInfo("item"), new ObjectInfo(Map.Entry.class),
new MappingInfo(null, new SubobjectInfo[]{
new SubobjectInfo(new AccessInfo("product-key", "key"))
})));
// Write the xml to the output file.
Writer xmlwriter = new Writer(new BeanObjectWriterHandler(typeinfos, false, true), false);
String xml = Writer.objectToXML(xmlwriter, object, null);
// OutputStream os = new FileOutputStream("out.xml");
// xmlwriter.write(object, os, null, null);
// os.close();
// And print out the result.
System.out.println("Read object: "+object);
System.out.println("Wrote xml: "+xml);
}
}