View Javadoc
1   package org.kuali.ole.handler;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.kuali.ole.bo.cql.CQLModifiers;
5   import org.kuali.ole.bo.cql.CQLPrefixes;
6   import org.kuali.ole.bo.cql.CQLResponseBO;
7   import org.kuali.ole.bo.cql.CQLSearchClauseTag;
8   
9   import java.io.IOException;
10  import java.net.URISyntaxException;
11  
12  public class OleCQLResponseHandler {
13  
14      /**
15       * this method is used to convert the xml generated from cql parser to CQLResponseBO object.
16       *
17       * @param fileContent
18       * @return CQLResponseBO object
19       * @throws URISyntaxException
20       * @throws IOException
21       */
22      public CQLResponseBO fromXML(String fileContent) throws URISyntaxException, IOException {
23  
24          XStream xStream = new XStream();
25          xStream.alias("triple", CQLResponseBO.class);
26          xStream.alias("modifier", CQLModifiers.class);
27          xStream.alias("prefix", CQLPrefixes.class);
28          xStream.aliasField("boolean", CQLResponseBO.class, "booleanTagValue");
29          xStream.aliasField("value", CQLResponseBO.class, "booleanTagValue");
30          xStream.aliasField("leftOperand", CQLResponseBO.class, "leftOperand");
31          xStream.aliasField("rightOperand", CQLResponseBO.class, "rightOperand");
32          xStream.aliasField("searchClause", CQLResponseBO.class, "searchClauseTag");
33          xStream.aliasField("relation", CQLSearchClauseTag.class, "relationTag");
34          Object object = xStream.fromXML(fileContent);
35          return (CQLResponseBO) object;
36      }
37  
38      /**
39       * this method is used to convert the xml generated from cql parser to CQLSearchClauseTag object.
40       *
41       * @param fileContent
42       * @return CQLSearchClauseTag object
43       * @throws URISyntaxException
44       * @throws IOException
45       */
46      public CQLSearchClauseTag fromCQLXML(String fileContent) throws URISyntaxException, IOException {
47  
48          XStream xStream = new XStream();
49          xStream.alias("searchClause", CQLSearchClauseTag.class);
50          xStream.alias("modifier", CQLModifiers.class);
51          xStream.alias("prefix", CQLPrefixes.class);
52          xStream.aliasField("relation", CQLSearchClauseTag.class, "relationTag");
53          Object object = xStream.fromXML(fileContent);
54          return (CQLSearchClauseTag) object;
55      }
56  
57      public String toXML(CQLResponseBO cqlResponseBO) {
58  
59          StringBuffer stringBuffer = new StringBuffer();
60          stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
61          XStream xStream = new XStream();
62          xStream.alias("triple", CQLResponseBO.class);
63          xStream.alias("modifier", CQLModifiers.class);
64          xStream.alias("prefix", CQLPrefixes.class);
65          xStream.aliasField("boolean", CQLResponseBO.class, "booleanTagValue");
66          xStream.aliasField("leftOperand", CQLResponseBO.class, "leftOperand");
67          xStream.aliasField("rightOperand", CQLResponseBO.class, "rightOperand");
68          xStream.aliasField("searchClause", CQLResponseBO.class, "searchClauseTag");
69          xStream.aliasField("relation", CQLSearchClauseTag.class, "relationTag");
70          String xml = xStream.toXML(cqlResponseBO);
71          System.out.println("XML :: " + xml);
72          stringBuffer.append(xml);
73          return stringBuffer.toString();
74      }
75  
76  
77  }