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.JpfLanguageConstants;
022: import org.apache.beehive.netui.compiler.CompilerUtils;
023: import org.apache.beehive.netui.compiler.model.ForwardModel;
024: import org.apache.beehive.netui.compiler.typesystem.declaration.Declaration;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.FieldDeclaration;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
028: import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
029:
030: import java.util.List;
031: import java.util.Iterator;
032:
033: public class GenSimpleActionModel extends GenActionModel implements
034: JpfLanguageConstants {
035: public GenSimpleActionModel(AnnotationInstance annotation,
036: GenStrutsApp parentApp, ClassDeclaration jclass) {
037: super (CompilerUtils.getString(annotation, NAME_ATTR, true),
038: annotation, parentApp, jclass);
039: }
040:
041: protected void init(String actionName,
042: AnnotationInstance annotation, GenStrutsApp parentApp,
043: ClassDeclaration jclass) {
044: super .init(actionName, annotation, parentApp, jclass);
045:
046: setSimpleAction(true);
047: addForwards(annotation, parentApp, jclass);
048:
049: String formMember = getFormMember();
050: if (formMember != null) {
051: FieldDeclaration field = CompilerUtils.findField(jclass,
052: formMember);
053: assert field != null; // checker should prevent this
054: setFormBeanName(addFormBean(field.getType(), parentApp));
055: } else {
056: setReadonly(Boolean.valueOf(true)); // can't modify member state; mark as read-only
057:
058: TypeInstance formBeanType = CompilerUtils.getTypeInstance(
059: annotation, USE_FORM_BEAN_TYPE_ATTR, true);
060:
061: if (formBeanType != null) {
062: setFormBeanName(addFormBean(formBeanType, parentApp));
063: }
064: }
065: }
066:
067: protected String getFormBean(Declaration sourceElement,
068: GenStrutsApp parentApp) {
069: return null;
070: }
071:
072: protected void getForwards(AnnotationInstance annotation,
073: ClassDeclaration jclass, GenStrutsApp parentApp) {
074: }
075:
076: private void addForwards(AnnotationInstance annotation,
077: GenStrutsApp parentApp, ClassDeclaration jclass) {
078: //
079: // First add the default forward -- the one that is parsed from the simple action annotation itself.
080: // But, if the "forwardRef" attribute was given, simply use the one referenced.
081: //
082: String forwardRef = CompilerUtils.getString(annotation,
083: FORWARD_REF_ATTR, true);
084:
085: if (forwardRef == null) {
086: forwardRef = DEFAULT_SIMPLE_ACTION_FORWARD_NAME;
087: ForwardModel fwd = new SimpleActionForward(forwardRef,
088: parentApp, annotation, jclass);
089:
090: if (fwd.getPath() != null || fwd.isReturnToAction()
091: || fwd.isReturnToPage() || fwd.isNestedReturn()) {
092: addForward(fwd);
093: }
094: }
095:
096: setDefaultForwardName(forwardRef);
097:
098: List conditionalFwdAnnotations = CompilerUtils
099: .getAnnotationArray(annotation,
100: CONDITIONAL_FORWARDS_ATTR, true);
101:
102: if (conditionalFwdAnnotations != null) {
103: int anonCount = 0;
104:
105: for (Iterator ii = conditionalFwdAnnotations.iterator(); ii
106: .hasNext();) {
107: AnnotationInstance conditionalFwdAnnotation = (AnnotationInstance) ii
108: .next();
109: ForwardModel conditionalFwd = new SimpleActionForward(
110: parentApp, conditionalFwdAnnotation, jclass);
111: String expression = CompilerUtils.getString(
112: conditionalFwdAnnotation, CONDITION_ATTR, true);
113: assert expression != null;
114:
115: if (conditionalFwd.getName() == null)
116: conditionalFwd.setName("_anon" + ++anonCount);
117: addForward(conditionalFwd);
118: addConditionalForward(expression, conditionalFwd
119: .getName());
120: }
121: }
122: }
123:
124: private static class SimpleActionForward extends GenForwardModel {
125: public SimpleActionForward(GenStrutsApp parent,
126: AnnotationInstance annotation, ClassDeclaration jclass) {
127: super (parent, annotation, jclass, null);
128: }
129:
130: public SimpleActionForward(String name, GenStrutsApp parent,
131: AnnotationInstance annotation, ClassDeclaration jclass) {
132: super (parent, annotation, jclass, null);
133: setName(name);
134: }
135:
136: protected void addActionOutputs(AnnotationInstance annotation,
137: ClassDeclaration jclass) {
138: // do nothing -- there are no action outputs on simple actions
139: }
140: }
141: }
|