1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.ListUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.xml.Document;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.SAXReaderUtil;
24  import com.liferay.portal.util.InitUtil;
25  
26  import java.io.File;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  import org.apache.tools.ant.DirectoryScanner;
34  
35  /**
36   * <a href="TLDFormatter.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class TLDFormatter {
41  
42      public static void main(String[] args) {
43          try {
44              InitUtil.initWithSpring();
45  
46              _formatTLD();
47          }
48          catch (Exception e) {
49              e.printStackTrace();
50          }
51      }
52  
53      private static void _formatTLD() throws Exception {
54          String basedir = "./util-taglib/src/META-INF/";
55  
56          if (!FileUtil.exists(basedir)) {
57              return;
58          }
59  
60          List<String> list = new ArrayList<String>();
61  
62          DirectoryScanner ds = new DirectoryScanner();
63  
64          ds.setBasedir(basedir);
65          ds.setExcludes(new String[] {"**\\liferay-portlet-ext.tld"});
66          ds.setIncludes(new String[] {"**\\*.tld"});
67  
68          ds.scan();
69  
70          list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
71  
72          String[] files = list.toArray(new String[list.size()]);
73  
74          for (int i = 0; i < files.length; i++) {
75              File file = new File(basedir + files[i]);
76  
77              String content = FileUtil.read(file);
78  
79              Document document = SAXReaderUtil.read(
80                  new UnsyncStringReader(
81                  StringUtil.replace(
82                      content, "xml/ns/j2ee/web-jsptaglibrary_2_0.xsd",
83                      "dtd/web-jsptaglibrary_1_2.dtd")));
84  
85              Element root = document.getRootElement();
86  
87              _sortElements(root, "tag", "name");
88  
89              List<Element> tagEls = root.elements("tag");
90  
91              for (Element tagEl : tagEls) {
92                  _sortElements(tagEl, "attribute", "name");
93  
94                  Element dynamicAttributesEl = tagEl.element(
95                      "dynamic-attributes");
96  
97                  if (dynamicAttributesEl != null) {
98                      dynamicAttributesEl.detach();
99  
100                     tagEl.add(dynamicAttributesEl);
101                 }
102             }
103 
104             String newContent = document.formattedString();
105 
106             int x = newContent.indexOf("<tlib-version");
107             int y = newContent.indexOf("</taglib>");
108 
109             newContent = newContent.substring(x, y);
110 
111             x = content.indexOf("<tlib-version");
112             y = content.indexOf("</taglib>");
113 
114             newContent =
115                 content.substring(0, x) + newContent + content.substring(y);
116 
117             if (!content.equals(newContent)) {
118                 FileUtil.write(file, newContent);
119 
120                 System.out.println(file);
121             }
122         }
123     }
124 
125     private static void _sortElements(
126         Element parentElement, String name, String sortBy) {
127 
128         Map<String, Element> map = new TreeMap<String, Element>();
129 
130         List<Element> elements = parentElement.elements(name);
131 
132         for (Element element : elements) {
133             map.put(element.elementText(sortBy), element);
134 
135             element.detach();
136         }
137 
138         for (Map.Entry<String, Element> entry : map.entrySet()) {
139             Element element = entry.getValue();
140 
141             parentElement.add(element);
142         }
143     }
144 
145 }