KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > yaml > JRubyConstructor


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.yaml;
29
30 import java.util.Date JavaDoc;
31 import java.util.Calendar JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import java.util.regex.Pattern JavaDoc;
39
40 import org.jvyamlb.Composer;
41 import org.jvyamlb.Constructor;
42 import org.jvyamlb.ConstructorException;
43 import org.jvyamlb.ConstructorImpl;
44 import org.jvyamlb.SafeConstructorImpl;
45
46 import org.jvyamlb.nodes.Node;
47
48 import org.jruby.Ruby;
49 import org.jruby.RubyClass;
50 import org.jruby.RubyModule;
51 import org.jruby.RubyObject;
52 import org.jruby.RubyHash;
53 import org.jruby.RubyString;
54 import org.jruby.runtime.builtin.IRubyObject;
55
56 import org.jruby.util.ByteList;
57
58 /**
59  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
60  */

61 public class JRubyConstructor extends ConstructorImpl {
62     private final static Map JavaDoc yamlConstructors = new HashMap JavaDoc();
63     private final static Map JavaDoc yamlMultiConstructors = new HashMap JavaDoc();
64     private final static Map JavaDoc yamlMultiRegexps = new HashMap JavaDoc();
65     public YamlConstructor getYamlConstructor(final Object JavaDoc key) {
66         return (YamlConstructor)yamlConstructors.get(key);
67     }
68
69     public YamlMultiConstructor getYamlMultiConstructor(final Object JavaDoc key) {
70         return (YamlMultiConstructor)yamlMultiConstructors.get(key);
71     }
72
73     public Pattern JavaDoc getYamlMultiRegexp(final Object JavaDoc key) {
74         return (Pattern JavaDoc)yamlMultiRegexps.get(key);
75     }
76
77     public Set JavaDoc getYamlMultiRegexps() {
78         return yamlMultiRegexps.keySet();
79     }
80
81     public static void addConstructor(final String JavaDoc tag, final YamlConstructor ctor) {
82         yamlConstructors.put(tag,ctor);
83     }
84
85     public static void addMultiConstructor(final String JavaDoc tagPrefix, final YamlMultiConstructor ctor) {
86         yamlMultiConstructors.put(tagPrefix,ctor);
87         yamlMultiRegexps.put(tagPrefix,Pattern.compile("^"+tagPrefix));
88     }
89
90     private final Ruby runtime;
91
92     public JRubyConstructor(final IRubyObject receiver, final Composer composer) {
93         super(composer);
94         this.runtime = receiver.getRuntime();
95     }
96
97     public Object JavaDoc constructRubyScalar(final Node node) {
98         return RubyString.newString(runtime,(ByteList)super.constructScalar(node));
99     }
100
101     public Object JavaDoc constructRubySequence(final Node node) {
102         return runtime.newArray((List JavaDoc)super.constructSequence(node));
103     }
104
105     public Object JavaDoc constructRubyMapping(final Node node) {
106         return RubyHash.newHash(runtime,(Map JavaDoc)super.constructMapping(node),runtime.getNil());
107     }
108
109     public Object JavaDoc constructRubyPairs(final Node node) {
110         return runtime.newArray((List JavaDoc)super.constructPairs(node));
111     }
112
113     public static Object JavaDoc constructYamlNull(final Constructor ctor, final Node node) {
114         return ((JRubyConstructor)ctor).runtime.getNil();
115     }
116     
117     public static Object JavaDoc constructYamlBool(final Constructor ctor, final Node node) {
118         return SafeConstructorImpl.constructYamlBool(ctor,node) == Boolean.TRUE ? ((JRubyConstructor)ctor).runtime.getTrue() : ((JRubyConstructor)ctor).runtime.getFalse();
119     }
120
121     public static Object JavaDoc constructYamlOmap(final Constructor ctor, final Node node) {
122         return ((JRubyConstructor)ctor).constructRubyPairs(node);
123     }
124
125     public static Object JavaDoc constructYamlPairs(final Constructor ctor, final Node node) {
126         return constructYamlOmap(ctor,node);
127     }
128
129     public static Object JavaDoc constructYamlSet(final Constructor ctor, final Node node) {
130         return SafeConstructorImpl.constructYamlSet(ctor,node);
131     }
132
133     public static Object JavaDoc constructYamlStr(final Constructor ctor, final Node node) {
134         final org.jruby.RubyString str = (org.jruby.RubyString)((JRubyConstructor)ctor).constructRubyScalar(node);
135         return (str.getValue().length() == 0 && ((org.jvyamlb.nodes.ScalarNode)node).getStyle() == 0) ? str.getRuntime().getNil() : str;
136     }
137
138     public static Object JavaDoc constructYamlSeq(final Constructor ctor, final Node node) {
139         return ((JRubyConstructor)ctor).constructRubySequence(node);
140     }
141
142     public static Object JavaDoc constructYamlMap(final Constructor ctor, final Node node) {
143         return ((JRubyConstructor)ctor).constructRubyMapping(node);
144     }
145
146     public static Object JavaDoc constructUndefined(final Constructor ctor, final Node node) {
147         throw new ConstructorException(null,"could not determine a constructor for the tag " + node.getTag(),null);
148     }
149
150     public static Object JavaDoc constructYamlTimestamp(final Constructor ctor, final Node node) {
151         return ((JRubyConstructor)ctor).runtime.newTime(((Date JavaDoc)SafeConstructorImpl.constructYamlTimestamp(ctor,node)).getTime()).callMethod(((JRubyConstructor)ctor).runtime.getCurrentContext(),"utc");
152     }
153
154     public static Object JavaDoc constructYamlTimestampYMD(final Constructor ctor, final Node node) {
155         Date JavaDoc d = (Date JavaDoc)SafeConstructorImpl.constructYamlTimestamp(ctor,node);
156         Calendar JavaDoc c = Calendar.getInstance();
157         c.setTime(d);
158         Ruby runtime = ((JRubyConstructor)ctor).runtime;
159         return runtime.getClass("Date").callMethod(runtime.getCurrentContext(),"new",new IRubyObject[]{runtime.newFixnum(c.get(Calendar.YEAR)),runtime.newFixnum(c.get(Calendar.MONTH)+1),runtime.newFixnum(c.get(Calendar.DAY_OF_MONTH)),});
160     }
161
162     public static Object JavaDoc constructYamlInt(final Constructor ctor, final Node node) {
163         return ((JRubyConstructor)ctor).runtime.newFixnum(((Long JavaDoc)SafeConstructorImpl.constructYamlInt(ctor,node)).longValue());
164     }
165     public static Object JavaDoc constructYamlFloat(final Constructor ctor, final Node node) {
166         return ((JRubyConstructor)ctor).runtime.newFloat(((Double JavaDoc)SafeConstructorImpl.constructYamlFloat(ctor,node)).doubleValue());
167     }
168     public static Object JavaDoc constructYamlBinary(final Constructor ctor, final Node node) {
169         return ((JRubyConstructor)ctor).runtime.newString(((String JavaDoc)SafeConstructorImpl.constructYamlBinary(ctor,node)));
170     }
171     public static Object JavaDoc constructJava(final Constructor ctor, final String JavaDoc pref, final Node node) {
172         return SafeConstructorImpl.constructJava(ctor,pref,node);
173     }
174     public static Object JavaDoc constructRuby(final Constructor ctor, final String JavaDoc tag, final Node node) {
175         final Ruby runtime = ((JRubyConstructor)ctor).runtime;
176         RubyModule objClass = runtime.getModule("Object");
177         if(tag != null) {
178             final String JavaDoc[] nms = tag.split("::");
179             for(int i=0,j=nms.length;i<j;i++) {
180                 objClass = (RubyModule)objClass.getConstant(nms[i]);
181             }
182         }
183         final RubyClass theCls = (RubyClass)objClass;
184         final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
185         final Map JavaDoc vars = (Map JavaDoc)(ctor.constructMapping(node));
186         for(final Iterator JavaDoc iter = vars.keySet().iterator();iter.hasNext();) {
187             final IRubyObject key = (IRubyObject)iter.next();
188             oo.setInstanceVariable("@" + key.toString(),(IRubyObject)vars.get(key));
189         }
190         return oo;
191     }
192
193     public static Object JavaDoc constructRubyMap(final Constructor ctor, final String JavaDoc tag, final Node node) {
194         final Ruby runtime = ((JRubyConstructor)ctor).runtime;
195         RubyModule objClass = runtime.getModule("Object");
196         if(tag != null) {
197             final String JavaDoc[] nms = tag.split("::");
198             for(int i=0,j=nms.length;i<j;i++) {
199                 objClass = (RubyModule)objClass.getConstant(nms[i]);
200             }
201         }
202         final RubyClass theCls = (RubyClass)objClass;
203         final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
204         final Map JavaDoc vars = (Map JavaDoc)(ctor.constructMapping(node));
205         for(final Iterator JavaDoc iter = vars.keySet().iterator();iter.hasNext();) {
206             final IRubyObject key = (IRubyObject)iter.next();
207             oo.callMethod(oo.getRuntime().getCurrentContext(),"[]=", new IRubyObject[]{key,(IRubyObject)vars.get(key)});
208         }
209         return oo;
210     }
211
212     public static Object JavaDoc constructRubySequence(final Constructor ctor, final String JavaDoc tag, final Node node) {
213         final Ruby runtime = ((JRubyConstructor)ctor).runtime;
214         RubyModule objClass = runtime.getModule("Object");
215         if(tag != null) {
216             final String JavaDoc[] nms = tag.split("::");
217             for(int i=0,j=nms.length;i<j;i++) {
218                 objClass = (RubyModule)objClass.getConstant(nms[i]);
219             }
220         }
221         final RubyClass theCls = (RubyClass)objClass;
222         final RubyObject oo = (RubyObject)theCls.getAllocator().allocate(runtime, theCls);
223         final List JavaDoc vars = (List JavaDoc)(ctor.constructSequence(node));
224         for(final Iterator JavaDoc iter = vars.iterator();iter.hasNext();) {
225             oo.callMethod(oo.getRuntime().getCurrentContext(),"<<", new IRubyObject[]{(IRubyObject)iter.next()});;
226         }
227         return oo;
228     }
229
230     static {
231         addConstructor("tag:yaml.org,2002:null",new YamlConstructor() {
232                 public Object JavaDoc call(final Constructor self, final Node node) {
233                     return constructYamlNull(self,node);
234                 }
235             });
236         addConstructor("tag:yaml.org,2002:bool",new YamlConstructor() {
237                 public Object JavaDoc call(final Constructor self, final Node node) {
238                     return constructYamlBool(self,node);
239                 }
240             });
241         addConstructor("tag:yaml.org,2002:omap",new YamlConstructor() {
242                 public Object JavaDoc call(final Constructor self, final Node node) {
243                     return constructYamlOmap(self,node);
244                 }
245             });
246         addConstructor("tag:yaml.org,2002:pairs",new YamlConstructor() {
247                 public Object JavaDoc call(final Constructor self, final Node node) {
248                     return constructYamlPairs(self,node);
249                 }
250             });
251         addConstructor("tag:yaml.org,2002:set",new YamlConstructor() {
252                 public Object JavaDoc call(final Constructor self, final Node node) {
253                     return constructYamlSet(self,node);
254                 }
255             });
256         addConstructor("tag:yaml.org,2002:int",new YamlConstructor() {
257                 public Object JavaDoc call(final Constructor self, final Node node) {
258                     return constructYamlInt(self,node);
259                 }
260             });
261         addConstructor("tag:yaml.org,2002:float",new YamlConstructor() {
262                 public Object JavaDoc call(final Constructor self, final Node node) {
263                     return constructYamlFloat(self,node);
264                 }
265             });
266         addConstructor("tag:yaml.org,2002:timestamp",new YamlConstructor() {
267                 public Object JavaDoc call(final Constructor self, final Node node) {
268                     return constructYamlTimestamp(self,node);
269                 }
270             });
271         addConstructor("tag:yaml.org,2002:timestamp#ymd",new YamlConstructor() {
272                 public Object JavaDoc call(final Constructor self, final Node node) {
273                     return constructYamlTimestampYMD(self,node);
274                 }
275             });
276         addConstructor("tag:yaml.org,2002:str",new YamlConstructor() {
277                 public Object JavaDoc call(final Constructor self, final Node node) {
278                     return constructYamlStr(self,node);
279                 }
280             });
281         addConstructor("tag:yaml.org,2002:binary",new YamlConstructor() {
282                 public Object JavaDoc call(final Constructor self, final Node node) {
283                     return constructYamlBinary(self,node);
284                 }
285             });
286         addConstructor("tag:yaml.org,2002:seq",new YamlConstructor() {
287                 public Object JavaDoc call(final Constructor self, final Node node) {
288                     return constructYamlSeq(self,node);
289                 }
290             });
291         addConstructor("tag:yaml.org,2002:map",new YamlConstructor() {
292                 public Object JavaDoc call(final Constructor self, final Node node) {
293                     return constructYamlMap(self,node);
294                 }
295             });
296         addConstructor(null,new YamlConstructor() {
297                 public Object JavaDoc call(final Constructor self, final Node node) {
298                     return self.constructPrivateType(node);
299                 }
300             });
301         addMultiConstructor("tag:yaml.org,2002:map:",new YamlMultiConstructor() {
302                 public Object JavaDoc call(final Constructor self, final String JavaDoc pref, final Node node) {
303                     return constructRubyMap(self,pref,node);
304                 }
305             });
306         addMultiConstructor("tag:yaml.org,2002:seq:",new YamlMultiConstructor() {
307                 public Object JavaDoc call(final Constructor self, final String JavaDoc pref, final Node node) {
308                     return constructRubySequence(self,pref,node);
309                 }
310             });
311         addMultiConstructor("tag:yaml.org,2002:ruby/object:",new YamlMultiConstructor() {
312                 public Object JavaDoc call(final Constructor self, final String JavaDoc pref, final Node node) {
313                     return constructRuby(self,pref,node);
314                 }
315             });
316         addMultiConstructor("tag:yaml.org,2002:java/object:",new YamlMultiConstructor() {
317                 public Object JavaDoc call(final Constructor self, final String JavaDoc pref, final Node node) {
318                     return constructJava(self,pref,node);
319                 }
320             });
321     }
322 }// JRubyConstructor
323

324
Popular Tags