Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.s1.options;
  
 
 import java.io.File;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;

Default options storage implementation
 
 public class OptionsStorage {
 
     private static final Logger LOG = LoggerFactory.getLogger(OptionsStorage.class);
 
     private String configHome;
 
     public OptionsStorage(){
          = Options.getParameter();
     }

    

Parameters:
cls
path
<T>
Returns:
 
     public <T> T getSystem(Class<T> clsString path){
         return get(cls,"",path);
     }

    

Parameters:
cls
path
def
<T>
Returns:
 
     public <T> T getSystem(Class<T> clsString path, T def){
         return get(cls,"",path,def);
     }

    

Parameters:
path
<T>
Returns:
 
     public <T> T getSystem(String path){
         return get("",path);
     }

    

Parameters:
path
def
<T>
Returns:
 
     public <T> T getSystem(String path, T def){
         return get("",path,def);
     }

    

Parameters:
cls
name
path
<T>
Returns:
 
     public <T> T get(Class<T> clsString nameString path){
         return get(cls,name,path,null);
     }

    

Parameters:
cls
name
path
def
<T>
Returns:
    public <T> T get(Class<T> clsString nameString path, T def){
        return Objects.cast(get(name,path,def),cls);
    }

    

Parameters:
name
path
<T>
Returns:
    public <T> T get(String nameString path){
        return get(name,path,null);
    }

    

Parameters:
name
Returns:
    public <T> T get(String nameString path, T def){
        if(Objects.isNullOrEmpty(name))
            name = ;
        return Objects.get(getMap(name),path,def);
    }
    public static final String SYSTEM_PROPERTIES = "System";
    public static final String CONFIG_HOME = "ConfigHome";

    

Parameters:
name
Returns:
    public Map<String,ObjectgetMap(String name){
        Map<String,Objectr = Objects.newHashMap();
        String cfgText = readConfigToString(name+".cfg");
        if(!Objects.isNullOrEmpty(cfgText)){
            try {
                r = parseToMap(cfgText);
            } catch (Exception e) {
                if(.isDebugEnabled())
                    .debug("Cannot parse properties: "+e.getMessage());
                e.printStackTrace();
            }
        }
        if(.isTraceEnabled())
            .trace("Read options properties "+name+": "+r);
        else if(.isDebugEnabled())
            .debug("Read options properties "+name);
        return r;
    }
    public final String TYPE_NS = "http://s1-platform.org/config/types/";

    
        if(Objects.isNullOrEmpty(config))
            return Objects.newHashMap();
        config = config.trim();
        if(config.startsWith("<")){
            //xml
            Document doc = XMLFormat.fromString(config);
            return (Map<String,Object>) parseElementToObject(doc.getDocumentElement());
        }else if(config.startsWith("{")){
            return Objects.fromWire(JSONFormat.evalJSON(config));
        }
        return Objects.newHashMap();
    }

    

Parameters:
m
Returns:
    public String formatMap(Map<String,Objectm){
        String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<!--\n" +
                "~ Copyright 2013-${new Date().format(\"yyyy\")} S1 Platform\n" +
                "~ Created at: ${new Date().format(\"yyyy-MM-dd HH:mm:ss\")}\n" +
                "-->\n" +
                "\n" +
                "<map:cfg\n" +
                "  xmlns:bi=\""++"BigInteger\"\n" +
                "  xmlns:bd=\""++"BigDecimal\"\n" +
                "  xmlns:i=\""++"Integer\"\n" +
                "  xmlns:l=\""++"Long\"\n" +
                "  xmlns:f=\""++"Float\"\n" +
                "  xmlns:d=\""++"Double\"\n" +
                "  xmlns:dt=\""++"Date\"\n" +
                "  xmlns:b=\""++"Boolean\"\n" +
                "  xmlns:map=\""++"Map\"\n" +
                "  xmlns:list=\""++"List\">\n";
        //iterate map
        s+=formatObject("  ",m);
        s+="\n</map:cfg>";
        return s;
    }

    

