View Javadoc

1   package org.apache.maven.plugin;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.jdom.Document;
24  import org.jdom.Element;
25  import org.jdom.JDOMException;
26  import org.jdom.input.SAXBuilder;
27  import org.jdom.xpath.XPath;
28  
29  import java.io.BufferedWriter;
30  import java.io.File;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.LinkedList;
36  import java.util.List;
37  import java.util.Map;
38  
39  public class PluginToTags
40  {
41      /** plugin script */
42      private String pluginScript;
43  
44      /** output file */
45      private String xdoc;
46  
47      /**
48       * @return Returns the outputFile.
49       */
50      public String getXdoc()
51      {
52          return xdoc;
53      }
54  
55      /**
56       * @param outputFile
57       *            The outputFile to set.
58       */
59      public void setXdoc(String outputFile)
60      {
61          this.xdoc = outputFile;
62      }
63  
64      /**
65       * @return Returns the pluginScript.
66       */
67      public String getPluginScript()
68      {
69          return pluginScript;
70      }
71  
72      /**
73       * @param pluginScript
74       *            The plugin script to generate the docs from.
75       */
76      public void setPluginScript(String pluginScript)
77      {
78          this.pluginScript = pluginScript;
79      }
80  
81      /**
82       * Transform the plugin script to the output file. The output file will be
83       * in xdoc format.
84       * 
85       * @throws IOException reading files
86       * @throws JDOMException on xml
87       */
88      public void transform() throws JDOMException, IOException
89      {
90          File output = new File(getXdoc());
91          BufferedWriter bw = new BufferedWriter(new FileWriter((output)));
92          bw.write("<document>\n");
93          bw.write("  <properties>\n");
94          bw.write("    <title>Plugin tags</title>\n");
95          bw.write("  </properties>\n");
96          bw.write("  <body>\n");
97          List taglibs = getTaglibs();
98          writeOverview(bw, taglibs);
99          writeTaglibs(bw, taglibs);
100         bw.write("  </body>\n");
101         bw.write("</document>\n");
102         bw.close();
103     }
104     
105     /**
106      * Write xdoc sections to the given writer for each taglib
107      * @param bw the writer
108      * @param taglibs a list of taglibs to output
109      * @throws IOException when there are errors writing
110      */
111     private void writeTaglibs(BufferedWriter bw, List taglibs) throws IOException
112     {
113         for (Iterator taglibIter = taglibs.iterator(); taglibIter.hasNext();)
114         {
115             Map taglib = (Map) taglibIter.next();
116             bw.write("    <section name='" + taglib.get("uri") + " Tag Library'>\n");
117             List tagNames = (List)taglib.get("tagNames");
118             // write out subsection with name, description, attr, desc etc....
119             for (Iterator tagIter = tagNames.iterator(); tagIter.hasNext();)
120             {
121                 String name = (String) tagIter.next();
122                 bw.write("      <subsection name='" + name + " Tag'>\n");
123                 bw.write("        <p>No description</p>\n");
124                 bw.write("        <table>\n");
125                 bw.write("          <tr><th>Attribute</th><th>Optional?</th><th>Description</th></tr>\n");
126                 bw.write("        </table>\n");
127                 bw.write("      </subsection>\n");
128             }
129             bw.write("    </section>\n");
130         }
131     }
132 
133     /**
134      * Write an xdoc overview section for all taglibs
135      * @param bw the writer for output
136      * @param taglibs the taglibs to output
137      * @throws IOException when writing fails
138      */
139     private void writeOverview(BufferedWriter bw, List taglibs) throws IOException
140     {
141         bw.write("    <section name='Overview'>\n");
142         bw.write("      <p>The following tag libraries and tags are provided by this plugin.</p>\n");
143         bw.write("      <ol>\n");
144         for (Iterator taglibIter = taglibs.iterator(); taglibIter.hasNext();)
145         {
146             Map taglib = (Map) taglibIter.next();
147             String title = taglib.get("uri") + " Tag Library";
148             String linkStart = "<a href='#" + StringUtils.replace(title, " ", "_")
149             	+ "'>";
150             bw.write("        <li>" + linkStart + taglib.get("uri") + "</a>\n");
151             bw.write("          <ol>\n");
152             List tagNames = (List)taglib.get("tagNames");
153             for (Iterator tagIter = tagNames.iterator(); tagIter.hasNext();)
154             {
155                 String name = (String) tagIter.next();
156                 bw.write("            <li><a href='#" + name + "_Tag'>" + name + "</a></li>\n");
157             }
158             bw.write("          </ol>\n");
159             bw.write("        </li>\n");
160         }    
161         bw.write("      </ol>\n");
162         bw.write("    </section>\n");
163     }
164 
165     /**
166      * @return a list of tag libraries. Each element in the list 
167      * is a {@link Map}. Each map contains an entry for "uri" (as a String)
168      * and one for tagNames (as a List).
169      * 
170      * @throws IOException reading files
171      * @throws JDOMException on xml
172      */
173     public List getTaglibs() throws JDOMException, IOException
174     {
175         SAXBuilder builder = new SAXBuilder();
176         Document doc = builder.build(new File(getPluginScript()));
177         XPath xpath = XPath.newInstance("/project/define:taglib");
178         xpath.addNamespace( "define", "jelly:define" );
179         List taglibElements = xpath.selectNodes(doc);
180         List taglibs = new LinkedList();
181         for (Iterator iter = taglibElements.iterator(); iter.hasNext();)
182         {
183             Element element = (Element) iter.next();
184             Map taglib = new HashMap();
185             taglib.put("uri", element.getAttributeValue("uri"));
186             taglib.put("tagNames", new LinkedList());
187             XPath tagsXp = XPath.newInstance("define:tag|define:jellybean");
188             List tagsElements = tagsXp.selectNodes(element);
189             for (Iterator tagsIter = tagsElements.iterator(); tagsIter.hasNext();)
190             {
191                 Element tagsElement = (Element) tagsIter.next();
192                 ((List) taglib.get("tagNames")).add(tagsElement.getAttributeValue("name"));
193             }
194             taglibs.add(taglib);
195         }
196         return taglibs;
197     }
198 
199 }