001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.progmodel.facets.param.validate.regexannot; 021 022import java.lang.annotation.Annotation; 023 024import org.apache.isis.applib.annotation.RegEx; 025import org.apache.isis.core.metamodel.facetapi.FacetHolder; 026import org.apache.isis.core.metamodel.facetapi.FacetUtil; 027import org.apache.isis.core.metamodel.facetapi.FeatureType; 028import org.apache.isis.core.metamodel.facets.Annotations; 029import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract; 030import org.apache.isis.core.metamodel.facets.object.title.TitleFacet; 031import org.apache.isis.core.progmodel.facets.object.regex.RegExFacet; 032import org.apache.isis.core.progmodel.facets.object.regex.TitleFacetFormattedByRegex; 033 034public class RegExFacetAnnotationForParameterFacetFactory extends FacetFactoryAbstract { 035 036 public RegExFacetAnnotationForParameterFacetFactory() { 037 super(FeatureType.PARAMETERS_ONLY); 038 } 039 040 @Override 041 public void processParams(final ProcessParameterContext processParameterContext) { 042 final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes(); 043 if (processParameterContext.getParamNum() >= parameterTypes.length) { 044 // ignore 045 return; 046 } 047 if (!Annotations.isString(parameterTypes[processParameterContext.getParamNum()])) { 048 return; 049 } 050 051 final Annotation[] parameterAnnotations = Annotations.getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()]; 052 for (final Annotation parameterAnnotation : parameterAnnotations) { 053 if (parameterAnnotation instanceof RegEx) { 054 final RegEx annotation = (RegEx) parameterAnnotation; 055 addRegexFacetAndCorrespondingTitleFacet(processParameterContext.getFacetHolder(), annotation); 056 return; 057 } 058 } 059 } 060 061 private void addRegexFacetAndCorrespondingTitleFacet(final FacetHolder holder, final RegEx annotation) { 062 final RegExFacet regexFacet = createRegexFacet(annotation, holder); 063 if (regexFacet == null) { 064 return; 065 } 066 FacetUtil.addFacet(regexFacet); 067 068 final TitleFacet titleFacet = createTitleFacet(regexFacet); 069 FacetUtil.addFacet(titleFacet); 070 } 071 072 private RegExFacet createRegexFacet(final RegEx annotation, final FacetHolder holder) { 073 if (annotation == null) { 074 return null; 075 } 076 077 final String validationExpression = annotation.validation(); 078 final boolean caseSensitive = annotation.caseSensitive(); 079 final String formatExpression = annotation.format(); 080 081 return new RegExFacetAnnotationForParameter(validationExpression, formatExpression, caseSensitive, holder); 082 } 083 084 private TitleFacet createTitleFacet(final RegExFacet regexFacet) { 085 return new TitleFacetFormattedByRegex(regexFacet); 086 } 087 088}