PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/cayenne-3.0.1/build-tools/maven-cayenne-doc-plugin/src/main/java/org/apache/cayenne/maven/plugin/confluence/DocGenerator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 196 lines | 122 code | 46 blank | 28 comment | 12 complexity | 681a4942946a4df1ea43735aebb8f6e8 MD5 | raw file
  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 org.apache.cayenne.maven.plugin.confluence;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.FileWriter;
  23. import java.util.Iterator;
  24. import org.objectstyle.confluence.rpc.soap_axis.confluenceservice_v1.ConfluenceSoapService;
  25. import org.objectstyle.confluence.rpc.soap_axis.confluenceservice_v1.ConfluenceSoapServiceProxy;
  26. import com.atlassian.confluence.rpc.soap.beans.RemoteAttachment;
  27. import com.atlassian.confluence.rpc.soap.beans.RemotePage;
  28. import com.atlassian.confluence.rpc.soap.beans.RemotePageSummary;
  29. /**
  30. * Generates standalone documentation for Cayenne based on Confluence content.
  31. *
  32. */
  33. public class DocGenerator {
  34. private static final String DEFAULT_TEMPLATE = "doctemplates/default.vm";
  35. private static final String ENDPOINT_SUFFIX = "/rpc/soap-axis/confluenceservice-v1";
  36. private String baseUrl;
  37. private String spaceKey;
  38. private String docBase;
  39. private String startPage;
  40. private String token;
  41. private ConfluenceSoapService service;
  42. private String username;
  43. private String password;
  44. private String template;
  45. private DocPageRenderer parser;
  46. public DocGenerator(String baseUrl, String spaceKey, String docBase,
  47. String startPage, String username, String password, String template) {
  48. ConfluenceSoapServiceProxy service = new ConfluenceSoapServiceProxy();
  49. // derive service URL from base URL
  50. if (baseUrl != null) {
  51. if (baseUrl.endsWith("/")) {
  52. baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
  53. }
  54. String endpoint = baseUrl + ENDPOINT_SUFFIX;
  55. service.setEndpoint(endpoint);
  56. }
  57. // service base URL from service default URL
  58. else if (service.getEndpoint().endsWith(ENDPOINT_SUFFIX)) {
  59. String endpoint = service.getEndpoint();
  60. baseUrl = endpoint.substring(0, endpoint.length()
  61. - ENDPOINT_SUFFIX.length());
  62. } else {
  63. throw new IllegalArgumentException(
  64. "Null base url and invalid service URL");
  65. }
  66. this.baseUrl = baseUrl;
  67. this.service = service;
  68. this.spaceKey = spaceKey;
  69. this.docBase = docBase;
  70. this.startPage = startPage;
  71. this.username = username;
  72. this.password = password;
  73. if (template == null) {
  74. this.template = DEFAULT_TEMPLATE;
  75. } else {
  76. this.template = template;
  77. }
  78. }
  79. /**
  80. * Main worker method for documentation generation from Wiki.
  81. */
  82. public void generateDocs() throws Exception {
  83. login();
  84. createPath(docBase);
  85. // Build a page hierarchy first..
  86. DocPage root = getPage(null, startPage);
  87. iterateChildren(root);
  88. renderPage(root, docBase);
  89. }
  90. protected void iterateChildren(DocPage parent) throws Exception {
  91. RemotePageSummary[] children = getChildren(parent);
  92. for (int i = 0; i < children.length; i++) {
  93. DocPage child = getPage(parent, children[i].getTitle());
  94. parent.addChild(child);
  95. iterateChildren(child);
  96. }
  97. }
  98. protected void renderPage(DocPage page, String basePath) throws Exception {
  99. String currentPath = basePath + "/" + page.getTitle();
  100. createPath(currentPath);
  101. FileWriter fw = new FileWriter(currentPath + "/index.html");
  102. parser.render(page, fw);
  103. fw.close();
  104. writeAttachments(currentPath, page);
  105. for (Iterator childIter = page.getChildren().iterator(); childIter
  106. .hasNext();) {
  107. renderPage((DocPage) childIter.next(), currentPath);
  108. }
  109. }
  110. protected RemotePageSummary[] getChildren(DocPage page) throws Exception {
  111. return service.getChildren(token, page.getId());
  112. }
  113. protected void writeAttachments(String basePath, DocPage page)
  114. throws Exception {
  115. RemoteAttachment[] attachments = service.getAttachments(token, page
  116. .getId());
  117. for (int j = 0; j < attachments.length; j++) {
  118. FileOutputStream fos = null;
  119. try {
  120. fos = new FileOutputStream(basePath + "/"
  121. + attachments[j].getFileName());
  122. fos.write(getAttachmentData(page, attachments[j]));
  123. } finally {
  124. fos.close();
  125. }
  126. }
  127. }
  128. protected byte[] getAttachmentData(DocPage page, RemoteAttachment attachment)
  129. throws Exception {
  130. return service.getAttachmentData(token, page.getId(), attachment
  131. .getFileName(), 0);
  132. }
  133. protected void login() throws Exception {
  134. token = service.login(username, password);
  135. parser = new DocPageRenderer(service, baseUrl, token, spaceKey,
  136. template);
  137. }
  138. protected DocPage getPage(DocPage parentPage, String pageTitle)
  139. throws Exception {
  140. RemotePage page = service.getPage(token, spaceKey, pageTitle);
  141. return new DocPage(parentPage, page.getTitle(), page.getId(), page
  142. .getContent());
  143. }
  144. protected void createPath(String path) {
  145. new File(path).mkdirs();
  146. }
  147. public String getBaseUrl() {
  148. return baseUrl;
  149. }
  150. }