View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole;
17  
18  import com.thoughtworks.xstream.XStream;
19  import com.thoughtworks.xstream.io.xml.DomDriver;
20  import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
21  import org.kuali.ole.docstore.model.xmlpojo.work.bib.dublin.unqualified.*;
22  import org.kuali.ole.docstore.model.xstream.work.bib.dublin.unqualified.HeaderConverter;
23  import org.kuali.ole.docstore.model.xstream.work.bib.dublin.unqualified.OaiDcDocConverter;
24  import org.kuali.ole.pojo.dublin.unqualified.UnQualifiedDublinRecord;
25  import org.xml.sax.SAXException;
26  
27  import javax.xml.parsers.ParserConfigurationException;
28  import javax.xml.xpath.XPathExpressionException;
29  import java.io.IOException;
30  
31  /**
32   * Class to process Work Bib Dublin Records from and to XML.
33   *
34   * @author Poornima
35   */
36  public class UnQualifiedDublinRecordHandler {
37      /**
38       * Method to covert xml content to WorkBibDublinUnQualifiedRecord.
39       *
40       * @param fileContent
41       * @return
42       * @throws javax.xml.parsers.ParserConfigurationException
43       *
44       * @throws java.io.IOException
45       * @throws org.xml.sax.SAXException
46       * @throws javax.xml.xpath.XPathExpressionException
47       *
48       */
49      public UnQualifiedDublinRecord fromXML(String fileContent) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
50          Object object = getXstream().fromXML(fileContent);
51          return (UnQualifiedDublinRecord) object;
52      }
53  
54      /**
55       * Method to covert WorkBibDublinUnQualifiedRecord to XML Format.
56       *
57       * @param rec
58       * @return
59       */
60      public String toXml(UnQualifiedDublinRecord rec) {
61          String xml = getXstream().toXML(rec);
62          return xml;
63      }
64  
65      private XStream getXstream() {
66          XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("ddd", "_");
67          XStream xStream = new XStream(new DomDriver("UTF-8", replacer));
68          xStream.alias("OAI-PMH", UnQualifiedDublinRecord.class);
69          xStream.alias("responseDate", String.class);
70          xStream.alias("request", String.class);
71          xStream.alias("ListRecords", ListRecords.class);
72          xStream.alias("record", Record.class);
73          xStream.alias("header", Header.class);
74          xStream.alias("metadata", MetaData.class);
75          xStream.alias("oai_dc:dc", OaiDcDoc.class);
76          xStream.addImplicitCollection(ListRecords.class, "recordsList", Record.class);
77          xStream.addImplicitCollection(MetaData.class, "oaiDcDocs", OaiDcDoc.class);
78          xStream.registerConverter(new HeaderConverter());
79          xStream.registerConverter(new OaiDcDocConverter());
80          return xStream;
81      }
82  }