Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.s1.objects.schema;
  
 
 import java.util.List;
 import java.util.Map;

Base class for schema attributes
 
 public abstract class ObjectSchemaAttribute<T> {

    
Create attribute from java.util.Map

Parameters:
a
Returns:
 
         ObjectSchemaAttribute attr = null;
         String type = Objects.get(a,"type","Object");
         if("Map".equals(type)){
             attr = new MapAttribute();
         }else if("List".equals(type)){
             attr = new ListAttribute();
         }else if("ComplexType".equals(type)){
             attr = new ComplexTypeAttribute();
         }else if(type.startsWith("#")){
             attr = new ReferenceAttribute();
         }else{
             attr = new SimpleTypeAttribute();
         }
         attr.fromMap(a);
         return attr;
     }
 
 
     }
 
     ObjectSchemaAttribute(String nameString labelString type){
         this. = name;
         this. = label;
         this. = type;
     }
 
         fromMap(a);
     }
 
     private Map<String,ObjectvalidationCtx;
 
     protected boolean required;
     protected boolean denied;
     protected boolean nonPresent;
 
     protected String type;
     protected String name;
     protected String label;
     protected String description;
 
 
     protected T def;
 
     protected Map<String,Objecthint;
 
     protected List<Objectvariants;
 
     protected String error;
     protected T data;

    
parent attribute
 
     protected ObjectSchemaAttribute parent;

    
schema
 
     protected ObjectSchema schema;

    

Parameters:
parent
 
     void setParent(ObjectSchemaAttribute parent) {
         this. = parent;
     }

    

Parameters:
schema
    void setSchema(ObjectSchema schema) {
        this. = schema;
    }

    
Get parent attribute

Returns:
    public ObjectSchemaAttribute getParent() {
        return ;
    }

    
Get schema reference

Returns:
    public ObjectSchema getSchema() {
        return ;
    }

    

Parameters:
delimiter
Returns:
    public String getPath(String delimiter){
        ObjectSchemaAttribute a = this;
        String path = "";
        while(a!=null && a.getParent()!=null){
            path = a.getLabel()+path;
            a = a.getParent();
            if(a!=null && a.getParent()!=null)
                path = delimiter+path;
        }
        return path;
    }

    
Update child schema and parent references
    protected void updateChildSchemaAndParent(){
    }

    
Set attribute properties from java.util.Map

Parameters:
m
        this. = null;
        this. = null;
        this. = Objects.get(m"name");
        this. = Objects.get(m"label");
        this. = Objects.get(m"description");
        this. = Objects.get(m"type""Object");
        this. = "required".equals(Objects.get(m"appearance"));
        this. = "denied".equals(Objects.get(m"appearance"));
        this. = "nonPresent".equals(Objects.get(m"appearance"));
        this. = Objects.get(m"default");
        this. = Objects.get(m"hint");
        List<Objectvars = Objects.get(m"variants");
        this. = Objects.newArrayList();
        if(!Objects.isNullOrEmpty(vars)){
            vars = Objects.copy(vars);
            for(Object avars){
                .add(Objects.cast(a,this.));
            }
        }
        final S1ScriptEngine scriptEngine = new S1ScriptEngine("objectSchema.scriptEngine");
        final String script = Objects.get(m"script");
        if(!Objects.isNullOrEmpty(script)){
            this. = new Closure<ObjectSchemaAttributeObjectSchemaAttribute>() {
                @Override
                public ObjectSchemaAttribute call(final ObjectSchemaAttribute input) {
                    //call script
                    Map<String,Objectm = input.getScriptMap();
                    scriptEngine.eval(script,m);
                    Map<String,Objectattr = Objects.get(m,"attr");
                    try {
                        return ObjectSchemaAttribute.createFromMap(attr);
                    } catch (ObjectSchemaFormatException e) {
                        throw S1SystemError.wrap(e);
                    }
                }
            };
        }
        final String validate = Objects.get(m"validate");
        if(!Objects.isNullOrEmpty(validate)){
            this. = new Closure<ObjectSchemaAttributeString>() {
                @Override
                public String call(ObjectSchemaAttribute input) {
                    Map<String,Objectm = input.getScriptMap();
                    try{
                        scriptEngine.eval(validate,m);
                    }catch (ScriptException e){
                        return ""+e.getData();
                    }
                    return null;
                }
            };
        }
    }

    

Returns:
    protected Map<String,ObjectgetParentMap(){
        Map<String,Objectm = null;
        if(getParent()!=null){
            m = Objects.newHashMap();
            m.putAll(getParent().toMap());
            m.put("parent",getParent().getParentMap());
            m.put("data",getParent().getData());
        }
        return m;
    }

    

Returns:
    protected Map<String,ObjectgetScriptMap(){
        Map<String,Objectm = Objects.newHashMap();
        m.put("data"getData());
        m.put("parent"getParentMap());
        m.put("attr"toMap());
        m.put("ctx"null);
        return m;
    }

    
Format to java.util.Map

