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