Subversion Repository Public Repository

WOX2

@ 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package wox.serial;

import java.lang.reflect.*;
import java.security.*;
import java.text.*;
import org.litesoft.core.typeutils.Objects;
import java.util.*;

import sun.reflect.*;

/**
 * Version: 1.0
 * Author: Carlos R. Jaimez Gonzalez
 * Author: Simon M. Lucas
 * Site: http://woxserializer.sourceforge.net/
 *
 * Version: 1.5
 * Author: Steven M Lewis
 *
 * Version: 2.0
 * Author: George Smith
 * SVN: http://svn.xp-dev.com/svn/WOX2/
 * Note: XML form for vs 2 is more compact and therefor incompatible with vs 1
 */

public class Util
{
    public static boolean areEqual( Object pO1, Object pO2 )
    {
        return (pO1 == pO2) || // Either the object reference or both null!
               ((pO1 != null) && // then we can use regular .equals()
                pO1.equals( pO2 ));
    }

    public static boolean isNullOrEmpty( String pValue )
    {
        return (pValue == null) || (pValue.length() == 0);
    }

    public static void assertRequired( String pWhat, Object pObject )
    {
        if ( pObject == null )
        {
            throw new NullPointerException( pWhat + " Required" );
        }
    }

    public static void assertXmlName( String pWhat, String pName )
    {
        assertRequired( pWhat, pName );
        if ( pName.length() == 0 )
        {
            throw new IllegalArgumentException( pWhat + " Required" );
        }
        if ( pName.indexOf( ' ' ) != -1 )
        {
            throw new IllegalArgumentException( pWhat + " may not contain spaces" );
        }
    }

    /**
     * reflection factory for forcing default constructors
     */
    @SuppressWarnings({"unchecked"}) private static final ReflectionFactory reflFactory = //
            (ReflectionFactory) AccessController.doPrivileged( new ReflectionFactory.GetReflectionFactoryAction() );

    /**
     * This method returns a default constructor for the specified class.
     * Despite appearences it can be used to construct objects
     * of the specified type! of first non-serializable.
     *
     * @param cl The class to be used to get its constructor.
     *
     * @return The default constructor for the specified class.
     *
     * @throws Exception If there is a problem.
     */
    public static Constructor forceDefaultConstructor( Class cl )
            throws Exception
    {
        Constructor cons = Object.class.getDeclaredConstructor( new Class[0] );
        cons = reflFactory.newConstructorForSerialization( cl, cons );
        cons.setAccessible( true );
        // logger.log(Level.INFO,"Cons: " + cons);
        return cons;
    }

    public static final String CSHARP_DATE_FORMAT = "MM/dd/yyyy hh:mm:ss aa";

    public static String dateToString( Date ipn )
    {
        DateFormat dateFormat = new SimpleDateFormat( CSHARP_DATE_FORMAT );
        if ( ipn != null )
        {
            return dateFormat.format( ipn );
        }
        return "";
    }

    public static Date stringToDate( String ipn )
    {
        DateFormat dateFormat = new SimpleDateFormat( CSHARP_DATE_FORMAT );
        try
        {
            if ( ipn != null && ipn.length() > 10 )
            {
                return dateFormat.parse( ipn );
            }
        }
        catch ( ParseException e )
        {
            return null;
        }
        return null;
    }

    public static Class resolveType( String pTypeName )
    {
        try
        {
            return Util.class.getClassLoader().loadClass( pTypeName );
        }
        catch ( ClassNotFoundException e )
        {
            return null;
        }
    }

    @SuppressWarnings({"unchecked"})
    public static <T> T createInstance( Class pType, String pWoxTypeName )
    {
        Exception zException;
        try
        {
            return (T) pType.newInstance();
        }
        catch ( InstantiationException e )
        {
            zException = e;
        }
        catch ( IllegalAccessException e )
        {
            zException = e;
        }
        catch ( RuntimeException e )
        {
            zException = e;
        }

        String zPlus = (pWoxTypeName == null) ? ": " + pType : " (" + pType + ") for Wox Type: " + pWoxTypeName;
        throw new ApplicationException( "Unable to instantiate instance from Native Type" + zPlus, zException );
    }

    @SuppressWarnings({"unchecked"})
    public static <T> T cast( Object pInstance )
    {
        return (T) pInstance;
    }

//    public static boolean isNullable( Type typeOb )
//    {
//        String typeName = typeOb.Name;
//        if ( typeName.StartsWith( "Nullable`1" ) )
//        {
//            if ( typeName == "Nullable`1" )
//            {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    /**
//     * Turn a String into a Type
//     *
//     * @param pTypeName - !null type name
//     */
//    public static String BuildCannotResolveMessage( String typeName )
//    {
//        StringBuilder sb = new StringBuilder();
//        sb.append( "Cannot resolve type " + typeName );
//        sb.append( "\n" );
//
//        sb.append( "Looked in Assemblies:" );
//        sb.append( "\n" );
//        Assembly[] appAssemblies = AppDomain.CurrentDomain.getAssemblies();
//        for ( int i = 0; i < appAssemblies.length(); i++ )
//        {
//            Assembly assembly = appAssemblies[i];
//            String name = assembly.getName().Name;
//            sb.append( name );
//            sb.append( "\n" );
//        }
//
//        return sb.toString();
//    }
}

Commits for WOX2/trunk/Java/src/wox/serial/Util.java

Diff revisions: vs.
Revision Author Commited Message
17 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:44:02 +0000

Extracting commonfoundation

15 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 22:29:49 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000