KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyYAML


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Ola Bini <ola@ologix.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby;
29
30 import java.io.IOException JavaDoc;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import java.util.regex.Pattern JavaDoc;
37
38 import org.jruby.runtime.Block;
39 import org.jruby.runtime.ThreadContext;
40 import org.jruby.runtime.Arity;
41 import org.jruby.runtime.Visibility;
42 import org.jruby.runtime.builtin.IRubyObject;
43
44 import org.jruby.javasupport.JavaEmbedUtils;
45
46 import org.jruby.internal.runtime.methods.MultiStub;
47 import org.jruby.internal.runtime.methods.MultiStubMethod;
48
49 import org.jruby.yaml.JRubyRepresenter;
50 import org.jruby.yaml.JRubyConstructor;
51 import org.jruby.yaml.JRubySerializer;
52 import org.jruby.util.IOInputStream;
53 import org.jruby.util.IOOutputStream;
54
55 import org.jvyamlb.Representer;
56 import org.jvyamlb.Constructor;
57 import org.jvyamlb.ParserImpl;
58 import org.jvyamlb.Scanner;
59 import org.jvyamlb.ScannerImpl;
60 import org.jvyamlb.ComposerImpl;
61 import org.jvyamlb.Serializer;
62 import org.jvyamlb.ResolverImpl;
63 import org.jvyamlb.EmitterImpl;
64 import org.jvyamlb.YAMLConfig;
65 import org.jvyamlb.YAML;
66
67 /**
68  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
69  */

