PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/ri/XmlToBinary.java

#
Java | 316 lines | 132 code | 23 blank | 161 comment | 11 complexity | 643412f463dff5b2415bd595bb8c8d3f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * RiParser.java -
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.ri;
  21. import java.beans.XMLDecoder;
  22. import java.io.*;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * @author robmckinnon at users.sourceforge.net
  27. */
  28. public final class XmlToBinary {
  29. private void convertXmlToBinary(String inputPath, String resultPath) {
  30. File directory = new File(inputPath);
  31. List<File> classDescriptions = findClassDescriptions(directory);
  32. for (File file : classDescriptions) {
  33. loadClassDescription(file, inputPath, resultPath);
  34. }
  35. }
  36. private static void loadClassDescription(File file, String inputPath, String resultPath) {
  37. FileInputStream stream = getStream(file);
  38. XMLDecoder d = new XMLDecoder(new BufferedInputStream(stream));
  39. ClassDescription result = null;
  40. try {
  41. result = (ClassDescription) d.readObject();
  42. } catch (Exception e) {
  43. System.out.println("Exception with: " + file.toString());
  44. e.printStackTrace();
  45. System.exit(1);
  46. } finally {
  47. d.close();
  48. }
  49. if (result != null) {
  50. encode(result, file, inputPath, resultPath);
  51. }
  52. }
  53. private static FileInputStream getStream(File file) {
  54. try {
  55. return new FileInputStream(file);
  56. } catch (FileNotFoundException e) {
  57. e.printStackTrace();
  58. return null;
  59. }
  60. }
  61. private static void makeDirectory(File parentFile) {
  62. if (!parentFile.getParentFile().exists()) {
  63. makeDirectory(parentFile.getParentFile());
  64. }
  65. parentFile.mkdir();
  66. }
  67. private static void encode(ClassDescription result, File file, String inputPath, String resultPath) {
  68. String name = file.getName();
  69. String subpath = file.getParent().substring(inputPath.length());
  70. int end = name.indexOf(".xml");
  71. name = name.substring(0, end) + ".dat";
  72. System.out.println(name);
  73. file = new File(resultPath + subpath, name);
  74. File parentFile = file.getParentFile();
  75. if(!parentFile.exists()) {
  76. makeDirectory(parentFile);
  77. }
  78. try {
  79. ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file));
  80. output.writeObject(result);
  81. output.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. // private void toXml(ClassDescription description) {
  87. // try {
  88. // XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("/home/a/tmp/Test.xml")));
  89. // e.writeObject(description);
  90. // e.close();
  91. // FileOutputStream fos = new FileOutputStream("/home/a/tmp/Test.txt");
  92. // ObjectOutputStream oos = new ObjectOutputStream(fos);
  93. // oos.writeObject(description);
  94. // oos.close();
  95. // } catch (IOException e) {
  96. // e.printStackTrace();
  97. // }
  98. // }
  99. private List<File> findClassDescriptions(File directory) {
  100. List<File> list = new ArrayList<File>();
  101. File[] entries = directory.listFiles();
  102. for (File entry : entries) {
  103. if (entry.isDirectory()) {
  104. list.addAll(findClassDescriptions(entry));
  105. } else if (entry.getName().endsWith("xml")) {
  106. list.add(entry);
  107. }
  108. }
  109. return list;
  110. }
  111. // private static final String classes = "RI, RI::AliasName, RI::AnsiFormatter, RI::Attribute,\n" +
  112. // " RI::AttributeFormatter, RI::AttributeFormatter::AttrChar,\n" +
  113. // " RI::AttributeFormatter::AttributeString, RI::ClassDescription,\n" +
  114. // " RI::ClassEntry, RI::Constant, RI::Description, RI::HtmlFormatter,\n" +
  115. // " RI::IncludedModule, RI::MethodDescription, RI::MethodEntry,\n" +
  116. // " RI::MethodDescription, RI::ModuleDescription, RI::NamedThing,\n" +
  117. // " RI::Options, RI::Options::OptionList, RI::OverstrikeFormatter,\n" +
  118. // " RI::Paths, RI::RiCache, RI::RiReader, RI::RiWriter,\n" +
  119. // " RI::SimpleFormatter, RI::TextFormatter, RI::TopLevelEntry,";
  120. public static void parse() {
  121. ClassDescription description = new ClassDescription();
  122. description.setAttributes(new ArrayList<Attribute>());
  123. List<MethodDescription> methods = new ArrayList<MethodDescription>();
  124. MethodDescription methodDescription = new MethodDescription();
  125. methodDescription.setName("new");
  126. methods.add(methodDescription);
  127. description.setClassMethods(methods);
  128. StringBuffer buffer = new StringBuffer();
  129. buffer.append("<tt>Object</tt> is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.");
  130. buffer.append("<tt>Object</tt> mixes in the <tt>Kernel</tt> module, making the built-in kernel\n" +
  131. " functions globally accessible. Although the instance methods of <tt>Object</tt>\n" +
  132. " are defined by the <tt>Kernel</tt> module, we have chosen to document them here\n" +
  133. " for clarity.");
  134. buffer.append("In the descriptions of Object's methods, the parameter <em>symbol</em> refers to\n" +
  135. " a symbol, which is either a quoted string or a <tt>Symbol</tt> (such as\n" +
  136. " <tt>:name</tt>).");
  137. description.setComment(buffer.toString());
  138. description.setConstants(new ArrayList<Constant>());
  139. description.setFullName("Object");
  140. description.setName("Object");
  141. description.setSuperclass("");
  142. ArrayList<IncludedModule> includes = new ArrayList<IncludedModule>();
  143. IncludedModule includedModule = new IncludedModule();
  144. includedModule.setName("Kernel");
  145. includes.add(includedModule);
  146. description.setIncludes(includes);
  147. ArrayList<MethodDescription> instanceMethods = new ArrayList<MethodDescription>();
  148. MethodDescription method = new MethodDescription();
  149. method.setName("==");
  150. method.setAliases(new ArrayList<String>());
  151. method.setBlockParameters("e1, e2");
  152. method.setParameters(" obj == other => true or false\n" +
  153. " obj.equal?(other) => true or false\n" +
  154. " obj.eql?(other) => true or false");
  155. method.setComment(buffer.toString());
  156. method.setFullName("Object#==");
  157. method.setIsSingleton(true);
  158. method.setVisibility("public");
  159. method.setIsClassMethod(true);
  160. instanceMethods.add(method);
  161. method = new MethodDescription();
  162. method.setName("===");
  163. instanceMethods.add(method);
  164. description.setInstanceMethods(instanceMethods);
  165. // toXml(description);
  166. }
  167. // private static final String objectClass = "--- !ruby/object:RI::ClassDescription \n" +
  168. // "attributes: []\n" +
  169. // "class_methods: \n" +
  170. // " - !ruby/object:RI::MethodDescription \n" +
  171. // " name: new\n" +
  172. // "comment: \n" +
  173. // " - !ruby/struct:SM::Flow::P \n" +
  174. // " body: \"<tt>Object</tt> is the parent class of all classes in Ruby. Its methods are\n" +
  175. // " therefore available to all objects unless explicitly overridden.\"\n" +
  176. // " - !ruby/struct:SM::Flow::P \n" +
  177. // " body: \"<tt>Object</tt> mixes in the <tt>Kernel</tt> module, making the built-in kernel\n" +
  178. // " functions globally accessible. Although the instance methods of <tt>Object</tt>\n" +
  179. // " are defined by the <tt>Kernel</tt> module, we have chosen to document them here\n" +
  180. // " for clarity.\"\n" +
  181. // " - !ruby/struct:SM::Flow::P \n" +
  182. // " body: \"In the descriptions of Object's methods, the parameter <em>symbol</em> refers to\n" +
  183. // " a symbol, which is either a quoted string or a <tt>Symbol</tt> (such as\n" +
  184. // " <tt>:name</tt>).\"\n" +
  185. // "constants: []\n" +
  186. // "full_name: Object\n" +
  187. // "includes: \n" +
  188. // " - !ruby/object:RI::IncludedModule \n" +
  189. // " name: Kernel\n" +
  190. // "instance_methods: \n" +
  191. // " - !ruby/object:RI::MethodDescription \n" +
  192. // " name: \"==\"\n" +
  193. // " - !ruby/object:RI::MethodDescription \n" +
  194. // " name: \"===\"\n" +
  195. // " - !ruby/object:RI::MethodDescription \n" +
  196. // " name: \"=~\"\n" +
  197. // " - !ruby/object:RI::MethodDescription \n" +
  198. // " name: \"__id__\"\n" +
  199. // " - !ruby/object:RI::MethodDescription \n" +
  200. // " name: \"__send__\"\n" +
  201. // " - !ruby/object:RI::MethodDescription \n" +
  202. // " name: class\n" +
  203. // " - !ruby/object:RI::MethodDescription \n" +
  204. // " name: clone\n" +
  205. // " - !ruby/object:RI::MethodDescription \n" +
  206. // " name: display\n" +
  207. // " - !ruby/object:RI::MethodDescription \n" +
  208. // " name: dup\n" +
  209. // " - !ruby/object:RI::MethodDescription \n" +
  210. // " name: \"eql?\"\n" +
  211. // " - !ruby/object:RI::MethodDescription \n" +
  212. // " name: \"equal?\"\n" +
  213. // " - !ruby/object:RI::MethodDescription \n" +
  214. // " name: extend\n" +
  215. // " - !ruby/object:RI::MethodDescription \n" +
  216. // " name: freeze\n" +
  217. // " - !ruby/object:RI::MethodDescription \n" +
  218. // " name: \"frozen?\"\n" +
  219. // " - !ruby/object:RI::MethodDescription \n" +
  220. // " name: hash\n" +
  221. // " - !ruby/object:RI::MethodDescription \n" +
  222. // " name: id\n" +
  223. // " - !ruby/object:RI::MethodDescription \n" +
  224. // " name: initialize_copy\n" +
  225. // " - !ruby/object:RI::MethodDescription \n" +
  226. // " name: inspect\n" +
  227. // " - !ruby/object:RI::MethodDescription \n" +
  228. // " name: instance_eval\n" +
  229. // " - !ruby/object:RI::MethodDescription \n" +
  230. // " name: \"instance_of?\"\n" +
  231. // " - !ruby/object:RI::MethodDescription \n" +
  232. // " name: instance_variable_get\n" +
  233. // " - !ruby/object:RI::MethodDescription \n" +
  234. // " name: instance_variable_set\n" +
  235. // " - !ruby/object:RI::MethodDescription \n" +
  236. // " name: instance_variables\n" +
  237. // " - !ruby/object:RI::MethodDescription \n" +
  238. // " name: \"is_a?\"\n" +
  239. // " - !ruby/object:RI::MethodDescription \n" +
  240. // " name: \"kind_of?\"\n" +
  241. // " - !ruby/object:RI::MethodDescription \n" +
  242. // " name: method\n" +
  243. // " - !ruby/object:RI::MethodDescription \n" +
  244. // " name: methods\n" +
  245. // " - !ruby/object:RI::MethodDescription \n" +
  246. // " name: \"nil?\"\n" +
  247. // " - !ruby/object:RI::MethodDescription \n" +
  248. // " name: object_id\n" +
  249. // " - !ruby/object:RI::MethodDescription \n" +
  250. // " name: private_methods\n" +
  251. // " - !ruby/object:RI::MethodDescription \n" +
  252. // " name: protected_methods\n" +
  253. // " - !ruby/object:RI::MethodDescription \n" +
  254. // " name: public_methods\n" +
  255. // " - !ruby/object:RI::MethodDescription \n" +
  256. // " name: remove_instance_variable\n" +
  257. // " - !ruby/object:RI::MethodDescription \n" +
  258. // " name: \"respond_to?\"\n" +
  259. // " - !ruby/object:RI::MethodDescription \n" +
  260. // " name: send\n" +
  261. // " - !ruby/object:RI::MethodDescription \n" +
  262. // " name: singleton_method_added\n" +
  263. // " - !ruby/object:RI::MethodDescription \n" +
  264. // " name: singleton_method_removed\n" +
  265. // " - !ruby/object:RI::MethodDescription \n" +
  266. // " name: singleton_method_undefined\n" +
  267. // " - !ruby/object:RI::MethodDescription \n" +
  268. // " name: singleton_methods\n" +
  269. // " - !ruby/object:RI::MethodDescription \n" +
  270. // " name: taint\n" +
  271. // " - !ruby/object:RI::MethodDescription \n" +
  272. // " name: \"tainted?\"\n" +
  273. // " - !ruby/object:RI::MethodDescription \n" +
  274. // " name: to_a\n" +
  275. // " - !ruby/object:RI::MethodDescription \n" +
  276. // " name: to_s\n" +
  277. // " - !ruby/object:RI::MethodDescription \n" +
  278. // " name: type\n" +
  279. // " - !ruby/object:RI::MethodDescription \n" +
  280. // " name: untaint\n" +
  281. // "name: Object\n" +
  282. // "superclass: ";
  283. public static void main(String[] args) {
  284. if (args.length < 2) {
  285. throw new RuntimeException("shit");
  286. } else {
  287. System.out.println(args[0]);
  288. System.out.println(args[1]);
  289. XmlToBinary parser = new XmlToBinary();
  290. parser.convertXmlToBinary(args[0], args[1]);
  291. }
  292. }
  293. }