summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/xml/XMLRPCDecoderHelper.java
blob: 2368672f07ee729e6e2e4fda7e5691be9eb45750 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Michael Powers

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.web.xml;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import net.wotonomy.foundation.internal.Introspector;
import net.wotonomy.foundation.internal.ValueConverter;
import net.wotonomy.foundation.internal.WotonomyException;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

/**
* Used by XMLDecoder to implement the necessary interfaces
* required by the jclark xp parser. 
* This class is not thread safe.
*/
class XMLRPCDecoderHelper extends DefaultHandler
{
    protected final Object nilMarker = new Object();
    protected Stack valueStack;
    protected String methodName;
    protected int faultCode;
    protected String faultString;
    protected List parameters;
    protected StringBuffer cdataBuffer;



	public XMLRPCDecoderHelper()
    {
        valueStack = new Stack();
        parameters = new LinkedList();
        cdataBuffer = new StringBuffer();
        reset();
    } 
    
    public void reset()
    {
        valueStack.clear();
        parameters.clear();
        cdataBuffer.setLength( 0 );
        methodName = null;
        faultCode = 0;
        faultString = null;
    }
    
    public boolean isRequest()
    {
        return ( methodName != null );   
    }
    
    public boolean isResponse()
    {
        return ( methodName == null );   
    }
    
    public boolean isFault()
    {
        // faults are responses
        return ( isResponse() ) && ( faultString != null );   
    }
    
    public int getFaultCode() 
    {
        return faultCode;   
    }
    
    public String getFaultString()
    {
        return faultString;   
    }
    
    public String getMethodName()
    {
        return methodName;   
    }
    
    public Object[] getParameters()
    {
        return parameters.toArray();   
    }
    
