1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id: CTFUtils.java 6787 2007-01-29 06:00:49Z ekuns $
44 *
45 */
46 package org.castor.xmlctf.util;
47
48 import java.io.StringReader;
49 import java.util.HashMap;
50 import java.util.Map;
51
52 import org.castor.xmlctf.xmldiff.XMLDiff;
53 import org.exolab.castor.xml.MarshalException;
54 import org.exolab.castor.xml.Unmarshaller;
55 import org.exolab.castor.xml.ValidationException;
56
57 /**
58 * This class contains utility methods needed by the CTF.
59 *
60 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
61 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
62 * @version $Revision: 6787 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
63 */
64 public class CTFUtils {
65
66 /**
67 * The Java primitives.
68 */
69 public static final String BOOLEAN = "boolean";
70 public static final String BYTE = "byte";
71 public static final String CHARACTER = "character";
72 public static final String DOUBLE = "double";
73 public static final String FLOAT = "float";
74 public static final String INT = "int";
75 public static final String LONG = "long";
76 public static final String SHORT = "short";
77 public static final String STRING = "String";
78
79 private static Map nameMap = new HashMap();
80 static {
81 nameMap.put(BOOLEAN, Boolean.TYPE);
82 nameMap.put(BYTE, Byte.TYPE);
83 nameMap.put(CHARACTER, Character.TYPE);
84 nameMap.put(DOUBLE, Double.TYPE);
85 nameMap.put(FLOAT, Float.TYPE);
86 nameMap.put(INT, Integer.TYPE);
87 nameMap.put(LONG, Long.TYPE);
88 nameMap.put(SHORT, Short.TYPE);
89 }
90
91 /**
92 * No-arg constructor. Private as we're a static utility class.
93 */
94 private CTFUtils() {
95 // Nothing to do
96 }
97
98 /**
99 * Compares two XML documents located at 2 given URLs, returning the number
100 * of differences or 0 if both documents are 'XML equivalent'.
101 *
102 * @param document1 the URL of the first XML document.
103 * @param document2 the URL of the second XML document.
104 * @return an int indicating the number of differences or 0 if both
105 * documents are 'XML equivalent'.
106 * @throws java.io.IOException if an error occurs reading either XML
107 * document
108 */
109 public static int compare(final String document1, final String document2)
110 throws java.io.IOException {
111 XMLDiff diff = new XMLDiff(document1, document2);
112 return diff.compare();
113 }
114
115 /**
116 * Returns the class associated with the given name.
117 *
118 * @param name the fully qualified name of the class to return. Primitives
119 * are handled through their name and not their class name. For
120 * instance 'boolean' should be used instead of
121 * 'java.lang.Boolean.TYPE'.
122 * @param loader the ClassLoader to use if the class needs to be loaded
123 * @return the class associated with given name.
124 * @throws ClassNotFoundException if the given class cannot be loaded using
125 * the provided class loader.
126 */
127 public static Class getClass(final String name, final ClassLoader loader)
128 throws ClassNotFoundException {
129 if (name == null) {
130 throw new IllegalArgumentException("Name shouldn't be null.");
131 }
132
133 Class clazz = (Class) nameMap.get(name);
134 if (clazz != null) {
135 return clazz;
136 }
137
138 return loader.loadClass(name);
139 }
140
141 /**
142 * Converts the given value to a Java representation that corresponds to the
143 * given type.
144 *
145 * @param value the value to be converted
146 * @param type a string representation of the java type.
147 * @param loader an optional ClassLoader used in case we need to use the
148 * Unmarshaller to retrieve a complex java object.
149 * @return an java object that corresponds to the given value converted to a
150 * java type according to the type passed as parameter.
151 * @throws ClassNotFoundException if the type is not a recognized primitive
152 * type and the class loader provided cannot load the type
153 * @throws MarshalException if the type is not a recognized primitive type
154 * and no Marshaller can be found for that type
155 */
156 public static Object instantiateObject(final String type, final String value,
157 final ClassLoader loader)
158 throws ClassNotFoundException, MarshalException {
159 if (type.equals(STRING) || type.equals(String.class.getName())) {
160 return value;
161 } else if (type.equals(BOOLEAN) || type.equals(Boolean.class.getName())) {
162 return new Boolean(value);
163 } else if (type.equals(BYTE) || type.equals(Byte.class.getName())) {
164 return new Byte(value);
165 } else if (type.equals(CHARACTER) || type.equals(Character.class.getName())) {
166 return new Character(value.charAt(0));
167 } else if (type.equals(DOUBLE) || type.equals(Double.class.getName())) {
168 return new Double(value);
169 } else if (type.equals(FLOAT) || type.equals(Float.class.getName())) {
170 return new Float(value);
171 } else if (type.equals(INT) || type.equals(Integer.class.getName())) {
172 return new Integer(value);
173 } else if (type.equals(LONG) || type.equals(Long.class.getName())) {
174 return new Long(value);
175 } else if (type.equals(SHORT) || type.equals(Short.class.getName())) {
176 return new Short(value);
177 }
178
179 //-- Else we let the unmarshaller get us the class
180 try {
181 Class clazz = loader.loadClass(type);
182 Unmarshaller unmarshaller = new Unmarshaller(clazz);
183 return unmarshaller.unmarshal(new StringReader(value));
184 } catch (ValidationException e) {
185 //--this can't happen, just log it
186 e.printStackTrace();
187 }
188
189 return null;
190 }
191
192 }