PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/com/google/code/twiddling/core/util/DocUtil.java

http://twiddling.googlecode.com/
Java | 188 lines | 122 code | 31 blank | 35 comment | 20 complexity | eff001fb556c9024e8d95d4c2b2b0346 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package com.google.code.twiddling.core.util;
  20. import java.io.PrintWriter;
  21. import java.lang.reflect.Field;
  22. import java.lang.reflect.Method;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import com.google.code.twiddling.core.clp.Argument;
  26. import com.google.code.twiddling.core.clp.Option;
  27. import com.google.code.twiddling.core.command.Command;
  28. import com.google.code.twiddling.core.command.ICommand;
  29. import com.google.code.twiddling.core.io.text.TextObjectFactory;
  30. import com.google.code.twiddling.core.io.text.TextTable;
  31. import com.google.code.twiddling.core.layout.CommandHelpLayout;
  32. import com.google.code.twiddling.core.layout.LayoutManager;
  33. /**
  34. * @author <a href="mailto:jeff.yuchang@gmail.com">Jeff Yu</a>
  35. *
  36. */
  37. public class DocUtil {
  38. public static final String END_OF_LINE = "\n";
  39. public static DocModel getDocModel(Class<? extends ICommand> command) {
  40. return new DocModel(command);
  41. }
  42. /**
  43. * Render the usage, options, arguments from its annotation into docbook format,
  44. * so that can be reused into the user guide.
  45. * @param command
  46. * @return
  47. */
  48. public static String renderToDocBook(Class<? extends ICommand> command) {
  49. DocModel doc= getDocModel(command);
  50. Command cmd = doc.getCommand();
  51. StringBuffer buffer = new StringBuffer();
  52. buffer.append("<section id = \"" + cmd.name() + "\">" + END_OF_LINE);
  53. buffer.append("<title> " + cmd.name() + " </title>" + END_OF_LINE);
  54. if (!cmd.usage().equals("")) {
  55. buffer.append("<para> Usage : " + cmd.usage() + "</para>" + END_OF_LINE);
  56. }
  57. if (!cmd.description().equals("")) {
  58. buffer.append("<para> Description : " + cmd.description() + "</para>" + END_OF_LINE);
  59. }
  60. if (!cmd.example().equals("")) {
  61. buffer.append("<para> Example : " + cmd.example() + "</para>" + END_OF_LINE);
  62. }
  63. if (doc.getOptions().size() > 0) {
  64. buffer.append("<table> \n");
  65. buffer.append(" <title> options </title> \n");
  66. buffer.append(" <tgroup cols=\"3\">\n");
  67. buffer.append(" <thead>\n");
  68. buffer.append(" <row>\n");
  69. buffer.append(" <entry> option </entry>\n");
  70. buffer.append(" <entry> alias </entry>\n");
  71. buffer.append(" <entry> description </entry>\n");
  72. buffer.append(" </row>\n");
  73. buffer.append(" </thead>\n");
  74. buffer.append(" <tbody>\n");
  75. for (Option option : doc.getOptions()) {
  76. buffer.append(" <row>" + END_OF_LINE);
  77. buffer.append(" <entry>" + option.name() + "</entry>" + END_OF_LINE);
  78. buffer.append(" <entry>" + StringUtil.getArrayString(option.aliases(), ",") + "</entry>" + END_OF_LINE);
  79. buffer.append(" <entry>" + option.description() + "</entry>" + END_OF_LINE);
  80. buffer.append(" </row>" + END_OF_LINE);
  81. }
  82. buffer.append(" </tbody>\n");
  83. buffer.append(" </tgroup>\n" );
  84. buffer.append("</table>\n");
  85. }
  86. buffer.append("</section>");
  87. return buffer.toString();
  88. }
  89. /**
  90. * Render the usage, options and arguments to console.
  91. * @param command
  92. * @param out
  93. */
  94. public static void renderToConsole(Class<? extends ICommand> command, PrintWriter out) {
  95. DocModel doc= getDocModel(command);
  96. Command cmd = doc.getCommand();
  97. CommandHelpLayout layout = LayoutManager.getHelpLayout();
  98. layout.setUsage(cmd.usage())
  99. .setDescription(cmd.description())
  100. .setExample(cmd.example());
  101. List<Option> options = doc.getOptions();
  102. for (Option option : options) {
  103. layout.addOption(option.name(), StringUtil.getArrayString(option.aliases(), ","), option.description());
  104. }
  105. layout.print(out);
  106. }
  107. public static void println(PrintWriter out, String value, String title) {
  108. if (!StringUtil.isEmpty(value)) {
  109. if (!StringUtil.isEmpty(title)) {
  110. out.println(title + " : " + value);
  111. } else {
  112. out.println(value);
  113. }
  114. }
  115. }
  116. public static class DocModel {
  117. private Command command;
  118. private List<Option> options = new LinkedList<Option>();
  119. private List<Argument> arguments = new LinkedList<Argument>();
  120. public DocModel(Class<? extends ICommand> command) {
  121. this.command = command.getAnnotation(Command.class);
  122. for (Class<?> type=command; type!=null; type=type.getSuperclass()) {
  123. // Discover methods
  124. for (Method method : type.getDeclaredMethods()) {
  125. Option option = method.getAnnotation(Option.class);
  126. if (option != null) {
  127. options.add(option);
  128. }
  129. Argument argument = method.getAnnotation(Argument.class);
  130. if (argument != null) {
  131. arguments.add(argument);
  132. }
  133. }
  134. // Discover fields
  135. for (Field field : type.getDeclaredFields()) {
  136. Option option = field.getAnnotation(Option.class);
  137. if (option != null) {
  138. options.add(option);
  139. }
  140. Argument argument = field.getAnnotation(Argument.class);
  141. if (argument != null) {
  142. arguments.add(argument);
  143. }
  144. }
  145. }
  146. }
  147. public Command getCommand() {
  148. return command;
  149. }
  150. public List<Option> getOptions() {
  151. return options;
  152. }
  153. public List<Argument> getArguments() {
  154. return arguments;
  155. }
  156. }
  157. }