001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropertiesUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    import com.liferay.portal.util.InitUtil;
026    
027    import java.io.File;
028    
029    import java.util.Arrays;
030    import java.util.Iterator;
031    import java.util.Properties;
032    import java.util.Set;
033    import java.util.TreeSet;
034    
035    import org.apache.tools.ant.DirectoryScanner;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class PluginsSummaryBuilder {
041    
042            public static void main(String[] args) {
043                    InitUtil.initWithSpring();
044    
045                    File pluginsDir = new File(System.getProperty("plugins.dir"));
046    
047                    new PluginsSummaryBuilder(pluginsDir);
048            }
049    
050            public PluginsSummaryBuilder(File pluginsDir) {
051                    try {
052                            _createPluginsSummary(pluginsDir);
053                    }
054                    catch (Exception e) {
055                            e.printStackTrace();
056                    }
057            }
058    
059            private void _createPluginsSummary(File pluginsDir) throws Exception {
060                    StringBundler sb = new StringBundler();
061    
062                    sb.append("<plugins-summary>\n");
063    
064                    DirectoryScanner ds = new DirectoryScanner();
065    
066                    ds.setBasedir(pluginsDir);
067                    ds.setIncludes(
068                            new String[] {
069                                    "**\\liferay-plugin-package.properties",
070                                    "**\\liferay-plugin-package.xml"
071                            });
072    
073                    ds.scan();
074    
075                    String[] files = ds.getIncludedFiles();
076    
077                    Arrays.sort(files);
078    
079                    for (String file : files) {
080                            _createPluginSummary(file, sb);
081                    }
082    
083                    for (String author : _distinctAuthors) {
084                            sb.append("\t<author>");
085                            sb.append(author);
086                            sb.append("</author>\n");
087                    }
088    
089                    for (String license : _distinctLicenses) {
090                            sb.append("\t<license>");
091                            sb.append(license);
092                            sb.append("</license>\n");
093                    }
094    
095                    sb.append("</plugins-summary>");
096    
097                    FileUtil.write(
098                            pluginsDir + File.separator + "summary.xml", sb.toString());
099            }
100    
101            private void _createPluginSummary(String file, StringBundler sb)
102                    throws Exception {
103    
104                    String content = FileUtil.read(file);
105    
106                    int x = file.indexOf(File.separatorChar);
107    
108                    String type = file.substring(0, x);
109    
110                    if (type.endsWith("s")) {
111                            type = type.substring(0, type.length() - 1);
112                    }
113    
114                    x = file.indexOf(File.separator, x) + 1;
115                    int y = file.indexOf(File.separator, x);
116    
117                    String artifactId = file.substring(x, y);
118    
119                    String name = StringPool.BLANK;
120                    String tags = StringPool.BLANK;
121                    String shortDescription = StringPool.BLANK;
122                    String changeLog = StringPool.BLANK;
123                    String pageURL = StringPool.BLANK;
124                    String author = StringPool.BLANK;
125                    String licenses = StringPool.BLANK;
126    
127                    if (file.endsWith(".properties")) {
128                            Properties props = PropertiesUtil.load(content);
129    
130                            name = _readProperty(props, "name");
131                            tags = _readProperty(props, "tags");
132                            shortDescription = _readProperty(props, "short-description");
133                            changeLog = _readProperty(props, "change-log");
134                            pageURL = _readProperty(props, "page-url");
135                            author = _readProperty(props, "author");
136                            licenses = _readProperty(props, "licenses");
137                    }
138                    else {
139                            Document doc = SAXReaderUtil.read(content);
140    
141                            Element root = doc.getRootElement();
142    
143                            name = root.elementText("name");
144                            tags = _readList(root.element("tags"), "tag");
145                            shortDescription = root.elementText("short-description");
146                            changeLog = root.elementText("change-log");
147                            pageURL = root.elementText("page-url");
148                            author = root.elementText("author");
149                            licenses = _readList(root.element("licenses"), "license");
150                    }
151    
152                    _distinctAuthors.add(author);
153                    _distinctLicenses.add(licenses);
154    
155                    sb.append("\t<plugin>\n");
156                    sb.append("\t\t<artifact-id>");
157                    sb.append(artifactId);
158                    sb.append("</artifact-id>\n");
159                    sb.append("\t\t<name>");
160                    sb.append(name);
161                    sb.append("</name>\n");
162                    sb.append("\t\t<type>");
163                    sb.append(type);
164                    sb.append("</type>\n");
165                    sb.append("\t\t<tags>");
166                    sb.append(tags);
167                    sb.append("</tags>\n");
168                    sb.append("\t\t<short-description>");
169                    sb.append(shortDescription);
170                    sb.append("</short-description>\n");
171                    sb.append("\t\t<change-log>");
172                    sb.append(changeLog);
173                    sb.append("</change-log>\n");
174                    sb.append("\t\t<page-url>");
175                    sb.append(pageURL);
176                    sb.append("</page-url>\n");
177                    sb.append("\t\t<author>");
178                    sb.append(author);
179                    sb.append("</author>\n");
180                    sb.append("\t\t<licenses>");
181                    sb.append(licenses);
182                    sb.append("</licenses>\n");
183                    sb.append("\t</plugin>\n");
184            }
185    
186            private String _readList(Element parentEl, String name) {
187                    if ((parentEl == null) || parentEl.elements(name).isEmpty()) {
188                            return StringPool.BLANK;
189                    }
190    
191                    StringBundler sb = new StringBundler(
192                            parentEl.elements(name).size() * 2 - 1);
193    
194                    Iterator<Element> itr = parentEl.elements(name).iterator();
195    
196                    while (itr.hasNext()) {
197                            Element el = itr.next();
198    
199                            String text = el.getText().trim();
200    
201                            sb.append(text);
202    
203                            if (itr.hasNext()) {
204                                    sb.append(", ");
205                            }
206                    }
207    
208                    return sb.toString();
209            }
210    
211            public String _readProperty(Properties props, String key) {
212                    return GetterUtil.getString(props.getProperty(key));
213            }
214    
215            private Set<String> _distinctAuthors = new TreeSet<String>();
216            private Set<String> _distinctLicenses = new TreeSet<String>();
217    
218    }