001    package org.apache.maven.scm.provider.accurev.cli;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *    http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Date;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.apache.maven.scm.log.ScmLogger;
027    import org.apache.maven.scm.provider.accurev.Transaction;
028    
029    public class HistoryConsumer
030        extends XppStreamConsumer
031    {
032    
033        private List<Transaction> transactions;
034    
035        private Transaction currentTran;
036    
037        private Long elementId;
038    
039        private String elementName;
040    
041        public HistoryConsumer( ScmLogger logger, List<Transaction> transactions )
042        {
043            super( logger );
044            this.transactions = transactions;
045        }
046    
047        @Override
048        protected void startTag( List<String> tagPath, Map<String, String> attributes )
049        {
050            String tagName = getTagName( tagPath );
051            if ( "transaction".equals( tagName ) )
052            {
053                Long id = Long.parseLong( attributes.get( "id" ) );
054                Date when = new Date( Long.parseLong( attributes.get( "time" ) ) * 1000 );
055                String tranType = attributes.get( "type" );
056                String user = attributes.get( "user" );
057                currentTran = new Transaction( id, when, tranType, user );
058                transactions.add( currentTran );
059    
060            }
061            else if ( "version".equals( tagName ) )
062            {
063                if ( currentTran != null )
064                {
065    
066                    if ( attributes.containsKey( "eid" ) )
067                    {
068                        elementId = Long.parseLong( attributes.get( "eid" ) );
069                        elementName = attributes.get( "path" );
070                    }
071    
072                    String virtualSpec = attributes.get( "virtual" );
073                    String realSpec = attributes.get( "real" );
074                    String ancestor = attributes.get( "ancestor" );
075    
076                    currentTran.addVersion( elementId, elementName, virtualSpec, realSpec, ancestor );
077                }
078            }
079            else if ( "element".equals( tagName ) )
080            {
081                elementId = Long.parseLong( attributes.get( "eid" ) );
082                elementName = attributes.get( "name" );
083            }
084        }
085    
086        @Override
087        protected void endTag( List<String> tagPath )
088        {
089            String tagName = getTagName( tagPath );
090            if ( "element".equals( tagName ) )
091            {
092                elementId = null;
093                elementName = null;
094            }
095            else if ( "transaction".equals( tagName ) )
096            {
097                currentTran = null;
098            }
099    
100        }
101    
102        @Override
103        protected void text( List<String> tagPath, String text )
104        {
105            String tagName = getTagName( tagPath );
106            if ( currentTran != null && "comment".equals( tagName ) )
107            {
108                currentTran.setComment( text );
109            }
110    
111        }
112    
113    }