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.genmodel;
020:
021: import org.apache.beehive.netui.compiler.model.ForwardModel;
022: import org.apache.beehive.netui.compiler.model.ForwardContainer;
023: import org.apache.beehive.netui.compiler.CompilerUtils;
024: import org.apache.beehive.netui.compiler.JpfLanguageConstants;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.FieldDeclaration;
028: import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
029: import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
030:
031: import java.util.List;
032: import java.util.Collection;
033: import java.util.Iterator;
034:
035: public class GenForwardModel extends ForwardModel implements
036: JpfLanguageConstants {
037: public GenForwardModel(GenStrutsApp parent,
038: AnnotationInstance annotation, ClassDeclaration jclass,
039: String commentSuffix) {
040: super (parent);
041:
042: setName(CompilerUtils.getString(annotation, NAME_ATTR, true));
043: setRedirect(CompilerUtils.getBoolean(annotation, REDIRECT_ATTR,
044: false).booleanValue());
045: if (CompilerUtils.getBoolean(annotation,
046: EXTERNAL_REDIRECT_ATTR, false).booleanValue()) {
047: setExternalRedirect(true);
048: }
049:
050: //
051: // outputFormBean/outputFormBeanType
052: //
053: DeclaredType outputFormType = CompilerUtils.getDeclaredType(
054: annotation, OUTPUT_FORM_BEAN_TYPE_ATTR, true);
055: String outputFormMember = CompilerUtils.getString(annotation,
056: OUTPUT_FORM_BEAN_ATTR, true);
057: if (outputFormMember != null) {
058: FieldDeclaration field = CompilerUtils.getClassField(
059: jclass, outputFormMember, null);
060: assert outputFormType == null; // checker should catch this
061: assert field != null; // checker should catch this
062: assert field.getType() instanceof DeclaredType : field
063: .getType().getClass().getName(); // checker enforces
064: outputFormType = (DeclaredType) field.getType();
065: }
066: setOutputFormBeanMember(outputFormMember);
067: setOutputFormBeanType(outputFormType != null ? CompilerUtils
068: .getLoadableName(outputFormType) : null);
069:
070: //
071: // path, tilesDefinition, navigateTo, returnAction (mutually exclusive)
072: //
073: String returnAction = CompilerUtils.getString(annotation,
074: RETURN_ACTION_ATTR, true);
075: String navigateTo = CompilerUtils.getEnumFieldName(annotation,
076: NAVIGATE_TO_ATTR, true);
077: String tilesDefinition = CompilerUtils.getString(annotation,
078: TILES_DEFINITION_ATTR, true);
079: String path = CompilerUtils.getString(annotation, PATH_ATTR,
080: true);
081: String action = CompilerUtils.getString(annotation,
082: ACTION_ATTR, true);
083:
084: if (action != null) {
085: assert path == null; // checker should enforce this
086: path = action + ACTION_EXTENSION_DOT;
087: }
088:
089: if (returnAction != null) {
090: assert navigateTo == null;
091: assert tilesDefinition == null;
092: assert path == null;
093: setIsNestedReturn(true);
094: setPath(returnAction); // set the returnAction as the path -- the runtime expects it there
095: } else if (navigateTo != null) {
096: assert tilesDefinition == null;
097: assert path == null;
098:
099: if (navigateTo.equals(NAVIGATE_TO_PAGE_LEGACY_STR)
100: || navigateTo.equals(NAVIGATE_TO_CURRENT_PAGE_STR)
101: || navigateTo.equals(NAVIGATE_TO_PREVIOUS_PAGE_STR)) {
102: setReturnToPage(true);
103: } else if (navigateTo
104: .equals(NAVIGATE_TO_PREVIOUS_ACTION_STR)) {
105: setReturnToAction(true);
106: } else {
107: assert false : "unknown value for navigateTo: \""
108: + navigateTo + '"';
109: }
110:
111: boolean restore = CompilerUtils.getBoolean(annotation,
112: RESTORE_QUERY_STRING_ATTR, false).booleanValue();
113: setRestoreQueryString(restore);
114: setPath(navigateTo); // set the actual navigateTo value as the path -- the runtime expects it there
115: } else if (tilesDefinition != null) {
116: assert path == null;
117: setPath(tilesDefinition); // set the tilesDefinition as the path -- the runtime expects it there
118: } else {
119: assert path != null; // checker should enforce this
120:
121: //
122: // Translate our relative-path convention (normal) to the Struts convention, which adds a '/'
123: // to any module-relative path.
124: //
125: boolean contextRelative = true;
126: if (!path.startsWith("/")) {
127: contextRelative = false;
128:
129: // If it's an absolute URL, then the path shouldn't have a slash inserted in front of it.
130: if (!CompilerUtils.isAbsoluteURL(path)) {
131: path = '/' + path;
132:
133: // See if we should be using the path for the struts module that this forward is relative to
134: TypeDeclaration typeDeclaration = annotation
135: .getContainingType();
136:
137: if (!typeDeclaration.equals(jclass)
138: && !path.endsWith(ACTION_EXTENSION_DOT)
139: && parent.getFlowControllerInfo()
140: .getMergedControllerAnnotation()
141: .isInheritLocalPaths()) {
142: setRelativeTo(CompilerUtils
143: .inferModulePathFromType(typeDeclaration));
144: }
145: }
146: }
147:
148: setPath(path);
149: setContextRelative(contextRelative);
150: }
151:
152: addActionOutputs(annotation, jclass);
153:
154: if (commentSuffix != null) {
155: setComment("forward \"" + getName() + '"' + commentSuffix); // @TODO I18N the comment
156: }
157: }
158:
159: static void addForwards(AnnotationInstance annotation,
160: ForwardContainer container, ClassDeclaration jclass,
161: GenStrutsApp strutsApp, String commentSuffix) {
162: List forwards = CompilerUtils.getAnnotationArray(annotation,
163: FORWARDS_ATTR, true);
164: addForwards(forwards, container, jclass, strutsApp,
165: commentSuffix);
166: }
167:
168: static void addForwards(Collection forwards,
169: ForwardContainer container, ClassDeclaration jclass,
170: GenStrutsApp strutsApp, String commentSuffix) {
171: if (forwards != null) {
172: for (Iterator ii = forwards.iterator(); ii.hasNext();) {
173: AnnotationInstance ann = (AnnotationInstance) ii.next();
174: container.addForward(new GenForwardModel(strutsApp,
175: ann, jclass, commentSuffix));
176: }
177: }
178: }
179:
180: protected void addActionOutputs(AnnotationInstance annotation,
181: ClassDeclaration jclass) {
182: List actionOutputs = CompilerUtils.getAnnotationArray(
183: annotation, ACTION_OUTPUTS_ATTR, true);
184:
185: if (actionOutputs != null) {
186: for (Iterator ii = actionOutputs.iterator(); ii.hasNext();) {
187: AnnotationInstance ann = (AnnotationInstance) ii.next();
188: addActionOutput(new GenActionOutputModel(ann, jclass));
189: }
190: }
191: }
192: }
|