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: * @author www.gerdziegler.de
013: */
014:
015: package org.ztemplates.actions.urlhandler.tree.term;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import org.ztemplates.actions.ZMatch;
021: import org.ztemplates.actions.expression.ZExpression;
022: import org.ztemplates.actions.expression.ZLiteral;
023: import org.ztemplates.actions.expression.ZNestedExpression;
024: import org.ztemplates.actions.expression.ZOptionalExpression;
025: import org.ztemplates.actions.expression.ZSlash;
026: import org.ztemplates.actions.expression.ZTail;
027: import org.ztemplates.actions.expression.ZTerm;
028: import org.ztemplates.actions.expression.ZVariable;
029: import org.ztemplates.actions.util.ZReflectionUtil;
030: import org.ztemplates.classpath.ZIClassRepository;
031:
032: public class ZTreeTermFactory {
033: public static List<ZTreeTermList> expand(ZIClassRepository repo,
034: Class clazz) throws Exception {
035: ZMatch match = (ZMatch) clazz.getAnnotation(ZMatch.class);
036: ZExpression expression = new ZExpression(match.value());
037: List<ZTreeTermList> ret = expand(expression, repo, clazz);
038: return ret;
039: }
040:
041: private static List<ZTreeTermList> expand(ZExpression expression,
042: ZIClassRepository repo, Class clazz) throws Exception {
043: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
044:
045: for (ZTerm t : expression.getContent()) {
046: if (t instanceof ZLiteral) {
047: ret = append(ret, expand((ZLiteral) t, clazz));
048: } else if (t instanceof ZSlash) {
049: ret = append(ret, expand((ZSlash) t, clazz));
050: } else if (t instanceof ZVariable) {
051: ret = append(ret, expand((ZVariable) t, clazz));
052: } else if (t instanceof ZOptionalExpression) {
053: ret = append(ret, expand((ZOptionalExpression) t, repo,
054: clazz));
055: } else if (t instanceof ZNestedExpression) {
056: ret = append(ret, expand((ZNestedExpression) t, repo,
057: clazz));
058: } else if (t instanceof ZTail) {
059: ret = append(ret, expand((ZTail) t, clazz));
060: } else {
061: throw new Exception("unknown term-type: " + t);
062: }
063: }
064: return ret;
065: }
066:
067: private static List<ZTreeTermList> expand(ZLiteral l, Class clazz)
068: throws Exception {
069: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
070: ZTreeTermList tl = new ZTreeTermList();
071: tl.getTerms().add(new ZTreeLiteral(clazz, l.getText()));
072: ret.add(tl);
073: return ret;
074: }
075:
076: private static List<ZTreeTermList> expand(ZSlash l, Class clazz)
077: throws Exception {
078: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
079: ZTreeTermList tl = new ZTreeTermList();
080: tl.getTerms().add(new ZTreeSlash(clazz));
081: ret.add(tl);
082: return ret;
083: }
084:
085: private static List<ZTreeTermList> expand(ZVariable l, Class clazz)
086: throws Exception {
087: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
088: ZTreeTermList tl = new ZTreeTermList();
089: tl.getTerms().add(new ZTreeVariable(clazz, l.getName()));
090: ret.add(tl);
091: return ret;
092: }
093:
094: private static List<ZTreeTermList> expand(ZTail l, Class clazz)
095: throws Exception {
096: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
097: ZTreeTermList tl = new ZTreeTermList();
098: tl.getTerms().add(new ZTreeTail(clazz, l.getName()));
099: ret.add(tl);
100: return ret;
101: }
102:
103: private static List<ZTreeTermList> expand(ZOptionalExpression e,
104: ZIClassRepository repo, Class clazz) throws Exception {
105: List<ZTreeTermList> ret = expand(e.getOptionalExpression(),
106: repo, clazz);
107: ret.add(new ZTreeTermList());
108: return ret;
109: }
110:
111: private static List<ZTreeTermList> expand(ZNestedExpression ne,
112: ZIClassRepository repo, Class clazz) throws Exception {
113: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
114: Class nestedClass = ZReflectionUtil.getReferenceType(clazz, ne
115: .getName());
116: List<Class> nestedClasses = repo
117: .getClassesAnnotatedWithAndAssignableFrom(ZMatch.class,
118: nestedClass);
119: String name = ne.getName();
120: for (Class c : nestedClasses) {
121: List<ZTreeTermList> nestedSamples = expand(repo, c);
122: for (ZTreeTermList l : nestedSamples) {
123: l.getTerms().add(0,
124: new ZTreeNestedBegin(clazz, name, c));
125: l.getTerms().add(new ZTreeNestedEnd(clazz, name, c));
126: }
127: ret.addAll(nestedSamples);
128: }
129:
130: return ret;
131: }
132:
133: private static List<ZTreeTermList> append(List<ZTreeTermList> l1,
134: List<ZTreeTermList> l2) {
135: List<ZTreeTermList> ret = new ArrayList<ZTreeTermList>();
136:
137: if (l1.isEmpty()) {
138: ret.addAll(l2);
139: } else if (l2.isEmpty()) {
140: ret.addAll(l1);
141: } else {
142: for (ZTreeTermList s1 : l1) {
143: for (ZTreeTermList s2 : l2) {
144: ret.add(ZTreeTermList.append(s1, s2));
145: }
146: }
147: }
148:
149: return ret;
150: }
151: }
|