Returns:
    public Map<String,ObjecttoMap(){
        Map<String,Objectm = Objects.newHashMap("name",
                "label",
                "description",
                "type",
                "appearance" ? "required" : ( ? "denied" : ( ? "nonPresent" : "normal")),
                "default",
                "hint",
                "variants", Objects.copy(),
                "data", Objects.copy());
        if(!=null){
            m.put("error",);
        }
        return m;
    }
        ObjectSchemaAttribute a = null;
        try {
            a = ObjectSchemaAttribute.createFromMap(toMap()).setValidate().setScript();;
        } catch (ObjectSchemaFormatException e) {
            e.printStackTrace();
        }
        return a;
    }
    public T getData() {
        return ;
    }
    void setData(T data) {
        this. = data;
    }
        this. = p;
        return this;
    }
        this. = p;
        return this;
    }
        this. = p;
        return this;
    }
    public ObjectSchemaAttribute setRequired(boolean p){
        this. = p;
        return this;
    }
    public ObjectSchemaAttribute setDenied(boolean p){
        this. = p;
        return this;
    }
    public ObjectSchemaAttribute setNonPresent(boolean p){
        this. = p;
        return this;
    }
        this. = p;
        return this;
    }
        this. = p;
        return this;
    }
    public ObjectSchemaAttribute setDefault(T p){
        this. = p;
        return this;
    }
        this. = p;
        return this;
    }
        this. = Objects.copy(p);
        return this;
    }
    public ObjectSchemaAttribute setVariants(Object ... args){
        this. = Arrays.asList(args);
        return this;
    }
    public String getType() {
        return ;
    }
    public boolean isRequired() {
        return ;
    }
    public boolean isDenied() {
        return ;
    }
    public boolean isNonPresent() {
        return ;
    }
    public String getName() {
        return ;
    }
    public String getLabel() {
        return ;
    }
    public String getDescription() {
        return ;
    }
        return ;
    }
        return ;
    }
    public T getDefault() {
        return ;
    }
    public List<ObjectgetVariants() {
        return ;
    }
    public Map<StringObjectgetHint() {
        return ;
    }
    public String getError() {
        return ;
    }

    
Validate attribute

Parameters:
data
expand
deep
ctx
quite
Returns:
    ObjectSchemaAttribute validate(T databoolean expandboolean deep,
                  Map<String,Objectctxboolean quite)  throws ObjectSchemaValidationException{
        ObjectSchemaAttribute attribute = this;
        attribute.error = null;
        attribute.validationCtx = ctx;
        try{
            String name = attribute.name;
            attribute.data = data;
            //run script
            if(attribute.script!=null){
                ObjectSchemaAttribute a1 = (ObjectSchemaAttribute)attribute.script.call(attribute);
                if(a1!=null){
                    a1.setParent(attribute.parent);
                    a1.setSchema(attribute.schema);
                    attribute = a1;
                }
                attribute.name = name;
                attribute.data = data;
                attribute.validationCtx = ctx;
            }
            if(!attribute.nonPresent){
                //default
                if((attribute.data==null) && attribute.def!=null){
                    attribute.data = attribute.def;
                }
                if(!quite){
                    //check required, denied
                    if(attribute.required && (attribute.data==null)){
                        throw new Exception("Attribute is required");
                    }
                    if(attribute.denied && (attribute.data!=null)){
                        throw new Exception("Attribute is denied");
                    }
                    if(attribute.denied){
                        attribute.data = null;
                        return attribute;
                    }
                }
                if(attribute instanceof ReferenceAttribute){
                    attribute = ((ReferenceAttribute)attribute).resolve();
                }
                //validate type
                attribute.validateType(expanddeepctxquite);
                //variants
                if(attribute.data!=null && !Objects.isNullOrEmpty(attribute.variants)){
                    final Object data1 = attribute.data;
                    final String type1 = attribute.type;
                    if(Objects.find(attribute.variantsnew Closure<ObjectBoolean>() {
                        @Override
                        public Boolean call(Object input) {
                            return Objects.equals(input, Objects.cast(data1type1));
                        }
                    })==null){
                        throw new Exception("Value not in available variants");
                    }
                }
                if(quite){
                    //check required, denied
                    if(attribute.required && (attribute.data==null)){
                        throw new Exception("Attribute is required");
                    }
                    if(attribute.denied && (attribute.data!=null)){
                        throw new Exception("Attribute is denied");
                    }
                    if(attribute.denied){
                        attribute.data = null;
                        return attribute;
                    }
                }
                //validate script
                if(attribute.validate!=null){
                    String msg = (String)attribute.validate.call(attribute);
                    if(!Objects.isNullOrEmpty(msg))
                        throw new Exception(msg);
                }
            }
        }catch (Throwable e){
            if(quite)
                attribute.error = e.getMessage();
            else{
                if(e instanceof ObjectSchemaValidationException)
                    throw new ObjectSchemaValidationException(e.getMessage(),e);
                else
                    throw new ObjectSchemaValidationException(getPath(" / ")+": "+e.getMessage(),e);
            }
        }
        return attribute;
    }

    
Validate attribute data

Parameters:
expand
deep
ctx
quite
Throws:
java.lang.Exception
    protected abstract void validateType(boolean expandboolean deep,  Map<String,Objectctxboolean quitethrows Exception;
New to GrepCode? Check out our FAQ X