StateELResolver.java 5.42 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.el.ELContext
 *  javax.el.ELResolver
 *  javax.el.PropertyNotWritableException
 *  javax.servlet.jsp.JspContext
 *  javax.servlet.jsp.PageContext
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.scripting.jsp.util.TagUtil
 */
package com.adobe.granite.ui.components.impl;

import com.adobe.granite.ui.components.State;
import java.beans.FeatureDescriptor;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.PageContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.scripting.jsp.util.TagUtil;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class StateELResolver
extends ELResolver {
    private static final String PROPERTY_NAME = "state";

    public Object getValue(ELContext context, Object base, Object property) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (base != null) {
            return null;
        }
        if ("state".equals(property)) {
            PageContext pageContext = (PageContext)context.getContext(JspContext.class);
            context.setPropertyResolved(true);
            return this.getState(TagUtil.getRequest((PageContext)pageContext));
        }
        return null;
    }

    public Class<?> getType(ELContext context, Object base, Object property) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (base == null && "state".equals(property)) {
            context.setPropertyResolved(true);
        }
        return null;
    }

    public void setValue(ELContext context, Object base, Object property, Object val) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (base == null && "state".equals(property)) {
            throw new PropertyNotWritableException();
        }
    }

    public boolean isReadOnly(ELContext context, Object base, Object property) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (base == null && "state".equals(property)) {
            context.setPropertyResolved(true);
            return true;
        }
        return false;
    }

    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
        ArrayList<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>(1);
        FeatureDescriptor descriptor = new FeatureDescriptor();
        descriptor.setName("state");
        descriptor.setDisplayName("state");
        descriptor.setShortDescription("A variable exposing the client state of current request");
        descriptor.setExpert(false);
        descriptor.setHidden(false);
        descriptor.setPreferred(true);
        descriptor.setValue("type", StateMap.class);
        descriptor.setValue("resolvableAtDesignTime", Boolean.TRUE);
        list.add(descriptor);
        return list.iterator();
    }

    public Class<?> getCommonPropertyType(ELContext context, Object base) {
        if (base == null) {
            return String.class;
        }
        return null;
    }

    private StateMap getState(SlingHttpServletRequest request) {
        String key = StateELResolver.class.getName();
        StateMap map = (StateMap)request.getAttribute(key);
        if (map == null) {
            map = new StateMap(new State(request));
            request.setAttribute(key, (Object)map);
        }
        return map;
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private class StateMap
    extends AbstractMap<String, State.Item> {
        private State state;

        public StateMap(State state) {
            this.state = state;
        }

        @Override
        public Set<Map.Entry<String, State.Item>> entrySet() {
            HashSet<Map.Entry<String, State.Item>> set = new HashSet<Map.Entry<String, State.Item>>();
            Iterator<String> it = this.state.names();
            while (it.hasNext()) {
                set.add(new StateMapEntry(this.state.getItem(it.next())));
            }
            return set;
        }

        /*
         * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
         */
        private class StateMapEntry
        implements Map.Entry<String, State.Item> {
            private State.Item item;

            public StateMapEntry(State.Item item) {
                this.item = item;
            }

            @Override
            public String getKey() {
                return this.item.getName();
            }

            @Override
            public State.Item getValue() {
                return this.item;
            }

            @Override
            public State.Item setValue(State.Item value) {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean equals(Object obj) {
                return obj != null && this.hashCode() == obj.hashCode();
            }

            @Override
            public int hashCode() {
                return this.getKey().hashCode();
            }
        }

    }

}