001package org.kuali.ole.handler;
002
003import com.thoughtworks.xstream.XStream;
004import org.kuali.ole.bo.explain.*;
005import org.kuali.ole.converters.*;
006
007/**
008 * Created with IntelliJ IDEA.
009 * User: ?
010 * Date: 7/19/12
011 * Time: 5:12 PM
012 * To change this template use File | Settings | File Templates.
013 */
014public class OleSRUExplainOperationHandler {
015
016    /**
017     * this method converts OleSRUExplainResponse object to an XML with required name space
018     *
019     * @param oleSRUExplainResponse object
020     * @return xml as a string
021     */
022    public String toXML(OleSRUExplainResponse oleSRUExplainResponse) {
023
024        StringBuffer stringBuffer = new StringBuffer();
025        stringBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
026        stringBuffer.append("\n");
027        XStream xStream = new XStream();
028        xStream.alias("sru:explainResponse", OleSRUExplainResponse.class);
029        xStream.aliasField("sru:version", OleSRUExplainResponse.class, "version");
030        xStream.aliasField("sru:record", OleSRUExplainResponse.class, "record");
031        xStream.aliasField("sru:recordPacking", OleSRUExplainRecord.class, "recordPacking");
032        xStream.aliasField("sru:recordSchema", OleSRUExplainRecord.class, "recordSchema");
033        xStream.aliasField("sru:recordData", OleSRUExplainRecord.class, "recordData");
034        xStream.aliasField("zr:explain", OleSRUExplainRecordData.class, "explain");
035        xStream.aliasField("zr:serverInfo", OleSRUExplainOperation.class, "serverInfo");
036        xStream.aliasField("zr:databaseInfo", OleSRUExplainOperation.class, "databaseInfo");
037        xStream.aliasField("zr:indexInfo", OleSRUExplainOperation.class, "indexInfo");
038        xStream.aliasField("zr:schemaInfo", OleSRUExplainOperation.class, "schemaInfo");
039        xStream.aliasField("zr:configInfo", OleSRUExplainOperation.class, "configInfo");
040        xStream.aliasField("zr:host", OleSRUExplainServerInfo.class, "host");
041        xStream.aliasField("zr:port", OleSRUExplainServerInfo.class, "port");
042        xStream.aliasField("zr:database", OleSRUExplainServerInfo.class, "database");
043        //xStream.aliasField("zr:title", OleSRUExplainDatabaseInfo.class, "title");
044        xStream.aliasField("zr:index", OleSRUExplainIndexInfo.class, "index");
045        xStream.aliasField("zr:set", OleSRUExplainIndexInfo.class, "set");
046        xStream.aliasField("zr:map", OleSRUExplainIndex.class, "indexMap");
047        xStream.aliasField("zr:name", OleSRUExplainIndexMap.class, "name");
048        xStream.aliasField("zr:schema", OleSRUExplainSchemaInfo.class, "schema");
049        xStream.aliasField("zr:title", OleSRUExplainSchema.class, "title");
050        xStream.aliasField("zr:default", OleSRUExplainConfigurationInfo.class, "defaultValue");
051        xStream.aliasField("zr:setting", OleSRUExplainConfigurationInfo.class, "setting");
052        xStream.aliasField("zr:supports", OleSRUExplainConfigurationInfo.class, "supports");
053        xStream.registerConverter(new OleSRUExplainServerInfoConverter());
054        xStream.registerConverter(new OleSRUExplainDatabaseInfoConverter());
055        xStream.registerConverter(new OleSRUExplainIndexInfoConverter());
056        xStream.registerConverter(new OleSRUExplainSchemaInfoConverter());
057        xStream.registerConverter(new OleSRUExplainIndexMapNameConverter());
058        xStream.registerConverter(new OleSRUExplainConfigInfoSupportConverter());
059        xStream.registerConverter(new OleSRUExplainConfigInfoSettingConverter());
060        xStream.registerConverter(new OleSRUExplainConfigInfoDefaultConverter());
061        String xml = xStream.toXML(oleSRUExplainResponse);
062        xml = xml.replaceAll("<sru:explainResponse>", "<sru:explainResponse xmlns:sru=\"http://www.loc.gov/zing/srw/\">");
063        xml = xml.replaceAll("<zr:explain>", " <zr:explain xmlns:zr=\"http://explain.z3950.org/dtd/2.1/\">");
064        stringBuffer.append(xml);
065        return stringBuffer.toString();
066    }
067
068
069}