PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/droiddoc/src/SampleCode.java

https://github.com/jsherman/platform_build
Java | 161 lines | 117 code | 25 blank | 19 comment | 19 complexity | 0d0a3d3d52a1adac15bcfa3e7b241b1f MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import org.clearsilver.HDF;
  17. import org.clearsilver.CS;
  18. import java.util.*;
  19. import java.io.*;
  20. import java.util.regex.Pattern;
  21. import java.util.regex.Matcher;
  22. public class SampleCode {
  23. String mSource;
  24. String mDest;
  25. String mTitle;
  26. public SampleCode(String source, String dest, String title) {
  27. mSource = source;
  28. mTitle = title;
  29. int len = dest.length();
  30. if (len > 1 && dest.charAt(len-1) != '/') {
  31. mDest = dest + '/';
  32. } else {
  33. mDest = dest;
  34. }
  35. }
  36. public void write() {
  37. File f = new File(mSource);
  38. if (!f.isDirectory()) {
  39. System.out.println("-samplecode not a directory: " + mSource);
  40. return;
  41. }
  42. writeDirectory(f, mDest);
  43. }
  44. public static String convertExtension(String s, String ext) {
  45. return s.substring(0, s.lastIndexOf('.')) + ext;
  46. }
  47. public static String[] IMAGES = { ".png", ".jpg", ".gif" };
  48. public static String[] TEMPLATED = { ".java", ".xml" };
  49. public static boolean inList(String s, String[] list) {
  50. for (String t: list) {
  51. if (s.endsWith(t)) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. public void writeDirectory(File dir, String relative) {
  58. TreeSet<String> dirs = new TreeSet<String>();
  59. TreeSet<String> files = new TreeSet<String>();
  60. String subdir = relative; //.substring(mDest.length());
  61. for (File f: dir.listFiles()) {
  62. String name = f.getName();
  63. if (name.startsWith(".") || name.startsWith("_")) {
  64. continue;
  65. }
  66. if (f.isFile()) {
  67. String out = relative + name;
  68. if (inList(out, IMAGES)) {
  69. // copied directly
  70. ClearPage.copyFile(f, out);
  71. writeImagePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
  72. files.add(name);
  73. }
  74. if (inList(out, TEMPLATED)) {
  75. // copied and goes through the template
  76. ClearPage.copyFile(f, out);
  77. writePage(f, convertExtension(out, DroidDoc.htmlExtension), subdir);
  78. files.add(name);
  79. }
  80. // else ignored
  81. }
  82. else if (f.isDirectory()) {
  83. writeDirectory(f, relative + name + "/");
  84. dirs.add(name);
  85. }
  86. }
  87. // write the index page
  88. int i;
  89. HDF hdf = DroidDoc.makeHDF();
  90. hdf.setValue("page.title", dir.getName() + " - " + mTitle);
  91. hdf.setValue("projectTitle", mTitle);
  92. hdf.setValue("subdir", subdir);
  93. i=0;
  94. for (String d: dirs) {
  95. hdf.setValue("subdirs." + i + ".name", d);
  96. i++;
  97. }
  98. i=0;
  99. for (String f: files) {
  100. hdf.setValue("files." + i + ".name", f);
  101. hdf.setValue("files." + i + ".href", convertExtension(f, ".html"));
  102. i++;
  103. }
  104. String filename = dir.getPath() + "/_index.html";
  105. String summary = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
  106. "sample code", true, false, true);
  107. if (summary == null) {
  108. summary = "";
  109. }
  110. hdf.setValue("summary", summary);
  111. ClearPage.write(hdf, "sampleindex.cs", relative + "/index" + DroidDoc.htmlExtension);
  112. }
  113. public void writePage(File f, String out, String subdir) {
  114. String name = f.getName();
  115. String filename = f.getPath();
  116. String data = SampleTagInfo.readFile(new SourcePositionInfo(filename, -1,-1), filename,
  117. "sample code", true, true, true);
  118. data = DroidDoc.escape(data);
  119. HDF hdf = DroidDoc.makeHDF();
  120. hdf.setValue("page.title", name);
  121. hdf.setValue("subdir", subdir);
  122. hdf.setValue("realFile", name);
  123. hdf.setValue("fileContents", data);
  124. ClearPage.write(hdf, "sample.cs", out);
  125. }
  126. public void writeImagePage(File f, String out, String subdir) {
  127. String name = f.getName();
  128. String data = "<img src=\"" + name + "\" title=\"" + name + "\" />";
  129. HDF hdf = DroidDoc.makeHDF();
  130. hdf.setValue("page.title", name);
  131. hdf.setValue("subdir", subdir);
  132. hdf.setValue("realFile", name);
  133. hdf.setValue("fileContents", data);
  134. ClearPage.write(hdf, "sample.cs", out);
  135. }
  136. }