    public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		reset();
	}
	
    public Object getResult() 
    {
        if ( valueStack.empty() ) return null;
        Object result = valueStack.peek();
        if ( result == nilMarker ) result = null;
        return result;
    }

    
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
        if ( XMLRPCEncoder.VALUE.equals( localName ) )
        {
            ValueMarker marker = new ValueMarker(); 
            String classname = attributes.getValue( uri, XMLRPCEncoder.CLASS );
            if ( classname != null )
            {
                try
                {
                    Class c = Class.forName( classname );
                    if ( c != null )
                    {
                        marker.setMarkerClass( c );
                    }
                }
                catch ( Exception exc )
                {
                    System.out.println( "XMLRPCDecoderHelper.startElement: " +
                        "Can't find class: " + classname );
                }
            }
            valueStack.push( marker );
        }
	}
	
    public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		char[] someChars = new char[((start+length)<=ch.length) ? length : ch.length-start];
		for (int i = 0; i < someChars.length; i++ ) {
			someChars[i] = ch[start+i];
		}
		cdataBuffer.append(someChars);
	}

	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);

        // if any cdata is buffered or if string value
        if ( ( XMLRPCEncoder.STRING.equals( localName ) ) 
        ||   ( cdataBuffer.length() > 0 ) )
        {
            // push value on the stack
            valueStack.push( cdataBuffer.toString() );
            cdataBuffer.setLength( 0 );
        }

        if ( XMLRPCEncoder.VALUE.equals( localName ) )  
        {	
            Object value = valueStack.pop();
            try
            {
//                ValueMarker marker = (ValueMarker) valueStack.pop();
                ValueMarker marker = null;
                Object markerValue = valueStack.pop();
                if ( markerValue instanceof ValueMarker )
                {
                    marker = (ValueMarker) markerValue;
                }
                else
                {
                    throw new WotonomyException( "Expected value marker, found" 
                        + markerValue.getClass() + " : " + markerValue );
                }
                
//System.out.println( "getMarkerClass: " + marker.getMarkerClass() + " : " + value );                            
//System.out.println( valueStack );
                if ( marker.getMarkerClass() != null )
                {
                    // apply introspection
                    if ( value instanceof Map )
                    {
                        Map map = (Map)value;
                        Map.Entry entry;
                        value = marker.getMarkerClass().newInstance();
                        Iterator it = map.entrySet().iterator();
                        Object entryValue;
                        while( it.hasNext() )
                        {
                            entry = (Map.Entry) it.next();
                            entryValue = entry.getValue();
                            if ( entryValue == nilMarker ) entryValue = null;
                            Introspector.set( 
                                value, entry.getKey().toString(), entryValue );
                        }
                    }
                    if ( ! ( value.getClass().equals( marker.getMarkerClass() ) ) )
                    {
                        Object converted = 
                            ValueConverter.convertObjectToClass( 
                                value, marker.getMarkerClass() );
                        if ( converted != null )
                        {
                            value = converted;
                        }
                    }
                }
            }
            catch ( Exception exc )
            {
                // fall back on unconverted value
            }
            
            valueStack.push( value );
// System.out.println( "convertedValue: " + value + "("+ value.getClass() +")" );            
        }
        else
        if ( XMLRPCEncoder.MEMBER.equals( localName ) )  // Map.Entry
        {
            // leave key and value to be handled by struct
        }
        else
        if ( XMLRPCEncoder.STRUCT.equals( localName ) ) // write Entries to map or object
        {
            // write values to array (reverse the order)  
            Object value;      
            Map map = new HashMap();
            while ( ( ! valueStack.empty() ) 
            && ( ! ( valueStack.peek() instanceof ValueMarker ) ) )
            {
                value = valueStack.pop();
                map.put( valueStack.pop(), value );
            }
            // push the list on the stack
            valueStack.push( map );
        }
        else
        if ( XMLRPCEncoder.ARRAY.equals( localName ) ) 
        {
//System.out.println( "ended ARRAY: " + valueStack.size() );
            // write values to array (reverse the order)        
            Object value;
            LinkedList list = new LinkedList();
            while ( ( ! valueStack.empty() ) 
            && ( ! ( valueStack.peek() instanceof ValueMarker ) ) )
            {
                value = valueStack.pop();
                if ( value == nilMarker ) value = null;                
                list.addFirst( value );
            }
            // push the list on the stack
            valueStack.push( list );
        }
        else
        if ( XMLRPCEncoder.INT.equals( localName ) )  
        {
            Object value = valueStack.pop();
            try
            {
                valueStack.push( 
                    new Integer( value.toString() ) );
            }
            catch ( NumberFormatException exc )
            {
                throw new WotonomyException( 
                    "Invalid double format: " + value.toString() );
            }
        }
        else
        if ( XMLRPCEncoder.I4.equals( localName ) )  
        {
            Object value = valueStack.pop();
            try
            {
                valueStack.push( 
                    new Integer( value.toString() ) );
            }
            catch ( NumberFormatException exc )
            {
                throw new WotonomyException( 
                    "Invalid double format: " + value.toString() );
            }
        }
        else
        if ( XMLRPCEncoder.NIL.equals( localName ) )  
        {
            valueStack.push( nilMarker );
        }
        else
        if ( XMLRPCEncoder.DOUBLE.equals( localName ) )  
        {
            Object value = valueStack.pop();
            try
            {
                valueStack.push( 
                    new Double( value.toString() ) );
            }
            catch ( NumberFormatException exc )
            {
                throw new WotonomyException( 
                    "Invalid double format: " + value.toString() );
            }
        }
        else
        if ( XMLRPCEncoder.DATE.equals( localName ) )  
        {
            Object value = valueStack.pop();
            try
            {
                valueStack.push( 
                    XMLRPCEncoder.DATEFORMAT8601.parseObject( 
                        value.toString() ) );
            }
            catch ( Exception exc )
            {
                throw new WotonomyException( 
                    "Invalid date format: " + value );
            }
        }
        else
        if ( XMLRPCEncoder.BOOLEAN.equals( localName ) )  
        {
            Object value = valueStack.pop();
            if ( XMLRPCEncoder.TRUE.equals( value ) )
            {
                valueStack.push( Boolean.TRUE );
            }
            else
            if ( XMLRPCEncoder.FALSE.equals( value ) )
            {
                valueStack.push( Boolean.FALSE );
            }
            else
            {
                throw new WotonomyException( 
                    "Invalid boolean format: " + value );
            }
        }
        else
        if ( XMLRPCEncoder.BASE64.equals( localName ) )  
        {
            throw new WotonomyException( "Not implemented yet." );
        }
        else
        if ( XMLRPCEncoder.FAULT.equals( localName ) )  
        {
            Map faultMap = (Map) valueStack.pop();
            try
            {
                faultCode = ((Integer)
                    faultMap.get( XMLRPCEncoder.FAULTCODE )).intValue();
                faultString = (String) faultMap.get( XMLRPCEncoder.FAULTSTRING );
            }
            catch ( Exception exc )
            {
                throw new WotonomyException(
                    "Invalid fault format: " + faultMap );
            }
        }
        else
        if ( XMLRPCEncoder.METHODNAME.equals( localName ) )  
        {
            methodName = (String) valueStack.pop();
        }
        else
        if ( XMLRPCEncoder.PARAM.equals( localName ) )  
        {
            //NOTE: this leaves the parameter on the stack
            parameters.add( getResult() );
        }
    }
    


	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		super.endPrefixMapping(prefix);
	}

	public void error(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.error(e);
	}

	public void fatalError(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.fatalError(e);
	}

	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		super.ignorableWhitespace(ch, start, length);
	}

	public void notationDecl(String name, String publicId, String systemId) throws SAXException {
		// TODO Auto-generated method stub
		super.notationDecl(name, publicId, systemId);
	}

	public void processingInstruction(String target, String data) throws SAXException {
		// TODO Auto-generated method stub
		super.processingInstruction(target, data);
	}

	public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
		// NOTE: Sun accepted an incompatible api difference.  The (false) should be tossed by hotspot
		try {
			if (false) throw new IOException("Fake exception to make it compile in both 1.4 and 1.5");
			return super.resolveEntity(publicId, systemId);		
		} catch (IOException e) {
			throw new SAXException(e.getClass().getName() + " thrown while resolving entity.",e);
		}
	}

	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		super.setDocumentLocator(locator);
	}

	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		super.skippedEntity(name);
	}



	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		// TODO Auto-generated method stub
		super.startPrefixMapping(prefix, uri);
	}

	public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException {
		// TODO Auto-generated method stub
		super.unparsedEntityDecl(name, publicId, systemId, notationName);
	}

	public void warning(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.warning(e);
	}

    
    // marker class
    
    private class ValueMarker 
    {
        private Class theClass;
    
        public ValueMarker()
        {
            theClass = null;
        }
    
        public void setMarkerClass( Class aClass )
        {
            theClass = aClass;
        }
        
        public Class getMarkerClass()
        {
            return theClass;
        }

        public String toString()
        {
            return "[ValueMarker: " + theClass + "]";
        }
    }
    
}

