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   */
37  public class UnQualifiedDublinRecordHandler {
38  	/**
39  	 * Method to covert xml content to WorkBibDublinUnQualifiedRecord.
40  	 *
41  	 * @param fileContent
42  	 * @return
43  	 * @throws javax.xml.parsers.ParserConfigurationException
44  	 * @throws java.io.IOException
45  	 * @throws org.xml.sax.SAXException
46  	 * @throws javax.xml.xpath.XPathExpressionException
47  	 */
48  	public UnQualifiedDublinRecord fromXML(String fileContent) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
49  		Object object = getXstream().fromXML(fileContent);
50  		return (UnQualifiedDublinRecord) object;
51  	}
52  
53  	/**
54  	 * Method to covert WorkBibDublinUnQualifiedRecord to XML Format.
55  	 * 
56  	 * @param rec
57  	 * @return
58  	 */
59  	public String toXml(UnQualifiedDublinRecord rec) {
60  		String xml = getXstream().toXML(rec);
61  		return xml;
62  	}
63  
64  	private XStream getXstream() {
65  		XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("ddd", "_");
66  		XStream xStream = new XStream(new DomDriver("UTF-8", replacer));
67  		xStream.alias("OAI-PMH", UnQualifiedDublinRecord.class);
68  		xStream.alias("responseDate", String.class);
69  		xStream.alias("request", String.class);
70  		xStream.alias("ListRecords", ListRecords.class);
71  		xStream.alias("record", Record.class);
72  		xStream.alias("header", Header.class);
73  		xStream.alias("metadata", MetaData.class);
74  		xStream.alias("oai_dc:dc", OaiDcDoc.class);
75  		xStream.addImplicitCollection(ListRecords.class, "recordsList", Record.class);
76  		xStream.addImplicitCollection(MetaData.class, "oaiDcDocs", OaiDcDoc.class);
77  		xStream.registerConverter(new HeaderConverter());
78  		xStream.registerConverter(new OaiDcDocConverter());
79  		return xStream;
80  	}
81  }