1   /**
2    * Copyright (c) 2000-2008 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.GetterUtil;
26  import com.liferay.portal.kernel.util.PropertiesUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.util.DocumentUtil;
29  import com.liferay.portal.util.FileImpl;
30  
31  import java.io.File;
32  
33  import java.util.Arrays;
34  import java.util.Iterator;
35  import java.util.Properties;
36  import java.util.Set;
37  import java.util.TreeSet;
38  
39  import org.apache.tools.ant.DirectoryScanner;
40  
41  import org.dom4j.Document;
42  import org.dom4j.Element;
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          File pluginsDir = new File(System.getProperty("plugins.dir"));
54  
55          new PluginsSummaryBuilder(pluginsDir);
56      }
57  
58      public PluginsSummaryBuilder(File pluginsDir) {
59          try {
60              _createPluginsSummary(pluginsDir);
61          }
62          catch (Exception e) {
63              e.printStackTrace();
64          }
65      }
66  
67      public void _createPluginsSummary(File pluginsDir) throws Exception {
68          StringBuilder sb = new StringBuilder();
69  
70          sb.append("<plugins-summary>\n");
71  
72          DirectoryScanner ds = new DirectoryScanner();
73  
74          ds.setBasedir(pluginsDir);
75          ds.setIncludes(
76              new String[] {
77                  "**\\liferay-plugin-package.properties",
78                  "**\\liferay-plugin-package.xml"
79              });
80  
81          ds.scan();
82  
83          String[] files = ds.getIncludedFiles();
84  
85          Arrays.sort(files);
86  
87          for (String file : files) {
88              _createPluginSummary(file, sb);
89          }
90  
91          for (String author : _distinctAuthors) {
92              sb.append("\t<author>");
93              sb.append(author);
94              sb.append("</author>\n");
95          }
96  
97          for (String license : _distinctLicenses) {
98              sb.append("\t<license>");
99              sb.append(license);
100             sb.append("</license>\n");
101         }
102 
103         sb.append("</plugins-summary>");
104 
105         _fileUtil.write(
106             pluginsDir + File.separator + "summary.xml", sb.toString());
107     }
108 
109     public void _createPluginSummary(String file, StringBuilder sb)
110         throws Exception {
111 
112         String content = _fileUtil.read(file);
113 
114         int x = file.indexOf(File.separator);
115 
116         String type = file.substring(0, x);
117 
118         if (type.endsWith("s")) {
119             type = type.substring(0, type.length() - 1);
120         }
121 
122         x = file.indexOf(File.separator, x) + 1;
123         int y = file.indexOf(File.separator, x);
124 
125         String artifactId = file.substring(x, y);
126 
127         String name = StringPool.BLANK;
128         String tags = StringPool.BLANK;
129         String shortDescription = StringPool.BLANK;
130         String changeLog = StringPool.BLANK;
131         String pageURL = StringPool.BLANK;
132         String author = StringPool.BLANK;
133         String licenses = StringPool.BLANK;
134 
135         if (file.endsWith(".properties")) {
136             Properties props = PropertiesUtil.load(content);
137 
138             name = _readProperty(props, "name");
139             tags = _readProperty(props, "tags");
140             shortDescription = _readProperty(props, "short-description");
141             changeLog = _readProperty(props, "change-log");
142             pageURL = _readProperty(props, "page-url");
143             author = _readProperty(props, "author");
144             licenses = _readProperty(props, "licenses");
145         }
146         else {
147             Document doc = DocumentUtil.readDocumentFromXML(content);
148 
149             Element root = doc.getRootElement();
150 
151             name = root.elementText("name");
152             tags = _readList(root.element("tags"), "tag");
153             shortDescription = root.elementText("short-description");
154             changeLog = root.elementText("change-log");
155             pageURL = root.elementText("page-url");
156             author = root.elementText("author");
157             licenses = _readList(root.element("licenses"), "license");
158         }
159 
160         _distinctAuthors.add(author);
161         _distinctLicenses.add(licenses);
162 
163         sb.append("\t<plugin>\n");
164         sb.append("\t\t<artifact-id>");
165         sb.append(artifactId);
166         sb.append("</artifact-id>\n");
167         sb.append("\t\t<name>");
168         sb.append(name);
169         sb.append("</name>\n");
170         sb.append("\t\t<type>");
171         sb.append(type);
172         sb.append("</type>\n");
173         sb.append("\t\t<tags>");
174         sb.append(tags);
175         sb.append("</tags>\n");
176         sb.append("\t\t<short-description>");
177         sb.append(shortDescription);
178         sb.append("</short-description>\n");
179         sb.append("\t\t<change-log>");
180         sb.append(changeLog);
181         sb.append("</change-log>\n");
182         sb.append("\t\t<page-url>");
183         sb.append(pageURL);
184         sb.append("</page-url>\n");
185         sb.append("\t\t<author>");
186         sb.append(author);
187         sb.append("</author>\n");
188         sb.append("\t\t<licenses>");
189         sb.append(licenses);
190         sb.append("</licenses>\n");
191         sb.append("\t</plugin>\n");
192     }
193 
194     private String _readList(Element parentEl, String name) {
195         StringBuilder sb = new StringBuilder();
196 
197         if (parentEl != null) {
198             Iterator<Element> itr = parentEl.elements(name).iterator();
199 
200             while (itr.hasNext()) {
201                 Element el = itr.next();
202 
203                 String text = el.getText().trim();
204 
205                 sb.append(text);
206 
207                 if (itr.hasNext()) {
208                     sb.append(", ");
209                 }
210             }
211         }
212 
213         return sb.toString();
214     }
215 
216     public String _readProperty(Properties props, String key) {
217         return GetterUtil.getString(props.getProperty(key));
218     }
219 
220     private static FileImpl _fileUtil = FileImpl.getInstance();
221 
222     private Set<String> _distinctAuthors = new TreeSet<String>();
223     private Set<String> _distinctLicenses = new TreeSet<String>();
224 
225 }