/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx.validation.model.dsds;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author themi
 */
public class DsdFactory {
    
    public static Dsd get(Document doc){
            String info = getInfo(doc);
            String type = info;
           
            //if DSD not located -> unknown DSD type
            if(type == null)
               return null;
            
            if(type.equals(Dsd.COMPACT))
                return new DsdCompact(doc);
            if(type.equals(Dsd.CROSS))
                return new DsdCross(doc);
            if(type.equals(Dsd.UTILITY))
                return new DsdUtility(doc);
            
        return null;
    }
    
    private static String getInfo(Document doc){
        Element root = doc.getDocumentElement();
        String[] nameSplit = root.getNodeName().split(":");
        if(nameSplit.length > 1){
            String name = nameSplit[1];
            if(name.equals("schema")){
                String urn = root.getAttribute("targetNamespace");
                if(urn.endsWith(Dsd.COMPACT))
                    return Dsd.COMPACT;
                if(urn.endsWith(Dsd.UTILITY))
                    return Dsd.UTILITY;
                if(urn.endsWith(Dsd.CROSS))
                    return Dsd.CROSS;
            }
        }
        
        return null;
    }
}
