/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.umich.lib.normalizers;

import java.util.regex.*;

/**
 *
 * @author BillDueber
 */
public class ISBNLongifier {

    private static String  ISBNDelimiiterPattern = "[\\-\\.]";
    //    private static Pattern ISBNPattern = Pattern.compile("^.*?(\\d{9})[\\dXx].*$");
    private static Pattern ISBNPattern = Pattern.compile("^.*\\b(\\d{9})[\\dXx](?:\\Z|\\D).*$");
    //    private static Pattern ISBN13Pattern = Pattern.compile("^.*?\\d{13}.*$");

    public static Boolean matches(String isbn) throws IllegalArgumentException {
        isbn = isbn.replaceAll(ISBNDelimiiterPattern, "");
        Matcher m = ISBNPattern.matcher(isbn);
        return m.matches();
	//        Matcher m2 = ISBN13Pattern.matcher(isbn);
	//        return m.matches() && !m2.matches();
    }

    public static String longify(String isbn) {
        isbn = isbn.replaceAll(ISBNDelimiiterPattern, "");
	//        System.out.println(isbn);
        Matcher m = ISBNPattern.matcher(isbn);
        if (!m.matches()) {
            throw new IllegalArgumentException(isbn + ": Not an ISBN");
        }
	//        Matcher m2 = ISBN13Pattern.matcher(isbn);
	//        if (m2.matches()) {
	//	    throw new IllegalArgumentException(isbn + ": Already 13 digits");
	//        }

        String longisbn = "978" + m.group(1);
        int[] digits = new int[12];
        for (int i=0;i<12;i++) {
            digits[i] =  new Integer(longisbn.substring(i, i+1));
        }

        Integer sum = 0;
        for (int i = 0; i < 12; i++) {
            sum = sum + digits[i] + (2 * digits[i] * (i % 2));
        }

        // Get the smallest multiple of ten > sum
        Integer top = sum + (10 - (sum % 10));
        Integer check = top - sum;
        if (check == 10) {
            return longisbn + "0";
        } else {
            return longisbn + check.toString();
        }

    }
}

@@ -466,24 +466,108 @@
 %7C%5C%5CD).*$%22);%0A
+    private static Pattern LongISBNPattern = Pattern.compile(%22%5E.*%5C%5Cb(%5C%5Cd%7B13%7D).*$%22);%0A
     //    pr
@@ -1065,24 +1065,253 @@
 ttern, %22%22);%0A
+        %0A        // Does it match a long one? If so, return it%0A        Matcher m = LongISBNPattern.matcher(isbn);%0A        if (m.matches()) %7B%0A          System.out.println(isbn + %22 is long%22);%0A          return m.group(1);%0A        %7D%0A
 %09//        S
@@ -1338,32 +1338,24 @@
 bn);%0A       
- Matcher
  m = ISBNPat

Allow long ISBNs as query strings