View Javadoc

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