PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-docco/src/main/java/com/atlassian/docco/builder/DoccoBatchBuilder.java

https://bitbucket.org/Arnauld/atlassian-docco
Java | 207 lines | 176 code | 28 blank | 3 comment | 15 complexity | ee819cfabf0f5072f764da9de35728a0 MD5 | raw file
  1. package com.atlassian.docco.builder;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.net.URISyntaxException;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import com.atlassian.docco.Docco;
  10. import com.atlassian.docco.DoccoBatch;
  11. import com.atlassian.docco.mapping.DoccoFileMappingManager;
  12. import org.apache.commons.io.FileUtils;
  13. import org.apache.commons.lang.StringUtils;
  14. import static com.google.common.base.Preconditions.checkNotNull;
  15. import static com.google.common.base.Preconditions.checkState;
  16. /**
  17. * @since 1.0
  18. */
  19. public class DoccoBatchBuilder
  20. {
  21. private File basePath;
  22. private File outputPath;
  23. private String[] excludes;
  24. private String[] includes;
  25. private URL hSoyTemplate;
  26. private URL vSoyTemplate;
  27. private DoccoFileMappingManager fileMappings;
  28. private String title;
  29. private String indexFilename;
  30. private List<File> customResources;
  31. private Boolean stripJavadoc;
  32. private Boolean skipNoDocco;
  33. public DoccoBatchBuilder(File basePath, File outputPath)
  34. {
  35. this.basePath = basePath;
  36. this.outputPath = outputPath;
  37. this.excludes = new String[0];
  38. this.includes = new String[0];
  39. }
  40. public DoccoBatchBuilder horizontalTemplate(File soyTemplate)
  41. {
  42. checkState(soyTemplate.exists(),"Horizontal Soy template doesn't exist!");
  43. try
  44. {
  45. this.hSoyTemplate = soyTemplate.toURI().toURL();
  46. }
  47. catch (MalformedURLException e)
  48. {
  49. e.printStackTrace();
  50. }
  51. return this;
  52. }
  53. public DoccoBatchBuilder verticalTemplate(File soyTemplate)
  54. {
  55. checkState(soyTemplate.exists(),"Vertical Soy template doesn't exist!");
  56. try
  57. {
  58. this.vSoyTemplate = soyTemplate.toURI().toURL();
  59. }
  60. catch (MalformedURLException e)
  61. {
  62. e.printStackTrace();
  63. }
  64. return this;
  65. }
  66. public DoccoBatchBuilder excludes(String[] excludes)
  67. {
  68. this.excludes = excludes;
  69. return this;
  70. }
  71. public DoccoBatchBuilder includes(String[] includes)
  72. {
  73. this.includes = includes;
  74. return this;
  75. }
  76. public DoccoBatchBuilder fileMappings(DoccoFileMappingManager fileMappings)
  77. {
  78. this.fileMappings = fileMappings;
  79. return this;
  80. }
  81. public DoccoBatchBuilder indexTitle(String title)
  82. {
  83. this.title = title;
  84. return this;
  85. }
  86. public DoccoBatchBuilder indexFilename(String indexFilename)
  87. {
  88. this.indexFilename = indexFilename;
  89. return this;
  90. }
  91. public DoccoBatchBuilder resources(List<File> customResources)
  92. {
  93. this.customResources = customResources;
  94. return this;
  95. }
  96. public DoccoBatchBuilder stripJavadoc(Boolean stripJavadoc)
  97. {
  98. this.stripJavadoc = stripJavadoc;
  99. return this;
  100. }
  101. public DoccoBatchBuilder skipNoDocco(Boolean skipNoDocco)
  102. {
  103. this.skipNoDocco = skipNoDocco;
  104. return this;
  105. }
  106. public DoccoBatch build()
  107. {
  108. checkNotNull(basePath);
  109. checkState(basePath.exists(),"Base Path doesn't exist!");
  110. checkNotNull(outputPath);
  111. if(!outputPath.exists())
  112. {
  113. try
  114. {
  115. FileUtils.forceMkdir(outputPath);
  116. }
  117. catch (IOException e)
  118. {
  119. e.printStackTrace();
  120. throw new IllegalStateException("Error creating docco output directory!", e);
  121. }
  122. }
  123. if(null == hSoyTemplate)
  124. {
  125. try
  126. {
  127. hSoyTemplate = Docco.getDefaultHorizontalTemplate();
  128. }
  129. catch (URISyntaxException e)
  130. {
  131. throw new IllegalStateException("Unable to load default horizontal soy template!", e);
  132. }
  133. }
  134. if(null == vSoyTemplate)
  135. {
  136. try
  137. {
  138. vSoyTemplate = Docco.getDefaultVerticalTemplate();
  139. }
  140. catch (URISyntaxException e)
  141. {
  142. throw new IllegalStateException("Unable to load default vertical soy template!", e);
  143. }
  144. }
  145. if(null == fileMappings)
  146. {
  147. fileMappings = new DoccoFileMappingManager();
  148. }
  149. if(StringUtils.isBlank(title))
  150. {
  151. try
  152. {
  153. title = basePath.getCanonicalFile().getName() + " index";
  154. }
  155. catch (IOException e)
  156. {
  157. title = "Index";
  158. }
  159. }
  160. if(StringUtils.isBlank(indexFilename))
  161. {
  162. indexFilename = "index.html";
  163. }
  164. if(null == customResources)
  165. {
  166. customResources = new ArrayList<File>();
  167. }
  168. if(null == stripJavadoc)
  169. {
  170. stripJavadoc = Boolean.TRUE;
  171. }
  172. if(null == skipNoDocco)
  173. {
  174. skipNoDocco = Boolean.TRUE;
  175. }
  176. Docco docco = Docco.builder().horizontalTemplate(hSoyTemplate).verticalTemplate(vSoyTemplate).fileMappings(fileMappings).stripJavadoc(stripJavadoc).resources(customResources).build();
  177. return new DoccoBatch(docco,basePath,outputPath,excludes,includes,hSoyTemplate,vSoyTemplate,fileMappings,title,indexFilename,skipNoDocco.booleanValue());
  178. }
  179. }