1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.PropertiesUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  import com.liferay.portal.util.InitUtil;
26  
27  import java.io.File;
28  
29  import java.util.Arrays;
30  import java.util.Iterator;
31  import java.util.Properties;
32  import java.util.Set;
33  import java.util.TreeSet;
34  
35  import org.apache.tools.ant.DirectoryScanner;
36  
37  /**
38   * <a href="PluginsSummaryBuilder.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class PluginsSummaryBuilder {
43  
44      public static void main(String[] args) {
45          InitUtil.initWithSpring();
46  
47          File pluginsDir = new File(System.getProperty("plugins.dir"));
48  
49          new PluginsSummaryBuilder(pluginsDir);
50      }
51  
52      public PluginsSummaryBuilder(File pluginsDir) {
53          try {
54              _createPluginsSummary(pluginsDir);
55          }
56          catch (Exception e) {
57              e.printStackTrace();
58          }
59      }
60  
61      private void _createPluginsSummary(File pluginsDir) throws Exception {
62          StringBundler sb = new StringBundler();
63  
64          sb.append("<plugins-summary>\n");
65  
66          DirectoryScanner ds = new DirectoryScanner();
67  
68          ds.setBasedir(pluginsDir);
69          ds.setIncludes(
70              new String[] {
71                  "**\\liferay-plugin-package.properties",
72                  "**\\liferay-plugin-package.xml"
73              });
74  
75          ds.scan();
76  
77          String[] files = ds.getIncludedFiles();
78  
79          Arrays.sort(files);
80  
81          for (String file : files) {
82              _createPluginSummary(file, sb);
83          }
84  
85          for (String author : _distinctAuthors) {
86              sb.append("\t<author>");
87              sb.append(author);
88              sb.append("</author>\n");
89          }
90  
91          for (String license : _distinctLicenses) {
92              sb.append("\t<license>");
93              sb.append(license);
94              sb.append("</license>\n");
95          }
96  
97          sb.append("</plugins-summary>");
98  
99          FileUtil.write(
100             pluginsDir + File.separator + "summary.xml", sb.toString());
101     }
102 
103     private void _createPluginSummary(String file, StringBundler sb)
104         throws Exception {
105 
106         String content = FileUtil.read(file);
107 
108         int x = file.indexOf(File.separatorChar);
109 
110         String type = file.substring(0, x);
111 
112         if (type.endsWith("s")) {
113             type = type.substring(0, type.length() - 1);
114         }
115 
116         x = file.indexOf(File.separator, x) + 1;
117         int y = file.indexOf(File.separator, x);
118 
119         String artifactId = file.substring(x, y);
120 
121         String name = StringPool.BLANK;
122         String tags = StringPool.BLANK;
123         String shortDescription = StringPool.BLANK;
124         String changeLog = StringPool.BLANK;
125         String pageURL = StringPool.BLANK;
126         String author = StringPool.BLANK;
127         String licenses = StringPool.BLANK;
128 
129         if (file.endsWith(".properties")) {
130             Properties properties = PropertiesUtil.load(content);
131 
132             name = _readProperty(properties, "name");
133             tags = _readProperty(properties, "tags");
134             shortDescription = _readProperty(properties, "short-description");
135             changeLog = _readProperty(properties, "change-log");
136             pageURL = _readProperty(properties, "page-url");
137             author = _readProperty(properties, "author");
138             licenses = _readProperty(properties, "licenses");
139         }
140         else {
141             Document doc = SAXReaderUtil.read(content);
142 
143             Element root = doc.getRootElement();
144 
145             name = root.elementText("name");
146             tags = _readList(root.element("tags"), "tag");
147             shortDescription = root.elementText("short-description");
148             changeLog = root.elementText("change-log");
149             pageURL = root.elementText("page-url");
150             author = root.elementText("author");
151             licenses = _readList(root.element("licenses"), "license");
152         }
153 
154         _distinctAuthors.add(author);
155         _distinctLicenses.add(licenses);
156 
157         sb.append("\t<plugin>\n");
158         sb.append("\t\t<artifact-id>");
159         sb.append(artifactId);
160         sb.append("</artifact-id>\n");
161         sb.append("\t\t<name>");
162         sb.append(name);
163         sb.append("</name>\n");
164         sb.append("\t\t<type>");
165         sb.append(type);
166         sb.append("</type>\n");
167         sb.append("\t\t<tags>");
168         sb.append(tags);
169         sb.append("</tags>\n");
170         sb.append("\t\t<short-description>");
171         sb.append(shortDescription);
172         sb.append("</short-description>\n");
173         sb.append("\t\t<change-log>");
174         sb.append(changeLog);
175         sb.append("</change-log>\n");
176         sb.append("\t\t<page-url>");
177         sb.append(pageURL);
178         sb.append("</page-url>\n");
179         sb.append("\t\t<author>");
180         sb.append(author);
181         sb.append("</author>\n");
182         sb.append("\t\t<licenses>");
183         sb.append(licenses);
184         sb.append("</licenses>\n");
185         sb.append("\t</plugin>\n");
186     }
187 
188     private String _readList(Element parentEl, String name) {
189         if ((parentEl == null) || parentEl.elements(name).isEmpty()) {
190             return StringPool.BLANK;
191         }
192 
193         StringBundler sb = new StringBundler(
194             parentEl.elements(name).size() * 2 - 1);
195 
196         Iterator<Element> itr = parentEl.elements(name).iterator();
197 
198         while (itr.hasNext()) {
199             Element el = itr.next();
200 
201             String text = el.getText().trim();
202 
203             sb.append(text);
204 
205             if (itr.hasNext()) {
206                 sb.append(", ");
207             }
208         }
209 
210         return sb.toString();
211     }
212 
213     public String _readProperty(Properties properties, String key) {
214         return GetterUtil.getString(properties.getProperty(key));
215     }
216 
217     private Set<String> _distinctAuthors = new TreeSet<String>();
218     private Set<String> _distinctLicenses = new TreeSet<String>();
219 
220 }