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.converters;
020:
021: import org.apache.jmeter.testelement.property.CollectionProperty;
022: import org.apache.jmeter.testelement.property.JMeterProperty;
023: import org.apache.jmeter.testelement.property.MapProperty;
024: import org.apache.jmeter.testelement.property.MultiProperty;
025: import org.apache.jmeter.testelement.property.PropertyIterator;
026:
027: import com.thoughtworks.xstream.mapper.Mapper;
028: import com.thoughtworks.xstream.converters.MarshallingContext;
029: import com.thoughtworks.xstream.converters.UnmarshallingContext;
030: import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
031: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
032: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
033:
034: /**
035: * @author mstover
036: *
037: * To change the template for this generated type comment go to Window -
038: * Preferences - Java - Code Generation - Code and Comments
039: */
040: public class MultiPropertyConverter extends AbstractCollectionConverter {
041:
042: private static final String ATT_NAME = "name"; //$NON-NLS-1$
043:
044: /**
045: * Returns the converter version; used to check for possible
046: * incompatibilities
047: */
048: public static String getVersion() {
049: return "$Revision: 514283 $"; //$NON-NLS-1$
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
056: */
057: public boolean canConvert(Class arg0) {
058: return arg0.equals(CollectionProperty.class)
059: || arg0.equals(MapProperty.class);
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
066: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
067: * com.thoughtworks.xstream.converters.MarshallingContext)
068: */
069: public void marshal(Object arg0, HierarchicalStreamWriter writer,
070: MarshallingContext context) {
071: MultiProperty prop = (MultiProperty) arg0;
072: writer.addAttribute(ATT_NAME, ConversionHelp.encode(prop
073: .getName()));
074: PropertyIterator iter = prop.iterator();
075: while (iter.hasNext()) {
076: writeItem(iter.next(), context, writer);
077: }
078:
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
085: * com.thoughtworks.xstream.converters.UnmarshallingContext)
086: */
087: public Object unmarshal(HierarchicalStreamReader reader,
088: UnmarshallingContext context) {
089: MultiProperty prop = (MultiProperty) createCollection(context
090: .getRequiredType());
091: prop.setName(ConversionHelp.decode(reader
092: .getAttribute(ATT_NAME)));
093: while (reader.hasMoreChildren()) {
094: reader.moveDown();
095: JMeterProperty subProp = (JMeterProperty) readItem(reader,
096: context, prop);
097: prop.addProperty(subProp);
098: reader.moveUp();
099: }
100: return prop;
101: }
102:
103: /**
104: * @param arg0
105: */
106: public MultiPropertyConverter(Mapper arg0) {
107: super(arg0);
108: }
109: }
|