001package org.kuali.ole.ingest;
002
003import com.thoughtworks.xstream.XStream;
004import org.kuali.ole.ingest.pojo.OleLocationGroup;
005import org.kuali.ole.ingest.pojo.OleLocationIngest;
006
007import java.io.IOException;
008import java.net.URISyntaxException;
009import java.util.List;
010
011/**
012 * OleLocationObjectGeneratorFromXML is used as a converter which converts xml string to
013 * OleLocationGroup object and OleLocationGroup object to xml string
014 */
015public class OleLocationObjectGeneratorFromXML {
016    /**
017     *  This method returns OleLocationGroup.
018     *  This method build the LocationGroup based on file content.
019     * @param fileContent
020     * @return OleLocationGroup
021     * @throws java.net.URISyntaxException
022     * @throws java.io.IOException
023     */
024   public OleLocationGroup buildLocationFromFileContent(String fileContent) throws URISyntaxException, IOException {
025
026        XStream xStream = new XStream();
027        xStream.alias("locationGroup", OleLocationGroup.class);
028        xStream.alias("location", OleLocationIngest.class);
029        xStream.addImplicitCollection(OleLocationGroup.class,"locationGroup");
030        Object object =  xStream.fromXML(fileContent);
031        return (OleLocationGroup)object;
032   }
033
034    /**
035     *   This method  returns xml.
036     *   This method  convert the oleLocationIngestList into xml.
037     * @param oleLocationIngestList
038     * @return  stringBuffer
039     * @throws java.net.URISyntaxException
040     * @throws java.io.IOException
041     */
042    public String toXML(List<OleLocationIngest> oleLocationIngestList) throws URISyntaxException, IOException {
043
044        StringBuffer stringBuffer = new StringBuffer();
045        stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<locationGroup xmlns=\"http://ole.kuali.org/standards/ole-location\">");
046        stringBuffer.append("\n");
047        for(OleLocationIngest oleLocationIngest : oleLocationIngestList){
048            XStream xStream = new XStream();
049            xStream.alias("location", OleLocationIngest.class);
050            xStream.addImplicitCollection(OleLocationGroup.class,"locationGroup");
051            String xml = xStream.toXML(oleLocationIngest);
052            stringBuffer.append(xml);
053            stringBuffer.append("\n");
054        }
055        stringBuffer.append("</locationGroup>");
056        return stringBuffer.toString();
057    }
058}