001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler.grammar;
020:
021: import org.apache.beehive.netui.compiler.AnnotationMemberType;
022: import org.apache.beehive.netui.compiler.AnnotationGrammar;
023: import org.apache.beehive.netui.compiler.CompilerUtils;
024: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
028: import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
029: import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
030:
031: public class BundleNameType extends AnnotationMemberType {
032: public BundleNameType(String requiredRuntimeVersion,
033: AnnotationGrammar parentGrammar) {
034: super (requiredRuntimeVersion, parentGrammar);
035: }
036:
037: public Object onCheck(AnnotationTypeElementDeclaration valueDecl,
038: AnnotationValue value,
039: AnnotationInstance[] parentAnnotations,
040: MemberDeclaration classMember, int annotationArrayIndex) {
041: //
042: // Check that the bundle attribute isn't used with commons-validator v1.0
043: //
044: String bundle = (String) value.getValue();
045:
046: if (bundle != null
047: && !bundlesSupported(parentAnnotations, classMember)) {
048: AnnotationInstance annotation = parentAnnotations[parentAnnotations.length - 1];
049: addError(value, "error.validation-bundle-support",
050: BUNDLE_NAME_ATTR, annotation);
051: }
052:
053: return null;
054: }
055:
056: protected static boolean bundlesSupported(
057: AnnotationInstance[] parentAnnotations,
058: MemberDeclaration classMember) {
059: //
060: // Find the validator version attribute from the controller.
061: // Look at the annotation parent root for @Jpf.Controller,
062: // @Jpf.Action or @Jpf.ValidatableProperty. If the root is
063: // the Controller then just get the required attribute.
064: // Otherwise, get the controller class declaration then the
065: // attribute.
066: //
067: AnnotationInstance ann = parentAnnotations[0];
068: String validatorVersion = null;
069:
070: if (CompilerUtils.isJpfAnnotation(ann, CONTROLLER_TAG_NAME)) {
071: validatorVersion = CompilerUtils.getEnumFieldName(ann,
072: VALIDATOR_VERSION_ATTR, true);
073: } else {
074: TypeDeclaration outerType = null;
075:
076: if (CompilerUtils.isJpfAnnotation(ann, ACTION_TAG_NAME)) {
077: outerType = CompilerUtils.getOuterClass(classMember);
078: } else if (CompilerUtils.isJpfAnnotation(ann,
079: VALIDATABLE_PROPERTY_TAG_NAME)) {
080: outerType = CompilerUtils
081: .getOutermostClass(classMember);
082: } else {
083: // Should not hit this condition
084: assert false;
085: }
086:
087: if (outerType instanceof ClassDeclaration) {
088: ann = CompilerUtils.getAnnotation(outerType,
089: CONTROLLER_TAG_NAME);
090:
091: if (ann != null) {
092: validatorVersion = CompilerUtils.getEnumFieldName(
093: ann, VALIDATOR_VERSION_ATTR, true);
094: }
095: }
096: }
097:
098: //
099: // Default is commons-validator v1.0 unless otherwise declared with the @Jpf.Controller
100: // validator version attribute.
101: if (validatorVersion == null
102: || validatorVersion
103: .equals(VALIDATOR_VERSION_ONE_ZERO_STR)) {
104: return false;
105: }
106:
107: return true;
108: }
109: }
|