KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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  * @author Timothy Larson
29  * @version $Id: NewDefinition.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

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