View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   /**
7    * ISBNUtil is for processing the request based on ISBN values
8    */
9   public class ISBNUtil {
10      /**
11       *  This method returns normalized isbn value based on isbn object.
12       * @param isbn
13       * @return  value
14       * @throws Exception
15       */
16      public String normalizeISBN(Object isbn) throws Exception {
17          String value = (String) isbn;
18          if (value != null) {
19              String modifiedValue = getModifiedString(value);
20              int len = modifiedValue.length();
21              if (len == 13) {
22                  return modifiedValue;
23              } else if (len == 10) {
24                  String regex = "[0-9]{9}[xX]{1}";
25                  String regexNum = "[0-9]{10}";
26                  value = getIsbnRegexValue(regexNum, modifiedValue);
27                  if (value.length() == 0) {
28                      value = getIsbnRegexValue(regex, modifiedValue);
29                  }
30                  if (value.length() > 0) {
31                      value = calculateIsbnValue(value);
32                  }
33              } else {
34                  throw new Exception("Invalid input" + isbn);
35              }
36              if (value.length() == 0) {
37                  throw new Exception("Normalization failed" + isbn);
38              }
39          }
40          return value;
41      }
42  
43      /**
44       *  This method returns isbn value with in the parentheses of parameter value.
45       * @param value
46       * @return  modifiedValue.
47       */
48      private String getModifiedString(String value) {
49          String modifiedValue = value;
50          if (modifiedValue.contains("(") && modifiedValue.contains(")")) {
51              String parenthesesValue = modifiedValue
52                      .substring(modifiedValue.indexOf("("), modifiedValue.lastIndexOf(")") + 1);
53              modifiedValue = modifiedValue.replace(parenthesesValue, "");
54          }
55          modifiedValue = modifiedValue.replaceAll("[-:\\s]", "");
56          return modifiedValue;
57      }
58  
59      /**
60       *  This method gets the isbn value based on matching regular expressions.
61       * @param regex
62       * @param value
63       * @return  matchingValue.
64       */
65      private String getIsbnRegexValue(String regex, String value) {
66          Pattern pattern = Pattern.compile(regex);
67          Matcher matcher = pattern.matcher(value);
68          String matchingValue = null;
69          if (matcher.find()) {
70              matchingValue = matcher.group(0);
71              matchingValue = value.substring(0, 9);
72          } else {
73              matchingValue = "";
74          }
75          return matchingValue;
76      }
77  
78      /**
79       * This method returns isbn number.
80       *  This method will calculate the isbn value if the length of the isbn value is 9.
81       * @param value
82       * @return  num.
83       */
84      private String calculateIsbnValue(String value) {
85          String num = value;
86          if (num.length() == 9) {
87              num = "978" + num;
88              num = getNormalizedIsbn(num);
89          }
90          return num;
91      }
92  
93      /**
94       *  This method returns normalized isbn value.
95       * @param value
96       * @return normalizeIsbn.
97       */
98      private String getNormalizedIsbn(String value) {
99          String normalizeIsbn = value;
100         int count = 0;
101         int multiple = 1;
102         for (int i = 0; i < value.length(); i++) {
103             Character c = new Character(value.charAt(i));
104             int j = Integer.parseInt(c.toString());
105             int sum = j * multiple;
106             count = count + sum;
107             if (i != 0 && i % 2 != 0) {
108                 multiple = 1;
109             } else {
110                 multiple = 3;
111             }
112         }
113         count = count % 10;
114         if (count == 0) {
115             count = 0;
116         } else {
117             count = 10 - count;
118         }
119         normalizeIsbn = normalizeIsbn + Integer.toString(count);
120         return normalizeIsbn;
121     }
122 }