KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > save > TestElementSaver


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

18
19 package org.apache.jmeter.save;
20
21 import java.util.LinkedList JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.DefaultConfiguration;
26 import org.apache.jmeter.testelement.TestElement;
27 import org.apache.jmeter.testelement.TestElementTraverser;
28 import org.apache.jmeter.testelement.property.CollectionProperty;
29 import org.apache.jmeter.testelement.property.JMeterProperty;
30 import org.apache.jmeter.testelement.property.MapProperty;
31 import org.apache.jmeter.testelement.property.TestElementProperty;
32
33 /**
34  * @version $Revision: 1.5 $
35  */

36 public class TestElementSaver
37     implements TestElementTraverser, SaveServiceConstants
38 {
39     String JavaDoc name;
40     LinkedList JavaDoc stack = new LinkedList JavaDoc();
41
42     DefaultConfiguration rootConfig = null;
43
44     public TestElementSaver(String JavaDoc name)
45     {
46         this.name = name;
47     }
48
49     public Configuration getConfiguration()
50     {
51         return rootConfig;
52     }
53
54     /* (non-Javadoc)
55      * @see TestElementTraverser#startTestElement(TestElement)
56      */

57     public void startTestElement(TestElement el)
58     {
59         DefaultConfiguration config =
60             new DefaultConfiguration("testelement", "testelement");
61         config.setAttribute("class", el.getClass().getName());
62         if (rootConfig == null)
63         {
64             rootConfig = config;
65             if (name != null && name.length() > 0)
66             {
67                 rootConfig.setAttribute("name", name);
68             }
69         }
70         else
71         {
72             setConfigName(config);
73         }
74         stack.add(config);
75     }
76
77     public void setConfigName(DefaultConfiguration config)
78     {
79         if (!(stack.getLast() instanceof Configuration))
80         {
81             Object JavaDoc key = stack.removeLast();
82             config.setAttribute("name", key.toString());
83         }
84     }
85
86     /* (non-Javadoc)
87      * @see TestElementTraverser#endTestElement(TestElement)
88      */

89     public void endTestElement(TestElement el)
90     {
91     }
92
93     /* (non-Javadoc)
94      * @see TestElementTraverser#simplePropertyValue(JMeterProperty)
95      */

96     public void simplePropertyValue(JMeterProperty value)
97     {
98         try
99         {
100             Object JavaDoc parent = stack.getLast();
101             if (!(parent instanceof Configuration))
102             {
103                 DefaultConfiguration config =
104                     new DefaultConfiguration("property", "property");
105                 config.setValue(value != null ? value.toString() : "");
106                 config.setAttribute("name", parent.toString());
107                 config.setAttribute(XML_SPACE, PRESERVE);
108                 stack.removeLast();
109                 stack.add(config);
110             }
111
112             if (parent instanceof DefaultConfiguration
113                 && value instanceof Configuration)
114             {
115                 ((DefaultConfiguration) parent).addChild((Configuration) value);
116             }
117             else if (
118                 parent instanceof DefaultConfiguration
119                     && !(value instanceof Configuration))
120             {
121                 DefaultConfiguration config =
122                     new DefaultConfiguration("string", "string");
123                 config.setValue(value.toString());
124                 config.setAttribute(XML_SPACE, PRESERVE);
125                 ((DefaultConfiguration) parent).addChild(config);
126             }
127         }
128         catch (NoSuchElementException JavaDoc e)
129         {}
130     }
131
132     /* (non-Javadoc)
133      * @see TestElementTraverser#startMap(MapProperty)
134      */

135     public void startMap(MapProperty map)
136     {
137         DefaultConfiguration config = new DefaultConfiguration("map", "map");
138         config.setAttribute("class", map.getObjectValue().getClass().getName());
139         config.setAttribute("name",map.getName());
140         config.setAttribute("propType",map.getClass().getName());
141         stack.add(config);
142     }
143
144     /* It appears that this method is no longer used.
145      * jeremy_a@bigfoot.com 02 May 2003
146     public void endMap(MapProperty map)
147     {
148         finishConfig();
149     }
150      */

151      
152     /* (non-Javadoc)
153      * @see TestElementTraverser#startCollection(CollectionProperty)
154      */

155     public void startCollection(CollectionProperty col)
156     {
157         DefaultConfiguration config =
158             new DefaultConfiguration("collection", "collection");
159         config.setAttribute("class", col.getObjectValue().getClass().getName());
160         config.setAttribute("name",col.getName());
161         config.setAttribute("propType",col.getClass().getName());
162         stack.add(config);
163     }
164
165     /* It appears that this method is no longer used.
166      * jeremy_a@bigfoot.com 02 May 2003
167     public void endCollection(CollectionProperty col)
168     {
169         finishConfig();
170     }
171      */

172      
173     /* (non-Javadoc)
174      * @see TestElementTraverser#endProperty(JMeterProperty)
175      */

176     public void endProperty(JMeterProperty key)
177     {
178         finishConfig();
179     }
180
181     private void finishConfig()
182     {
183         if (stack.size() > 1)
184         {
185             Configuration config = (Configuration) stack.removeLast();
186             ((DefaultConfiguration) stack.getLast()).addChild(config);
187         }
188     }
189
190     /* (non-Javadoc)
191      * @see TestElementTraverser#startProperty(JMeterProperty)
192      */

193     public void startProperty(JMeterProperty key)
194     {
195         if (key instanceof CollectionProperty)
196         {
197             startCollection((CollectionProperty) key);
198         }
199         else if (key instanceof MapProperty)
200         {
201             startMap((MapProperty) key);
202         }
203         else if (key instanceof TestElementProperty)
204         {
205             stack.addLast(key.getName());
206         }
207         else
208         {
209             DefaultConfiguration config =
210                 new DefaultConfiguration("property", "property");
211             config.setValue(key.getStringValue());
212             config.setAttribute("name", key.getName());
213             config.setAttribute("propType", key.getClass().getName());
214             config.setAttribute(XML_SPACE, PRESERVE);
215             stack.addLast(config);
216         }
217
218     }
219
220 }
221
Popular Tags