/*
 * $Log$
 * Revision 1.1  2006/02/19 01:44:03  cgruber
 * Add xmlrpc files
 * Remove jclark and replace with dom4j and javax.xml.sax stuff
 * Re-work dependencies and imports so it all compiles.
 *
 * Revision 1.1  2006/02/16 13:22:22  cgruber
 * Check in all sources in eclipse-friendly maven-enabled packages.
 *
 * Revision 1.7  2003/08/06 23:07:53  chochos
 * general code cleanup (mostly, removing unused imports)
 *
 * Revision 1.6  2001/03/03 15:16:35  mpowers
 * Fixed bug in decoding empty strings: string handler did nothing and
 * empty cdatas were ignored, so no value was placed on the stack.
 *
 * Revision 1.5  2001/02/17 16:52:06  mpowers
 * Changes in imports to support building with jdk1.1 collections.
 *
 * Revision 1.4  2001/02/09 15:51:39  mpowers
 * Fixed a pernicious bug: I was using the character event entirely
 * incorrectly, but the problem only exhibited itself with large data
 * and even then only randomly.  Now using a string buffer.
 *
 * Revision 1.3  2001/02/07 19:24:28  mpowers
 * Moved XML classes to separate package.
 *
 * Revision 1.2  2001/02/06 14:34:23  mpowers
 * Forgot to rename the package declarations.
 *
 * Revision 1.1  2001/02/06 14:31:19  mpowers
 * Moving XML utilities from util to xml package.
 *
 * Revision 1.1.1.1  2000/12/21 15:52:39  mpowers
 * Contributing wotonomy.
 *
 * Revision 1.2  2000/12/20 16:25:48  michael
 * Added log to all files.
 *
 *
 */