jvyaml 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ #
2
+ # JvYAML::Store
3
+ #
4
+ require 'jvyaml'
5
+ require 'pstore'
6
+
7
+ class JvYAML::Store < PStore
8
+ def initialize( *o )
9
+ @opt = JvYAML::DEFAULTS.dup
10
+ if String === o.first
11
+ super(o.shift)
12
+ end
13
+ if o.last.is_a? Hash
14
+ @opt.update(o.pop)
15
+ end
16
+ end
17
+
18
+ def dump(table)
19
+ @table.to_jvyaml(@opt)
20
+ end
21
+
22
+ def load(content)
23
+ JvYAML::load(content)
24
+ end
25
+
26
+ def load_file(file)
27
+ JvYAML::load(file)
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ # this is a dummy file for those apps (like RubyGems) that explicitly require 'yaml/syck'
2
+
3
+ module JvYAML
4
+ module Syck
5
+ Map = ::JvYAML::JvYAMLi::Map
6
+ Seq = ::JvYAML::JvYAMLi::Seq
7
+ Scalar = ::JvYAML::JvYAMLi::Scalar
8
+ end
9
+ end
Binary file
data/lib/jvyamlb.jar ADDED
Binary file
@@ -0,0 +1,23 @@
1
+ def java_classpath_arg # myriad of ways to discover JRuby classpath
2
+ begin
3
+ cpath = Java::java.lang.System.getProperty('java.class.path').split(File::PATH_SEPARATOR)
4
+ cpath += Java::java.lang.System.getProperty('sun.boot.class.path').split(File::PATH_SEPARATOR)
5
+ jruby_cpath = cpath.compact.join(File::PATH_SEPARATOR)
6
+ rescue => e
7
+ end
8
+ unless jruby_cpath
9
+ jruby_cpath = ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] &&
10
+ FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR)
11
+ end
12
+ jruby_cpath ? "-cp \"#{jruby_cpath}\":lib/jvyamlb.jar" : ""
13
+ end
14
+
15
+ desc "Compile the native Java code."
16
+ task :java_compile do
17
+ pkg_classes = File.join(*%w(pkg classes))
18
+ jar_name = File.join(*%w(lib jvyaml_internal.jar))
19
+ mkdir_p pkg_classes
20
+ sh "javac -target 1.5 -source 1.5 -d pkg/classes #{java_classpath_arg} #{FileList['src/java/**/*.java'].join(' ')}"
21
+ sh "jar cf #{jar_name} -C #{pkg_classes} ."
22
+ end
23
+ file "lib/jvyaml_internal.jar" => :java_compile
@@ -0,0 +1,27 @@
1
+ MANIFEST = FileList["Manifest.txt", "README.txt", "Rakefile", "lib/**/*.rb", "lib/jvyamlb.jar", "lib/jvyaml_internal.jar", "test/**/*.rb", "lib/**/*.rake", "src/**/*.java", "rakelib/*.rake"]
2
+
3
+ file "Manifest.txt" => :manifest
4
+ task :manifest do
5
+ File.open("Manifest.txt", "w") {|f| MANIFEST.each {|n| f << "#{n}\n"} }
6
+ end
7
+ Rake::Task['manifest'].invoke # Always regen manifest, so Hoe has up-to-date list of files
8
+
9
+ begin
10
+ require 'hoe'
11
+ hoe = Hoe.spec("jvyaml") do |p|
12
+ p.version = "0.0.1"
13
+ p.url = "http://github.com/olabini/jvyaml-gem"
14
+ p.author = "Ola Bini"
15
+ p.email = "ola.bini@gmail.com"
16
+ p.summary = "Alternative YAML engine for JRuby"
17
+ end
18
+ hoe.spec.files = MANIFEST
19
+ hoe.spec.dependencies.delete_if { |dep| dep.name == "hoe" }
20
+ rescue LoadError => le
21
+ puts le.to_s, *le.backtrace
22
+ puts "Problem loading Hoe; please check the error above to ensure that Hoe is installed correctly"
23
+ end
24
+
25
+ def rake(*args)
26
+ ruby "-S", "rake", *args
27
+ end
data/rakelib/test.rake ADDED
@@ -0,0 +1,3 @@
1
+ Rake::TestTask.new do |t|
2
+ t.test_files = FileList['test/**/test*.rb']
3
+ end
@@ -0,0 +1,14 @@
1
+ /*
2
+ * See LICENSE file in distribution for copyright and licensing information.
3
+ */
4
+ import java.io.IOException;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.runtime.load.BasicLibraryService;
8
+
9
+ public class JvyamlInternalService implements BasicLibraryService {
10
+ public boolean basicLoad(final Ruby runtime) throws IOException {
11
+ org.jruby.ext.jvyaml.RubyYAML.createYAMLModule(runtime);
12
+ return true;
13
+ }
14
+ }
@@ -0,0 +1,679 @@
1
+ /*
2
+ * See LICENSE file in distribution for copyright and licensing information.
3
+ */
4
+ package org.jruby.ext.jvyaml;
5
+
6
+ import java.util.HashMap;
7
+ import java.util.Iterator;
8
+ import java.util.List;
9
+ import java.util.Map;
10
+ import java.util.Set;
11
+
12
+ import java.util.regex.Pattern;
13
+
14
+ import org.jvyamlb.Composer;
15
+ import org.jvyamlb.Constructor;
16
+ import org.jvyamlb.exceptions.ConstructorException;
17
+ import org.jvyamlb.ConstructorImpl;
18
+ import org.jvyamlb.SafeConstructorImpl;
19
+ import org.jvyamlb.Scanner;
20
+ import org.jvyamlb.ScannerImpl;
21
+ import org.jvyamlb.ComposerImpl;
22
+ import org.jvyamlb.ParserImpl;
23
+ import org.jvyamlb.ResolverImpl;
24
+ import org.jvyamlb.YAML;
25
+
26
+ import org.jvyamlb.nodes.Node;
27
+ import org.jvyamlb.nodes.LinkNode;
28
+
29
+
30
+ import org.jruby.Ruby;
31
+ import org.jruby.RubyArray;
32
+ import org.jruby.RubyClass;
33
+ import org.jruby.RubyModule;
34
+ import org.jruby.RubyObject;
35
+ import org.jruby.RubyHash;
36
+ import org.jruby.RubyString;
37
+ import org.jruby.RubyStruct;
38
+ import org.jruby.RubyRange;
39
+ import org.jruby.javasupport.util.RuntimeHelpers;
40
+ import org.jruby.runtime.Block;
41
+ import org.jruby.runtime.builtin.IRubyObject;
42
+
43
+ import org.jruby.util.ByteList;
44
+
45
+ import org.joda.time.DateTime;
46
+ import org.jruby.runtime.ThreadContext;
47
+ import org.jvyamlb.nodes.ScalarNode;
48
+
49
+ /**
50
+ * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
51
+ */
52
+ public class JRubyConstructor extends ConstructorImpl {
53
+ private final static Map yamlConstructors = new HashMap();
54
+ private final static Map yamlMultiConstructors = new HashMap();
55
+ private final static Map yamlMultiRegexps = new HashMap();
56
+ public YamlConstructor getYamlConstructor(final Object key) {
57
+ return (YamlConstructor)yamlConstructors.get(key);
58
+ }
59
+
60
+ public YamlMultiConstructor getYamlMultiConstructor(final Object key) {
61
+ return (YamlMultiConstructor)yamlMultiConstructors.get(key);
62
+ }
63
+
64
+ public Pattern getYamlMultiRegexp(final Object key) {
65
+ return (Pattern)yamlMultiRegexps.get(key);
66
+ }
67
+
68
+ public Set getYamlMultiRegexps() {
69
+ return yamlMultiRegexps.keySet();
70
+ }
71
+
72
+ public static void addConstructor(final String tag, final YamlConstructor ctor) {
73
+ yamlConstructors.put(tag,ctor);
74
+ }
75
+
76
+ public static void addMultiConstructor(final String tagPrefix, final YamlMultiConstructor ctor) {
77
+ yamlMultiConstructors.put(tagPrefix,ctor);
78
+ yamlMultiRegexps.put(tagPrefix,Pattern.compile("^"+tagPrefix));
79
+ }
80
+
81
+ private final Ruby runtime;
82
+
83
+ public JRubyConstructor(final IRubyObject receiver, final Composer composer) {
84
+ this(receiver.getRuntime(), composer);
85
+ }
86
+
87
+ public JRubyConstructor(final Ruby runtime, final Composer composer) {
88
+ super(composer);
89
+ this.runtime = runtime;
90
+ }
91
+
92
+ public Object constructRubyScalar(final Node node) {
93
+ if(node instanceof org.jvyamlb.nodes.ScalarNode) {
94
+ ByteList sc = (ByteList)super.constructScalar(node);
95
+ if(sc.length() > 1 && sc.charAt(0) == ':' && ((org.jvyamlb.nodes.ScalarNode)node).getStyle() == 0) {
96
+ int first = sc.get(1);
97
+ int last = sc.get(sc.realSize-1);
98
+ if((first == '"' && last == '"') ||
99
+ (first == '\'' && last == '\'')) {
100
+
101
+ Scanner scn = new ScannerImpl(sc.makeShared(1, sc.realSize-1));
102
+ Constructor ctor = new JRubyConstructor(runtime, new ComposerImpl(new ParserImpl(scn,YAML.config().version("1.0")),new ResolverImpl()));
103
+ ctor.checkData();
104
+ return ((RubyString)ctor.getData()).intern();
105
+ }
106
+
107
+ return runtime.newSymbol(new String(sc.bytes, sc.begin+1, sc.realSize-1));
108
+ }
109
+
110
+ return RubyString.newString(runtime,(ByteList)super.constructScalar(node));
111
+ } else {
112
+ // Assume it's a mapping node
113
+
114
+ Map val = (Map)(constructMapping(node));
115
+ RubyString str = (RubyString)val.get(runtime.newString("str"));
116
+
117
+ Map props = new HashMap();
118
+ for(Iterator iter = val.entrySet().iterator();iter.hasNext();) {
119
+ Map.Entry em = (Map.Entry)iter.next();
120
+ if(em.getKey().toString().startsWith("@")) {
121
+ props.put(em.getKey(),em.getValue());
122
+ iter.remove();
123
+ }
124
+ }
125
+ for(Iterator iter = props.entrySet().iterator();iter.hasNext();) {
126
+ Map.Entry em = (Map.Entry)iter.next();
127
+ str.instance_variable_set((IRubyObject)em.getKey(),(IRubyObject)em.getValue());
128
+ }
129
+
130
+ return str;
131
+ }
132
+ }
133
+
134
+ public Object constructPrivateType(final Node node) {
135
+ Object val = null;
136
+ if(node.getValue() instanceof Map) {
137
+ val = constructRubyMapping(node);
138
+ } else if(node.getValue() instanceof List) {
139
+ val = constructRubySequence(node);
140
+ } else {
141
+ val = constructRubyScalar(node);
142
+ }
143
+ return RuntimeHelpers.invoke(
144
+ runtime.getCurrentContext(),
145
+ runtime.fastGetModule("JvYAML").fastGetConstant("PrivateType"),
146
+ "new", runtime.newString(node.getTag()),(IRubyObject)val);
147
+ }
148
+
149
+ public Object constructRubySequence(final Node node) {
150
+ final RubyArray arr = runtime.newArray();
151
+ List l = (List)super.constructSequence(node);
152
+ doRecursionFix(node, arr);
153
+ int i = 0;
154
+ for(Iterator iter = l.iterator();iter.hasNext();i++) {
155
+ Object oo = iter.next();
156
+ if(oo instanceof LinkNode) {
157
+ arr.append(runtime.getNil());
158
+ final IRubyObject ix = runtime.newFixnum(i);
159
+ addFixer((Node)(((LinkNode)oo).getValue()), new RecursiveFixer() {
160
+ public void replace(Node node, Object real) {
161
+ arr.aset(ix, (IRubyObject)real);
162
+ }
163
+ });
164
+ } else {
165
+ arr.append((IRubyObject)oo);
166
+ }
167
+ }
168
+ return arr;
169
+ }
170
+
171
+ public Object constructRubyMapping(final Node node) {
172
+ RubyHash h1 = RubyHash.newHash(runtime, (Map)super.constructMapping(node), runtime.getNil());
173
+ return h1;
174
+ }
175
+
176
+ public Object constructRubyPairs(final Node node) {
177
+ return runtime.newArray((List)super.constructPairs(node));
178
+ }
179
+
180
+ public static Object constructYamlNull(final Constructor ctor, final Node node) {
181
+ return ((JRubyConstructor)ctor).runtime.getNil();
182
+ }
183
+
184
+ public static Object constructYamlBool(final Constructor ctor, final Node node) {
185
+ return SafeConstructorImpl.constructYamlBool(ctor,node) == Boolean.TRUE ? ((JRubyConstructor)ctor).runtime.getTrue() : ((JRubyConstructor)ctor).runtime.getFalse();
186
+ }
187
+
188
+ public static Object constructYamlOmap(final Constructor ctor, final Node node) {
189
+ Ruby runtime = ((JRubyConstructor)ctor).runtime;
190
+ RubyArray arr = (RubyArray)(runtime.fastGetModule("JvYAML").fastGetConstant("Omap").callMethod(runtime.getCurrentContext(),"new"));
191
+ List l = (List)ctor.constructSequence(node);
192
+ ctor.doRecursionFix(node, arr);
193
+ for(Iterator iter = l.iterator();iter.hasNext();) {
194
+ IRubyObject v = (IRubyObject)iter.next();
195
+ if(v instanceof RubyHash) {
196
+ arr.concat(((RubyHash)v).to_a());
197
+ } else {
198
+ throw new ConstructorException(null,"Invalid !omap entry: " + l,null);
199
+ }
200
+ }
201
+ return arr;
202
+ }
203
+
204
+ public static Object constructYamlPairs(final Constructor ctor, final Node node) {
205
+ return ((JRubyConstructor)ctor).constructRubyPairs(node);
206
+ }
207
+
208
+ public static Object constructYamlSet(final Constructor ctor, final Node node) {
209
+ return SafeConstructorImpl.constructYamlSet(ctor,node);
210
+ }
211
+
212
+ public static Object constructYamlStr(final Constructor ctor, final Node node) {
213
+ final Object _str = ((JRubyConstructor)ctor).constructRubyScalar(node);
214
+ if(_str instanceof RubyString) {
215
+ final RubyString str = (RubyString)_str;
216
+ if (node instanceof ScalarNode) {
217
+ return (str.getByteList().realSize == 0 && ((ScalarNode)node).getStyle() == 0) ? str.getRuntime().getNil() : str;
218
+ }
219
+ }
220
+ return _str;
221
+ }
222
+
223
+ public static Object constructYamlSeq(final Constructor ctor, final Node node) {
224
+ return ((JRubyConstructor)ctor).constructRubySequence(node);
225
+ }
226
+
227
+ public static Object constructYamlMap(final Constructor ctor, final Node node) {
228
+ return ((JRubyConstructor)ctor).constructRubyMapping(node);
229
+ }
230
+
231
+ public static Object constructUndefined(final Constructor ctor, final Node node) {
232
+ throw new ConstructorException(null,"could not determine a constructor for the tag " + node.getTag(),null);
233
+ }
234
+
235
+ public static Object constructYamlTimestamp(final Constructor ctor, final Node node) {
236
+ Object[] value = (Object[])SafeConstructorImpl.constructYamlTimestamp(ctor,node);
237
+ DateTime dt = (DateTime)(value[0]);
238
+ org.jruby.RubyTime rt = org.jruby.RubyTime.newTime(((JRubyConstructor)ctor).runtime,dt);
239
+ rt.setUSec(((Integer)value[1]));
240
+ return rt;
241
+ }
242
+
243
+ public static Object constructYamlTimestampYMD(final Constructor ctor, final Node node) {
244
+ DateTime dt = (DateTime)(((Object[])SafeConstructorImpl.constructYamlTimestamp(ctor,node))[0]);
245
+ Ruby runtime = ((JRubyConstructor)ctor).runtime;
246
+ return RuntimeHelpers.invoke(runtime.getCurrentContext(), runtime.fastGetClass("Date"), "new", runtime.newFixnum(dt.getYear()),runtime.newFixnum(dt.getMonthOfYear()),runtime.newFixnum(dt.getDayOfMonth()));
247
+ }
248
+
249
+ public static Object constructYamlInt(final Constructor ctor, final Node node) {
250
+ return org.jruby.javasupport.JavaUtil.convertJavaToRuby(((JRubyConstructor)ctor).runtime,SafeConstructorImpl.constructYamlInt(ctor,node));
251
+ }
252
+ public static Object constructYamlFloat(final Constructor ctor, final Node node) {
253
+ return ((JRubyConstructor)ctor).runtime.newFloat(((Double)SafeConstructorImpl.constructYamlFloat(ctor,node)).doubleValue());
254
+ }
255
+ public static Object constructYamlBinary(final Constructor ctor, final Node node) {
256
+ Object b = SafeConstructorImpl.constructYamlBinary(ctor,node);
257
+ if(b instanceof byte[]) {
258
+ return RubyString.newString(((JRubyConstructor)ctor).runtime, new ByteList((byte[])b,false));
259
+ } else {
260
+ return ((JRubyConstructor)ctor).runtime.newString((String)b);
261
+ }
262
+ }
263
+
264
+ public static Object constructJava(final Constructor ctor, final String pref, final Node node) {
265
+ return SafeConstructorImpl.constructJava(ctor,pref,node);
266
+ }
267
+
268
+ public static Object constructRubyException(final Constructor ctor, final String tag, final Node node) {
269
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
270
+ RubyModule objClass = runtime.getObject();
271
+ if(tag != null) {
272
+ final String[] nms = tag.split("::");
273
+ try {
274
+ for(int i=0,j=nms.length;i<j;i++) {
275
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
276
+ }
277
+ } catch(Exception e) {
278
+ // No constant available, so we'll fall back on YAML::Object
279
+ objClass = (RubyClass)runtime.fastGetModule("JvYAML").fastGetConstant("Object");
280
+ final RubyHash vars = (RubyHash)(((JRubyConstructor)ctor).constructRubyMapping(node));
281
+ return RuntimeHelpers.invoke(runtime.getCurrentContext(), objClass, "new", runtime.newString(tag), vars);
282
+ }
283
+ }
284
+ final RubyClass theCls = (RubyClass)objClass;
285
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
286
+ final Map vars = (Map)(ctor.constructMapping(node));
287
+ ctor.doRecursionFix(node, oo);
288
+ for(final Iterator<Map.Entry> iter = vars.entrySet().iterator();iter.hasNext();) {
289
+ Map.Entry entry = iter.next();
290
+ final IRubyObject key = (IRubyObject)entry.getKey();
291
+ final Object val = vars.get(key);
292
+ if(val instanceof LinkNode) {
293
+ final String KEY = "@" + key.toString();
294
+ ctor.addFixer((Node)(((LinkNode)val).getValue()), new RecursiveFixer() {
295
+ public void replace(Node node, Object real) {
296
+ oo.setInstanceVariable(KEY,(IRubyObject)real);
297
+ }
298
+ });
299
+ } else {
300
+ oo.setInstanceVariable("@" + key.toString(),(IRubyObject)val);
301
+ }
302
+ }
303
+ return oo;
304
+ }
305
+
306
+ public static Object constructRubyStruct(final Constructor ctor, final String tag, final Node node) {
307
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
308
+ RubyModule sClass = runtime.fastGetModule("Struct");
309
+ RubyClass struct_type;
310
+ String[] nms = tag.split("::");
311
+ for(int i=0,j=nms.length;i<j && sClass != null;i++) {
312
+ sClass = (RubyModule)sClass.getConstant(nms[i]);
313
+ }
314
+
315
+ Map props = new HashMap();
316
+ Map val = (Map)(ctor.constructMapping(node));
317
+ for(Iterator iter = val.entrySet().iterator();iter.hasNext();) {
318
+ Map.Entry em = (Map.Entry)iter.next();
319
+ if(em.getKey().toString().startsWith("@")) {
320
+ props.put(em.getKey(),em.getValue());
321
+ iter.remove();
322
+ }
323
+ }
324
+
325
+ // If no such struct exists...
326
+ if(sClass == null) {
327
+ IRubyObject[] params = new IRubyObject[val.size()+1];
328
+ params[0] = runtime.newString(tag);
329
+ int i = 1;
330
+ for(Iterator iter = val.entrySet().iterator();iter.hasNext();i++) {
331
+ Map.Entry em = (Map.Entry)iter.next();
332
+ params[i] = ((RubyString)em.getKey()).intern();
333
+ }
334
+ struct_type = RubyStruct.newInstance(runtime.fastGetModule("Struct"),params,Block.NULL_BLOCK);
335
+ } else {
336
+ struct_type = (RubyClass)sClass;
337
+ }
338
+ IRubyObject st = struct_type.callMethod(runtime.getCurrentContext(),"new");
339
+ RubyArray members = RubyStruct.members(struct_type,Block.NULL_BLOCK);
340
+ for(int i=0,j=members.size();i<j;i++) {
341
+ IRubyObject m = members.eltInternal(i);
342
+ st.callMethod(runtime.getCurrentContext(), m.toString() + "=", (IRubyObject)val.get(m));
343
+ }
344
+ for(Iterator iter = props.entrySet().iterator();iter.hasNext();) {
345
+ Map.Entry em = (Map.Entry)iter.next();
346
+ ((RubyObject)st).instance_variable_set((IRubyObject)em.getKey(),(IRubyObject)em.getValue());
347
+ }
348
+ return st;
349
+ }
350
+
351
+ public static Object constructRuby(final Constructor ctor, final RubyClass theCls, final Node node) {
352
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
353
+ if(theCls.respondsTo("jvyaml_new")) {
354
+ final RubyHash vars = (RubyHash)(((JRubyConstructor)ctor).constructRubyMapping(node));
355
+ return RuntimeHelpers.invoke(runtime.getCurrentContext(), theCls, "jvyaml_new", theCls, runtime.newString(node.getTag()), vars);
356
+ } else {
357
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
358
+ if (oo.respondsTo("jvyaml_initialize")) {
359
+ RubyHash vars = (RubyHash)(((JRubyConstructor)ctor).constructRubyMapping(node));
360
+ RuntimeHelpers.invoke(runtime.getCurrentContext(), oo, "jvyaml_initialize", runtime.newString(node.getTag()), vars);
361
+ } else {
362
+ final Map vars = (Map)(ctor.constructMapping(node));
363
+ ctor.doRecursionFix(node, oo);
364
+ for(final Iterator iter = vars.keySet().iterator();iter.hasNext();) {
365
+ final IRubyObject key = (IRubyObject)iter.next();
366
+ final Object val = vars.get(key);
367
+ if(val instanceof LinkNode) {
368
+ final String KEY = "@" + key.toString();
369
+ ctor.addFixer((Node)(((LinkNode)val).getValue()), new RecursiveFixer() {
370
+ public void replace(Node node, Object real) {
371
+ oo.setInstanceVariable(KEY,(IRubyObject)real);
372
+ }
373
+ });
374
+ } else {
375
+ oo.setInstanceVariable("@" + key.toString(),(IRubyObject)val);
376
+ }
377
+ }
378
+ }
379
+ return oo;
380
+ }
381
+ }
382
+
383
+ public static Object constructRuby(final Constructor ctor, final String tag, final Node node) {
384
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
385
+ RubyModule objClass = runtime.getObject();
386
+ if(tag != null) {
387
+ final String[] nms = tag.split("::");
388
+ try {
389
+ for(int i=0,j=nms.length;i<j;i++) {
390
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
391
+ }
392
+ } catch(Exception e) {
393
+ // No constant available, so we'll fall back on YAML::Object
394
+ objClass = (RubyClass)runtime.fastGetModule("JvYAML").fastGetConstant("Object");
395
+ final RubyHash vars = (RubyHash)(((JRubyConstructor)ctor).constructRubyMapping(node));
396
+ return RuntimeHelpers.invoke(runtime.getCurrentContext(), objClass, "new", runtime.newString(tag), vars);
397
+ }
398
+ }
399
+ final RubyClass theCls = (RubyClass)objClass;
400
+ return constructRuby(ctor,theCls,node);
401
+ }
402
+
403
+ public static Object constructRubyRegexp(final Constructor ctor, final Node node) {
404
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
405
+ String s1 = ctor.constructScalar(node).toString();
406
+ // This should be fixed in some way
407
+ return runtime.evalScriptlet(s1);
408
+ }
409
+
410
+ public static Object constructRubyRange(final Constructor ctor, final Node node) {
411
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
412
+ ThreadContext context = runtime.getCurrentContext();
413
+ if (node instanceof org.jvyamlb.nodes.ScalarNode) {
414
+ String s1 = ctor.constructScalar(node).toString();
415
+ String first;
416
+ String second;
417
+ boolean exc = false;
418
+ int ix = -1;
419
+ if((ix = s1.indexOf("...")) != -1) {
420
+ first = s1.substring(0,ix);
421
+ second = s1.substring(ix+3);
422
+ exc = true;
423
+ } else {
424
+ ix = s1.indexOf("..");
425
+ first = s1.substring(0,ix);
426
+ second = s1.substring(ix+2);
427
+ }
428
+ IRubyObject fist = runtime.fastGetModule("JvYAML").callMethod(context,"load",runtime.newString(first));
429
+ IRubyObject sic = runtime.fastGetModule("JvYAML").callMethod(context,"load",runtime.newString(second));
430
+ return RubyRange.newRange(runtime, context, fist, sic, exc);
431
+ } else {
432
+ final Map vars = (Map)(ctor.constructMapping(node));
433
+ IRubyObject beg = (IRubyObject)vars.get(runtime.newString("begin"));
434
+ IRubyObject end = (IRubyObject)vars.get(runtime.newString("end"));
435
+ boolean excl = ((IRubyObject)vars.get(runtime.newString("excl"))).isTrue();
436
+ return RubyRange.newRange(runtime, context, beg, end, excl);
437
+ }
438
+ }
439
+
440
+ public static Object findAndCreateFromCustomTagging(final Constructor ctor, final Node node) {
441
+ String tag = node.getTag();
442
+ Ruby runtime = ((JRubyConstructor)ctor).runtime;
443
+ IRubyObject _cl = runtime.fastGetModule("JvYAML").callMethod(runtime.getCurrentContext(), "tagged_classes").callMethod(runtime.getCurrentContext(),"[]", runtime.newString(tag));
444
+ if(!(_cl instanceof RubyClass)) {
445
+ return null;
446
+ }
447
+ RubyClass clazz = (RubyClass)_cl;
448
+ if(clazz != null && !clazz.isNil()) {
449
+ return constructRuby(ctor, clazz, node);
450
+ }
451
+ return null;
452
+ }
453
+
454
+ public static Object constructRubyInt(final Constructor ctor, final String tag, final Node node) {
455
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
456
+ RubyModule objClass = runtime.getObject();
457
+ if(tag != null) {
458
+ final String[] nms = tag.split("::");
459
+ for(int i=0,j=nms.length;i<j;i++) {
460
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
461
+ }
462
+ }
463
+ final RubyClass theCls = (RubyClass)objClass;
464
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
465
+ final IRubyObject val = (IRubyObject)constructYamlInt(ctor, node);
466
+ oo.callInit(new IRubyObject[]{val},org.jruby.runtime.Block.NULL_BLOCK);
467
+ return oo;
468
+ }
469
+
470
+ public static Object constructRubyString(final Constructor ctor, final String tag, final Node node) {
471
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
472
+ RubyModule objClass = runtime.getObject();
473
+ if(tag != null) {
474
+ final String[] nms = tag.split("::");
475
+ for(int i=0,j=nms.length;i<j;i++) {
476
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
477
+ }
478
+ }
479
+ final RubyClass theCls = (RubyClass)objClass;
480
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
481
+ final IRubyObject val = (IRubyObject)constructYamlStr(ctor, node);
482
+ oo.callInit(new IRubyObject[]{val},org.jruby.runtime.Block.NULL_BLOCK);
483
+ return oo;
484
+ }
485
+
486
+ public static Object constructRubyMap(final Constructor ctor, final String tag, final Node node) {
487
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
488
+ RubyModule objClass = runtime.getObject();
489
+ if(tag != null) {
490
+ final String[] nms = tag.split("::");
491
+ for(int i=0,j=nms.length;i<j;i++) {
492
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
493
+ }
494
+ }
495
+ final RubyClass theCls = (RubyClass)objClass;
496
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
497
+ final Map vars = (Map)(ctor.constructMapping(node));
498
+ for(final Iterator iter = vars.keySet().iterator();iter.hasNext();) {
499
+ final IRubyObject key = (IRubyObject)iter.next();
500
+ RuntimeHelpers.invoke(oo.getRuntime().getCurrentContext(), oo, "[]=", key, (IRubyObject)vars.get(key));
501
+ }
502
+ return oo;
503
+ }
504
+
505
+ public static Object constructRubySequence(final Constructor ctor, final String tag, final Node node) {
506
+ final Ruby runtime = ((JRubyConstructor)ctor).runtime;
507
+ RubyModule objClass = runtime.getObject();
508
+ if(tag != null) {
509
+ final String[] nms = tag.split("::");
510
+ for(int i=0,j=nms.length;i<j;i++) {
511
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
512
+ }
513
+ }
514
+ final RubyClass theCls = (RubyClass)objClass;
515
+ final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
516
+ final List vars = (List)(ctor.constructSequence(node));
517
+ for(final Iterator iter = vars.iterator();iter.hasNext();) {
518
+ RuntimeHelpers.invoke(oo.getRuntime().getCurrentContext(), oo, "<<", (IRubyObject)iter.next());
519
+ }
520
+ return oo;
521
+ }
522
+
523
+ static {
524
+ addConstructor("tag:yaml.org,2002:null",new YamlConstructor() {
525
+ public Object call(final Constructor self, final Node node) {
526
+ return constructYamlNull(self,node);
527
+ }
528
+ });
529
+ addConstructor("tag:yaml.org,2002:bool",new YamlConstructor() {
530
+ public Object call(final Constructor self, final Node node) {
531
+ return constructYamlBool(self,node);
532
+ }
533
+ });
534
+ addConstructor("tag:yaml.org,2002:omap",new YamlConstructor() {
535
+ public Object call(final Constructor self, final Node node) {
536
+ return constructYamlOmap(self,node);
537
+ }
538
+ });
539
+ addConstructor("tag:yaml.org,2002:pairs",new YamlConstructor() {
540
+ public Object call(final Constructor self, final Node node) {
541
+ return constructYamlPairs(self,node);
542
+ }
543
+ });
544
+ addConstructor("tag:yaml.org,2002:set",new YamlConstructor() {
545
+ public Object call(final Constructor self, final Node node) {
546
+ return constructYamlSet(self,node);
547
+ }
548
+ });
549
+ addConstructor("tag:yaml.org,2002:int",new YamlConstructor() {
550
+ public Object call(final Constructor self, final Node node) {
551
+ return constructYamlInt(self,node);
552
+ }
553
+ });
554
+ addConstructor("tag:yaml.org,2002:float",new YamlConstructor() {
555
+ public Object call(final Constructor self, final Node node) {
556
+ return constructYamlFloat(self,node);
557
+ }
558
+ });
559
+ addConstructor("tag:yaml.org,2002:timestamp",new YamlConstructor() {
560
+ public Object call(final Constructor self, final Node node) {
561
+ java.util.regex.Matcher match = SafeConstructorImpl.YMD_REGEXP.matcher(node.getValue().toString());
562
+ if(match.matches()) {
563
+ return constructYamlTimestampYMD(self,node);
564
+ } else {
565
+ return constructYamlTimestamp(self,node);
566
+ }
567
+ }
568
+ });
569
+ addConstructor("tag:yaml.org,2002:timestamp#ymd",new YamlConstructor() {
570
+ public Object call(final Constructor self, final Node node) {
571
+ return constructYamlTimestampYMD(self,node);
572
+ }
573
+ });
574
+ addConstructor("tag:yaml.org,2002:str",new YamlConstructor() {
575
+ public Object call(final Constructor self, final Node node) {
576
+ return constructYamlStr(self,node);
577
+ }
578
+ });
579
+ addConstructor("tag:yaml.org,2002:binary",new YamlConstructor() {
580
+ public Object call(final Constructor self, final Node node) {
581
+ return constructYamlBinary(self,node);
582
+ }
583
+ });
584
+ addConstructor("tag:yaml.org,2002:seq",new YamlConstructor() {
585
+ public Object call(final Constructor self, final Node node) {
586
+ return constructYamlSeq(self,node);
587
+ }
588
+ });
589
+ addConstructor("tag:yaml.org,2002:map",new YamlConstructor() {
590
+ public Object call(final Constructor self, final Node node) {
591
+ return constructYamlMap(self,node);
592
+ }
593
+ });
594
+ addConstructor("tag:ruby.yaml.org,2002:range",new YamlConstructor() {
595
+ public Object call(final Constructor self, final Node node) {
596
+ return constructRubyRange(self,node);
597
+ }
598
+ });
599
+ addConstructor("tag:ruby.yaml.org,2002:regexp",new YamlConstructor() {
600
+ public Object call(final Constructor self, final Node node) {
601
+ return constructRubyRegexp(self,node);
602
+ }
603
+ });
604
+ addConstructor(null,new YamlConstructor() {
605
+ public Object call(final Constructor self, final Node node) {
606
+ Object v1 = findAndCreateFromCustomTagging(self, node);
607
+ if(null != v1) {
608
+ return v1;
609
+ }
610
+ return self.constructPrivateType(node);
611
+ }
612
+ });
613
+ addMultiConstructor("tag:yaml.org,2002:map:",new YamlMultiConstructor() {
614
+ public Object call(final Constructor self, final String pref, final Node node) {
615
+ return constructRubyMap(self,pref,node);
616
+ }
617
+ });
618
+ addMultiConstructor("tag:ruby.yaml.org,2002:hash:",new YamlMultiConstructor() {
619
+ public Object call(final Constructor self, final String pref, final Node node) {
620
+ return constructRubyMap(self,pref,node);
621
+ }
622
+ });
623
+ addMultiConstructor("tag:yaml.org,2002:int:",new YamlMultiConstructor() {
624
+ public Object call(final Constructor self, final String pref, final Node node) {
625
+ return constructRubyInt(self,pref,node);
626
+ }
627
+ });
628
+ addMultiConstructor("tag:yaml.org,2002:seq:",new YamlMultiConstructor() {
629
+ public Object call(final Constructor self, final String pref, final Node node) {
630
+ return constructRubySequence(self,pref,node);
631
+ }
632
+ });
633
+ addMultiConstructor("tag:ruby.yaml.org,2002:array:",new YamlMultiConstructor() {
634
+ public Object call(final Constructor self, final String pref, final Node node) {
635
+ return constructRubySequence(self,pref,node);
636
+ }
637
+ });
638
+ addMultiConstructor("tag:yaml.org,2002:str:",new YamlMultiConstructor() {
639
+ public Object call(final Constructor self, final String pref, final Node node) {
640
+ return constructRubyString(self,pref,node);
641
+ }
642
+ });
643
+ addMultiConstructor("tag:ruby.yaml.org,2002:string:",new YamlMultiConstructor() {
644
+ public Object call(final Constructor self, final String pref, final Node node) {
645
+ return constructRubyString(self,pref,node);
646
+ }
647
+ });
648
+ addMultiConstructor("tag:yaml.org,2002:ruby/object:",new YamlMultiConstructor() {
649
+ public Object call(final Constructor self, final String pref, final Node node) {
650
+ return constructRuby(self,pref,node);
651
+ }
652
+ });
653
+ addMultiConstructor("tag:ruby.yaml.org,2002:object:",new YamlMultiConstructor() {
654
+ public Object call(final Constructor self, final String pref, final Node node) {
655
+ return constructRuby(self,pref,node);
656
+ }
657
+ });
658
+ addMultiConstructor("tag:yaml.org,2002:java/object:",new YamlMultiConstructor() {
659
+ public Object call(final Constructor self, final String pref, final Node node) {
660
+ return constructJava(self,pref,node);
661
+ }
662
+ });
663
+ addMultiConstructor("tag:java.yaml.org,2002:object:",new YamlMultiConstructor() {
664
+ public Object call(final Constructor self, final String pref, final Node node) {
665
+ return constructJava(self,pref,node);
666
+ }
667
+ });
668
+ addMultiConstructor("tag:ruby.yaml.org,2002:struct:",new YamlMultiConstructor() {
669
+ public Object call(final Constructor self, final String pref, final Node node) {
670
+ return constructRubyStruct(self,pref,node);
671
+ }
672
+ });
673
+ addMultiConstructor("tag:ruby.yaml.org,2002:exception:",new YamlMultiConstructor() {
674
+ public Object call(final Constructor self, final String pref, final Node node) {
675
+ return constructRuby(self,pref,node);
676
+ }
677
+ });
678
+ }
679
+ }// JRubyConstructor