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