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.select.service.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.ole.select.businessobject.BibInfoBean;
20  
21  import java.util.HashMap;
22  import java.util.StringTokenizer;
23  
24  public class BuildOpenUrlBibInfoBean {
25      private Logger LOG = org.apache.log4j.Logger.getLogger(BuildOpenUrlBibInfoBean.class);
26      private BibInfoBean bibInfoBean;
27      private HashMap<String, String> openUrlMap;
28      private String openUrlString;
29  
30      public BibInfoBean getBean(BibInfoBean bibInfoBean, String openUrl) throws Exception {
31          this.bibInfoBean = bibInfoBean;
32          this.openUrlString = openUrl;
33          if (openUrlString != null) {
34              replaceEncodedValues();
35              parseOpenUrl();
36              populateBibInfoBean();
37          }
38          return bibInfoBean;
39      }
40  
41      private void replaceEncodedValues() {
42          openUrlString = openUrlString.replaceAll("\\+", " ");
43          openUrlString = openUrlString.replaceAll("%20", " ");
44          openUrlString = openUrlString.replaceAll("%23", "#");
45          openUrlString = openUrlString.replaceAll("%25", "%");
46          openUrlString = openUrlString.replaceAll("%26", "&");
47          openUrlString = openUrlString.replaceAll("%2B", "+");
48          openUrlString = openUrlString.replaceAll("%2F", "/");
49          openUrlString = openUrlString.replaceAll("%3C", "<");
50          openUrlString = openUrlString.replaceAll("%3D", "=");
51          openUrlString = openUrlString.replaceAll("%3E", ">");
52          openUrlString = openUrlString.replaceAll("%3F", "?");
53          openUrlString = openUrlString.replaceAll("%3A", ":");
54      }
55  
56      private void parseOpenUrl() {
57          openUrlMap = new HashMap();
58          String[] url = openUrlString.split("\\?");
59          if (url.length > 1) {
60              StringTokenizer st = new StringTokenizer(url[1], "&");
61              while (st.hasMoreElements()) {
62                  String[] values = ((String) st.nextElement()).split("=");
63                  if (values.length > 1)
64                      openUrlMap.put(values[0], values[1]);
65              }
66          }
67      }
68  
69      private void populateBibInfoBean() {
70          if (openUrlMap.get("rft.btitle") != null)
71              bibInfoBean.setTitle(openUrlMap.get("rft.btitle"));
72  
73          if (bibInfoBean.getTitle() == null) {
74              if (openUrlMap.get("rft.title") != null)
75                  bibInfoBean.setTitle(openUrlMap.get("rft.title"));
76          }
77  
78          if (openUrlMap.get("rft.isbn") != null) {
79              bibInfoBean.setStandardNumber(openUrlMap.get("rft.isbn"));
80              bibInfoBean.setTypeOfStandardNumber("ISBN");
81          }
82  
83          if (openUrlMap.get("rft.issn") != null) {
84              bibInfoBean.setStandardNumber(openUrlMap.get("rft.issn"));
85              bibInfoBean.setTypeOfStandardNumber("ISSN");
86          }
87  
88          if (openUrlMap.get("rft.epage") != null)
89              bibInfoBean.setEndPage(new Long(openUrlMap.get("rft.epage")));
90  
91          if (openUrlMap.get("rft.spage") != null)
92              bibInfoBean.setStartPage(new Long(openUrlMap.get("rft.spage")));
93  
94          if (openUrlMap.get("rft.genre") != null)
95              bibInfoBean.setCategory(openUrlMap.get("rft.genre"));
96  
97          if (openUrlMap.get("rft.edition") != null)
98              bibInfoBean.setEdition(openUrlMap.get("rft.edition"));
99  
100         if (openUrlMap.get("rft.series") != null)
101             bibInfoBean.setSeries(openUrlMap.get("rft.series"));
102 
103         if (openUrlMap.get("rft.place") != null)
104             bibInfoBean.setPlaceOfPublication(openUrlMap.get("rft.place"));
105 
106         if (openUrlMap.get("rft.date") != null)
107             bibInfoBean.setYearOfPublication(openUrlMap.get("rft.date"));
108 
109         if (openUrlMap.get("rft.pub") != null)
110             bibInfoBean.setPublisher(openUrlMap.get("rft.pub"));
111 
112         if (openUrlMap.get("rft.au") != null)
113             bibInfoBean.setAuthor(openUrlMap.get("rft.au"));
114     }
115 }