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: */
018:
019: package org.apache.jmeter.save;
020:
021: import java.util.LinkedList;
022: import java.util.NoSuchElementException;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.DefaultConfiguration;
026: import org.apache.jmeter.testelement.TestElement;
027: import org.apache.jmeter.testelement.TestElementTraverser;
028: import org.apache.jmeter.testelement.property.CollectionProperty;
029: import org.apache.jmeter.testelement.property.JMeterProperty;
030: import org.apache.jmeter.testelement.property.MapProperty;
031: import org.apache.jmeter.testelement.property.TestElementProperty;
032:
033: /**
034: * Helper class for OldSaveService
035: */
036: public class TestElementSaver implements TestElementTraverser {
037: private String name;
038:
039: private LinkedList stack = new LinkedList();
040:
041: private DefaultConfiguration rootConfig = null;
042:
043: public TestElementSaver(String name) {
044: this .name = name;
045: }
046:
047: public Configuration getConfiguration() {
048: return rootConfig;
049: }
050:
051: /*
052: * (non-Javadoc)
053: *
054: * @see TestElementTraverser#startTestElement(TestElement)
055: */
056: public void startTestElement(TestElement el) {
057: DefaultConfiguration config = new DefaultConfiguration(
058: "testelement", "testelement");
059: config.setAttribute("class", el.getClass().getName());
060: if (rootConfig == null) {
061: rootConfig = config;
062: if (name != null && name.length() > 0) {
063: rootConfig.setAttribute("name", name);
064: }
065: } else {
066: setConfigName(config);
067: }
068: stack.add(config);
069: }
070:
071: public void setConfigName(DefaultConfiguration config) {
072: if (!(stack.getLast() instanceof Configuration)) {
073: Object key = stack.removeLast();
074: config.setAttribute("name", key.toString());
075: }
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see TestElementTraverser#endTestElement(TestElement)
082: */
083: public void endTestElement(TestElement el) {
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see TestElementTraverser#simplePropertyValue(JMeterProperty)
090: */
091: public void simplePropertyValue(JMeterProperty value) {
092: try {
093: Object parent = stack.getLast();
094: if (!(parent instanceof Configuration)) {
095: DefaultConfiguration config = new DefaultConfiguration(
096: "property", "property");
097: config.setValue(value != null ? value.toString() : "");
098: config.setAttribute("name", parent.toString());
099: config.setAttribute(OldSaveService.XML_SPACE,
100: OldSaveService.PRESERVE);
101: stack.removeLast();
102: stack.add(config);
103: }
104:
105: if (parent instanceof DefaultConfiguration
106: && value instanceof Configuration) {
107: ((DefaultConfiguration) parent)
108: .addChild((Configuration) value);
109: } else if (parent instanceof DefaultConfiguration
110: && !(value instanceof Configuration)) {
111: DefaultConfiguration config = new DefaultConfiguration(
112: "string", "string");
113: config.setValue(value.toString());
114: config.setAttribute(OldSaveService.XML_SPACE,
115: OldSaveService.PRESERVE);
116: ((DefaultConfiguration) parent).addChild(config);
117: }
118: } catch (NoSuchElementException e) {
119: }
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see TestElementTraverser#startMap(MapProperty)
126: */
127: public void startMap(MapProperty map) {
128: DefaultConfiguration config = new DefaultConfiguration("map",
129: "map");
130: config.setAttribute("class", map.getObjectValue().getClass()
131: .getName());
132: config.setAttribute("name", map.getName());
133: config.setAttribute("propType", map.getClass().getName());
134: stack.add(config);
135: }
136:
137: /*
138: * It appears that this method is no longer used. jeremy_a@bigfoot.com 02
139: * May 2003 public void endMap(MapProperty map) { finishConfig(); }
140: */
141:
142: /*
143: * (non-Javadoc)
144: *
145: * @see TestElementTraverser#startCollection(CollectionProperty)
146: */
147: public void startCollection(CollectionProperty col) {
148: DefaultConfiguration config = new DefaultConfiguration(
149: "collection", "collection");
150: config.setAttribute("class", col.getObjectValue().getClass()
151: .getName());
152: config.setAttribute("name", col.getName());
153: config.setAttribute("propType", col.getClass().getName());
154: stack.add(config);
155: }
156:
157: /*
158: * It appears that this method is no longer used. jeremy_a@bigfoot.com 02
159: * May 2003 public void endCollection(CollectionProperty col) {
160: * finishConfig(); }
161: */
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see TestElementTraverser#endProperty(JMeterProperty)
167: */
168: public void endProperty(JMeterProperty key) {
169: finishConfig();
170: }
171:
172: private void finishConfig() {
173: if (stack.size() > 1) {
174: Configuration config = (Configuration) stack.removeLast();
175: ((DefaultConfiguration) stack.getLast()).addChild(config);
176: }
177: }
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see TestElementTraverser#startProperty(JMeterProperty)
183: */
184: public void startProperty(JMeterProperty key) {
185: if (key instanceof CollectionProperty) {
186: startCollection((CollectionProperty) key);
187: } else if (key instanceof MapProperty) {
188: startMap((MapProperty) key);
189: } else if (key instanceof TestElementProperty) {
190: stack.addLast(key.getName());
191: } else {
192: DefaultConfiguration config = new DefaultConfiguration(
193: "property", "property");
194: config.setValue(key.getStringValue());
195: config.setAttribute("name", key.getName());
196: config.setAttribute("propType", key.getClass().getName());
197: config.setAttribute(OldSaveService.XML_SPACE,
198: OldSaveService.PRESERVE);
199: stack.addLast(config);
200: }
201:
202: }
203:
204: }
|