public Value precompile(Value value, Context context, FunctionManager manager) { Value compiled = staticPreCompile(value, context, manager); return null != compiled ? compiled : compile(value, context); }
public void process(V view, Value value) { if (value.isBinding()) { handleBinding(view, value.getAsBinding()); } else if (value.isResource()) { handleResource(view, value.getAsResource()); } else if (value.isAttributeResource()) { handleAttributeResource(view, value.getAsAttributeResource()); } else if (value.isStyleResource()) { handleStyleResource(view, value.getAsStyleResource()); } else { handleValue(view, value); } }
public void handleBinding(V view, Binding value) { DataContext dataContext = ((ProteusView) view).getViewManager().getDataContext(); Value resolved = evaluate(value, view.getContext(), dataContext.getData(), dataContext.getIndex()); handleValue(view, resolved); }
public static FunctionBinding valueOf(@NonNull String name, @NonNull String args, Context context, @NonNull FunctionManager manager) { Function function = manager.get(name); String[] tokens = FUNCTION_ARGS_DELIMITER.split(args); Value[] arguments = new Value[tokens.length]; String token; Value resolved; for (int i = 0; i < tokens.length; i++) { token = tokens[i].trim(); if (!token.isEmpty() && token.charAt(0) == '\'') { token = token.substring(1, token.length() - 1); resolved = new Primitive(token); } else { resolved = AttributeProcessor.staticPreCompile(new Primitive(token), context, manager); } arguments[i] = resolved != null ? resolved : new Primitive(token); } return new FunctionBinding(function, arguments); }
Value value = processor.compile(new Primitive(entry.getKey()), null); processor.handleValue(null, value);
@Test public void parse_complex() throws Exception { AttributeProcessor processor = new GravityAttributeProcessor() { @Override public void setGravity(View view, @Gravity int gravity) { } }; Value value = processor.compile(new Primitive("top|left"), null); int gravity = Gravity.TOP | Gravity.LEFT; assertThat(value.getAsInt(), is(gravity)); }
processor.handleValue(null, new Primitive(entry.getKey()));
public static Value evaluate(final Context context, final Value input, final Value data, final int index) { final Value[] output = new Value[1]; AttributeProcessor processor = new AttributeProcessor<View>() { @Override public void handleBinding(View view, Binding binding) { output[0] = binding.evaluate(context, data, index); } @Override public void handleValue(View view, Value value) { output[0] = value; } @Override public void handleResource(View view, Resource resource) { output[0] = new Primitive(resource.getString(context)); } @Override public void handleAttributeResource(View view, AttributeResource attribute) { output[0] = new Primitive(attribute.apply(context).getString(0)); } @Override public void handleStyleResource(View view, StyleResource style) { output[0] = new Primitive(style.apply(context).getString(0)); } }; //noinspection unchecked processor.process(null, input); return output[0]; }
private static Value[] resolve(Context context, Value[] in, Value data, int index) { //noinspection ConstantConditions because we want it to crash, it is an illegal state anyway Value[] out = new Value[in.length]; for (int i = 0; i < in.length; i++) { out[i] = AttributeProcessor.evaluate(context, in[i], data, index); } return out; }
public Layout read(String type, Proteus proteus, JsonReader in) throws IOException { List<Layout.Attribute> attributes = new ArrayList<>(); Map<String, Value> data = null; ObjectValue extras = new ObjectValue(); String name; while (in.hasNext()) { name = in.nextName(); if (ProteusConstants.DATA.equals(name)) { data = readData(in); } else { ViewTypeParser.AttributeSet.Attribute attribute = proteus.getAttributeId(name, type); if (null != attribute) { FunctionManager manager = PROTEUS_INSTANCE_HOLDER.getProteus().functions; Value value = attribute.processor.precompile(VALUE_TYPE_ADAPTER.read(in), getContext(), manager); attributes.add(new Layout.Attribute(attribute.id, value)); } else { extras.add(name, VALUE_TYPE_ADAPTER.read(in)); } } } return new Layout(type, attributes.size() > 0 ? attributes : null, data, extras.entrySet().size() > 0 ? extras : null); }
@Nullable public static Value staticPreCompile(Value value, Context context, FunctionManager manager) { Value compiled = null; if (value.isPrimitive()) { compiled = staticPreCompile(value.getAsPrimitive(), context, manager); } else if (value.isObject()) { compiled = staticPreCompile(value.getAsObject(), context, manager); } else if (value.isBinding() || value.isResource() || value.isAttributeResource() || value.isStyleResource()) { return value; } return compiled; }
Value value = processor.compile(new Primitive(entry.getKey()), null); assertThat(value.getAsInt(), is(entry.getValue()));
/** * @param view * @param attributeId * @param value * @return */ public boolean handleAttribute(V view, int attributeId, Value value) { int position = getPosition(attributeId); if (position < 0) { //noinspection unchecked return null != parent && parent.handleAttribute(view, attributeId, value); } AttributeProcessor attributeProcessor = processors[position]; //noinspection unchecked attributeProcessor.process(view, value); return true; }
public Map<String, Value> readData(JsonReader in) throws IOException { JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return new HashMap<>(); } if (peek != JsonToken.BEGIN_OBJECT) { throw new JsonSyntaxException("data must be a Map<String, String>."); } Map<String, Value> data = new LinkedHashMap<>(); in.beginObject(); while (in.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in); String key = in.nextString(); Value value = VALUE_TYPE_ADAPTER.read(in); Value compiled = AttributeProcessor.staticPreCompile(value, context, PROTEUS_INSTANCE_HOLDER.getProteus().functions); if (compiled != null) { value = compiled; } Value replaced = data.put(key, value); if (replaced != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); return data; } }
public static Value staticCompile(@Nullable Value value, Context context) { if (null == value || !value.isPrimitive()) { return Dimension.ZERO; } if (value.isDimension()) { return value; } Value precompiled = AttributeProcessor.staticPreCompile(value.getAsPrimitive(), context, null); if (null != precompiled) { return precompiled; } return Dimension.valueOf(value.getAsString()); }
public static Value staticCompile(@Nullable Value value, Context context) { if (null == value) { return Color.Int.BLACK; } if (value.isColor()) { return value; } else if (value.isObject()) { return Color.valueOf(value.getAsObject(), context); } else if (value.isPrimitive()) { Value precompiled = AttributeProcessor.staticPreCompile(value.getAsPrimitive(), context, null); if (null != precompiled) { return precompiled; } return Color.valueOf(value.getAsString(), Color.Int.BLACK); } else { return Color.Int.BLACK; } }
public static Value staticCompile(@Nullable Value value, Context context) { if (null == value) { return DrawableValue.ColorValue.BLACK; } if (value.isDrawable()) { return value; } else if (value.isPrimitive()) { Value precompiled = AttributeProcessor.staticPreCompile(value.getAsPrimitive(), context, null); if (null != precompiled) { return precompiled; } return DrawableValue.valueOf(value.getAsString(), context); } else if (value.isObject()) { return DrawableValue.valueOf(value.getAsObject(), context); } else { return DrawableValue.ColorValue.BLACK; } }