KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > definition > registry > FlowDefinitionResource


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.webflow.definition.registry;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.core.io.Resource;
21 import org.springframework.core.style.ToStringCreator;
22 import org.springframework.util.Assert;
23 import org.springframework.webflow.core.collection.AttributeMap;
24 import org.springframework.webflow.core.collection.CollectionUtils;
25
26 /**
27  * A pointer to an externalized flow definition resource. Adds assigned
28  * identification information about the resource including the flow id and
29  * attributes.
30  *
31  * @see ExternalizedFlowDefinitionRegistrar
32  *
33  * @author Keith Donald
34  */

35 public class FlowDefinitionResource implements Serializable JavaDoc {
36
37     /**
38      * The identifier to assign to the flow definition.
39      */

40     private String JavaDoc id;
41
42     /**
43      * Attributes that can be used to affect flow construction.
44      */

45     private AttributeMap attributes;
46
47     /**
48      * The externalized location of the flow definition resource.
49      */

50     private Resource location;
51
52     /**
53      * Creates a new externalized flow definition resource. The flow id assigned will be
54      * the same name as the externalized resource's filename, excluding the extension.
55      * @param location the flow resource location.
56      */

57     public FlowDefinitionResource(Resource location) {
58         Assert.notNull(location, "The location of the externalized flow definition is required");
59         init(conventionalFlowId(location), location, null);
60     }
61
62     /**
63      * Creates a new externalized flow definition.
64      * @param id the flow id to be assigned
65      * @param location the flow resource location
66      */

67     public FlowDefinitionResource(String JavaDoc id, Resource location) {
68         init(id, location, null);
69     }
70
71     /**
72      * Creates a new externalized flow definition.
73      * @param id the flow id to be assigned
74      * @param location the flow resource location
75      * @param attributes flow definition attributes to be assigned
76      */

77     public FlowDefinitionResource(String JavaDoc id, Resource location, AttributeMap attributes) {
78         init(id, location, attributes);
79     }
80
81     /**
82      * Returns the identifier to assign to the flow definition.
83      */

84     public String JavaDoc getId() {
85         return id;
86     }
87
88     /**
89      * Returns the externalized flow definition resource location.
90      */

91     public Resource getLocation() {
92         return location;
93     }
94
95     /**
96      * Returns arbitrary flow definition attributes.
97      */

98     public AttributeMap getAttributes() {
99         return attributes;
100     }
101
102     public boolean equals(Object JavaDoc o) {
103         if (!(o instanceof FlowDefinitionResource)) {
104             return false;
105         }
106         FlowDefinitionResource other = (FlowDefinitionResource)o;
107         return id.equals(other.id) && location.equals(other.location);
108     }
109
110     public int hashCode() {
111         return id.hashCode() + location.hashCode();
112     }
113     
114     // internal helpers
115

116     /**
117      * Initialize this object.
118      */

119     private void init(String JavaDoc id, Resource location, AttributeMap attributes) {
120         Assert.hasText(id, "The id of the externalized flow definition is required");
121         Assert.notNull(location, "The location of the externalized flow definition is required");
122         this.id = id;
123         this.location = location;
124         if (attributes != null) {
125             this.attributes = attributes;
126         }
127         else {
128             this.attributes = CollectionUtils.EMPTY_ATTRIBUTE_MAP;
129         }
130     }
131
132     /**
133      * Returns the flow id assigned to the flow definition contained in given resource.
134      * By convention this will be the filename of the resource, excluding extension.
135      */

136     private String JavaDoc conventionalFlowId(Resource location) {
137         String JavaDoc fileName = location.getFilename();
138         int extensionIndex = fileName.lastIndexOf('.');
139         if (extensionIndex != -1) {
140             return fileName.substring(0, extensionIndex);
141         }
142         else {
143             return fileName;
144         }
145     }
146
147     public String JavaDoc toString() {
148         return new ToStringCreator(this).append("id", id).append("location", location).append("attributes", attributes)
149                 .toString();
150     }
151 }
Popular Tags