This project has retired. For details please refer to its Attic page.
UsersManagementStaxReader 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.users.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.users.jdo.JdoUser;
27  import org.apache.archiva.redback.users.jdo.UserDatabase;
28  
29  /**
30   * Class UsersManagementStaxReader.
31   * 
32   * @version $Revision$ $Date$
33   */
34  public class UsersManagementStaxReader
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 UserDatabase
49       */
50      public UserDatabase read( Reader reader, boolean strict )
51          throws IOException, XMLStreamException
52      {
53          XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( reader );
54  
55          return read( xmlStreamReader, strict );
56      } //-- UserDatabase read( Reader, boolean )
57  
58      /**
59       * Method read.
60       * 
61       * @param reader
62       * @throws IOException
63       * @throws XMLStreamException
64       * @return UserDatabase
65       */
66      public UserDatabase read( Reader reader )
67          throws IOException, XMLStreamException
68      {
69          return read( reader, true );
70      } //-- UserDatabase read( Reader )
71  
72      /**
73       * Method read.
74       * 
75       * @param stream
76       * @param strict
77       * @throws IOException
78       * @throws XMLStreamException
79       * @return UserDatabase
80       */
81      public UserDatabase read( InputStream stream, boolean strict )
82          throws IOException, XMLStreamException
83      {
84          XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( stream );
85  
86          return read( xmlStreamReader, strict );
87      } //-- UserDatabase read( InputStream, boolean )
88  
89      /**
90       * Method read.
91       * 
92       * @param stream
93       * @throws IOException
94       * @throws XMLStreamException
95       * @return UserDatabase
96       */
97      public UserDatabase read( InputStream stream )
98          throws IOException, XMLStreamException
99      {
100         return read( stream, true );
101     } //-- UserDatabase read( InputStream )
102 
103     /**
104      * Method read.
105      * 
106      * @param filePath
107      * @param strict
108      * @throws IOException
109      * @throws XMLStreamException
110      * @return UserDatabase
111      */
112     public UserDatabase 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     } //-- UserDatabase read( String, boolean )
120 
121     /**
122      * Method read.
123      * 
124      * @param filePath
125      * @throws IOException
126      * @throws XMLStreamException
127      * @return UserDatabase
128      */
129     public UserDatabase read( String filePath )
130         throws IOException, XMLStreamException
131     {
132         return read( filePath, true );
133     } //-- UserDatabase 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 parseJdoUser.
539      * 
540      * @param xmlStreamReader
541      * @param strict
542      * @throws IOException
543      * @throws XMLStreamException
544      * @return JdoUser
545      */
546     private JdoUser parseJdoUser( XMLStreamReader xmlStreamReader, boolean strict )
547         throws IOException, XMLStreamException
548     {
549         JdoUser jdoUser = new JdoUser();
550         java.util.Set parsed = new java.util.HashSet();
551         while ( ( strict ? xmlStreamReader.nextTag() : nextTag( xmlStreamReader ) ) == XMLStreamConstants.START_ELEMENT )
552         {
553             if ( checkFieldWithDuplicate( xmlStreamReader, "username", null, parsed ) )
554             {
555                 jdoUser.setUsername( getTrimmedValue( xmlStreamReader.getElementText() ) );
556             }
557             else if ( checkFieldWithDuplicate( xmlStreamReader, "password", null, parsed ) )
558             {
559                 jdoUser.setPassword( getTrimmedValue( xmlStreamReader.getElementText() ) );
560             }
561             else if ( checkFieldWithDuplicate( xmlStreamReader, "encodedPassword", null, parsed ) )
562             {
563                 jdoUser.setEncodedPassword( getTrimmedValue( xmlStreamReader.getElementText() ) );
564             }
565             else if ( checkFieldWithDuplicate( xmlStreamReader, "fullName", null, parsed ) )
566             {
567                 jdoUser.setFullName( getTrimmedValue( xmlStreamReader.getElementText() ) );
568             }
569             else if ( checkFieldWithDuplicate( xmlStreamReader, "email", null, parsed ) )
570             {
571                 jdoUser.setEmail( getTrimmedValue( xmlStreamReader.getElementText() ) );
572             }
573             else if ( checkFieldWithDuplicate( xmlStreamReader, "lastPasswordChange", null, parsed ) )
574             {
575                 String dateFormat = "long";
576                 jdoUser.setLastPasswordChange( getDateValue( getTrimmedValue( xmlStreamReader.getElementText() ), "lastPasswordChange", dateFormat, xmlStreamReader ) );
577             }
578             else if ( checkFieldWithDuplicate( xmlStreamReader, "lastLoginDate", null, parsed ) )
579             {
580                 String dateFormat = "long";
581                 jdoUser.setLastLoginDate( getDateValue( getTrimmedValue( xmlStreamReader.getElementText() ), "lastLoginDate", dateFormat, xmlStreamReader ) );
582             }
583             else if ( checkFieldWithDuplicate( xmlStreamReader, "countFailedLoginAttempts", null, parsed ) )
584             {
585                 jdoUser.setCountFailedLoginAttempts( getIntegerValue( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "0" ) ), "countFailedLoginAttempts", xmlStreamReader, strict ) );
586             }
587             else if ( checkFieldWithDuplicate( xmlStreamReader, "locked", null, parsed ) )
588             {
589                 jdoUser.setLocked( getBooleanValue( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "false" ) ), "locked", xmlStreamReader ) );
590             }
591             else if ( checkFieldWithDuplicate( xmlStreamReader, "permanent", null, parsed ) )
592             {
593                 jdoUser.setPermanent( getBooleanValue( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "false" ) ), "permanent", xmlStreamReader ) );
594             }
595             else if ( checkFieldWithDuplicate( xmlStreamReader, "validated", null, parsed ) )
596             {
597                 jdoUser.setValidated( getBooleanValue( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "false" ) ), "validated", xmlStreamReader ) );
598             }
599             else if ( checkFieldWithDuplicate( xmlStreamReader, "passwordChangeRequired", null, parsed ) )
600             {
601                 jdoUser.setPasswordChangeRequired( getBooleanValue( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "false" ) ), "passwordChangeRequired", xmlStreamReader ) );
602             }
603             else if ( checkFieldWithDuplicate( xmlStreamReader, "previousEncodedPasswords", null, parsed ) )
604             {
605                 java.util.List previousEncodedPasswords = new java.util.ArrayList/*<String>*/();
606                 jdoUser.setPreviousEncodedPasswords( previousEncodedPasswords );
607                 while ( xmlStreamReader.nextTag() == XMLStreamConstants.START_ELEMENT )
608                 {
609                     if ( "previousEncodedPassword".equals( xmlStreamReader.getLocalName() ) )
610                     {
611                         previousEncodedPasswords.add( getTrimmedValue( getDefaultValue( xmlStreamReader.getElementText(), "new java.util.ArrayList/*<String>*/()" ) ) );
612                     }
613                     else
614                     {
615                         throw new XMLStreamException( "Unrecognised tag: '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation() );
616                     }
617                 }
618             }
619             else if ( checkFieldWithDuplicate( xmlStreamReader, "accountCreationDate", null, parsed ) )
620             {
621                 String dateFormat = "long";
622                 jdoUser.setAccountCreationDate( getDateValue( getTrimmedValue( xmlStreamReader.getElementText() ), "accountCreationDate", dateFormat, xmlStreamReader ) );
623             }
624             else
625             {
626                 checkUnknownElement( xmlStreamReader, strict );
627             }
628         }
629         return jdoUser;
630     } //-- JdoUser parseJdoUser( XMLStreamReader, boolean )
631 
632     /**
633      * Method parseUserDatabase.
634      * 
635      * @param xmlStreamReader
636      * @param strict
637      * @throws IOException
638      * @throws XMLStreamException
639      * @return UserDatabase
640      */
641     private UserDatabase parseUserDatabase( XMLStreamReader xmlStreamReader, boolean strict )
642         throws IOException, XMLStreamException
643     {
644         UserDatabase userDatabase = new UserDatabase();
645         java.util.Set parsed = new java.util.HashSet();
646         while ( ( strict ? xmlStreamReader.nextTag() : nextTag( xmlStreamReader ) ) == XMLStreamConstants.START_ELEMENT )
647         {
648             if ( checkFieldWithDuplicate( xmlStreamReader, "users", null, parsed ) )
649             {
650                 java.util.List users = new java.util.ArrayList/*<JdoUser>*/();
651                 userDatabase.setUsers( users );
652                 while ( xmlStreamReader.nextTag() == XMLStreamConstants.START_ELEMENT )
653                 {
654                     if ( "user".equals( xmlStreamReader.getLocalName() ) )
655                     {
656                         userDatabase.addUser( parseJdoUser( xmlStreamReader, strict ) );
657                     }
658                     else
659                     {
660                         throw new XMLStreamException( "Unrecognised tag: '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation() );
661                     }
662                 }
663             }
664             else
665             {
666                 checkUnknownElement( xmlStreamReader, strict );
667             }
668         }
669         return userDatabase;
670     } //-- UserDatabase parseUserDatabase( XMLStreamReader, boolean )
671 
672     /**
673      * Method read.
674      * 
675      * @param xmlStreamReader
676      * @param strict
677      * @throws IOException
678      * @throws XMLStreamException
679      * @return UserDatabase
680      */
681     private UserDatabase read( XMLStreamReader xmlStreamReader, boolean strict )
682         throws IOException, XMLStreamException
683     {
684         int eventType = xmlStreamReader.getEventType();
685         String encoding = null;
686         while ( eventType != XMLStreamConstants.END_DOCUMENT )
687         {
688             if ( eventType == XMLStreamConstants.START_DOCUMENT )
689             {
690                 encoding = xmlStreamReader.getCharacterEncodingScheme();
691             }
692             if ( eventType == XMLStreamConstants.START_ELEMENT )
693             {
694                 if ( strict && ! "userDatabase".equals( xmlStreamReader.getLocalName() ) )
695                 {
696                     throw new XMLStreamException( "Expected root element 'userDatabase' but found '" + xmlStreamReader.getLocalName() + "'", xmlStreamReader.getLocation(), null );
697                 }
698                 UserDatabase userDatabase = parseUserDatabase( xmlStreamReader, strict );
699                 userDatabase.setModelEncoding( encoding );
700                 resolveReferences( userDatabase );
701                 return userDatabase;
702             }
703             eventType = xmlStreamReader.next();
704         }
705         throw new XMLStreamException( "Expected root element 'userDatabase' but found no element at all: invalid XML document", xmlStreamReader.getLocation(), null );
706     } //-- UserDatabase read( XMLStreamReader, boolean )
707 
708     /**
709      * Method resolveReferences.
710      * 
711      * @param value
712      */
713     private void resolveReferences( UserDatabase value )
714     {
715         java.util.Map refs;
716     } //-- void resolveReferences( UserDatabase )
717 
718 }