70 public class RubyYAML {
71     public static RubyModule createYAMLModule(Ruby runtime) {
72         runtime.getModule("Kernel").callMethod(runtime.getCurrentContext(),"require", runtime.newString("date"));
73         RubyModule result = runtime.defineModule("YAML");
74
75         YAMLStub0 ystub = new YAMLStub0();
76         ystub.yaml_dump = new MultiStubMethod(ystub,0,result,Arity.optional(),Visibility.PUBLIC);
77         ystub.yaml_dump_all = new MultiStubMethod(ystub,3,result,Arity.optional(),Visibility.PUBLIC);
78         ystub.yaml_load = new MultiStubMethod(ystub,1,result,Arity.singleArgument(),Visibility.PUBLIC);
79         ystub.yaml_load_file = new MultiStubMethod(ystub,2,result,Arity.singleArgument(),Visibility.PUBLIC);
80         ystub.yaml_each_document = new MultiStubMethod(ystub,4,result,Arity.singleArgument(),Visibility.PUBLIC);
81         ystub.yaml_load_documents = new MultiStubMethod(ystub,5,result,Arity.singleArgument(),Visibility.PUBLIC);
82         ystub.yaml_load_stream = new MultiStubMethod(ystub,6,result,Arity.singleArgument(),Visibility.PUBLIC);
83         ystub.yaml_dump_stream = new MultiStubMethod(ystub,7,result,Arity.optional(),Visibility.PUBLIC);
84         ystub.yaml_quick_emit_node = new MultiStubMethod(ystub,8,result,Arity.optional(),Visibility.PUBLIC);
85         ystub.yaml_quick_emit = new MultiStubMethod(ystub,9,result,Arity.optional(),Visibility.PUBLIC);
86         result.addModuleFunction("dump",ystub.yaml_dump);
87         result.addModuleFunction("dump_all",ystub.yaml_dump_all);
88         result.addModuleFunction("load",ystub.yaml_load);
89         result.addModuleFunction("load_file",ystub.yaml_load_file);
90         result.addModuleFunction("each_document",ystub.yaml_each_document);
91         result.addModuleFunction("load_documents",ystub.yaml_load_documents);
92         result.addModuleFunction("load_stream",ystub.yaml_load_stream);
93         result.addModuleFunction("dump_stream",ystub.yaml_dump_stream);
94         result.addModuleFunction("quick_emit_node",ystub.yaml_quick_emit_node);
95         result.addModuleFunction("quick_emit",ystub.yaml_quick_emit);
96
97         ToYAMLNodeStub0 stub0 = new ToYAMLNodeStub0();
98         ToYAMLNodeStub1 stub1 = new ToYAMLNodeStub1();
99         ToYAMLNodeStub2 stub2 = new ToYAMLNodeStub2();
100         ToYAMLNodeStub3 stub3 = new ToYAMLNodeStub3();
101
102         RubyClass obj = runtime.getClass("Object");
103         RubyClass clazz = runtime.getClass("Class");
104         RubyClass hash = runtime.getClass("Hash");
105         RubyClass array = runtime.getClass("Array");
106         RubyClass struct = runtime.getClass("Struct");
107         RubyClass exception = runtime.getClass("Exception");
108         RubyClass string = runtime.getClass("String");
109         RubyClass symbol = runtime.getClass("Symbol");
110         RubyClass range = runtime.getClass("Range");
111         RubyClass regexp = runtime.getClass("Regexp");
112         RubyClass time = runtime.getClass("Time");
113         RubyClass date = runtime.getClass("Date");
114         RubyClass numeric = runtime.getClass("Numeric");
115         RubyClass fixnum = runtime.getClass("Fixnum");
116         RubyClass flt = runtime.getClass("Float");
117         RubyClass trueClass = runtime.getClass("TrueClass");
118         RubyClass falseClass = runtime.getClass("FalseClass");
119         RubyClass nilClass = runtime.getClass("NilClass");
120        
121         stub0.obj_to_yaml_properties = new MultiStubMethod(stub0,2,obj,Arity.noArguments(),Visibility.PUBLIC);
122         stub0.obj_to_yaml_style = new MultiStubMethod(stub0,3,obj,Arity.noArguments(),Visibility.PUBLIC);
123         stub0.obj_to_yaml_node = new MultiStubMethod(stub0,4,obj,Arity.optional(),Visibility.PUBLIC);
124         stub0.obj_to_yaml = new MultiStubMethod(stub0,7,obj,Arity.optional(),Visibility.PUBLIC);
125         stub0.obj_taguri = new MultiStubMethod(stub0,5,obj,Arity.noArguments(),Visibility.PUBLIC);
126         stub0.class_to_yaml = new MultiStubMethod(stub0,6,clazz,Arity.optional(),Visibility.PUBLIC);
127         stub0.hash_to_yaml_node = new MultiStubMethod(stub0,0,hash,Arity.singleArgument(),Visibility.PUBLIC);
128         stub0.hash_taguri = new MultiStubMethod(stub0,1,hash,Arity.noArguments(),Visibility.PUBLIC);
129         stub0.array_to_yaml_node = new MultiStubMethod(stub0,8,hash,Arity.singleArgument(),Visibility.PUBLIC);
130         stub0.array_taguri = new MultiStubMethod(stub0,9,hash,Arity.noArguments(),Visibility.PUBLIC);
131         stub1.struct_to_yaml_node = new MultiStubMethod(stub1,0,struct,Arity.singleArgument(),Visibility.PUBLIC);
132         stub1.struct_taguri = new MultiStubMethod(stub1,1,struct,Arity.noArguments(),Visibility.PUBLIC);
133         stub1.exception_to_yaml_node = new MultiStubMethod(stub1,2,exception,Arity.singleArgument(),Visibility.PUBLIC);
134         stub1.exception_taguri = new MultiStubMethod(stub1,3,exception,Arity.noArguments(),Visibility.PUBLIC);
135         stub1.string_is_complex = new MultiStubMethod(stub1,4,string,Arity.noArguments(),Visibility.PUBLIC);
136         stub1.string_is_binary = new MultiStubMethod(stub1,5,string,Arity.noArguments(),Visibility.PUBLIC);
137         stub1.string_to_yaml_node = new MultiStubMethod(stub1,6,string,Arity.singleArgument(),Visibility.PUBLIC);
138         stub1.string_taguri = new MultiStubMethod(stub1,7,string,Arity.noArguments(),Visibility.PUBLIC);
139         stub1.symbol_to_yaml_node = new MultiStubMethod(stub1,8,symbol,Arity.singleArgument(),Visibility.PUBLIC);
140         stub2.range_to_yaml_node = new MultiStubMethod(stub2,0,range,Arity.singleArgument(),Visibility.PUBLIC);
141         stub2.range_taguri = new MultiStubMethod(stub2,1,range,Arity.noArguments(),Visibility.PUBLIC);
142         stub2.regexp_to_yaml_node = new MultiStubMethod(stub2,2,regexp,Arity.singleArgument(),Visibility.PUBLIC);
143         stub2.regexp_taguri = new MultiStubMethod(stub2,3,regexp,Arity.noArguments(),Visibility.PUBLIC);
144         stub2.time_to_yaml_node = new MultiStubMethod(stub2,4,time,Arity.singleArgument(),Visibility.PUBLIC);
145         stub2.time_taguri = new MultiStubMethod(stub2,5,time,Arity.noArguments(),Visibility.PUBLIC);
146         stub2.date_to_yaml_node = new MultiStubMethod(stub2,6,date,Arity.singleArgument(),Visibility.PUBLIC);
147         stub2.date_taguri = new MultiStubMethod(stub2,7,date,Arity.noArguments(),Visibility.PUBLIC);
148         stub1.numeric_to_yaml_node = new MultiStubMethod(stub1,9,numeric,Arity.singleArgument(),Visibility.PUBLIC);
149         stub2.fixnum_taguri = new MultiStubMethod(stub2,8,fixnum,Arity.noArguments(),Visibility.PUBLIC);
150         stub2.float_taguri = new MultiStubMethod(stub2,9,flt,Arity.noArguments(),Visibility.PUBLIC);
151         stub3.true_to_yaml_node = new MultiStubMethod(stub3,0,trueClass,Arity.singleArgument(),Visibility.PUBLIC);
152         stub3.true_taguri = new MultiStubMethod(stub3,1,trueClass,Arity.noArguments(),Visibility.PUBLIC);
153         stub3.false_to_yaml_node = new MultiStubMethod(stub3,2,falseClass,Arity.singleArgument(),Visibility.PUBLIC);
154         stub3.false_taguri = new MultiStubMethod(stub3,3,falseClass,Arity.noArguments(),Visibility.PUBLIC);
155         stub3.nil_to_yaml_node = new MultiStubMethod(stub3,4,nilClass,Arity.singleArgument(),Visibility.PUBLIC);
156         stub3.nil_taguri = new MultiStubMethod(stub3,5,nilClass,Arity.noArguments(),Visibility.PUBLIC);
157
158         clazz.addMethod("to_yaml",stub0.class_to_yaml);
159         
160         obj.addMethod("to_yaml_properties",stub0.obj_to_yaml_properties);
161         obj.addMethod("to_yaml_style",stub0.obj_to_yaml_style);
162         obj.addMethod("to_yaml_node",stub0.obj_to_yaml_node);
163         obj.addMethod("to_yaml",stub0.obj_to_yaml);
164         obj.addMethod("taguri",stub0.obj_taguri);
165         
166         hash.addMethod("to_yaml_node",stub0.hash_to_yaml_node);
167         hash.addMethod("taguri",stub0.hash_taguri);
168
169         array.addMethod("to_yaml_node",stub0.array_to_yaml_node);
170         array.addMethod("taguri",stub0.array_taguri);
171
172         struct.addMethod("to_yaml_node",stub1.struct_to_yaml_node);
173         struct.addMethod("taguri",stub1.struct_taguri);
174
175         exception.addMethod("to_yaml_node",stub1.exception_to_yaml_node);
176         exception.addMethod("taguri",stub1.exception_taguri);
177
178         string.addMethod("is_complex_yaml?",stub1.string_is_complex);
179         string.addMethod("is_binary_data?",stub1.string_is_binary);
180         string.addMethod("to_yaml_node",stub1.string_to_yaml_node);
181         string.addMethod("taguri",stub1.string_taguri);
182
183         symbol.addMethod("to_yaml_node",stub1.symbol_to_yaml_node);
184         symbol.addMethod("taguri",stub1.string_taguri);
185
186         range.addMethod("to_yaml_node",stub2.range_to_yaml_node);
187         range.addMethod("taguri",stub2.range_taguri);
188
189         regexp.addMethod("to_yaml_node",stub2.regexp_to_yaml_node);
190         regexp.addMethod("taguri",stub2.regexp_taguri);
191
192         time.addMethod("to_yaml_node",stub2.time_to_yaml_node);
193         time.addMethod("taguri",stub2.time_taguri);
194
195         date.addMethod("to_yaml_node",stub2.date_to_yaml_node);
196         date.addMethod("taguri",stub2.date_taguri);
197
198         numeric.addMethod("to_yaml_node",stub1.numeric_to_yaml_node);
199
200         fixnum.addMethod("taguri",stub2.fixnum_taguri);
201         flt.addMethod("taguri",stub2.float_taguri);
202
203         trueClass.addMethod("to_yaml_node",stub3.true_to_yaml_node);
204         trueClass.addMethod("taguri",stub3.true_taguri);
205
206         falseClass.addMethod("to_yaml_node",stub3.false_to_yaml_node);
207         falseClass.addMethod("taguri",stub3.false_taguri);
208
209         nilClass.addMethod("to_yaml_node",stub3.nil_to_yaml_node);
210         nilClass.addMethod("taguri",stub3.nil_taguri);
211
212         return result;
213     }
214
215     public static class YAMLStub0 implements MultiStub {
216         public MultiStubMethod yaml_dump;
217         public MultiStubMethod yaml_dump_all;
218         public MultiStubMethod yaml_load;
219         public MultiStubMethod yaml_load_file;
220         public MultiStubMethod yaml_each_document;
221         public MultiStubMethod yaml_load_documents;
222         public MultiStubMethod yaml_load_stream;
223         public MultiStubMethod yaml_dump_stream;
224         public MultiStubMethod yaml_quick_emit_node;
225         public MultiStubMethod yaml_quick_emit;
226
227         public IRubyObject method0(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
228             //yaml_dump
229
IRubyObject obj = args[0];
230             IRubyObject val = self.getRuntime().newArray(obj);
231             if(args.length>1) {
232                 return self.callMethod(context,"dump_all", new IRubyObject[]{val,args[1]});
233             } else {
234                 return self.callMethod(context,"dump_all", val);
235             }
236         }
237         public IRubyObject method1(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
238             //yaml_load
239
IRubyObject io = args[0];
240             Scanner scn = null;
241             if(io instanceof RubyString) {
242                 scn = new ScannerImpl(((RubyString)io).getByteList());
243             } else {
244                 scn = new ScannerImpl(new IOInputStream(io));
245             }
246             Constructor ctor = new JRubyConstructor(self,new ComposerImpl(new ParserImpl(scn,YAML.config().version("1.0")),new ResolverImpl()));
247             if(ctor.checkData()) {
248                 return JavaEmbedUtils.javaToRuby(self.getRuntime(),ctor.getData());
249             }
250             return self.getRuntime().getNil();
251         }
252         public IRubyObject method2(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
253             //yaml_load_file
254
IRubyObject io = self.getRuntime().getClass("File").callMethod(context,"open", new IRubyObject[]{args[0],self.getRuntime().newString("r")});
255             IRubyObject val = self.callMethod(context,"load", io);
256             io.callMethod(context, "close");
257             return val;
258
259         }
260         public IRubyObject method3(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
261             //yaml_dump_all
262
RubyArray objs = (RubyArray)args[0];
263             IRubyObject io = null;
264             IRubyObject io2 = null;
265             if(args.length == 2 && args[1] != null && !args[1].isNil()) {
266                 io = args[1];
267             }
268             YAMLConfig cfg = YAML.config().version("1.0");
269             IOOutputStream iox = null;
270             if(null == io) {
271                 self.getRuntime().getModule("Kernel").callMethod(context,"require", self.getRuntime().newString("stringio"));
272                 io2 = self.getRuntime().getClass("StringIO").callMethod(context, "new");
273                 iox = new IOOutputStream(io2);
274             } else {
275                 iox = new IOOutputStream(io);
276             }
277             Serializer ser = new JRubySerializer(new EmitterImpl(iox,cfg),new ResolverImpl(),cfg);
278             try {
279                 ser.open();
280                 Representer r = new JRubyRepresenter(ser, cfg);
281                 for(Iterator JavaDoc iter = objs.getList().iterator();iter.hasNext();) {
282                     r.represent(iter.next());
283                 }
284                 ser.close();
285             } catch(IOException JavaDoc e) {
286                 throw self.getRuntime().newIOErrorFromException(e);
287             }
288             if(null == io) {
289                 io2.callMethod(context, "rewind");
290                 return io2.callMethod(context, "read");
291             } else {
292                 return io;
293             }
294         }
295         public IRubyObject method4(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
296             //yaml_each_document
297
IRubyObject io = args[0];
298             Scanner scn = null;
299             if(io instanceof RubyString) {
300                 scn = new ScannerImpl(((RubyString)io).getByteList());
301             } else {
302                 scn = new ScannerImpl(new IOInputStream(io));
303             }
304             Constructor ctor = new JRubyConstructor(self,new ComposerImpl(new ParserImpl(scn,YAML.config().version("1.0")),new ResolverImpl()));
305             while(ctor.checkData()) {
306                 context.yield(JavaEmbedUtils.javaToRuby(self.getRuntime(),ctor.getData()), block);
307             }
308             return self.getRuntime().getNil();
309         }
310         public IRubyObject method5(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
311             //yaml_load_documents
312
IRubyObject io = args[0];
313             Scanner scn = null;
314             if(io instanceof RubyString) {
315                 scn = new ScannerImpl(((RubyString)io).getByteList());
316             } else {
317                 scn = new ScannerImpl(new IOInputStream(io));
318             }
319             Constructor ctor = new JRubyConstructor(self,new ComposerImpl(new ParserImpl(scn,YAML.config().version("1.0")),new ResolverImpl()));
320             while(ctor.checkData()) {
321                 context.yield(JavaEmbedUtils.javaToRuby(self.getRuntime(),ctor.getData()), block);
322             }
323             return self.getRuntime().getNil();
324         }
325         public IRubyObject method6(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
326             //yaml_load_stream
327
IRubyObject d = self.getRuntime().getNil();
328             IRubyObject io = args[0];
329             Scanner scn = null;
330             if(io instanceof RubyString) {
331                 scn = new ScannerImpl(((RubyString)io).getByteList());
332             } else {
333                 scn = new ScannerImpl(new IOInputStream(io));
334             }
335             Constructor ctor = new JRubyConstructor(self,new ComposerImpl(new ParserImpl(scn,YAML.config().version("1.0")),new ResolverImpl()));
336             while(ctor.checkData()) {
337                 if(d.isNil()) {
338                     d = self.getRuntime().getModule("YAML").getClass("Stream").callMethod(context,"new", d);
339                 }
340                 d.callMethod(context,"add", JavaEmbedUtils.javaToRuby(self.getRuntime(),ctor.getData()));
341             }
342             return d;
343         }
344         public IRubyObject method7(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
345             //yaml_dump_stream
346
IRubyObject stream = self.getRuntime().getModule("YAML").getClass("Stream").callMethod(context, "new");
347             for(int i=0,j=args.length;i<j;) {
348                 stream.callMethod(context,"add", args[i]);
349             }
350             return stream.callMethod(context, "emit");
351         }
352         public IRubyObject method8(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
353             //yaml_quick_emit_node
354
return context.yield(args[0], block);
355         }
356         public IRubyObject method9(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
357             //yaml_quick_emit
358
return self.getRuntime().getNil();
359         }
360     }
361
362     public static class ToYAMLNodeStub0 implements MultiStub {
363         public MultiStubMethod obj_to_yaml_properties;
364         public MultiStubMethod obj_to_yaml_style;
365         public MultiStubMethod obj_to_yaml_node;
366         public MultiStubMethod obj_to_yaml;
367         public MultiStubMethod obj_taguri;
368         public MultiStubMethod class_to_yaml;
369         public MultiStubMethod hash_to_yaml_node;
370         public MultiStubMethod hash_taguri;
371         public MultiStubMethod array_to_yaml_node;
372         public MultiStubMethod array_taguri;
373
374         public IRubyObject method0(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
375             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),self,self.callMethod(context, "to_yaml_style")});
376         }
377         public IRubyObject method1(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
378             String JavaDoc className = self.getType().getName();
379             if("Hash".equals(className)) {
380                 return self.getRuntime().newString("tag:yaml.org,2002:map");
381             } else {
382                 return self.getRuntime().newString("tag:yaml.org,2002:map:" + className);
383             }
384         }
385         public IRubyObject method2(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
386             return self.callMethod(context, "instance_variables").callMethod(context, "sort");
387         }
388         public IRubyObject method3(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
389             return self.getRuntime().getNil();
390         }
391         public IRubyObject method4(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
392             Map JavaDoc mep = (Map JavaDoc)(new RubyHash(self.getRuntime()));
393             RubyArray props = (RubyArray)self.callMethod(context, "to_yaml_properties");
394             for(Iterator JavaDoc iter = props.getList().iterator(); iter.hasNext();) {
395                 String JavaDoc m = iter.next().toString();
396                 mep.put(self.getRuntime().newString(m.substring(1)), self.callMethod(context,"instance_variable_get", self.getRuntime().newString(m)));
397             }
398             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),(IRubyObject)mep,self.callMethod(context, "to_yaml_style")});
399         }
400         public IRubyObject method5(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
401             return self.getRuntime().newString("!ruby/object:" + self.getType().getName());
402         }
403         public IRubyObject method6(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
404             throw self.getRuntime().newTypeError("can't dump anonymous class " + self.getType().getName());
405         }
406         public IRubyObject method7(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
407             return self.getRuntime().getModule("YAML").callMethod(context,"dump", self);
408         }
409         public IRubyObject method8(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
410             return args[0].callMethod(context,"seq", new IRubyObject[]{self.callMethod(context, "taguri"),self,self.callMethod(context, "to_yaml_style")});
411         }
412         public IRubyObject method9(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
413             String JavaDoc className = self.getType().getName();
414             if("Array".equals(className)) {
415                 return self.getRuntime().newString("tag:yaml.org,2002:seq");
416             } else {
417                 return self.getRuntime().newString("tag:yaml.org,2002:seq:" + className);
418             }
419         }
420     }
421
422     public static class ToYAMLNodeStub1 implements MultiStub {
423         public MultiStubMethod struct_taguri;
424         public MultiStubMethod struct_to_yaml_node;
425         public MultiStubMethod exception_taguri;
426         public MultiStubMethod exception_to_yaml_node;
427         public MultiStubMethod string_is_complex;
428         public MultiStubMethod string_is_binary;
429         public MultiStubMethod string_to_yaml_node;
430         public MultiStubMethod string_taguri;
431         public MultiStubMethod symbol_to_yaml_node;
432
433         public MultiStubMethod numeric_to_yaml_node;
434
435         public IRubyObject method0(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
436             //struct_to_yaml_node
437
Map JavaDoc mep = (Map JavaDoc)(new RubyHash(self.getRuntime()));
438             for(Iterator JavaDoc iter = ((RubyArray)self.callMethod(context, "members")).getList().iterator();iter.hasNext();) {
439                 IRubyObject key = self.getRuntime().newString(iter.next().toString());
440                 mep.put(key,self.callMethod(context,"[]", key));
441             }
442             for(Iterator JavaDoc iter = ((RubyArray)self.callMethod(context, "to_yaml_properties")).getList().iterator(); iter.hasNext();) {
443                 String JavaDoc m = iter.next().toString();
444                 mep.put(self.getRuntime().newString(m.substring(1)), self.callMethod(context,"instance_variable_get", self.getRuntime().newString(m)));
445             }
446             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),(IRubyObject)mep,self.callMethod(context, "to_yaml_style")});
447         }
448         public IRubyObject method1(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
449             return self.getRuntime().newString("!ruby/struct:" + self.getType().getName());
450         }
451         public IRubyObject method2(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
452             //exception_to_yaml_node
453
Map JavaDoc mep = (Map JavaDoc)(new RubyHash(self.getRuntime()));
454             mep.put(self.getRuntime().newString("message"),self.callMethod(context, "message"));
455             for(Iterator JavaDoc iter = ((RubyArray)self.callMethod(context, "to_yaml_properties")).getList().iterator(); iter.hasNext();) {
456                 String JavaDoc m = iter.next().toString();
457                 mep.put(self.getRuntime().newString(m.substring(1)), self.callMethod(context,"instance_variable_get", self.getRuntime().newString(m)));
458             }
459             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),(IRubyObject)mep,self.callMethod(context, "to_yaml_style")});
460         }
461         public IRubyObject method3(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
462             return self.getRuntime().newString("!ruby/exception:" + self.getType().getName());
463         }
464         private static final Pattern JavaDoc AFTER_NEWLINE = Pattern.compile("\n.+", Pattern.DOTALL);
465         public IRubyObject method4(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
466             return (self.callMethod(context, "to_yaml_style").isTrue() ||
467                     ((List JavaDoc)self.callMethod(context, "to_yaml_properties")).isEmpty() ||
468                     AFTER_NEWLINE.matcher(self.toString()).find()) ? self.getRuntime().getTrue() : self.getRuntime().getFalse();
469         }
470         public IRubyObject method5(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
471             if(self.callMethod(context, "empty?").isTrue()) {
472                 return self.getRuntime().getNil();
473             }
474             return self.toString().indexOf('\0') != -1 ? self.getRuntime().getTrue() : self.getRuntime().getFalse();
475         }
476         public IRubyObject method6(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
477             Ruby rt = self.getRuntime();
478             if(self.callMethod(context, "is_binary_data?").isTrue()) {
479                 return args[0].callMethod(context,"scalar", new IRubyObject[]{rt.newString("tag:yaml.org,2002:binary"),rt.newArray(self).callMethod(context,"pack", rt.newString("m")),rt.newString("|")});
480             }
481             if(((List JavaDoc)self.callMethod(context, "to_yaml_properties")).isEmpty()) {
482                 return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self,self.toString().startsWith(":") ? rt.newString("\"") : self.callMethod(context, "to_yaml_style")});
483             }
484             
485             Map JavaDoc mep = (Map JavaDoc)(new RubyHash(self.getRuntime()));
486             mep.put(self.getRuntime().newString("str"),rt.newString(self.toString()));
487             for(Iterator JavaDoc iter = ((RubyArray)self.callMethod(context, "to_yaml_properties")).getList().iterator(); iter.hasNext();) {
488                 String JavaDoc m = iter.next().toString();
489                 mep.put(self.getRuntime().newString(m.substring(1)), self.callMethod(context,"instance_variable_get", self.getRuntime().newString(m)));
490             }
491             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),(IRubyObject)mep,self.callMethod(context, "to_yaml_style")});
492         }
493         public IRubyObject method7(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
494             return self.getRuntime().newString("tag:yaml.org,2002:str");
495         }
496         public IRubyObject method8(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
497             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.callMethod(context, "inspect"),self.callMethod(context, "to_yaml_style")});
498         }
499         public IRubyObject method9(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
500             String JavaDoc val = self.toString();
501             if("Infinity".equals(val)) {
502                 val = ".Inf";
503             } else if("-Infinity".equals(val)) {
504                 val = "-.Inf";
505             } else if("NaN".equals(val)) {
506                 val = ".NaN";
507             }
508             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.getRuntime().newString(val),self.callMethod(context, "to_yaml_style")});
509         }
510     }
511
512     public static class ToYAMLNodeStub2 implements MultiStub {
513         public MultiStubMethod range_taguri;
514         public MultiStubMethod range_to_yaml_node;
515
516         public MultiStubMethod regexp_taguri;
517         public MultiStubMethod regexp_to_yaml_node;
518
519         public MultiStubMethod time_taguri;
520         public MultiStubMethod time_to_yaml_node;
521
522         public MultiStubMethod date_taguri;
523         public MultiStubMethod date_to_yaml_node;
524
525         public MultiStubMethod fixnum_taguri;
526         public MultiStubMethod float_taguri;
527
528         public IRubyObject method0(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
529             Map JavaDoc mep = (Map JavaDoc)(new RubyHash(self.getRuntime()));
530             mep.put(self.getRuntime().newString("begin"),self.callMethod(context, "begin"));
531             mep.put(self.getRuntime().newString("end"),self.callMethod(context, "end"));
532             mep.put(self.getRuntime().newString("excl"),self.callMethod(context, "exclude_end?"));
533             for(Iterator JavaDoc iter = ((RubyArray)self.callMethod(context, "to_yaml_properties")).getList().iterator(); iter.hasNext();) {
534                 String JavaDoc m = iter.next().toString();
535                 mep.put(self.getRuntime().newString(m.substring(1)), self.callMethod(context,"instance_variable_get", self.getRuntime().newString(m)));
536             }
537             return args[0].callMethod(context,"map", new IRubyObject[]{self.callMethod(context, "taguri"),(IRubyObject)mep,self.callMethod(context, "to_yaml_style")});
538         }
539         public IRubyObject method1(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
540             return self.getRuntime().newString("!ruby/range");
541         }
542         public IRubyObject method2(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
543             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.callMethod(context, "inspect"),self.callMethod(context, "to_yaml_style")});
544         }
545         public IRubyObject method3(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
546             return self.getRuntime().newString("!ruby/regexp");
547         }
548         public IRubyObject method4(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
549             IRubyObject tz = self.getRuntime().newString("Z");
550             IRubyObject difference_sign = self.getRuntime().newString("-");
551             if(!self.callMethod(context, "utc?").isTrue()) {
552                 IRubyObject utc_same_instant = self.callMethod(context, "dup").callMethod(context, "utc");
553                 IRubyObject utc_same_writing = self.getRuntime().getClass("Time").callMethod(context,"utc", new IRubyObject[]{
554                         self.callMethod(context, "year"),self.callMethod(context, "month"),self.callMethod(context, "day"),self.callMethod(context, "hour"),
555                         self.callMethod(context, "min"),self.callMethod(context, "sec"),self.callMethod(context, "usec")});
556                 IRubyObject difference_to_utc = utc_same_writing.callMethod(context,"-", utc_same_instant);
557                 IRubyObject absolute_difference;
558                 if(difference_to_utc.callMethod(context,"<", RubyFixnum.zero(self.getRuntime())).isTrue()) {
559                     difference_sign = self.getRuntime().newString("-");
560                     absolute_difference = RubyFixnum.zero(self.getRuntime()).callMethod(context,"-", difference_to_utc);
561                 } else {
562                     difference_sign = self.getRuntime().newString("+");
563                     absolute_difference = difference_to_utc;
564                 }
565                 IRubyObject difference_minutes = absolute_difference.callMethod(context,"/", self.getRuntime().newFixnum(60)).callMethod(context, "round");
566                 tz = self.getRuntime().newString("%s%02d:%02d").callMethod(context,"%", self.getRuntime().newArrayNoCopy(new IRubyObject[]{difference_sign,difference_minutes.callMethod(context,"/", self.getRuntime().newFixnum(60)),difference_minutes.callMethod(context,"%", self.getRuntime().newFixnum(60))}));
567             }
568             IRubyObject standard = self.callMethod(context,"strftime", self.getRuntime().newString("%Y-%m-%d %H:%M:%S"));
569             if(self.callMethod(context, "usec").callMethod(context, "nonzero?").isTrue()) {
570                 standard = standard.callMethod(context, "+", self.getRuntime().newString(".%06d").callMethod(context,"%", self.getRuntime().newArray(self.callMethod(context, "usec"))));
571             }
572             standard = standard.callMethod(context,"+", self.getRuntime().newString(" %s").callMethod(context,"%", self.getRuntime().newArray(tz)));
573             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),standard,self.callMethod(context, "to_yaml_style")});
574         }
575         public IRubyObject method5(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
576             return self.getRuntime().newString("tag:yaml.org,2002:timestamp");
577         }
578         public IRubyObject method6(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
579             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.callMethod(context, "to_s"),self.callMethod(context, "to_yaml_style")});
580         }
581         public IRubyObject method7(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
582             return self.getRuntime().newString("tag:yaml.org,2002:timestamp#ymd");
583         }
584         public IRubyObject method8(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
585             return self.getRuntime().newString("tag:yaml.org,2002:int");
586         }
587         public IRubyObject method9(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
588             return self.getRuntime().newString("tag:yaml.org,2002:float");
589         }
590     }
591
592     public static class ToYAMLNodeStub3 implements MultiStub {
593         public MultiStubMethod true_taguri;
594         public MultiStubMethod true_to_yaml_node;
595         public MultiStubMethod false_taguri;
596         public MultiStubMethod false_to_yaml_node;
597         public MultiStubMethod nil_taguri;
598         public MultiStubMethod nil_to_yaml_node;
599
600         public IRubyObject method0(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
601             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.callMethod(context, "to_s"),self.callMethod(context, "to_yaml_style")});
602         }
603         public IRubyObject method1(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
604             return self.getRuntime().newString("tag:yaml.org,2002:bool");
605         }
606         public IRubyObject method2(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
607             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.callMethod(context, "to_s"),self.callMethod(context, "to_yaml_style")});
608         }
609         public IRubyObject method3(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
610             return self.getRuntime().newString("tag:yaml.org,2002:bool");
611         }
612         public IRubyObject method4(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
613             return args[0].callMethod(context,"scalar", new IRubyObject[]{self.callMethod(context, "taguri"),self.getRuntime().newString(""),self.callMethod(context, "to_yaml_style")});
614         }
615         public IRubyObject method5(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
616             return self.getRuntime().newString("tag:yaml.org,2002:null");
617         }
618         public IRubyObject method6(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
619             return null;
620         }
621         public IRubyObject method7(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
622             return null;
623         }
624         public IRubyObject method8(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
625             return null;
626         }
627         public IRubyObject method9(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
628             return null;
629         }
630     }
631 }// RubyYAML
632
Popular Tags