001package 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
022import java.util.Date;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.maven.scm.log.ScmLogger;
027import org.apache.maven.scm.provider.accurev.Transaction;
028
029/**
030 * 
031 */
032public class HistoryConsumer
033    extends XppStreamConsumer
034{
035
036    private List<Transaction> transactions;
037
038    private Transaction currentTran;
039
040    private Long elementId;
041
042    private String elementName;
043
044    public HistoryConsumer( ScmLogger logger, List<Transaction> transactions )
045    {
046        super( logger );
047        this.transactions = transactions;
048    }
049
050    @Override
051    protected void startTag( List<String> tagPath, Map<String, String> attributes )
052    {
053        String tagName = getTagName( tagPath );
054        if ( "transaction".equals( tagName ) )
055        {
056            Long id = Long.parseLong( attributes.get( "id" ) );
057            Date when = new Date( Long.parseLong( attributes.get( "time" ) ) * 1000 );
058            String tranType = attributes.get( "type" );
059            String user = attributes.get( "user" );
060            currentTran = new Transaction( id, when, tranType, user );
061            transactions.add( currentTran );
062
063        }
064        else if ( "version".equals( tagName ) )
065        {
066            if ( currentTran != null )
067            {
068
069                if ( attributes.containsKey( "eid" ) )
070                {
071                    elementId = Long.parseLong( attributes.get( "eid" ) );
072                    elementName = attributes.get( "path" );
073                }
074
075                String virtualSpec = attributes.get( "virtual" );
076                String realSpec = attributes.get( "real" );
077                String ancestor = attributes.get( "ancestor" );
078
079                currentTran.addVersion( elementId, elementName, virtualSpec, realSpec, ancestor );
080            }
081        }
082        else if ( "element".equals( tagName ) )
083        {
084            elementId = Long.parseLong( attributes.get( "eid" ) );
085            elementName = attributes.get( "name" );
086        }
087    }
088
089    @Override
090    protected void endTag( List<String> tagPath )
091    {
092        String tagName = getTagName( tagPath );
093        if ( "element".equals( tagName ) )
094        {
095            elementId = null;
096            elementName = null;
097        }
098        else if ( "transaction".equals( tagName ) )
099        {
100            currentTran = null;
101        }
102
103    }
104
105    @Override
106    protected void text( List<String> tagPath, String text )
107    {
108        String tagName = getTagName( tagPath );
109        if ( currentTran != null && "comment".equals( tagName ) )
110        {
111            currentTran.setComment( text );
112        }
113
114    }
115
116}