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