package org.datacite.mds.util;

import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.datacite.mds.domain.Allocator;
import org.datacite.mds.domain.Datacentre;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * Class with several static util methods
 */
public class Utils {

    /**
     * returns the prefix of a doi
     * 
     * @param doi
     *            Doi (e.g. "10.5072/foobar")
     * @return doi prefix (e.g. "10.5072")
     */
    public static String getDoiPrefix(String doi) {
        return doi.split("/")[0];
    }

    /**
     * returns the suffix of a doi
     * 
     * @param doi
     *            Doi (e.g. "10.5072/foobar")
     * @return doi prefix (e.g. "foobar")
     */
    public static String getDoiSuffix(String doi) {
        return doi.split("/")[1];
    }

    /**
     * converts a string with comma separated values to a List of Strings
     * 
     * @param csv
     *            comma separated values
     * @return List of Strings
     */
    public static List<String> csvToList(String csv) {
        if (csv == null) {
            return new ArrayList<String>();
        }
        return Arrays.asList(csv.split(","));
    }

    /**
     * returns the hostname of the given URL
     * 
     * @param urlStr
     *            URL
     * @return hostname; null if the url is malformed
     */
    public static String getHostname(String urlStr) {
        URL url;
        try {
            url = new URL(urlStr);
            String hostname = url.getHost();
            return hostname;
        } catch (MalformedURLException e) {
            return null;
        }

    }

    /**
     * pretty print an xml string
     * 
     * @param xml String containing a xml document
     * @return
     * @throws Exception if the xml cannot be parsed 
     */
    public static String formatXML(String xml) throws Exception {
        Document doc = DocumentHelper.parseText(xml);
        StringWriter sw = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xw = new XMLWriter(sw, format);
        xw.write(doc);
        String result = sw.toString();
        return result;
    }

    /**
     * @return joined list of allocator and datacentre symbols
     */
    public static SortedSet<String> getAllSymbols() {
        SortedSet<String> symbols = new TreeSet<String>();
        for (Datacentre datacentre : Datacentre.findAllDatacentres()) {
            symbols.add(datacentre.getSymbol());
        }
        for (Allocator allocator : Allocator.findAllAllocators()) {
            symbols.add(allocator.getSymbol());
        }
        return symbols;
    }

}

@@ -528,16 +528,229 @@
 tils %7B%0A%0A
+    private static String splitDoi(String doi, int index) %7B%0A        if (doi == null %7C%7C !doi.contains(%22/%22))%0A            return null;%0A        String%5B%5D split = doi.split(%22/%22, -1);%0A        return split%5Bindex%5D;%0A    %7D%0A%0A
     /**%0A
@@ -978,25 +978,24 @@
 urn 
-doi.
 split
-(%22/%22)%5B0%5D
+Doi(doi, 0)
 ;%0A  
@@ -1235,25 +1235,24 @@
 urn 
-doi.
 split
-(%22/%22)%5B1%5D
+Doi(doi, 1)
 ;%0A  
@@ -1622,16 +1622,20 @@
 plit(%22,%22
+, -1
 ));%0A    
@@ -2151,16 +2151,34 @@
 aram xml
+%0A     *           
  String 
@@ -2242,16 +2242,35 @@
 xception
+%0A     *            
  if the 
@@ -2289,17 +2289,16 @@
 e parsed
- 
 %0A     */

fix for getDoiPrefix/Suffix to handle null values correctly