This project has retired. For details please refer to its
Attic page.
RedbackKeyManagementJdoStaxReader xref
1
2
3
4
5
6 package org.apache.archiva.redback.keys.jdo.io.stax;
7
8
9
10
11
12 import java.io.ByteArrayInputStream;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.Reader;
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.text.DateFormat;
21 import java.text.ParsePosition;
22 import java.util.Locale;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import javax.xml.stream.*;
26 import org.apache.archiva.redback.keys.jdo.AuthenticationKeyDatabase;
27 import org.apache.archiva.redback.keys.jdo.JdoAuthenticationKey;
28
29
30
31
32
33
34 public class RedbackKeyManagementJdoStaxReader
35 {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public AuthenticationKeyDatabase read( Reader reader, boolean strict )
51 throws IOException, XMLStreamException
52 {
53 XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( reader );
54
55 return read( xmlStreamReader, strict );
56 }
57
58
59
60
61
62
63
64
65
66 public AuthenticationKeyDatabase read( Reader reader )
67 throws IOException, XMLStreamException
68 {
69 return read( reader, true );
70 }
71
72
73
74
75
76
77
78
79
80
81 public AuthenticationKeyDatabase read( InputStream stream, boolean strict )
82 throws IOException, XMLStreamException
83 {
84 XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( stream );
85
86 return read( xmlStreamReader, strict );
87 }
88
89
90
91
92
93
94
95
96
97 public AuthenticationKeyDatabase read( InputStream stream )
98 throws IOException, XMLStreamException
99 {
100 return read( stream, true );
101 }
102
103
104
105
106
107
108
109
110
111
112 public AuthenticationKeyDatabase read( String filePath, boolean strict )
113 throws IOException, XMLStreamException
114 {
115 java.io.File file = new java.io.File( filePath );
116 XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( file.toURI().toURL().toExternalForm(), new FileInputStream( file ) );
117
118 return read( xmlStreamReader, strict );
119 }
120
121
122
123
124
125
126
127
128
129 public AuthenticationKeyDatabase read( String filePath )
130 throws IOException, XMLStreamException
131 {
132 return read( filePath, true );
133 }
134
135
136
137
138
139
140
141
142
143
144
145 private boolean checkFieldWithDuplicate( XMLStreamReader xmlStreamReader, String tagName, String alias, java.util.Set parsed )
146 throws XMLStreamException
147 {
148 if ( !( xmlStreamReader.getLocalName().equals( tagName ) || xmlStreamReader.getLocalName().equals( alias ) ) )
149 {
150 return false;
151 }
152 if ( !parsed.add( tagName ) )
153 {
154 throw new XMLStreamException( "Duplicated tag: '" + tagName + "'", xmlStreamReader.getLocation() );
155 }
156 return true;
157 }
158
159
160
161
162
163
164
165
166 private void checkUnknownElement( XMLStreamReader xmlStreamReader, boolean strict )
167 throws XMLStreamException
168 {
169 if ( strict )
170 {
171 throw new XMLStreamException( "Unrecognised tag: '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation() );
172 }
173 int unrecognizedTagCount = 1;
174 while( unrecognizedTagCount != 0 )
175 {
176 xmlStreamReader.next();
177 if ( xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT )
178 {
179 unrecognizedTagCount++;
180 }
181 else if ( xmlStreamReader.getEventType() == XMLStreamConstants.END_ELEMENT )
182 {
183 unrecognizedTagCount--;
184 }
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197 private boolean getBooleanValue( String s, String attribute, XMLStreamReader xmlStreamReader )
198 throws XMLStreamException
199 {
200 if ( s != null )
201 {
202 return Boolean.valueOf( s ).booleanValue();
203 }
204 return false;
205 }
206
207
208
209
210
211
212
213
214
215
216
217 private byte getByteValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
218 throws XMLStreamException
219 {
220 if ( s != null )
221 {
222 try
223 {
224 return Byte.valueOf( s ).byteValue();
225 }
226 catch ( NumberFormatException nfe )
227 {
228 if ( strict )
229 {
230 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be a byte but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
231 }
232 }
233 }
234 return 0;
235 }
236
237
238
239
240
241
242
243
244
245
246 private char getCharacterValue( String s, String attribute, XMLStreamReader xmlStreamReader )
247 throws XMLStreamException
248 {
249 if ( s != null )
250 {
251 return s.charAt( 0 );
252 }
253 return 0;
254 }
255
256
257
258
259
260
261
262
263
264
265
266 private java.util.Date getDateValue( String s, String attribute, String dateFormat, XMLStreamReader xmlStreamReader )
267 throws XMLStreamException
268 {
269 if ( s != null )
270 {
271 String effectiveDateFormat = dateFormat;
272 if ( dateFormat == null )
273 {
274 effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
275 }
276 if ( "long".equals( effectiveDateFormat ) )
277 {
278 try
279 {
280 return new java.util.Date( Long.parseLong( s ) );
281 }
282 catch ( NumberFormatException e )
283 {
284 throw new XMLStreamException( e.getMessage(), xmlStreamReader.getLocation(), e );
285 }
286 }
287 else
288 {
289 try
290 {
291 DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
292 return dateParser.parse( s );
293 }
294 catch ( java.text.ParseException e )
295 {
296 throw new XMLStreamException( e.getMessage(), xmlStreamReader.getLocation(), e );
297 }
298 }
299 }
300 return null;
301 }
302
303
304
305
306
307
308
309
310 private String getDefaultValue( String s, String v )
311 {
312 if ( s == null )
313 {
314 s = v;
315 }
316 return s;
317 }
318
319
320
321
322
323
324
325
326
327
328
329 private double getDoubleValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
330 throws XMLStreamException
331 {
332 if ( s != null )
333 {
334 try
335 {
336 return Double.valueOf( s ).doubleValue();
337 }
338 catch ( NumberFormatException nfe )
339 {
340 if ( strict )
341 {
342 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be a floating point number but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
343 }
344 }
345 }
346 return 0;
347 }
348
349
350
351
352
353
354
355
356
357
358
359 private float getFloatValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
360 throws XMLStreamException
361 {
362 if ( s != null )
363 {
364 try
365 {
366 return Float.valueOf( s ).floatValue();
367 }
368 catch ( NumberFormatException nfe )
369 {
370 if ( strict )
371 {
372 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be a floating point number but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
373 }
374 }
375 }
376 return 0;
377 }
378
379
380
381
382
383
384
385
386
387
388
389 private int getIntegerValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
390 throws XMLStreamException
391 {
392 if ( s != null )
393 {
394 try
395 {
396 return Integer.valueOf( s ).intValue();
397 }
398 catch ( NumberFormatException nfe )
399 {
400 if ( strict )
401 {
402 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be an integer but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
403 }
404 }
405 }
406 return 0;
407 }
408
409
410
411
412
413
414
415
416
417
418
419 private long getLongValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
420 throws XMLStreamException
421 {
422 if ( s != null )
423 {
424 try
425 {
426 return Long.valueOf( s ).longValue();
427 }
428 catch ( NumberFormatException nfe )
429 {
430 if ( strict )
431 {
432 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be a long integer but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
433 }
434 }
435 }
436 return 0;
437 }
438
439
440
441
442
443
444
445
446
447
448
449 private String getRequiredAttributeValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
450 throws XMLStreamException
451 {
452 if ( s == null )
453 {
454 if ( strict )
455 {
456 throw new XMLStreamException( "Missing required value for attribute '" + attribute + "'", xmlStreamReader.getLocation() );
457 }
458 }
459 return s;
460 }
461
462
463
464
465
466
467
468
469
470
471
472 private short getShortValue( String s, String attribute, XMLStreamReader xmlStreamReader, boolean strict )
473 throws XMLStreamException
474 {
475 if ( s != null )
476 {
477 try
478 {
479 return Short.valueOf( s ).shortValue();
480 }
481 catch ( NumberFormatException nfe )
482 {
483 if ( strict )
484 {
485 throw new XMLStreamException( "Unable to parse element '" + attribute + "', must be a short integer but was '" + s + "'", xmlStreamReader.getLocation(), nfe );
486 }
487 }
488 }
489 return 0;
490 }
491
492
493
494
495
496
497
498 private String getTrimmedValue( String s )
499 {
500 if ( s != null )
501 {
502 s = s.trim();
503 }
504 return s;
505 }
506
507
508
509
510
511
512
513
514 private int nextTag( XMLStreamReader xmlStreamReader )
515 throws XMLStreamException
516 {
517 while ( true )
518 {
519 int eventType = xmlStreamReader.next();
520 switch ( eventType )
521 {
522 case XMLStreamConstants.CHARACTERS:
523 case XMLStreamConstants.CDATA:
524 case XMLStreamConstants.SPACE:
525 case XMLStreamConstants.PROCESSING_INSTRUCTION:
526 case XMLStreamConstants.COMMENT:
527 break;
528 case XMLStreamConstants.START_ELEMENT:
529 case XMLStreamConstants.END_ELEMENT:
530 return eventType;
531 default:
532 throw new XMLStreamException( "expected start or end tag", xmlStreamReader.getLocation() );
533 }
534 }
535 }
536
537
538
539
540
541
542
543
544
545
546 private AuthenticationKeyDatabase parseAuthenticationKeyDatabase( XMLStreamReader xmlStreamReader, boolean strict )
547 throws IOException, XMLStreamException
548 {
549 AuthenticationKeyDatabase authenticationKeyDatabase = new AuthenticationKeyDatabase();
550 java.util.Set parsed = new java.util.HashSet();
551 while ( ( strict ? xmlStreamReader.nextTag() : nextTag( xmlStreamReader ) ) == XMLStreamConstants.START_ELEMENT )
552 {
553 if ( checkFieldWithDuplicate( xmlStreamReader, "keys", null, parsed ) )
554 {
555 java.util.List keys = new java.util.ArrayList
556 authenticationKeyDatabase.setKeys( keys );
557 while ( xmlStreamReader.nextTag() == XMLStreamConstants.START_ELEMENT )
558 {
559 if ( "key".equals( xmlStreamReader.getLocalName() ) )
560 {
561 authenticationKeyDatabase.addKey( parseJdoAuthenticationKey( xmlStreamReader, strict ) );
562 }
563 else
564 {
565 throw new XMLStreamException( "Unrecognised tag: '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation() );
566 }
567 }
568 }
569 else
570 {
571 checkUnknownElement( xmlStreamReader, strict );
572 }
573 }
574 return authenticationKeyDatabase;
575 }
576
577
578
579
580
581
582
583
584
585
586 private JdoAuthenticationKey parseJdoAuthenticationKey( XMLStreamReader xmlStreamReader, boolean strict )
587 throws IOException, XMLStreamException
588 {
589 JdoAuthenticationKey jdoAuthenticationKey = new JdoAuthenticationKey();
590 java.util.Set parsed = new java.util.HashSet();
591 while ( ( strict ? xmlStreamReader.nextTag() : nextTag( xmlStreamReader ) ) == XMLStreamConstants.START_ELEMENT )
592 {
593 if ( checkFieldWithDuplicate( xmlStreamReader, "key", null, parsed ) )
594 {
595 jdoAuthenticationKey.setKey( getTrimmedValue( xmlStreamReader.getElementText() ) );
596 }
597 else if ( checkFieldWithDuplicate( xmlStreamReader, "forPrincipal", null, parsed ) )
598 {
599 jdoAuthenticationKey.setForPrincipal( getTrimmedValue( xmlStreamReader.getElementText() ) );
600 }
601 else if ( checkFieldWithDuplicate( xmlStreamReader, "purpose", null, parsed ) )
602 {
603 jdoAuthenticationKey.setPurpose( getTrimmedValue( xmlStreamReader.getElementText() ) );
604 }
605 else if ( checkFieldWithDuplicate( xmlStreamReader, "dateCreated", null, parsed ) )
606 {
607 String dateFormat = "long";
608 jdoAuthenticationKey.setDateCreated( getDateValue( getTrimmedValue( xmlStreamReader.getElementText() ), "dateCreated", dateFormat, xmlStreamReader ) );
609 }
610 else if ( checkFieldWithDuplicate( xmlStreamReader, "dateExpires", null, parsed ) )
611 {
612 String dateFormat = "long";
613 jdoAuthenticationKey.setDateExpires( getDateValue( getTrimmedValue( xmlStreamReader.getElementText() ), "dateExpires", dateFormat, xmlStreamReader ) );
614 }
615 else
616 {
617 checkUnknownElement( xmlStreamReader, strict );
618 }
619 }
620 return jdoAuthenticationKey;
621 }
622
623
624
625
626
627
628
629
630
631
632 private AuthenticationKeyDatabase read( XMLStreamReader xmlStreamReader, boolean strict )
633 throws IOException, XMLStreamException
634 {
635 int eventType = xmlStreamReader.getEventType();
636 String encoding = null;
637 while ( eventType != XMLStreamConstants.END_DOCUMENT )
638 {
639 if ( eventType == XMLStreamConstants.START_DOCUMENT )
640 {
641 encoding = xmlStreamReader.getCharacterEncodingScheme();
642 }
643 if ( eventType == XMLStreamConstants.START_ELEMENT )
644 {
645 if ( strict && ! "authenticationKeyDatabase".equals( xmlStreamReader.getLocalName() ) )
646 {
647 throw new XMLStreamException( "Expected root element 'authenticationKeyDatabase' but found '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation(), null );
648 }
649 AuthenticationKeyDatabase authenticationKeyDatabase = parseAuthenticationKeyDatabase( xmlStreamReader, strict );
650 authenticationKeyDatabase.setModelEncoding( encoding );
651 resolveReferences( authenticationKeyDatabase );
652 return authenticationKeyDatabase;
653 }
654 eventType = xmlStreamReader.next();
655 }
656 throw new XMLStreamException( "Expected root element 'authenticationKeyDatabase' but found no element at all: invalid XML document", xmlStreamReader.getLocation(), null );
657 }
658
659
660
661
662
663
664 private void resolveReferences( AuthenticationKeyDatabase value )
665 {
666 java.util.Map refs;
667 }
668
669 }