1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.PropertiesUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portal.util.InitUtil;
33  
34  import java.io.File;
35  
36  import java.util.Arrays;
37  import java.util.Iterator;
38  import java.util.Properties;
39  import java.util.Set;
40  import java.util.TreeSet;
41  
42  import org.apache.tools.ant.DirectoryScanner;
43  
44  /**
45   * <a href="PluginsSummaryBuilder.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class PluginsSummaryBuilder {
51  
52      public static void main(String[] args) {
53          InitUtil.initWithSpring();
54  
55          File pluginsDir = new File(System.getProperty("plugins.dir"));
56  
57          new PluginsSummaryBuilder(pluginsDir);
58      }
59  
60      public PluginsSummaryBuilder(File pluginsDir) {
61          try {
62              _createPluginsSummary(pluginsDir);
63          }
64          catch (Exception e) {
65              e.printStackTrace();
66          }
67      }
68  
69      public void _createPluginsSummary(File pluginsDir) throws Exception {
70          StringBuilder sb = new StringBuilder();
71  
72          sb.append("<plugins-summary>\n");
73  
74          DirectoryScanner ds = new DirectoryScanner();
75  
76          ds.setBasedir(pluginsDir);
77          ds.setIncludes(
78              new String[] {
79                  "**\\liferay-plugin-package.properties",
80                  "**\\liferay-plugin-package.xml"
81              });
82  
83          ds.scan();
84  
85          String[] files = ds.getIncludedFiles();
86  
87          Arrays.sort(files);
88  
89          for (String file : files) {
90              _createPluginSummary(file, sb);
91          }
92  
93          for (String author : _distinctAuthors) {
94              sb.append("\t<author>");
95              sb.append(author);
96              sb.append("</author>\n");
97          }
98  
99          for (String license : _distinctLicenses) {
100             sb.append("\t<license>");
101             sb.append(license);
102             sb.append("</license>\n");
103         }
104 
105         sb.append("</plugins-summary>");
106 
107         FileUtil.write(
108             pluginsDir + File.separator + "summary.xml", sb.toString());
109     }
110 
111     public void _createPluginSummary(String file, StringBuilder sb)
112         throws Exception {
113 
114         String content = FileUtil.read(file);
115 
116         int x = file.indexOf(File.separator);
117 
118         String type = file.substring(0, x);
119 
120         if (type.endsWith("s")) {
121             type = type.substring(0, type.length() - 1);
122         }
123 
124         x = file.indexOf(File.separator, x) + 1;
125         int y = file.indexOf(File.separator, x);
126 
127         String artifactId = file.substring(x, y);
128 
129         String name = StringPool.BLANK;
130         String tags = StringPool.BLANK;
131         String shortDescription = StringPool.BLANK;
132         String changeLog = StringPool.BLANK;
133         String pageURL = StringPool.BLANK;
134         String author = StringPool.BLANK;
135         String licenses = StringPool.BLANK;
136 
137         if (file.endsWith(".properties")) {
138             Properties props = PropertiesUtil.load(content);
139 
140             name = _readProperty(props, "name");
141             tags = _readProperty(props, "tags");
142             shortDescription = _readProperty(props, "short-description");
143             changeLog = _readProperty(props, "change-log");
144             pageURL = _readProperty(props, "page-url");
145             author = _readProperty(props, "author");
146             licenses = _readProperty(props, "licenses");
147         }
148         else {
149             Document doc = SAXReaderUtil.read(content);
150 
151             Element root = doc.getRootElement();
152 
153             name = root.elementText("name");
154             tags = _readList(root.element("tags"), "tag");
155             shortDescription = root.elementText("short-description");
156             changeLog = root.elementText("change-log");
157             pageURL = root.elementText("page-url");
158             author = root.elementText("author");
159             licenses = _readList(root.element("licenses"), "license");
160         }
161 
162         _distinctAuthors.add(author);
163         _distinctLicenses.add(licenses);
164 
165         sb.append("\t<plugin>\n");
166         sb.append("\t\t<artifact-id>");
167         sb.append(artifactId);
168         sb.append("</artifact-id>\n");
169         sb.append("\t\t<name>");
170         sb.append(name);
171         sb.append("</name>\n");
172         sb.append("\t\t<type>");
173         sb.append(type);
174         sb.append("</type>\n");
175         sb.append("\t\t<tags>");
176         sb.append(tags);
177         sb.append("</tags>\n");
178         sb.append("\t\t<short-description>");
179         sb.append(shortDescription);
180         sb.append("</short-description>\n");
181         sb.append("\t\t<change-log>");
182         sb.append(changeLog);
183         sb.append("</change-log>\n");
184         sb.append("\t\t<page-url>");
185         sb.append(pageURL);
186         sb.append("</page-url>\n");
187         sb.append("\t\t<author>");
188         sb.append(author);
189         sb.append("</author>\n");
190         sb.append("\t\t<licenses>");
191         sb.append(licenses);
192         sb.append("</licenses>\n");
193         sb.append("\t</plugin>\n");
194     }
195 
196     private String _readList(Element parentEl, String name) {
197         StringBuilder sb = new StringBuilder();
198 
199         if (parentEl != null) {
200             Iterator<Element> itr = parentEl.elements(name).iterator();
201 
202             while (itr.hasNext()) {
203                 Element el = itr.next();
204 
205                 String text = el.getText().trim();
206 
207                 sb.append(text);
208 
209                 if (itr.hasNext()) {
210                     sb.append(", ");
211                 }
212             }
213         }
214 
215         return sb.toString();
216     }
217 
218     public String _readProperty(Properties props, String key) {
219         return GetterUtil.getString(props.getProperty(key));
220     }
221 
222     private Set<String> _distinctAuthors = new TreeSet<String>();
223     private Set<String> _distinctLicenses = new TreeSet<String>();
224 
225 }