1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.project;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.jar.JarFile;
29  import java.util.zip.ZipEntry;
30  
31  import org.codehaus.plexus.util.ReaderFactory;
32  import org.codehaus.plexus.util.xml.Xpp3Dom;
33  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  
37  
38  
39  
40  
41  public class ExtensionDescriptorBuilder {
42  
43      
44  
45  
46      public String getExtensionDescriptorLocation() {
47          return "META-INF/maven/extension.xml";
48      }
49  
50      
51  
52  
53  
54  
55  
56  
57      public ExtensionDescriptor build(File extensionJar) throws IOException {
58          ExtensionDescriptor extensionDescriptor = null;
59  
60          if (extensionJar.isFile()) {
61              try (JarFile pluginJar = new JarFile(extensionJar, false)) {
62                  ZipEntry pluginDescriptorEntry = pluginJar.getEntry(getExtensionDescriptorLocation());
63  
64                  if (pluginDescriptorEntry != null) {
65                      try (InputStream is = pluginJar.getInputStream(pluginDescriptorEntry)) {
66                          extensionDescriptor = build(is);
67                      }
68                  }
69              }
70          } else {
71              File pluginXml = new File(extensionJar, getExtensionDescriptorLocation());
72  
73              if (pluginXml.canRead()) {
74                  try (InputStream is = new BufferedInputStream(new FileInputStream(pluginXml))) {
75                      extensionDescriptor = build(is);
76                  }
77              }
78          }
79  
80          return extensionDescriptor;
81      }
82  
83      
84  
85  
86      public ExtensionDescriptor build(InputStream is) throws IOException {
87          ExtensionDescriptor extensionDescriptor = new ExtensionDescriptor();
88  
89          Xpp3Dom dom;
90          try {
91              dom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(is));
92          } catch (XmlPullParserException e) {
93              throw (IOException) new IOException(e.getMessage()).initCause(e);
94          }
95  
96          if (!"extension".equals(dom.getName())) {
97              throw new IOException("Unexpected root element \"" + dom.getName() + "\", expected \"extension\"");
98          }
99  
100         extensionDescriptor.setExportedPackages(parseStrings(dom.getChild("exportedPackages")));
101 
102         extensionDescriptor.setExportedArtifacts(parseStrings(dom.getChild("exportedArtifacts")));
103 
104         return extensionDescriptor;
105     }
106 
107     private List<String> parseStrings(Xpp3Dom dom) {
108         List<String> strings = null;
109 
110         if (dom != null) {
111             strings = new ArrayList<>();
112 
113             for (Xpp3Dom child : dom.getChildren()) {
114                 String string = child.getValue();
115                 if (string != null) {
116                     string = string.trim();
117                     if (string.length() > 0) {
118                         strings.add(string);
119                     }
120                 }
121             }
122         }
123 
124         return strings;
125     }
126 }