1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.service.impl;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.ole.select.CitationParser;
20 import org.kuali.ole.select.businessobject.BibInfoBean;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.NodeList;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.xpath.XPath;
27 import javax.xml.xpath.XPathConstants;
28 import javax.xml.xpath.XPathExpression;
29 import javax.xml.xpath.XPathFactory;
30 import java.io.ByteArrayInputStream;
31 import java.util.ArrayList;
32
33 public class BuildCitationBibInfoBean {
34
35
36 private Logger LOG = org.apache.log4j.Logger.getLogger(BuildCitationBibInfoBean.class);
37 private DocumentBuilderFactory domFactory;
38 private DocumentBuilder builder;
39 private Document doc;
40 private String responseXmlString;
41 private CitationParser ciatationParser;
42 private BibInfoBean bibInfoBean;
43 private ArrayList<String> authorList;
44 private String citation;
45
46
47 public BibInfoBean getBean(String citationString, BibInfoBean bibInfoBean) throws Exception {
48 this.bibInfoBean = bibInfoBean;
49 if (null != citationString && !citationString.equals("")) {
50 initiateDocument();
51 citationResposeXML(citationString);
52 iterateXML();
53 }
54 return this.bibInfoBean;
55 }
56
57 private void initiateDocument() throws Exception {
58 domFactory = DocumentBuilderFactory.newInstance();
59 builder = domFactory.newDocumentBuilder();
60 }
61
62 private void citationResposeXML(String citationString) throws Exception {
63 ciatationParser = getCiatationParser();
64 citation = citationString;
65 String citationParamerter = "citation=";
66 String citationStringToParse = citationParamerter + citationString;
67 responseXmlString = ciatationParser.parse(citationStringToParse);
68 }
69
70 private void iterateXML() throws Exception {
71 if (responseXmlString != null) {
72 byte b[] = responseXmlString.getBytes();
73 ByteArrayInputStream input = new ByteArrayInputStream(b);
74 Document doc = builder.parse(input);
75 XPath xpath = XPathFactory.newInstance().newXPath();
76 XPathExpression expr = xpath.compile("//journal/*/text()");
77 XPathExpression expr1 = xpath.compile("//journal/*");
78
79 Object result = expr.evaluate(doc, XPathConstants.NODESET);
80 Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
81 NodeList nodes = (NodeList) result;
82 NodeList nodes1 = (NodeList) result1;
83
84 authorList = new ArrayList();
85 if (nodes.getLength() > 0) {
86
87 for (int i = 0; i < nodes.getLength(); i++) {
88 String nodeName = nodes1.item(i).getNodeName();
89 String nodeValue = nodes.item(i).getNodeValue();
90 convertToBean(nodeName, nodeValue);
91 }
92 } else {
93 String nodeName = "rft:atitle";
94 String nodeValue = citation;
95 convertToBean(nodeName, nodeValue);
96
97 }
98 } else {
99 String nodeName = "rft:atitle";
100 String nodeValue = citation;
101 convertToBean(nodeName, nodeValue);
102 }
103 }
104
105 private void convertToBean(String nodeName, String nodeValue) throws Exception {
106 if (nodeName.equalsIgnoreCase("rft:atitle")) {
107
108 bibInfoBean.setTitle(nodeValue);
109 } else if (nodeName.equalsIgnoreCase("rft:stitle")) {
110 bibInfoBean.setSubTitle(nodeValue);
111 } else if (nodeName.equalsIgnoreCase("rft:au")) {
112 authorList.add(nodeValue);
113
114 bibInfoBean.setAuthor(authorList.get(0));
115 } else if (nodeName.equalsIgnoreCase("rft:spage")) {
116 bibInfoBean.setStartPage(new Long(nodeValue));
117 } else if (nodeName.equalsIgnoreCase("rft:epage")) {
118 bibInfoBean.setEndPage(new Long(nodeValue));
119 } else if (nodeName.equalsIgnoreCase("rft:genre")) {
120 bibInfoBean.setCategory(nodeValue);
121 } else if (nodeName.equalsIgnoreCase("rft:date")) {
122 bibInfoBean.setYearOfPublication(nodeValue);
123 }
124 }
125
126 public CitationParser getCiatationParser() {
127 if (null == ciatationParser) {
128 ciatationParser = new CitationParser();
129 }
130 return ciatationParser;
131 }
132
133
134 public void setCiatationParser(CitationParser ciatationParser) {
135 this.ciatationParser = ciatationParser;
136 }
137
138
139 }