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.AnnotationGrammar;
022: import org.apache.beehive.netui.compiler.CompilerUtils;
023: import org.apache.beehive.netui.compiler.Diagnostics;
024: import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
025: import org.apache.beehive.netui.compiler.FatalCompileTimeException;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
028: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessorEnv;
029:
030: import java.util.List;
031: import java.util.Iterator;
032:
033: public class BaseValidationRuleGrammar extends AnnotationGrammar {
034: private static final String[][] MUTUALLY_EXCLUSIVE_ATTRS = { {
035: MESSAGE_KEY_ATTR, MESSAGE_ATTR } };
036:
037: private static final String[][] ATTR_DEPENDENCIES = { {
038: BUNDLE_NAME_ATTR, MESSAGE_KEY_ATTR } };
039:
040: private String[][] _requiredAttrs = null;
041:
042: public BaseValidationRuleGrammar(CoreAnnotationProcessorEnv env,
043: Diagnostics diags, RuntimeVersionChecker rvc) {
044: super (env, diags, VERSION_9_0_STRING, rvc);
045:
046: addMemberType(MESSAGE_KEY_ATTR, new MessageKeyType(null, this ));
047: addMemberArrayGrammar(MESSAGE_ARGS_ATTR,
048: new ValidationMessageArgsGrammar(env, diags, rvc));
049: addMemberType(BUNDLE_NAME_ATTR, new BundleNameType(null, this ));
050: }
051:
052: public BaseValidationRuleGrammar(CoreAnnotationProcessorEnv env,
053: Diagnostics diags, RuntimeVersionChecker rvc,
054: String[][] requiredAttrs) {
055: this (env, diags, rvc);
056:
057: _requiredAttrs = requiredAttrs;
058: }
059:
060: public String[][] getRequiredAttrs() {
061: return _requiredAttrs;
062: }
063:
064: public String[][] getMutuallyExclusiveAttrs() {
065: return MUTUALLY_EXCLUSIVE_ATTRS;
066: }
067:
068: public String[][] getAttrDependencies() {
069: return ATTR_DEPENDENCIES;
070: }
071:
072: protected boolean onBeginCheck(AnnotationInstance annotation,
073: AnnotationInstance[] parentAnnotations,
074: MemberDeclaration classMember)
075: throws FatalCompileTimeException {
076: //
077: // Check to make sure that either the parent ValidatableProperty annotation has a displayName property,
078: // or this rule specifies a first argument to the default message, or this rule specifies its own message.
079: // If none of these are true, output a warning.
080: //
081: assert parentAnnotations.length > 0;
082: AnnotationInstance immediateParent = parentAnnotations[parentAnnotations.length - 1];
083:
084: if (CompilerUtils.getString(immediateParent, DISPLAY_NAME_ATTR,
085: true) == null
086: && CompilerUtils.getString(immediateParent,
087: DISPLAY_NAME_KEY_ATTR, true) == null
088: && CompilerUtils.getString(annotation,
089: MESSAGE_KEY_ATTR, true) == null
090: && CompilerUtils.getString(annotation, MESSAGE_ATTR,
091: true) == null) {
092: boolean useDefaultDisplayName = true;
093: List messageArgs = CompilerUtils.getAnnotationArray(
094: annotation, MESSAGE_ARGS_ATTR, true);
095:
096: if (messageArgs != null) {
097: boolean firstArg = true;
098:
099: for (Iterator ii = messageArgs.iterator(); ii.hasNext();) {
100: AnnotationInstance messageArg = (AnnotationInstance) ii
101: .next();
102: Integer position = CompilerUtils.getInteger(
103: messageArg, POSITION_ATTR, true);
104:
105: if ((position == null && firstArg)
106: || (position != null && position.intValue() == 0)) {
107: useDefaultDisplayName = false;
108: break;
109: }
110:
111: firstArg = false;
112: }
113: }
114:
115: if (useDefaultDisplayName) {
116: addWarning(annotation,
117: "warning.using-default-display-name",
118: CompilerUtils.getDeclaration(
119: immediateParent.getAnnotationType())
120: .getSimpleName());
121: }
122: }
123:
124: return super.onBeginCheck(annotation, parentAnnotations,
125: classMember);
126: }
127: }
|