/*-
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Licensed 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.
 */
package org.fedoraproject.xmvn.tools.install.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.xml.stream.XMLStreamException;

import org.fedoraproject.xmvn.metadata.PackageMetadata;
import org.fedoraproject.xmvn.metadata.io.stax.MetadataStaxWriter;

/**
 * Class describing a Java package as a package which besides other files files also installs Java metadata as an
 * additional file.
 * 
 * @author Mikolaj Izdebski
 */
class JavaPackage
    extends Package
{
    /**
     * Metadata associated with this package.
     */
    private final PackageMetadata metadata = new PackageMetadata();

    private final Path metadataPath;

    /**
     * Create an empty Java package with given ID.
     * 
     * @param id package ID
     * @param settings installer settings to use
     * @throws IOException
     */
    public JavaPackage( String id, Path metadataPath )
        throws IOException
    {
        super( id );
        this.metadataPath = metadataPath;

        Path sourcePath = Files.createTempFile( "xmvn-metadata", "xml" );
        File metadataFile = new RegularFile( metadataPath, sourcePath );
        addFile( metadataFile );
    }

    /**
     * Get metadata associated with this package.
     * 
     * @return package metadata object
     */
    public PackageMetadata getMetadata()
    {
        return metadata;
    }

    @Override
    public void install( Path installRoot )
        throws IOException
    {
        writeMetadataFile();

        super.install( installRoot );
    }

    private void writeMetadataFile()
        throws IOException
    {
        try (OutputStream stream = Files.newOutputStream( metadataPath ))
        {
            new MetadataStaxWriter().write( stream, metadata );
        }
        catch ( XMLStreamException e )
        {
            throw new IOException( "Failed to write package metadata", e );
        }
    }
}

@@ -1293,32 +1293,30 @@
  final Path 
-metadata
+source
 Path;%0A%0A    /
@@ -1423,42 +1423,88 @@
 ram 
-settings installer settings to use
+metadataPath installation path for metadata relative to%0A     * installation root
 %0A   
@@ -1640,24 +1640,25 @@
 uper( id );%0A
+%0A
         this
@@ -1662,51 +1662,8 @@
 his.
-metadataPath = metadataPath;%0A%0A        Path 
 sour
@@ -2273,16 +2273,17 @@
    try (
+ 
 OutputSt
@@ -2319,30 +2319,29 @@
 Stream( 
-metadata
+source
 Path )
+ 
 )%0A      

Fix metadata file writing in JavaPackage