Source Code Cross Referenced for ConversionHelp.java in  » Testing » jakarta-jmeter » org » apache » jmeter » save » converters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.save.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.