1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.ListUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.util.InitUtil;
32  
33  import java.io.File;
34  import java.io.StringReader;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.TreeMap;
40  
41  import org.apache.tools.ant.DirectoryScanner;
42  
43  /**
44   * <a href="TLDFormatter.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class TLDFormatter {
49  
50      public static void main(String[] args) {
51          try {
52              InitUtil.initWithSpring();
53  
54              _formatTLD();
55          }
56          catch (Exception e) {
57              e.printStackTrace();
58          }
59      }
60  
61      private static void _formatTLD() throws Exception {
62          String basedir = "./util-taglib/src/META-INF/";
63  
64          if (!FileUtil.exists(basedir)) {
65              return;
66          }
67  
68          List<String> list = new ArrayList<String>();
69  
70          DirectoryScanner ds = new DirectoryScanner();
71  
72          ds.setBasedir(basedir);
73          ds.setExcludes(new String[] {"**\\liferay-portlet-ext.tld"});
74          ds.setIncludes(new String[] {"**\\*.tld"});
75  
76          ds.scan();
77  
78          list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
79  
80          String[] files = list.toArray(new String[list.size()]);
81  
82          for (int i = 0; i < files.length; i++) {
83              File file = new File(basedir + files[i]);
84  
85              String content = FileUtil.read(file);
86  
87              Document document = SAXReaderUtil.read(
88                  new StringReader(
89                  StringUtil.replace(
90                      content, "xml/ns/j2ee/web-jsptaglibrary_2_0.xsd",
91                      "dtd/web-jsptaglibrary_1_2.dtd")));
92  
93              Element root = document.getRootElement();
94  
95              _sortElements(root, "tag", "name");
96  
97              List<Element> tagEls = root.elements("tag");
98  
99              for (Element tagEl : tagEls) {
100                 _sortElements(tagEl, "attribute", "name");
101 
102                 Element dynamicAttributesEl = tagEl.element(
103                     "dynamic-attributes");
104 
105                 if (dynamicAttributesEl != null) {
106                     dynamicAttributesEl.detach();
107 
108                     tagEl.add(dynamicAttributesEl);
109                 }
110             }
111 
112             String newContent = document.formattedString();
113 
114             int x = newContent.indexOf("<tlib-version");
115             int y = newContent.indexOf("</taglib>");
116 
117             newContent = newContent.substring(x, y);
118 
119             x = content.indexOf("<tlib-version");
120             y = content.indexOf("</taglib>");
121 
122             newContent =
123                 content.substring(0, x) + newContent + content.substring(y);
124 
125             if (!content.equals(newContent)) {
126                 FileUtil.write(file, newContent);
127 
128                 System.out.println(file);
129             }
130         }
131     }
132 
133     private static void _sortElements(
134         Element parentElement, String name, String sortBy) {
135 
136         Map<String, Element> map = new TreeMap<String, Element>();
137 
138         List<Element> elements = parentElement.elements(name);
139 
140         for (Element element : elements) {
141             map.put(element.elementText(sortBy), element);
142 
143             element.detach();
144         }
145 
146         for (Map.Entry<String, Element> entry : map.entrySet()) {
147             Element element = entry.getValue();
148 
149             parentElement.add(element);
150         }
151     }
152 
153 }