Parameters:
name
closure
Returns:
    public Object readConfig(String nameClosure<InputStreamObjectclosurethrows ClosureException {
        String configPath =  + . + name;
        File file = new File(configPath);
        if(closure!=null){
            InputStream is = null;
            try{
                if(file.exists() && file.isFile()){
                    try {
                        is = new FileInputStream(file);
                    } catch (FileNotFoundException e) {
                        .debug("Config "+name+" not found");
                    }
                }
                return closure.call(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        return null;
    }

    

Parameters:
name
Returns:
    public String readConfigToString(String name){
        try{
            return (StringreadConfig(namenew Closure<InputStreamObject>() {
                @Override
                public Object call(InputStream input) {
                    if(input==null)
                        return null;
                    return IOUtils.toString(input"UTF-8");
                }
            });
        }catch (ClosureException e){
            throw e.toSystemError();
        }
    }

    

Parameters:
el
Returns:
    private Object parseElementToObject(Element el){
        String ns = el.getNamespaceURI();
        String type = "";
        if(!Objects.isNullOrEmpty(ns) && ns.startsWith()){
            type = ns.substring(.length());
        }
        if(Objects.isNullOrEmpty(type))
            type="String";
        if("Map".equals(type)){
            Map<String,Objectm = Objects.newHashMap();
            NamedNodeMap attrs = el.getAttributes();
            for(int i=0;i<attrs.getLength();i++){
                Node attr = attrs.item(i);
                String attr_ns = attr.getNamespaceURI();
                if(!"http://www.w3.org/2000/xmlns/".equals(attr_ns)){
                    String o = attr.getNodeValue();
                    String attr_type = "";
                    if(!Objects.isNullOrEmpty(attr_ns) && attr_ns.startsWith()){
                        attr_type = attr_ns.substring(.length());
                    }
                    if(Objects.isNullOrEmpty(attr_type))
                        attr_type="String";
                    m.put(attr.getLocalName(),Objects.cast(o,attr_type));
                }
            }
            //children
            for(Element it:XMLFormat.getChildElementList(el,null,null)){
                m.put(it.getLocalName(), parseElementToObject(it));
            }
            return m;
        }else if("List".equals(type)){
            List<Objectlst = Objects.newArrayList();
            //children
            for(Element it:XMLFormat.getChildElementList(el,null,null)){
                lst.add(parseElementToObject(it));
            }
            return lst;
        }else{
            Object o = el.getTextContent();
            if("Date".equals(type)){
                try{
                    o = Objects.parseDate((String)o,"yyyy-MM-dd HH:mm:ss.SSS");
                }catch (Exception e){
                    o = Objects.fromWire(Objects.newHashMap(String.class,Object.class,"date",o)).get("date");
                }
            }
            o = Objects.cast(o,type);
            return o;
        }
    }

    

Parameters:
tab
name
o
Returns:
    private String wrapObject(String tabString nameObject o){
        String s = "\n"+tab+"<";
        String t = "";
        String end = "";
        if(o instanceof Map){
            t="map:";
            end = "\n"+tab;
        }else if(o instanceof List){
            t="list:";
            end = "\n"+tab;
        }else if(o instanceof Date){
            t="dt:";
        }else if(o instanceof Boolean){
            t="b:";
        }else if(o instanceof Integer){
            t="i:";
        }else if(o instanceof Long){
            t="l:";
        }else if(o instanceof Float){
            t="f:";
        }else if(o instanceof Double){
            t="d:";
        }else if(o instanceof BigDecimal){
            t="bd:";
        }else if(o instanceof BigInteger){
            t="bi:";
        }else if(o instanceof String){
        }
        s+=t+name+">";
        s+=formatObject(tab,o);
        s+=end+"</"+t+name+">";
        return s;
    }

    

Parameters:
tab
o
Returns:
    private String formatObject(String tabObject o){
        String s = "";
        if(o instanceof Map){
            for(Map.Entry<String,Objectit:((Map<String,Object>) o).entrySet()) {
                s+=wrapObject(tab+"  ",it.getKey(),it.getValue());
            }
        }else if(o instanceof List){
            for(Object it:(List)o) {
                s+=wrapObject(tab+"  ","e",it);
            }
        }else if(o instanceof Date){
            s = escapeXml(Objects.formatDate((Dateo"yyyy-MM-dd HH:mm:ss.SSS"));
        }else if(o instanceof Boolean){
            s = escapeXml("" + o);
        }else if(o instanceof Integer){
            s = escapeXml("" + o);
        }else if(o instanceof Long){
            s = escapeXml("" + o);
        }else if(o instanceof Float){
            s = escapeXml("" + o);
        }else if(o instanceof Double){
            s = escapeXml("" + o);
        }else if(o instanceof BigDecimal){
            s = escapeXml("" + o);
        }else if(o instanceof BigInteger){
            s = escapeXml("" + o);
        }else if(o instanceof String){
            s = escapeXml((Stringo);
        }
        return s;
    }

    

Parameters:
s
Returns:
    private String escapeXml(String s){
        return s.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("\"","&quot;");
    }
New to GrepCode? Check out our FAQ X