/* * Copyright 2009-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.codehaus.groovy.eclipse.dsl.pointcuts.impl; import java.util.Collection; import java.util.Collections; import org.codehaus.groovy.eclipse.dsl.pointcuts.AbstractPointcut; import org.codehaus.groovy.eclipse.dsl.pointcuts.GroovyDSLDContext; import org.codehaus.groovy.eclipse.dsl.pointcuts.PointcutVerificationException; import org.eclipse.core.resources.IStorage; /** * Tests that the file currently being checked ends in the given extension. * Do not include the dot in the pointcut, but the dot must exist before the extension * This pointcut should be optimized so that it runs before any others in an 'and' or 'or' clause. Also, its result should be cached in the * pattern providing a fail/succeed fast strategy. * @author andrew * @created Feb 10, 2011 */ public class FileExtensionPointcut extends AbstractPointcut { public FileExtensionPointcut(IStorage containerIdentifier, String pointcutName) { super(containerIdentifier, pointcutName); } @Override public Collection<?> matches(GroovyDSLDContext pattern, Object toMatch) { if (pattern.fullPathName != null && pattern.fullPathName.endsWith("." + (String) getFirstArgument())) { return Collections.singleton(pattern.fullPathName); } else { return null; } } @Override public boolean fastMatch(GroovyDSLDContext pattern) { return matches(pattern, null) != null; } @Override public void verify() throws PointcutVerificationException { String maybeStatus = allArgsAreStrings(); if (maybeStatus != null) { throw new PointcutVerificationException(maybeStatus, this); } maybeStatus = hasOneArg(); if (maybeStatus != null) { throw new PointcutVerificationException(maybeStatus, this); } super.verify(); } }