This project has retired. For details please refer to its Attic page.
RedbackKeyManagementJdoStaxReader xref
View Javadoc

1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 1.7,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.apache.archiva.redback.keys.jdo.io.stax;
7   
8     //---------------------------------/
9    //- Imported classes and packages -/
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   * Class RedbackKeyManagementJdoStaxReader.
31   * 
32   * @version $Revision$ $Date$
33   */
34  public class RedbackKeyManagementJdoStaxReader
35  {
36  
37        //-----------/
38       //- Methods -/
39      //-----------/
40  
41      /**
42       * Method read.
43       * 
44       * @param reader
45       * @param strict
46       * @throws IOException
47       * @throws XMLStreamException
48       * @return AuthenticationKeyDatabase
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      } //-- AuthenticationKeyDatabase read( Reader, boolean )
57  
58      /**
59       * Method read.
60       * 
61       * @param reader
62       * @throws IOException
63       * @throws XMLStreamException
64       * @return AuthenticationKeyDatabase
65       */
66      public AuthenticationKeyDatabase read( Reader reader )
67          throws IOException, XMLStreamException
68      {
69          return read( reader, true );
70      } //-- AuthenticationKeyDatabase read( Reader )
71  
72      /**
73       * Method read.
74       * 
75       * @param stream
76       * @param strict
77       * @throws IOException
78       * @throws XMLStreamException
79       * @return AuthenticationKeyDatabase
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      } //-- AuthenticationKeyDatabase read( InputStream, boolean )
88  
89      /**
90       * Method read.
91       * 
92       * @param stream
93       * @throws IOException
94       * @throws XMLStreamException
95       * @return AuthenticationKeyDatabase
96       */
97      public AuthenticationKeyDatabase read( InputStream stream )
98          throws IOException, XMLStreamException
99      {
100         return read( stream, true );
101     } //-- AuthenticationKeyDatabase read( InputStream )
102 
103     /**
104      * Method read.
105      * 
106      * @param filePath
107      * @param strict
108      * @throws IOException
109      * @throws XMLStreamException
110      * @return AuthenticationKeyDatabase
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     } //-- AuthenticationKeyDatabase read( String, boolean )
120 
121     /**
122      * Method read.
123      * 
124      * @param filePath
125      * @throws IOException
126      * @throws XMLStreamException
127      * @return AuthenticationKeyDatabase
128      */
129     public AuthenticationKeyDatabase read( String filePath )
130         throws IOException, XMLStreamException
131     {
132         return read( filePath, true );
133     } //-- AuthenticationKeyDatabase read( String )
134 
135     /**
136      * Method checkFieldWithDuplicate.
137      * 
138      * @param xmlStreamReader
139      * @param parsed
140      * @param alias
141      * @param tagName
142      * @throws XMLStreamException
143      * @return boolean
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     } //-- boolean checkFieldWithDuplicate( XMLStreamReader, String, String, java.util.Set )
158 
159     /**
160      * Method checkUnknownElement.
161      * 
162      * @param xmlStreamReader
163      * @param strict
164      * @throws XMLStreamException
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     } //-- void checkUnknownElement( XMLStreamReader, boolean )
187 
188     /**
189      * Method getBooleanValue.
190      * 
191      * @param s
192      * @param xmlStreamReader
193      * @param attribute
194      * @throws XMLStreamException
195      * @return boolean
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     } //-- boolean getBooleanValue( String, String, XMLStreamReader )
206 
207     /**
208      * Method getByteValue.
209      * 
210      * @param s
211      * @param strict
212      * @param xmlStreamReader
213      * @param attribute
214      * @throws XMLStreamException
215      * @return byte
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     } //-- byte getByteValue( String, String, XMLStreamReader, boolean )
236 
237     /**
238      * Method getCharacterValue.
239      * 
240      * @param s
241      * @param xmlStreamReader
242      * @param attribute
243      * @throws XMLStreamException
244      * @return char
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     } //-- char getCharacterValue( String, String, XMLStreamReader )
255 
256     /**
257      * Method getDateValue.
258      * 
259      * @param s
260      * @param xmlStreamReader
261      * @param dateFormat
262      * @param attribute
263      * @throws XMLStreamException
264      * @return Date
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     } //-- java.util.Date getDateValue( String, String, String, XMLStreamReader )
302 
303     /**
304      * Method getDefaultValue.
305      * 
306      * @param s
307      * @param v
308      * @return String
309      */
310     private String getDefaultValue( String s, String v )
311     {
312         if ( s == null )
313         {
314             s = v;
315         }
316         return s;
317     } //-- String getDefaultValue( String, String )
318 
319     /**
320      * Method getDoubleValue.
321      * 
322      * @param s
323      * @param strict
324      * @param xmlStreamReader
325      * @param attribute
326      * @throws XMLStreamException
327      * @return double
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     } //-- double getDoubleValue( String, String, XMLStreamReader, boolean )
348 
349     /**
350      * Method getFloatValue.
351      * 
352      * @param s
353      * @param strict
354      * @param xmlStreamReader
355      * @param attribute
356      * @throws XMLStreamException
357      * @return float
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     } //-- float getFloatValue( String, String, XMLStreamReader, boolean )
378 
379     /**
380      * Method getIntegerValue.
381      * 
382      * @param s
383      * @param strict
384      * @param xmlStreamReader
385      * @param attribute
386      * @throws XMLStreamException
387      * @return int
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     } //-- int getIntegerValue( String, String, XMLStreamReader, boolean )
408 
409     /**
410      * Method getLongValue.
411      * 
412      * @param s
413      * @param strict
414      * @param xmlStreamReader
415      * @param attribute
416      * @throws XMLStreamException
417      * @return long
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     } //-- long getLongValue( String, String, XMLStreamReader, boolean )
438 
439     /**
440      * Method getRequiredAttributeValue.
441      * 
442      * @param s
443      * @param strict
444      * @param xmlStreamReader
445      * @param attribute
446      * @throws XMLStreamException
447      * @return String
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     } //-- String getRequiredAttributeValue( String, String, XMLStreamReader, boolean )
461 
462     /**
463      * Method getShortValue.
464      * 
465      * @param s
466      * @param strict
467      * @param xmlStreamReader
468      * @param attribute
469      * @throws XMLStreamException
470      * @return short
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     } //-- short getShortValue( String, String, XMLStreamReader, boolean )
491 
492     /**
493      * Method getTrimmedValue.
494      * 
495      * @param s
496      * @return String
497      */
498     private String getTrimmedValue( String s )
499     {
500         if ( s != null )
501         {
502             s = s.trim();
503         }
504         return s;
505     } //-- String getTrimmedValue( String )
506 
507     /**
508      * Method nextTag.
509      * 
510      * @param xmlStreamReader
511      * @throws XMLStreamException
512      * @return int
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     } //-- int nextTag( XMLStreamReader )
536 
537     /**
538      * Method parseAuthenticationKeyDatabase.
539      * 
540      * @param xmlStreamReader
541      * @param strict
542      * @throws IOException
543      * @throws XMLStreamException
544      * @return AuthenticationKeyDatabase
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/*<JdoAuthenticationKey>*/();
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     } //-- AuthenticationKeyDatabase parseAuthenticationKeyDatabase( XMLStreamReader, boolean )
576 
577     /**
578      * Method parseJdoAuthenticationKey.
579      * 
580      * @param xmlStreamReader
581      * @param strict
582      * @throws IOException
583      * @throws XMLStreamException
584      * @return JdoAuthenticationKey
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     } //-- JdoAuthenticationKey parseJdoAuthenticationKey( XMLStreamReader, boolean )
622 
623     /**
624      * Method read.
625      * 
626      * @param xmlStreamReader
627      * @param strict
628      * @throws IOException
629      * @throws XMLStreamException
630      * @return AuthenticationKeyDatabase
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     } //-- AuthenticationKeyDatabase read( XMLStreamReader, boolean )
658 
659     /**
660      * Method resolveReferences.
661      * 
662      * @param value
663      */
664     private void resolveReferences( AuthenticationKeyDatabase value )
665     {
666         java.util.Map refs;
667     } //-- void resolveReferences( AuthenticationKeyDatabase )
668 
669 }