All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robobinding.attribute.StaticResourcesAttribute Maven / Gradle / Ivy

There is a newer version: 0.8.14
Show newest version
package org.robobinding.attribute;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;

import com.google.common.collect.Lists;

/**
 * @since 1.0
 * @author Cheng Wei
 *
 */
public class StaticResourcesAttribute extends AbstractPropertyAttribute {
	private static final Pattern RESOURCES_ATTRIBUTE_PATTERN = 
			Pattern.compile("^\\[" + StaticResource.PATTERN + "(,\\s?" + StaticResource.PATTERN + ")*\\]$");

	private final List resources;
	
	public StaticResourcesAttribute(String name, String value) {
		super(name);
		
		if(!isStaticResourcesAttribute(value)) {
			throw new MalformedAttributeException(getName(), "Invalid resource syntax: " + value);
		}
		
		resources = Lists.newArrayList();
		
		String[] resourceValues = value.substring(1, value.length()-1).split(",");
		for (String resourceValue : resourceValues) {
			resources.add(new StaticResource(resourceValue.trim()));
		}
	}
	
	public List getResourceIds(Context context) {
		List resourceIds = Lists.newArrayList();
		for (StaticResource resource : resources) {
			resourceIds.add(resource.getResourceId(context));
		}
		return resourceIds;
	}

	@Override
	public boolean isTwoWayBinding() {
		return false;
	}
	
	@Override
	public  T accept(PropertyAttributeVisitor visitor) {
		return visitor.visitStaticResources(this);
	}

	static boolean isStaticResourcesAttribute(String value) {
		Matcher matcher = RESOURCES_ATTRIBUTE_PATTERN.matcher(value);

		return matcher.matches();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy