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