KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > NewDefinition


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ListIterator JavaDoc;
21
22 // TODO: The exception messages should use I18n.
23
/**
24  * This is the "{@link WidgetDefinition}" which is used to instantiate a
25  * {@link ClassDefinition}. The resolve step replaces this definition with
26  * the definitions contained in the referenced {@link ClassDefinition}.
27  *
28  * @version $Id: NewDefinition.java 289538 2005-09-16 13:46:22Z sylvain $
29  */

30 public class NewDefinition extends AbstractWidgetDefinition {
31     private boolean resolving;
32     private ClassDefinition classDefinition;
33
34     public NewDefinition() {
35         super();
36         resolving = false;
37         classDefinition = null;
38     }
39
40     private ClassDefinition getClassDefinition() throws Exception JavaDoc {
41         FormDefinition formDefinition = getFormDefinition();
42         WidgetDefinition classDefinition = formDefinition.getWidgetDefinition(getId());
43         
44         if (classDefinition == null) // not found in local form, try library
45
classDefinition = formDefinition.getLocalLibrary().getDefinition(getId());
46         
47         if (classDefinition == null)
48             throw new Exception JavaDoc("NewDefinition: Class with id \"" + getId() + "\" does not exist (" + getLocation() + ")");
49         if (!(classDefinition instanceof ClassDefinition))
50             throw new Exception JavaDoc("NewDefinition: Id \"" + getId() + "\" is not a class (" + getLocation() + ")");
51         return (ClassDefinition)classDefinition;
52     }
53
54     // TODO: Should add checking for union defaults which would cause non-terminating recursion.
55
public void resolve(List JavaDoc parents, WidgetDefinition parent) throws Exception JavaDoc {
56         // Non-terminating recursion detection
57
if (resolving) {
58             // Search up parent list in hopes of finding a "Union" before finding previous "New" for this "Class".
59
ListIterator JavaDoc parentsIt = parents.listIterator(parents.size());
60             while(parentsIt.hasPrevious()) {
61                 WidgetDefinition definition = (WidgetDefinition)parentsIt.previous();
62                 if (definition instanceof UnionDefinition) break;
63                 if (definition == this)
64                     throw new Exception JavaDoc("NewDefinition: Non-terminating recursion detected in widget definition : "
65                         + parent.getId() + " (" + getLocation() + ")");
66             }
67         }
68         // Resolution
69
resolving = true;
70         parents.add(this);
71         classDefinition = getClassDefinition();
72         Iterator JavaDoc definitionsIt = classDefinition.getWidgetDefinitions().iterator();
73         parents.add(this);
74         while (definitionsIt.hasNext()) {
75             WidgetDefinition definition = (WidgetDefinition)definitionsIt.next();
76             // Recursively resolve containers
77
if (definition instanceof ContainerDefinition) {
78                 ((ContainerDefinition)definition).resolve(parents, parent);
79             }
80             
81             // Add the current definition if it's not itself a "fd:new"
82
if (definition instanceof NewDefinition) {
83                 ((NewDefinition)definition).resolve(parents, parent);
84             } else {
85                 ((ContainerDefinition)parent).addWidgetDefinition(definition);
86             }
87         }
88         parents.remove(parents.size()-1);
89         resolving = false;
90     }
91
92     public Widget createInstance() {
93         return null;
94     }
95 }
96
Popular Tags