001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import org.apache.commons.logging.Log;
007    import org.apache.commons.logging.LogFactory;
008    import org.w3c.dom.Element;
009    import org.w3c.dom.NodeList;
010    
011    /**
012     * @author lakshman
013     *
014     */
015    public class DataEncoderFactory {
016            private Log log = LogFactory.getLog(DataEncoderFactory.class);
017            private static DataEncoderFactory instance = new DataEncoderFactory();
018            private DataEncoder defaultEncoder = null;
019            private DataEncoder polarGridEncoder = null;
020            private DataEncoder tableEncoder = null;
021            
022            private DataEncoderFactory() {
023                    try{
024                            Element dataformat = W2Config.getFileElement("misc/dataformat");
025                            NodeList settings = dataformat.getElementsByTagName("setting");
026                            for (int i=0; i < settings.getLength(); ++i){
027                                    Element setting = (Element) settings.item(i);
028                                    String datatype = setting.getAttribute("datatype").toLowerCase();
029                                    String format = setting.getAttribute("format");
030                                    DataEncoder dataEncoder = getEncoder(format, setting);
031                                    if ( datatype.equals("default") ){
032                                            defaultEncoder = dataEncoder;
033                                    } else if ( datatype.equals("radialset") || datatype.equals("polargrid") ){
034                                            polarGridEncoder = dataEncoder;
035                                    } else if ( datatype.equals("datatable") ){
036                                            tableEncoder = dataEncoder;
037                                    } else {
038                                            log.warn("Ignoring setting for datatype=" + datatype + " format=" + format);
039                                    }
040                            }
041                    } catch (ConfigurationException e){
042                            log.error(e);
043                            throw new RuntimeException(e);
044                    }
045            }
046            
047            public static DataEncoderFactory getInstance(){
048                    return instance;
049            }
050            
051            public DataEncoder getEncoder(DataType dt){
052                    if (dt instanceof PolarGrid || dt instanceof RadialSet){
053                            return (polarGridEncoder == null)? defaultEncoder : polarGridEncoder; 
054                    }
055                    if (dt instanceof DataTable){
056                            return (tableEncoder == null)? defaultEncoder : tableEncoder;
057                    }
058                    return defaultEncoder;
059            }
060            
061            private DataEncoder getEncoder(String format, Element setting){
062                    DataEncoder dataEncoder = null;
063                    if ( format.equals("netcdf") ){
064                            dataEncoder = new NetcdfDataEncoder();
065                    } else if (format.equals("xml")){
066                            dataEncoder = new XmlDataEncoder();
067                    }
068                    
069                    if ( dataEncoder == null ){
070                            log.warn("DataEncoder format=" + format + " not implemented");
071                            return null;
072                    }
073    
074                    dataEncoder.setSparseGridThreshold(setting.getAttribute("sparsegrid"));
075                    dataEncoder.setCompressionEnabled(setting.getAttribute("compression"));
076                    
077                    return dataEncoder;
078            }
079            
080    }