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 java.util.Iterator;
022:
023: import org.apache.jorphan.collections.HashTree;
024:
025: import com.thoughtworks.xstream.mapper.Mapper;
026: import com.thoughtworks.xstream.converters.MarshallingContext;
027: import com.thoughtworks.xstream.converters.UnmarshallingContext;
028: import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
029: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
030: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
031:
032: /**
033: * @author mstover
034: *
035: * To change the template for this generated type comment go to Window -
036: * Preferences - Java - Code Generation - Code and Comments
037: */
038: public class HashTreeConverter extends AbstractCollectionConverter {
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: /*
049: * (non-Javadoc)
050: *
051: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
052: */
053: public boolean canConvert(Class arg0) {
054: return HashTree.class.isAssignableFrom(arg0);
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
061: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
062: * com.thoughtworks.xstream.converters.MarshallingContext)
063: */
064: public void marshal(Object arg0, HierarchicalStreamWriter writer,
065: MarshallingContext context) {
066: HashTree tree = (HashTree) arg0;
067: Iterator iter = tree.list().iterator();
068: while (iter.hasNext()) {
069: Object item = iter.next();
070: writeItem(item, context, writer);
071: writeItem(tree.getTree(item), context, writer);
072: }
073:
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
080: * com.thoughtworks.xstream.converters.UnmarshallingContext)
081: */
082: public Object unmarshal(HierarchicalStreamReader reader,
083: UnmarshallingContext context) {
084: boolean isKey = true;
085: Object current = null;
086: HashTree tree = (HashTree) createCollection(context
087: .getRequiredType());
088: while (reader.hasMoreChildren()) {
089: reader.moveDown();
090: Object item = readItem(reader, context, tree);
091: if (isKey) {
092: tree.add(item);
093: current = item;
094: isKey = false;
095: } else {
096: tree.set(current, (HashTree) item);
097: isKey = true;
098: }
099: reader.moveUp();
100: }
101: return tree;
102: }
103:
104: /**
105: * @param arg0
106: */
107: public HashTreeConverter(Mapper arg0) {
108: super(arg0);
109: }
110: }
|