001package org.apache.maven.project;
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.io.BufferedInputStream;
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.util.ArrayList;
028import java.util.List;
029import java.util.jar.JarFile;
030import java.util.zip.ZipEntry;
031
032import org.codehaus.plexus.util.IOUtil;
033import org.codehaus.plexus.util.ReaderFactory;
034import org.codehaus.plexus.util.xml.Xpp3Dom;
035import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
036import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
037
038/**
039 * Creates an extension descriptor from some XML stream.
040 *
041 * @author Benjamin Bentmann
042 */
043public class ExtensionDescriptorBuilder
044{
045
046    /**
047     * @since 3.3.0 
048     */
049    public String getExtensionDescriptorLocation()
050    {
051        return "META-INF/maven/extension.xml";
052    }
053
054    /**
055     * Extracts the extension descriptor (if any) from the specified JAR file.
056     *
057     * @param extensionJar The JAR file or directory to extract the descriptor from, must not be {@code null}.
058     * @return The extracted descriptor or {@code null} if no descriptor was found.
059     * @throws IOException If the descriptor is present but could not be parsed.
060     */
061    public ExtensionDescriptor build( File extensionJar )
062        throws IOException
063    {
064        ExtensionDescriptor extensionDescriptor = null;
065
066        if ( extensionJar.isFile() )
067        {
068            JarFile pluginJar = new JarFile( extensionJar, false );
069            try
070            {
071                ZipEntry pluginDescriptorEntry = pluginJar.getEntry( getExtensionDescriptorLocation() );
072
073                if ( pluginDescriptorEntry != null )
074                {
075                    InputStream is = pluginJar.getInputStream( pluginDescriptorEntry );
076
077                    try
078                    {
079                        extensionDescriptor = build( is );
080                    }
081                    finally
082                    {
083                        IOUtil.close( is );
084                    }
085                }
086            }
087            finally
088            {
089                pluginJar.close();
090            }
091        }
092        else
093        {
094            File pluginXml = new File( extensionJar, getExtensionDescriptorLocation() );
095
096            if ( pluginXml.canRead() )
097            {
098                InputStream is = new BufferedInputStream( new FileInputStream( pluginXml ) );
099                try
100                {
101                    extensionDescriptor = build( is );
102                }
103                finally
104                {
105                    IOUtil.close( is );
106                }
107            }
108        }
109
110        return extensionDescriptor;
111    }
112
113    /**
114     * @since 3.3.0
115     */
116    public ExtensionDescriptor build( InputStream is )
117        throws IOException
118    {
119        ExtensionDescriptor extensionDescriptor = new ExtensionDescriptor();
120
121        Xpp3Dom dom;
122        try
123        {
124            dom = Xpp3DomBuilder.build( ReaderFactory.newXmlReader( is ) );
125        }
126        catch ( XmlPullParserException e )
127        {
128            throw (IOException) new IOException( e.getMessage() ).initCause( e );
129        }
130
131        if ( !"extension".equals( dom.getName() ) )
132        {
133            throw new IOException( "Unexpected root element \"" + dom.getName() + "\", expected \"extension\"" );
134        }
135
136        extensionDescriptor.setExportedPackages( parseStrings( dom.getChild( "exportedPackages" ) ) );
137
138        extensionDescriptor.setExportedArtifacts( parseStrings( dom.getChild( "exportedArtifacts" ) ) );
139
140        return extensionDescriptor;
141    }
142
143    private List<String> parseStrings( Xpp3Dom dom )
144    {
145        List<String> strings = null;
146
147        if ( dom != null )
148        {
149            strings = new ArrayList<String>();
150
151            for ( Xpp3Dom child : dom.getChildren() )
152            {
153                String string = child.getValue();
154                if ( string != null )
155                {
156                    string = string.trim();
157                    if ( string.length() > 0 )
158                    {
159                        strings.add( string );
160                    }
161                }
162            }
163        }
164
165        return strings;
166    }
167
168}