001/*
002 Licensed to the Apache Software Foundation (ASF) under one
003 or more contributor license agreements.  See the NOTICE file
004 distributed with this work for additional information
005 regarding copyright ownership.  The ASF licenses this file
006 to you under the Apache License, Version 2.0 (the
007 "License"); you may not use this file except in compliance
008 with the License.  You may obtain a copy of the License at
009 
010     http://www.apache.org/licenses/LICENSE-2.0
011 
012 Unless required by applicable law or agreed to in writing,
013 software distributed under the License is distributed on an
014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 KIND, either express or implied.  See the License for the
016 specific language governing permissions and limitations
017 under the License.
018 =================== DO NOT EDIT THIS FILE ====================
019 Generated by Modello 2.5.1,
020 any modifications will be overwritten.
021 ==============================================================
022 */
023
024package org.apache.maven.scm.provider.local.metadata.io.xpp3;
025
026  //---------------------------------/
027 //- Imported classes and packages -/
028//---------------------------------/
029
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.Reader;
033import java.text.DateFormat;
034import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
035import org.codehaus.plexus.util.xml.XmlStreamReader;
036import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
037import org.codehaus.plexus.util.xml.pull.MXParser;
038import org.codehaus.plexus.util.xml.pull.XmlPullParser;
039import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
040
041/**
042 * Class LocalScmMetadataXpp3Reader.
043 * 
044 * @version $Revision$ $Date$
045 */
046@SuppressWarnings( "all" )
047public class LocalScmMetadataXpp3Reader
048{
049
050      //--------------------------/
051     //- Class/Member Variables -/
052    //--------------------------/
053
054    /**
055     * If set the parser will be loaded with all single characters
056     * from the XHTML specification.
057     * The entities used:
058     * <ul>
059     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>
060     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>
061     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>
062     * </ul>
063     */
064    private boolean addDefaultEntities = true;
065
066    /**
067     * Field contentTransformer.
068     */
069    public final ContentTransformer contentTransformer;
070
071
072      //----------------/
073     //- Constructors -/
074    //----------------/
075
076    public LocalScmMetadataXpp3Reader()
077    {
078        this( new ContentTransformer()
079        {
080            public String transform( String source, String fieldName )
081            {
082                return source;
083            }
084        } );
085    } //-- org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader()
086
087    public LocalScmMetadataXpp3Reader(ContentTransformer contentTransformer)
088    {
089        this.contentTransformer = contentTransformer;
090    } //-- org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader(ContentTransformer)
091
092
093      //-----------/
094     //- Methods -/
095    //-----------/
096
097    /**
098     * Method checkFieldWithDuplicate.
099     * 
100     * @param parser a parser object.
101     * @param parsed a parsed object.
102     * @param alias a alias object.
103     * @param tagName a tagName object.
104     * @throws XmlPullParserException XmlPullParserException if
105     * any.
106     * @return boolean
107     */
108    private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set<String> parsed )
109        throws XmlPullParserException
110    {
111        if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
112        {
113            return false;
114        }
115        if ( !parsed.add( tagName ) )
116        {
117            throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
118        }
119        return true;
120    } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set )
121
122    /**
123     * Method checkUnknownAttribute.
124     * 
125     * @param parser a parser object.
126     * @param strict a strict object.
127     * @param tagName a tagName object.
128     * @param attribute a attribute object.
129     * @throws XmlPullParserException XmlPullParserException if
130     * any.
131     * @throws IOException IOException if any.
132     */
133    private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
134        throws XmlPullParserException, IOException
135    {
136        // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
137        if ( strict )
138        {
139            throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
140        }
141    } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
142
143    /**
144     * Method checkUnknownElement.
145     * 
146     * @param parser a parser object.
147     * @param strict a strict object.
148     * @throws XmlPullParserException XmlPullParserException if
149     * any.
150     * @throws IOException IOException if any.
151     */
152    private void checkUnknownElement( XmlPullParser parser, boolean strict )
153        throws XmlPullParserException, IOException
154    {
155        if ( strict )
156        {
157            throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
158        }
159
160        for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; )
161        {
162            int eventType = parser.next();
163            if ( eventType == XmlPullParser.START_TAG )
164            {
165                unrecognizedTagCount++;
166            }
167            else if ( eventType == XmlPullParser.END_TAG )
168            {
169                unrecognizedTagCount--;
170            }
171        }
172    } //-- void checkUnknownElement( XmlPullParser, boolean )
173
174    /**
175     * Returns the state of the "add default entities" flag.
176     * 
177     * @return boolean
178     */
179    public boolean getAddDefaultEntities()
180    {
181        return addDefaultEntities;
182    } //-- boolean getAddDefaultEntities()
183
184    /**
185     * Method getBooleanValue.
186     * 
187     * @param s a s object.
188     * @param parser a parser object.
189     * @param attribute a attribute object.
190     * @throws XmlPullParserException XmlPullParserException if
191     * any.
192     * @return boolean
193     */
194    private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
195        throws XmlPullParserException
196    {
197        return getBooleanValue( s, attribute, parser, null );
198    } //-- boolean getBooleanValue( String, String, XmlPullParser )
199
200    /**
201     * Method getBooleanValue.
202     * 
203     * @param s a s object.
204     * @param defaultValue a defaultValue object.
205     * @param parser a parser object.
206     * @param attribute a attribute object.
207     * @throws XmlPullParserException XmlPullParserException if
208     * any.
209     * @return boolean
210     */
211    private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue )
212        throws XmlPullParserException
213    {
214        if ( s != null && s.length() != 0 )
215        {
216            return Boolean.valueOf( s ).booleanValue();
217        }
218        if ( defaultValue != null )
219        {
220            return Boolean.valueOf( defaultValue ).booleanValue();
221        }
222        return false;
223    } //-- boolean getBooleanValue( String, String, XmlPullParser, String )
224
225    /**
226     * Method getByteValue.
227     * 
228     * @param s a s object.
229     * @param strict a strict object.
230     * @param parser a parser object.
231     * @param attribute a attribute object.
232     * @throws XmlPullParserException XmlPullParserException if
233     * any.
234     * @return byte
235     */
236    private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict )
237        throws XmlPullParserException
238    {
239        if ( s != null )
240        {
241            try
242            {
243                return Byte.valueOf( s ).byteValue();
244            }
245            catch ( NumberFormatException nfe )
246            {
247                if ( strict )
248                {
249                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
250                }
251            }
252        }
253        return 0;
254    } //-- byte getByteValue( String, String, XmlPullParser, boolean )
255
256    /**
257     * Method getCharacterValue.
258     * 
259     * @param s a s object.
260     * @param parser a parser object.
261     * @param attribute a attribute object.
262     * @throws XmlPullParserException XmlPullParserException if
263     * any.
264     * @return char
265     */
266    private char getCharacterValue( String s, String attribute, XmlPullParser parser )
267        throws XmlPullParserException
268    {
269        if ( s != null )
270        {
271            return s.charAt( 0 );
272        }
273        return 0;
274    } //-- char getCharacterValue( String, String, XmlPullParser )
275
276    /**
277     * Method getDateValue.
278     * 
279     * @param s a s object.
280     * @param parser a parser object.
281     * @param attribute a attribute object.
282     * @throws XmlPullParserException XmlPullParserException if
283     * any.
284     * @return Date
285     */
286    private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser )
287        throws XmlPullParserException
288    {
289        return getDateValue( s, attribute, null, parser );
290    } //-- java.util.Date getDateValue( String, String, XmlPullParser )
291
292    /**
293     * Method getDateValue.
294     * 
295     * @param s a s object.
296     * @param parser a parser object.
297     * @param dateFormat a dateFormat object.
298     * @param attribute a attribute object.
299     * @throws XmlPullParserException XmlPullParserException if
300     * any.
301     * @return Date
302     */
303    private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser )
304        throws XmlPullParserException
305    {
306        if ( s != null )
307        {
308            String effectiveDateFormat = dateFormat;
309            if ( dateFormat == null )
310            {
311                effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
312            }
313            if ( "long".equals( effectiveDateFormat ) )
314            {
315                try
316                {
317                    return new java.util.Date( Long.parseLong( s ) );
318                }
319                catch ( NumberFormatException e )
320                {
321                    throw new XmlPullParserException( e.getMessage(), parser, e );
322                }
323            }
324            else
325            {
326                try
327                {
328                    DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
329                    return dateParser.parse( s );
330                }
331                catch ( java.text.ParseException e )
332                {
333                    throw new XmlPullParserException( e.getMessage(), parser, e );
334                }
335            }
336        }
337        return null;
338    } //-- java.util.Date getDateValue( String, String, String, XmlPullParser )
339
340    /**
341     * Method getDoubleValue.
342     * 
343     * @param s a s object.
344     * @param strict a strict object.
345     * @param parser a parser object.
346     * @param attribute a attribute object.
347     * @throws XmlPullParserException XmlPullParserException if
348     * any.
349     * @return double
350     */
351    private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict )
352        throws XmlPullParserException
353    {
354        if ( s != null )
355        {
356            try
357            {
358                return Double.valueOf( s ).doubleValue();
359            }
360            catch ( NumberFormatException nfe )
361            {
362                if ( strict )
363                {
364                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
365                }
366            }
367        }
368        return 0;
369    } //-- double getDoubleValue( String, String, XmlPullParser, boolean )
370
371    /**
372     * Method getFloatValue.
373     * 
374     * @param s a s object.
375     * @param strict a strict object.
376     * @param parser a parser object.
377     * @param attribute a attribute object.
378     * @throws XmlPullParserException XmlPullParserException if
379     * any.
380     * @return float
381     */
382    private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict )
383        throws XmlPullParserException
384    {
385        if ( s != null )
386        {
387            try
388            {
389                return Float.valueOf( s ).floatValue();
390            }
391            catch ( NumberFormatException nfe )
392            {
393                if ( strict )
394                {
395                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
396                }
397            }
398        }
399        return 0;
400    } //-- float getFloatValue( String, String, XmlPullParser, boolean )
401
402    /**
403     * Method getIntegerValue.
404     * 
405     * @param s a s object.
406     * @param strict a strict object.
407     * @param parser a parser object.
408     * @param attribute a attribute object.
409     * @throws XmlPullParserException XmlPullParserException if
410     * any.
411     * @return int
412     */
413    private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict )
414        throws XmlPullParserException
415    {
416        if ( s != null )
417        {
418            try
419            {
420                return Integer.valueOf( s ).intValue();
421            }
422            catch ( NumberFormatException nfe )
423            {
424                if ( strict )
425                {
426                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
427                }
428            }
429        }
430        return 0;
431    } //-- int getIntegerValue( String, String, XmlPullParser, boolean )
432
433    /**
434     * Method getLongValue.
435     * 
436     * @param s a s object.
437     * @param strict a strict object.
438     * @param parser a parser object.
439     * @param attribute a attribute object.
440     * @throws XmlPullParserException XmlPullParserException if
441     * any.
442     * @return long
443     */
444    private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict )
445        throws XmlPullParserException
446    {
447        if ( s != null )
448        {
449            try
450            {
451                return Long.valueOf( s ).longValue();
452            }
453            catch ( NumberFormatException nfe )
454            {
455                if ( strict )
456                {
457                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
458                }
459            }
460        }
461        return 0;
462    } //-- long getLongValue( String, String, XmlPullParser, boolean )
463
464    /**
465     * Method getRequiredAttributeValue.
466     * 
467     * @param s a s object.
468     * @param strict a strict object.
469     * @param parser a parser object.
470     * @param attribute a attribute object.
471     * @throws XmlPullParserException XmlPullParserException if
472     * any.
473     * @return String
474     */
475    private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict )
476        throws XmlPullParserException
477    {
478        if ( s == null )
479        {
480            if ( strict )
481            {
482                throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
483            }
484        }
485        return s;
486    } //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean )
487
488    /**
489     * Method getShortValue.
490     * 
491     * @param s a s object.
492     * @param strict a strict object.
493     * @param parser a parser object.
494     * @param attribute a attribute object.
495     * @throws XmlPullParserException XmlPullParserException if
496     * any.
497     * @return short
498     */
499    private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict )
500        throws XmlPullParserException
501    {
502        if ( s != null )
503        {
504            try
505            {
506                return Short.valueOf( s ).shortValue();
507            }
508            catch ( NumberFormatException nfe )
509            {
510                if ( strict )
511                {
512                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
513                }
514            }
515        }
516        return 0;
517    } //-- short getShortValue( String, String, XmlPullParser, boolean )
518
519    /**
520     * Method getTrimmedValue.
521     * 
522     * @param s a s object.
523     * @return String
524     */
525    private String getTrimmedValue( String s )
526    {
527        if ( s != null )
528        {
529            s = s.trim();
530        }
531        return s;
532    } //-- String getTrimmedValue( String )
533
534    /**
535     * Method interpolatedTrimmed.
536     * 
537     * @param value a value object.
538     * @param context a context object.
539     * @return String
540     */
541    private String interpolatedTrimmed( String value, String context )
542    {
543        return getTrimmedValue( contentTransformer.transform( value, context ) );
544    } //-- String interpolatedTrimmed( String, String )
545
546    /**
547     * Method nextTag.
548     * 
549     * @param parser a parser object.
550     * @throws IOException IOException if any.
551     * @throws XmlPullParserException XmlPullParserException if
552     * any.
553     * @return int
554     */
555    private int nextTag( XmlPullParser parser )
556        throws IOException, XmlPullParserException
557    {
558        int eventType = parser.next();
559        if ( eventType == XmlPullParser.TEXT )
560        {
561            eventType = parser.next();
562        }
563        if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG )
564        {
565            throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
566        }
567        return eventType;
568    } //-- int nextTag( XmlPullParser )
569
570    /**
571     * Method read.
572     * 
573     * @param parser a parser object.
574     * @param strict a strict object.
575     * @throws IOException IOException if any.
576     * @throws XmlPullParserException XmlPullParserException if
577     * any.
578     * @return LocalScmMetadata
579     */
580    public LocalScmMetadata read( XmlPullParser parser, boolean strict )
581        throws IOException, XmlPullParserException
582    {
583        LocalScmMetadata localScmMetadata = null;
584        int eventType = parser.getEventType();
585        boolean parsed = false;
586        while ( eventType != XmlPullParser.END_DOCUMENT )
587        {
588            if ( eventType == XmlPullParser.START_TAG )
589            {
590                if ( strict && ! "localScmMetadata".equals( parser.getName() ) )
591                {
592                    throw new XmlPullParserException( "Expected root element 'localScmMetadata' but found '" + parser.getName() + "'", parser, null );
593                }
594                else if ( parsed )
595                {
596                    // fallback, already expected a XmlPullParserException due to invalid XML
597                    throw new XmlPullParserException( "Duplicated tag: 'localScmMetadata'", parser, null );
598                }
599                localScmMetadata = parseLocalScmMetadata( parser, strict );
600                localScmMetadata.setModelEncoding( parser.getInputEncoding() );
601                parsed = true;
602            }
603            eventType = parser.next();
604        }
605        if ( parsed )
606        {
607            return localScmMetadata;
608        }
609        throw new XmlPullParserException( "Expected root element 'localScmMetadata' but found no element at all: invalid XML document", parser, null );
610    } //-- LocalScmMetadata read( XmlPullParser, boolean )
611
612    /**
613     * @see XmlStreamReader
614     * 
615     * @param reader a reader object.
616     * @param strict a strict object.
617     * @throws IOException IOException if any.
618     * @throws XmlPullParserException XmlPullParserException if
619     * any.
620     * @return LocalScmMetadata
621     */
622    public LocalScmMetadata read( Reader reader, boolean strict )
623        throws IOException, XmlPullParserException
624    {
625        XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
626
627        parser.setInput( reader );
628
629
630        return read( parser, strict );
631    } //-- LocalScmMetadata read( Reader, boolean )
632
633    /**
634     * @see XmlStreamReader
635     * 
636     * @param reader a reader object.
637     * @throws IOException IOException if any.
638     * @throws XmlPullParserException XmlPullParserException if
639     * any.
640     * @return LocalScmMetadata
641     */
642    public LocalScmMetadata read( Reader reader )
643        throws IOException, XmlPullParserException
644    {
645        return read( reader, true );
646    } //-- LocalScmMetadata read( Reader )
647
648    /**
649     * Method read.
650     * 
651     * @param in a in object.
652     * @param strict a strict object.
653     * @throws IOException IOException if any.
654     * @throws XmlPullParserException XmlPullParserException if
655     * any.
656     * @return LocalScmMetadata
657     */
658    public LocalScmMetadata read( InputStream in, boolean strict )
659        throws IOException, XmlPullParserException
660    {
661        return read( new XmlStreamReader( in ), strict );
662    } //-- LocalScmMetadata read( InputStream, boolean )
663
664    /**
665     * Method read.
666     * 
667     * @param in a in object.
668     * @throws IOException IOException if any.
669     * @throws XmlPullParserException XmlPullParserException if
670     * any.
671     * @return LocalScmMetadata
672     */
673    public LocalScmMetadata read( InputStream in )
674        throws IOException, XmlPullParserException
675    {
676        return read( new XmlStreamReader( in ) );
677    } //-- LocalScmMetadata read( InputStream )
678
679    /**
680     * Method parseLocalScmMetadata.
681     * 
682     * @param parser a parser object.
683     * @param strict a strict object.
684     * @throws IOException IOException if any.
685     * @throws XmlPullParserException XmlPullParserException if
686     * any.
687     * @return LocalScmMetadata
688     */
689    private LocalScmMetadata parseLocalScmMetadata( XmlPullParser parser, boolean strict )
690        throws IOException, XmlPullParserException
691    {
692        String tagName = parser.getName();
693        LocalScmMetadata localScmMetadata = new LocalScmMetadata();
694        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
695        {
696            String name = parser.getAttributeName( i );
697            String value = parser.getAttributeValue( i );
698
699            if ( name.indexOf( ':' ) >= 0 )
700            {
701                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
702            }
703            else if ( "xmlns".equals( name ) )
704            {
705                // ignore xmlns attribute in root class, which is a reserved attribute name
706            }
707            else
708            {
709                checkUnknownAttribute( parser, name, tagName, strict );
710            }
711        }
712        java.util.Set<String> parsed = new java.util.HashSet<String>();
713        while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
714        {
715            if ( checkFieldWithDuplicate( parser, "repositoryFileNames", null, parsed ) )
716            {
717                java.util.List<String> repositoryFileNames = new java.util.ArrayList<String>();
718                while ( parser.nextTag() == XmlPullParser.START_TAG )
719                {
720                    if ( "repositoryFileName".equals( parser.getName() ) )
721                    {
722                        repositoryFileNames.add( interpolatedTrimmed( parser.nextText(), "repositoryFileNames" ) );
723                    }
724                    else
725                    {
726                        checkUnknownElement( parser, strict );
727                    }
728                }
729                localScmMetadata.setRepositoryFileNames( repositoryFileNames );
730            }
731            else
732            {
733                checkUnknownElement( parser, strict );
734            }
735        }
736        return localScmMetadata;
737    } //-- LocalScmMetadata parseLocalScmMetadata( XmlPullParser, boolean )
738
739    /**
740     * Sets the state of the "add default entities" flag.
741     * 
742     * @param addDefaultEntities a addDefaultEntities object.
743     */
744    public void setAddDefaultEntities( boolean addDefaultEntities )
745    {
746        this.addDefaultEntities = addDefaultEntities;
747    } //-- void setAddDefaultEntities( boolean )
748
749    public static interface ContentTransformer
750{
751    /**
752     * Interpolate the value read from the xpp3 document
753     * @param source The source value
754     * @param fieldName A description of the field being interpolated. The implementation may use this to
755     *                           log stuff.
756     * @return The interpolated value.
757     */
758    String transform( String source, String fieldName );
759}
760
761}