package org.apache.maven.plugin.changes;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.changes.model.Body;
import org.apache.maven.plugins.changes.model.ChangesDocument;
import org.apache.maven.plugins.changes.model.Properties;
import org.apache.maven.plugins.changes.model.io.xpp3.ChangesXpp3Reader;

/**
 * A facade for a changes.xml file.
 *
 * @version $Id$
 */
public class ChangesXML
{

    private List releaseList;

    private String author;

    private String title;

    private String authorEmail;

    private ChangesDocument changesDocument;

    public ChangesXML( File xmlPath, Log log )
    {

        if ( xmlPath == null || !xmlPath.exists() )
        {
            log.error( "changes xml file is null or not exists " );
            return;
        }

        try
        {

            ChangesXpp3Reader reader = new ChangesXpp3Reader();

            changesDocument = reader.read( new FileInputStream( xmlPath ), false );

            if ( changesDocument == null )
            {
                log.error( "Cannot build Changes Report from file: " + xmlPath.getPath() );
                return;
            }

            Properties properties = changesDocument.getProperties();

            if ( properties != null )
            {
                if ( properties.getAuthor() != null )
                {
                    this.author = properties.getAuthor().getName();
                    this.authorEmail = properties.getAuthor().getName();
                }
                this.title = properties.getTitle();
            }


            Body body = changesDocument.getBody();


            if ( body != null )
            {
                this.releaseList = body.getReleases();
            }

        }
        catch ( Throwable e )
        {
            // FIXME throw an Exception ?
            log.error( "An error occurred when parsing the changes.xml file: ", e );
        }
    }

    public void setAuthor( String author )
    {
        this.author = author;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setReleaseList( List releaseList )
    {
        this.releaseList = releaseList;
    }

    public List getReleaseList()
    {
        return releaseList == null ? Collections.EMPTY_LIST : releaseList;
    }

    public void setTitle( String title )
    {
        this.title = title;
    }

    public String getTitle()
    {
        return title;
    }

    public ChangesDocument getChangesDocument()
    {
        return changesDocument;
    }

    public String getAuthorEmail()
    {
        return authorEmail;
    }

    public void setAuthorEmail( String authorEmail )
    {
        this.authorEmail = authorEmail;
    }

}

@@ -1240,16 +1240,56 @@
 3Reader;
+%0Aimport org.codehaus.plexus.util.IOUtil;
 %0A%0A/**%0A *
@@ -1745,32 +1745,81 @@
 urn;%0A        %7D%0A%0A
+        FileInputStream fileInputStream = null;%0A%0A
         try%0A    
@@ -1814,16 +1814,16 @@
     try%0A
-
         
@@ -1886,24 +1886,86 @@
 3Reader();%0A%0A
+            fileInputStream = new FileInputStream( xmlPath );%0A
             
@@ -1995,21 +1995,17 @@
 r.read( 
-new F
+f
 ileInput
@@ -2010,27 +2010,16 @@
 utStream
-( xmlPath )
 , false 
@@ -2983,24 +2983,105 @@
 ;%0A        %7D%0A
+        finally%0A        %7B%0A            IOUtil.close( fileInputStream );%0A        %7D%0A
     %7D%0A%0A    p

Fix problem reported by FindBugs: may fail to clean up java.io.InputStream