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 org.apache.jmeter.save.converters.ConversionHelp;
022: import org.apache.jorphan.collections.HashTree;
023:
024: import com.thoughtworks.xstream.mapper.Mapper;
025: import com.thoughtworks.xstream.converters.Converter;
026: import com.thoughtworks.xstream.converters.MarshallingContext;
027: import com.thoughtworks.xstream.converters.UnmarshallingContext;
028: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
029: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
030:
031: /**
032: * @author mstover
033: *
034: */
035: public class ScriptWrapperConverter implements Converter {
036:
037: private static final String ATT_PROPERTIES = "properties"; // $NON-NLS-1$
038: private static final String ATT_VERSION = "version"; // $NON-NLS-1$
039:
040: /**
041: * Returns the converter version; used to check for possible
042: * incompatibilities
043: */
044: public static String getVersion() {
045: return "$Revision: 514283 $"; // $NON-NLS-1$
046: }
047:
048: Mapper classMapper;
049:
050: public ScriptWrapperConverter(Mapper classMapper) {
051: this .classMapper = classMapper;
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
058: */
059: public boolean canConvert(Class arg0) {
060: return arg0.equals(ScriptWrapper.class);
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
067: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
068: * com.thoughtworks.xstream.converters.MarshallingContext)
069: */
070: public void marshal(Object arg0, HierarchicalStreamWriter writer,
071: MarshallingContext context) {
072: ScriptWrapper wrap = (ScriptWrapper) arg0;
073: String version = SaveService.getVERSION();
074: ConversionHelp.setOutVersion(version);// Ensure output follows version
075: writer.addAttribute(ATT_VERSION, version);
076: writer.addAttribute(ATT_PROPERTIES, SaveService
077: .getPropertiesVersion());
078: writer.startNode(classMapper.serializedClass(wrap.testPlan
079: .getClass()));
080: context.convertAnother(wrap.testPlan);
081: writer.endNode();
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
088: * com.thoughtworks.xstream.converters.UnmarshallingContext)
089: */
090: public Object unmarshal(HierarchicalStreamReader reader,
091: UnmarshallingContext context) {
092: ScriptWrapper wrap = new ScriptWrapper();
093: wrap.version = reader.getAttribute(ATT_VERSION);
094: ConversionHelp.setInVersion(wrap.version);// Make sure decoding
095: // follows input file
096: reader.moveDown();
097: wrap.testPlan = (HashTree) context.convertAnother(wrap,
098: getNextType(reader));
099: return wrap;
100: }
101:
102: protected Class getNextType(HierarchicalStreamReader reader) {
103: String classAttribute = reader
104: .getAttribute(ConversionHelp.ATT_CLASS);
105: Class type;
106: if (classAttribute == null) {
107: type = classMapper.realClass(reader.getNodeName());
108: } else {
109: type = classMapper.realClass(classAttribute);
110: }
111: return type;
112: }
113: }
|