Android Open Source - android-view-selector Attribute Specifier Checker






From Project

Back to project page android-view-selector.

License

The source code is released under:

Copyright (c) 2013 Nik Haldimann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project android-view-selector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nikhaldimann.viewselector.checker;
//from   ww  w. j av a  2s .c  om
import java.util.Set;

import se.fishtank.css.selectors.specifier.AttributeSpecifier;
import android.view.View;

import com.nikhaldimann.viewselector.attributes.AttributeAccessException;
import com.nikhaldimann.viewselector.attributes.ViewAttributes;
import com.nikhaldimann.viewselector.selection.ViewSelection;

/**
 * Checks views based on a selectors attribute specifier, handling all the
 * attribute selection. Note that selection by id via a hash (e.g.,
 * {@code TextView#foo}) is also translated to an attribute specifier.
 */
public class AttributeSpecifierChecker implements ViewTraversalChecker {

    private static class ExactMatchPredicate implements MatchPredicate {

        private final String methodName;
        private final String value;

        public ExactMatchPredicate(String methodName, String value) {
            this.methodName = methodName;
            this.value = value;
        }

        public boolean matches(View view) {
            Object actualValue;
            try {
                actualValue = ViewAttributes.callGetterNormalizingStrings(view, methodName);
            } catch (AttributeAccessException ex) {
                return false;
            }
            Object convertedValue = value;
            if (actualValue instanceof Integer) {
                try {
                    convertedValue = Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    // Do nothing
                }
            } else if (actualValue instanceof Long) {
                try {
                    convertedValue = Long.parseLong(value);
                } catch (NumberFormatException ex) {
                    // Do nothing
                }
            } else if (actualValue instanceof Boolean) {
                if ("true".equals(value)) {
                    convertedValue = true;
                } else if ("false".equals(value)) {
                    convertedValue = false;
                }
            }
            return convertedValue.equals(actualValue);
        }
    }

    private final MatchPredicate matchPredicate;

    public AttributeSpecifierChecker(final AttributeSpecifier specifier, View root) {
        final String methodName = ViewAttributes.getGetterMethodName(specifier.getName());

        if (specifier.getValue() == null) {
            matchPredicate = new MatchPredicate() {
                public boolean matches(View view) {
                    try {
                        return ViewAttributes.callGetter(view, methodName) != null;
                    } catch (AttributeAccessException ex) {
                        return false;
                    }
                }
            };
        } else if ("id".equals(specifier.getName())) {
            // TODO This supports only user-defined ids, but not globally defined ids in android.
            // Can this be fixed?
            String id = specifier.getValue();
            final int numId = root.getResources().getIdentifier(
                    id, "id", root.getContext().getPackageName());
            if (numId == View.NO_ID) {
                matchPredicate = MatchPredicates.ALWAYS_FALSE_PREDICATE;
            } else {
                matchPredicate = new MatchPredicate() {
                    public boolean matches(View view) {
                        return numId == view.getId();
                    }
                };
            }
        } else {
            switch (specifier.getMatch()) {
                case EXACT:
                    matchPredicate = new ExactMatchPredicate(methodName, specifier.getValue());
                    break;
                case CONTAINS:
                case PREFIX:
                case SUFFIX:
                    matchPredicate = new MatchPredicate() {
                        public boolean matches(View view) {
                            Object actualValue;
                            try {
                                actualValue = ViewAttributes.callGetter(view, methodName);
                            } catch (AttributeAccessException ex) {
                                return false;
                            }
                            if (!(actualValue instanceof CharSequence)) {
                                return false;
                            }

                            // Android is using some CharSequence representations internally that
                            // don't compare well with strings (in particularly as returned from
                            // TextView.getText()), so we convert to a strong explicitly.
                            String actualString = actualValue.toString();

                            switch (specifier.getMatch()) {
                                case CONTAINS:
                                    return actualString.contains(specifier.getValue());
                                case PREFIX:
                                    return actualString.startsWith(specifier.getValue());
                                case SUFFIX:
                                    return actualString.endsWith(specifier.getValue());
                                default:
                                    throw new UnsupportedOperationException();
                            }
                        }
                    };
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    }

    public ViewSelection check(Set<View> views) {
        ViewSelection result = new ViewSelection();

        if (matchPredicate == MatchPredicates.ALWAYS_FALSE_PREDICATE) {
            return result;
        }

        for (View view : views) {
            if (matchPredicate.matches(view)) {
                result.add(view);
            }
        }
        return result;
    }

}




Java Source Code List

com.nikhaldimann.viewselector.Attributes.java
com.nikhaldimann.viewselector.InvalidSelectorException.java
com.nikhaldimann.viewselector.ViewSelectionAssert.java
com.nikhaldimann.viewselector.ViewSelectionAttributeAssert.java
com.nikhaldimann.viewselector.ViewSelectorAssertions.java
com.nikhaldimann.viewselector.ViewSelector.java
com.nikhaldimann.viewselector.android.AllTests.java
com.nikhaldimann.viewselector.android.ViewSelectionAssertTest.java
com.nikhaldimann.viewselector.android.ViewSelectorAssertionsTest.java
com.nikhaldimann.viewselector.android.ViewSelectorTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleFragmentActivityTest.java
com.nikhaldimann.viewselector.android.activities.ListViewExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.SimpleFragmentTest.java
com.nikhaldimann.viewselector.android.attributes.ViewAttributesTest.java
com.nikhaldimann.viewselector.android.checker.AttributeSpecifierCheckerTest.java
com.nikhaldimann.viewselector.android.checker.ClassCheckerTest.java
com.nikhaldimann.viewselector.android.testutil.AndroidTestViewFactory.java
com.nikhaldimann.viewselector.attributes.AttributeAccessException.java
com.nikhaldimann.viewselector.attributes.ViewAttributes.java
com.nikhaldimann.viewselector.attributes.ViewSelectionAttribute.java
com.nikhaldimann.viewselector.checker.AttributeSpecifierChecker.java
com.nikhaldimann.viewselector.checker.ClassChecker.java
com.nikhaldimann.viewselector.checker.MatchPredicate.java
com.nikhaldimann.viewselector.checker.MatchPredicates.java
com.nikhaldimann.viewselector.checker.ViewTraversalChecker.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldActivity.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldFragmentActivity.java
com.nikhaldimann.viewselector.selection.ViewSelection.java