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