|
JMeter example source code file (ConversionHelp.java)
The JMeter ConversionHelp.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ /* * Created on Jul 27, 2004 */ package org.apache.jmeter.save.converters; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; /** * Utility conversion routines for use with XStream * */ public class ConversionHelp { private static final Logger log = LoggingManager.getLoggerForClass(); private static final String CHAR_SET = "UTF-8"; //$NON-NLS-1$ // Attributes for TestElement and TestElementProperty // Must all be unique public static final String ATT_CLASS = "class"; //$NON-NLS-1$ public static final String ATT_NAME = "name"; // $NON-NLS-1$ public static final String ATT_ELEMENT_TYPE = "elementType"; // $NON-NLS-1$ private static final String ATT_TE_ENABLED = "enabled"; //$NON-NLS-1$ private static final String ATT_TE_TESTCLASS = "testclass"; //$NON-NLS-1$ private static final String ATT_TE_GUICLASS = "guiclass"; //$NON-NLS-1$ private static final String ATT_TE_NAME = "testname"; //$NON-NLS-1$ /* * These must be set before reading/writing the XML. Rather a hack, but * saves changing all the method calls to include an extra variable. * * AFAIK the variables should only be accessed from one thread, so no need to synchronize. */ private static String inVersion; private static String outVersion = "1.1"; // Default for writing//$NON-NLS-1$ public static void setInVersion(String v) { inVersion = v; } public static void setOutVersion(String v) { outVersion = v; } /** * Encode a string (if necessary) for output to a JTL file. * Strings are only encoded if the output version is 1.0, * but nulls are always converted to the empty string. * * @param p string to encode * @return encoded string (will never be null) */ public static String encode(String p) { if (p == null) {// Nulls cannot be written using PrettyPrintWriter - they cause an NPE return ""; // $NON-NLS-1$ } // Only encode strings if outVersion = 1.0 if (!"1.0".equals(outVersion)) {//$NON-NLS-1$ return p; } try { String p1 = URLEncoder.encode(p, CHAR_SET); return p1; } catch (UnsupportedEncodingException e) { log.warn("System doesn't support " + CHAR_SET, e); return p; } } public static String decode(String p) { if (!"1.0".equals(inVersion)) {//$NON-NLS-1$ return p; } // Only decode strings if inVersion = 1.0 if (p == null) { return null; } try { return URLDecoder.decode(p, CHAR_SET); } catch (UnsupportedEncodingException e) { log.warn("System doesn't support " + CHAR_SET, e); return p; } } public static String cdata(byte[] chars, String encoding) throws UnsupportedEncodingException { StringBuilder buf = new StringBuilder("<![CDATA["); buf.append(new String(chars, encoding)); buf.append("]]>"); return buf.toString(); } // Names of properties that are handled specially private static final Map<String, String> propertyToAttribute=new HashMap Other JMeter examples (source code examples)Here is a short list of links related to this JMeter ConversionHelp.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.