001: /*
002: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: * http://www.apache.org/licenses/LICENSE-2.0
007: * Unless required by applicable law or agreed to in writing,
008: * software distributed under the License is distributed on an
009: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
010: * either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: * 17.11.2007
013: * @author www.gerdziegler.de
014: */
015: package org.ztemplates.actions.urlhandler;
016:
017: import org.ztemplates.actions.ZGetter;
018: import org.ztemplates.actions.ZMatch;
019: import org.ztemplates.actions.ZSetter;
020: import org.ztemplates.actions.expression.ZExpression;
021: import org.ztemplates.actions.expression.ZNestedExpression;
022: import org.ztemplates.actions.expression.ZOptionalExpression;
023: import org.ztemplates.actions.expression.ZTail;
024: import org.ztemplates.actions.expression.ZTerm;
025: import org.ztemplates.actions.expression.ZVariable;
026: import org.ztemplates.actions.util.ZReflectionUtil;
027:
028: public class ZClassValidator {
029:
030: public static void validate(Class clazz) throws Exception {
031: ZMatch match = (ZMatch) clazz.getAnnotation(ZMatch.class);
032:
033: StringBuffer buff = new StringBuffer();
034:
035: ZExpression expression = new ZExpression(match.value());
036:
037: validate(buff, expression, clazz);
038:
039: String[] parameters = match.parameters();
040: for (String name : parameters) {
041: // if (ReflectionUtil.getGetter(clazz, name) == null)
042: // {
043: // buff.append("\nmissing @" + ZGetter.class.getSimpleName() + "(\"" +
044: // name + "\") or " + ReflectionUtil.computePrefixName("get", name) +
045: // "()");
046: // }
047: if (ZReflectionUtil.getSetter(clazz, name) == null) {
048: buff.append("\nmissing @"
049: + ZSetter.class.getSimpleName()
050: + "(\""
051: + name
052: + "\") or "
053: + ZReflectionUtil
054: .computePrefixName("set", name) + "()");
055: }
056: }
057:
058: if (buff.length() > 0) {
059: throw new Exception("validation errors:\n"
060: + clazz.getName() + buff.toString());
061: }
062: }
063:
064: private static void validate(StringBuffer buff,
065: ZExpression expression, Class clazz) throws Exception {
066: for (ZTerm t : expression.getContent()) {
067: if (t instanceof ZVariable) {
068: ZVariable v = (ZVariable) t;
069: String name = v.getName();
070: if (ZReflectionUtil.getGetter(clazz, name) == null) {
071: buff.append("\nmissing @"
072: + ZGetter.class.getSimpleName()
073: + "(\""
074: + name
075: + "\") or "
076: + ZReflectionUtil.computePrefixName("get",
077: name) + "()");
078: }
079: if (ZReflectionUtil.getSetter(clazz, name) == null) {
080: buff.append("\nmissing @"
081: + ZSetter.class.getSimpleName()
082: + "(\""
083: + name
084: + "\") or "
085: + ZReflectionUtil.computePrefixName("set",
086: name) + "()");
087: }
088: } else if (t instanceof ZOptionalExpression) {
089: ZOptionalExpression oe = (ZOptionalExpression) t;
090: validate(buff, oe.getOptionalExpression(), clazz);
091: } else if (t instanceof ZNestedExpression) {
092: ZNestedExpression ne = (ZNestedExpression) t;
093: String name = ne.getName();
094: if (ZReflectionUtil.getGetter(clazz, name) == null) {
095: buff.append("\nmissing @"
096: + ZGetter.class.getSimpleName()
097: + "(\""
098: + name
099: + "\") or "
100: + ZReflectionUtil.computePrefixName("get",
101: name) + "()");
102: }
103: if (ZReflectionUtil.getSetter(clazz, name) == null) {
104: buff.append("\nmissing @"
105: + ZSetter.class.getSimpleName()
106: + "(\""
107: + name
108: + "\") or "
109: + ZReflectionUtil.computePrefixName("set",
110: name) + "()");
111: }
112: } else if (t instanceof ZTail) {
113: ZTail v = (ZTail) t;
114: String name = v.getName();
115: if (ZReflectionUtil.getGetter(clazz, name) == null) {
116: buff.append("\nmissing @"
117: + ZGetter.class.getSimpleName()
118: + "(\""
119: + name
120: + "\") or "
121: + ZReflectionUtil.computePrefixName("get",
122: name) + "()");
123: }
124: if (ZReflectionUtil.getSetter(clazz, name) == null) {
125: buff.append("\nmissing @"
126: + ZSetter.class.getSimpleName()
127: + "(\""
128: + name
129: + "\") or "
130: + ZReflectionUtil.computePrefixName("set",
131: name) + "()");
132: }
133: }
134: }
135: }
136: }
|