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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: /*
020: * Created on Jul 27, 2004
021: */
022: package org.apache.jmeter.save.converters;
023:
024: import java.io.UnsupportedEncodingException;
025: import java.net.URLDecoder;
026: import java.net.URLEncoder;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import org.apache.jmeter.save.SaveService;
031: import org.apache.jmeter.testelement.TestElement;
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.log.Logger;
034:
035: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
036: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
037:
038: /**
039: * Utility conversion routines for use with XStream
040: *
041: */
042: public class ConversionHelp {
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: private static final String CHAR_SET = "UTF-8"; //$NON-NLS-1$
047:
048: // Attributes for TestElement and TestElementProperty
049: // Must all be unique
050: public static final String ATT_CLASS = "class"; //$NON-NLS-1$
051: public static final String ATT_NAME = "name"; // $NON-NLS-1$
052: public static final String ATT_ELEMENT_TYPE = "elementType"; // $NON-NLS-1$
053:
054: private static final String ATT_TE_ENABLED = "enabled"; //$NON-NLS-1$
055: private static final String ATT_TE_TESTCLASS = "testclass"; //$NON-NLS-1$
056: private static final String ATT_TE_GUICLASS = "guiclass"; //$NON-NLS-1$
057: private static final String ATT_TE_NAME = "testname"; //$NON-NLS-1$
058:
059: /*
060: * These must be set before reading/writing the XML. Rather a hack, but
061: * saves changing all the method calls to include an extra variable.
062: */
063: private static String inVersion;
064:
065: private static String outVersion = "1.1"; // Default for writing//$NON-NLS-1$
066:
067: public static void setInVersion(String v) {
068: inVersion = v;
069: }
070:
071: public static void setOutVersion(String v) {
072: outVersion = v;
073: }
074:
075: /**
076: * Encode a string (if necessary) for output to a JTL file.
077: * Strings are only encoded if the output version is 1.0,
078: * but nulls are always converted to the empty string.
079: *
080: * @param p string to encode
081: * @return encoded string (will never be null)
082: */
083: public static String encode(String p) {
084: if (p == null) {// Nulls cannot be written using PrettyPrintWriter - they cause an NPE
085: return ""; // $NON-NLS-1$
086: }
087: // Only encode strings if outVersion = 1.0
088: if (!"1.0".equals(outVersion))//$NON-NLS-1$
089: return p;
090: try {
091: String p1 = URLEncoder.encode(p, CHAR_SET);
092: return p1;
093: } catch (UnsupportedEncodingException e) {
094: log.warn("System doesn't support " + CHAR_SET, e);
095: return p;
096: }
097: }
098:
099: public static String decode(String p) {
100: if (!"1.0".equals(inVersion))//$NON-NLS-1$
101: return p;
102: // Only decode strings if inVersion = 1.0
103: if (p == null) {
104: return null;
105: }
106: try {
107: return URLDecoder.decode(p, CHAR_SET);
108: } catch (UnsupportedEncodingException e) {
109: log.warn("System doesn't support " + CHAR_SET, e);
110: return p;
111: }
112: }
113:
114: public static String cdata(byte[] chars, String encoding)
115: throws UnsupportedEncodingException {
116: StringBuffer buf = new StringBuffer("<![CDATA[");
117: buf.append(new String(chars, encoding));
118: buf.append("]]>");
119: return buf.toString();
120: }
121:
122: // Names of properties that are handled specially
123: private static final Map propertyToAttribute = new HashMap();
124:
125: private static void mapentry(String prop, String att) {
126: propertyToAttribute.put(prop, att);
127: }
128:
129: static {
130: mapentry(TestElement.NAME, ATT_TE_NAME);
131: mapentry(TestElement.GUI_CLASS, ATT_TE_GUICLASS);//$NON-NLS-1$
132: mapentry(TestElement.TEST_CLASS, ATT_TE_TESTCLASS);//$NON-NLS-1$
133: mapentry(TestElement.ENABLED, ATT_TE_ENABLED);
134: }
135:
136: private static void saveClass(TestElement el,
137: HierarchicalStreamWriter writer, String prop) {
138: String clazz = el.getPropertyAsString(prop);
139: if (clazz.length() > 0) {
140: writer.addAttribute((String) propertyToAttribute.get(prop),
141: SaveService.classToAlias(clazz));
142: }
143: }
144:
145: private static void restoreClass(TestElement el,
146: HierarchicalStreamReader reader, String prop) {
147: String att = (String) propertyToAttribute.get(prop);
148: String alias = reader.getAttribute(att);
149: if (alias != null) {
150: el.setProperty(prop, SaveService.aliasToClass(alias));
151: }
152: }
153:
154: private static void saveItem(TestElement el,
155: HierarchicalStreamWriter writer, String prop, boolean encode) {
156: String item = el.getPropertyAsString(prop);
157: if (item.length() > 0) {
158: if (encode)
159: item = ConversionHelp.encode(item);
160: writer.addAttribute((String) propertyToAttribute.get(prop),
161: item);
162: }
163: }
164:
165: private static void restoreItem(TestElement el,
166: HierarchicalStreamReader reader, String prop, boolean decode) {
167: String att = (String) propertyToAttribute.get(prop);
168: String value = reader.getAttribute(att);
169: if (value != null) {
170: if (decode)
171: value = ConversionHelp.decode(value);
172: el.setProperty(prop, value);
173: }
174: }
175:
176: public static boolean isSpecialProperty(String name) {
177: return propertyToAttribute.containsKey(name);
178: }
179:
180: public static void saveSpecialProperties(TestElement el,
181: HierarchicalStreamWriter writer) {
182: saveClass(el, writer, TestElement.GUI_CLASS);
183: saveClass(el, writer, TestElement.TEST_CLASS);
184: saveItem(el, writer, TestElement.NAME, true);
185: saveItem(el, writer, TestElement.ENABLED, false);
186: }
187:
188: public static void restoreSpecialProperties(TestElement el,
189: HierarchicalStreamReader reader) {
190: restoreClass(el, reader, TestElement.GUI_CLASS);
191: restoreClass(el, reader, TestElement.TEST_CLASS);
192: restoreItem(el, reader, TestElement.NAME, true);
193: restoreItem(el, reader, TestElement.ENABLED, false);
194: }
195: }
|