View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.kuali.ole.ingest.pojo.OleLocationGroup;
5   import org.kuali.ole.ingest.pojo.OleLocationIngest;
6   
7   import java.io.IOException;
8   import java.net.URISyntaxException;
9   import java.util.List;
10  
11  /**
12   * OleLocationObjectGeneratorFromXML is used as a converter which converts xml string to
13   * OleLocationGroup object and OleLocationGroup object to xml string
14   */
15  public class OleLocationObjectGeneratorFromXML {
16      /**
17       *  This method returns OleLocationGroup.
18       *  This method build the LocationGroup based on file content.
19       * @param fileContent
20       * @return OleLocationGroup
21       * @throws java.net.URISyntaxException
22       * @throws java.io.IOException
23       */
24     public OleLocationGroup buildLocationFromFileContent(String fileContent) throws URISyntaxException, IOException {
25  
26          XStream xStream = new XStream();
27          xStream.alias("locationGroup", OleLocationGroup.class);
28          xStream.alias("location", OleLocationIngest.class);
29          xStream.addImplicitCollection(OleLocationGroup.class,"locationGroup");
30          Object object =  xStream.fromXML(fileContent);
31          return (OleLocationGroup)object;
32     }
33  
34      /**
35       *   This method  returns xml.
36       *   This method  convert the oleLocationIngestList into xml.
37       * @param oleLocationIngestList
38       * @return  stringBuffer
39       * @throws java.net.URISyntaxException
40       * @throws java.io.IOException
41       */
42      public String toXML(List<OleLocationIngest> oleLocationIngestList) throws URISyntaxException, IOException {
43  
44          StringBuffer stringBuffer = new StringBuffer();
45          stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<locationGroup xmlns=\"http://ole.kuali.org/standards/ole-location\">");
46          stringBuffer.append("\n");
47          for(OleLocationIngest oleLocationIngest : oleLocationIngestList){
48              XStream xStream = new XStream();
49              xStream.alias("location", OleLocationIngest.class);
50              xStream.addImplicitCollection(OleLocationGroup.class,"locationGroup");
51              String xml = xStream.toXML(oleLocationIngest);
52              stringBuffer.append(xml);
53              stringBuffer.append("\n");
54          }
55          stringBuffer.append("</locationGroup>");
56          return stringBuffer.toString();
57      }
58  }