Browse files

adding Jv/RbYAML code and docs to cnutter_work1

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@2057 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent a926866 commit dbac74b8535e0dbad76b07bb68f2346f46b068da @headius headius committed Jun 8, 2006
View
1 .classpath
@@ -6,5 +6,6 @@
<classpathentry kind="lib" path="lib/bsf.jar"/>
<classpathentry kind="lib" path="lib/junit.jar"/>
<classpathentry kind="lib" path="lib/ant.jar"/>
+ <classpathentry kind="lib" path="lib/jvyaml.jar"/>
<classpathentry kind="output" path="build/classes/jruby"/>
</classpath>
View
4 docs/jvyaml/CREDITS
@@ -0,0 +1,4 @@
+Credits
+-------
+
+Kirill Simonov <xi@resolvent.net> - for writing the original Python code.
View
19 docs/jvyaml/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2006 Ola Bini
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
View
31 docs/jvyaml/README
@@ -0,0 +1,31 @@
+= RbYAML - A pure Ruby YAML 1.1 loader and dumper
+
+Project Contact: Ola Bini <ola@ologix.com>
+
+The code is based mostly on the Python code written by Kirill Simonov <xi@resolvent.net> for PyYAML3000.
+
+RbYAML is a project originating in the JRuby project (http://jruby.sourceforge.net), to create a pure Ruby
+YAML parser for use in JRuby and SYCK cannot be used in this case.
+Since the effort of writing a new one from scratch seemed like a major undertaking it seemed easier to
+port an existing one.
+
+The current functionality is more or less 1.1-compliant. What's missing is the Unicode-support. The idea
+is to have the interface resemble SYCK as much as possible, but this is still work in progress, since some
+of the major architectural choices are quite different.
+
+== Use
+
+Just require 'rbyaml' and use it as you would use YAML, but in module RbYAML instead:
+
+ require 'rbyaml'
+
+ RbYAML.load("--- \n- A\n- b\n- c\n") ----> ["A","b","c"]
+ "foo".to_yaml ----> "foo\n"
+
+== More information
+
+Visit http://rbyaml.rubyforge.org for more information and updated versions
+
+== License
+
+RbYAML is distributed with a MIT license, which can be found in the file LICENSE.
View
19 docs/rbyaml/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2006 Ola Bini
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
View
31 docs/rbyaml/README
@@ -0,0 +1,31 @@
+= RbYAML - A pure Ruby YAML 1.1 loader and dumper
+
+Project Contact: Ola Bini <ola@ologix.com>
+
+The code is based mostly on the Python code written by Kirill Simonov <xi@resolvent.net> for PyYAML3000.
+
+RbYAML is a project originating in the JRuby project (http://jruby.sourceforge.net), to create a pure Ruby
+YAML parser for use in JRuby and SYCK cannot be used in this case.
+Since the effort of writing a new one from scratch seemed like a major undertaking it seemed easier to
+port an existing one.
+
+The current functionality is more or less 1.1-compliant. What's missing is the Unicode-support. The idea
+is to have the interface resemble SYCK as much as possible, but this is still work in progress, since some
+of the major architectural choices are quite different.
+
+== Use
+
+Just require 'rbyaml' and use it as you would use YAML, but in module RbYAML instead:
+
+ require 'rbyaml'
+
+ RbYAML.load("--- \n- A\n- b\n- c\n") ----> ["A","b","c"]
+ "foo".to_yaml ----> "foo\n"
+
+== More information
+
+Visit http://rbyaml.rubyforge.org for more information and updated versions
+
+== License
+
+RbYAML is distributed with a MIT license, which can be found in the file LICENSE.
View
BIN lib/jvyaml.jar
Binary file not shown.
View
3,573 src/builtin/yaml.rb
23 additions, 3,550 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
71 src/org/jruby/util/IOReader.java
@@ -0,0 +1,71 @@
+/***** BEGIN LICENSE BLOCK *****
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Common Public
+ * License Version 1.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * Copyright (C) 2006 Ola Bini <ola@ologix.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the CPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the CPL, the GPL or the LGPL.
+ ***** END LICENSE BLOCK *****/
+/**
+ * $Id$
+ */
+package org.jruby.util;
+
+import java.io.IOException;
+import java.io.Reader;
+
+import org.jruby.RubyString;
+import org.jruby.runtime.builtin.IRubyObject;
+
+/**
+ * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
+ * @version $Revision$
+ */
+public class IOReader extends Reader {
+ private IRubyObject io;
+
+ public IOReader(final IRubyObject io) {
+ if(!io.respondsTo("read")) {
+ throw new IllegalArgumentException("Object: " + io + " is not a legal argument to this wrapper, cause it doesn't respond to \"read\".");
+ }
+ this.io = io;
+ }
+
+ public void close() throws IOException {
+ if(io.respondsTo("close")) {
+ io.callMethod("close");
+ }
+ }
+
+ public int read(final char[] arr, final int off, final int len) {
+ final IRubyObject read = io.callMethod("read",io.getRuntime().newFixnum(len));
+ if(read.isNil() || ((RubyString)read).getValue().length() == 0) {
+ return -1;
+ } else {
+ final RubyString str = (RubyString)read;
+ final CharSequence val = str.getValue();
+ System.arraycopy(val.toString().toCharArray(),0,arr,off,val.length());
+ return val.length();
+ }
+ }
+}// IOReader
+
View
258 src/org/jruby/yaml/JRubyConstructor.java
@@ -0,0 +1,258 @@
+/***** BEGIN LICENSE BLOCK *****
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Common Public
+ * License Version 1.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * Copyright (C) 2006 Ola Bini <ola@ologix.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the CPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the CPL, the GPL or the LGPL.
+ ***** END LICENSE BLOCK *****/
+/**
+ * $Id$
+ */
+package org.jruby.yaml;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import java.util.regex.Pattern;
+
+import org.jvyaml.Composer;
+import org.jvyaml.Constructor;
+import org.jvyaml.ConstructorException;
+import org.jvyaml.ConstructorImpl;
+import org.jvyaml.SafeConstructorImpl;
+
+import org.jvyaml.nodes.Node;
+
+import org.jruby.IRuby;
+import org.jruby.RubyClass;
+import org.jruby.RubyModule;
+import org.jruby.RubyObject;
+import org.jruby.RubyHash;
+import org.jruby.RubyTime;
+import org.jruby.runtime.builtin.IRubyObject;
+
+/**
+ * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
+ * @version $Revision$
+ */
+public class JRubyConstructor extends ConstructorImpl {
+ private final static Map yamlConstructors = new HashMap();
+ private final static Map yamlMultiConstructors = new HashMap();
+ private final static Map yamlMultiRegexps = new HashMap();
+ public YamlConstructor getYamlConstructor(final Object key) {
+ return (YamlConstructor)yamlConstructors.get(key);
+ }
+
+ public YamlMultiConstructor getYamlMultiConstructor(final Object key) {
+ return (YamlMultiConstructor)yamlMultiConstructors.get(key);
+ }
+
+ public Pattern getYamlMultiRegexp(final Object key) {
+ return (Pattern)yamlMultiRegexps.get(key);
+ }
+
+ public Set getYamlMultiRegexps() {
+ return yamlMultiRegexps.keySet();
+ }
+
+ public static void addConstructor(final String tag, final YamlConstructor ctor) {
+ yamlConstructors.put(tag,ctor);
+ }
+
+ public static void addMultiConstructor(final String tagPrefix, final YamlMultiConstructor ctor) {
+ yamlMultiConstructors.put(tagPrefix,ctor);
+ yamlMultiRegexps.put(tagPrefix,Pattern.compile("^"+tagPrefix));
+ }
+
+ private final IRuby runtime;
+
+ public JRubyConstructor(final IRubyObject receiver, final Composer composer) {
+ super(composer);
+ this.runtime = receiver.getRuntime();
+ }
+
+ public Object constructRubyScalar(final Node node) {
+ return runtime.newString((String)super.constructScalar(node));
+ }
+
+ public Object constructRubySequence(final Node node) {
+ return runtime.newArray((List)super.constructSequence(node));
+ }
+
+ public Object constructRubyMapping(final Node node) {
+ return RubyHash.newHash(runtime,(Map)super.constructMapping(node),runtime.getNil());
+ }
+
+ public Object constructRubyPairs(final Node node) {
+ return runtime.newArray((List)super.constructPairs(node));
+ }
+
+ public static Object constructYamlNull(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).runtime.getNil();
+ }
+
+ public static Object constructYamlBool(final Constructor ctor, final Node node) {
+ return SafeConstructorImpl.constructYamlBool(ctor,node) == Boolean.TRUE ? ((JRubyConstructor)ctor).runtime.getTrue() : ((JRubyConstructor)ctor).runtime.getFalse();
+ }
+
+ public static Object constructYamlOmap(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).constructRubyPairs(node);
+ }
+
+ public static Object constructYamlPairs(final Constructor ctor, final Node node) {
+ return constructYamlOmap(ctor,node);
+ }
+
+ public static Object constructYamlSet(final Constructor ctor, final Node node) {
+ return SafeConstructorImpl.constructYamlSet(ctor,node);
+ }
+
+ public static Object constructYamlStr(final Constructor ctor, final Node node) {
+ final org.jruby.RubyString str = (org.jruby.RubyString)((JRubyConstructor)ctor).constructRubyScalar(node);
+ return str.getValue().length() == 0 ? str.getRuntime().getNil() : str;
+ }
+
+ public static Object constructYamlSeq(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).constructRubySequence(node);
+ }
+
+ public static Object constructYamlMap(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).constructRubyMapping(node);
+ }
+
+ public static Object constructUndefined(final Constructor ctor, final Node node) {
+ throw new ConstructorException(null,"could not determine a constructor for the tag " + node.getTag(),null);
+ }
+
+ public static Object constructYamlTimestamp(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).runtime.newTime(((Date)SafeConstructorImpl.constructYamlTimestamp(ctor,node)).getTime());
+ }
+
+ public static Object constructYamlInt(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).runtime.newFixnum(((Long)SafeConstructorImpl.constructYamlInt(ctor,node)).longValue());
+ }
+ public static Object constructYamlFloat(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).runtime.newFloat(((Double)SafeConstructorImpl.constructYamlFloat(ctor,node)).doubleValue());
+ }
+ public static Object constructYamlBinary(final Constructor ctor, final Node node) {
+ return ((JRubyConstructor)ctor).runtime.newString(((String)SafeConstructorImpl.constructYamlBinary(ctor,node)));
+ }
+ public static Object constructRuby(final Constructor ctor, final String tag, final Node node) {
+ final IRuby runtime = ((JRubyConstructor)ctor).runtime;
+ RubyModule objClass = runtime.getModule("Object");
+ if(tag != null) {
+ final String[] nms = tag.split("::");
+ for(int i=0,j=nms.length;i<j;i++) {
+ objClass = (RubyModule)objClass.getConstant(nms[i]);
+ }
+ }
+ final RubyClass theCls = (RubyClass)objClass;
+ final RubyObject oo = (RubyObject)theCls.allocate();
+ final Map vars = (Map)(ctor.constructMapping(node));
+ for(final Iterator iter = vars.keySet().iterator();iter.hasNext();) {
+ final IRubyObject key = (IRubyObject)iter.next();
+ oo.setInstanceVariable("@" + key.toString(),(IRubyObject)vars.get(key));
+ }
+ return oo;
+ }
+
+ static {
+ addConstructor("tag:yaml.org,2002:null",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlNull(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:bool",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlBool(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:omap",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlOmap(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:pairs",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlPairs(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:set",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlSet(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:int",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlInt(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:float",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlFloat(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:timestamp",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlTimestamp(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:str",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlStr(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:binary",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlBinary(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:seq",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlSeq(self,node);
+ }
+ });
+ addConstructor("tag:yaml.org,2002:map",new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return constructYamlMap(self,node);
+ }
+ });
+ addConstructor(null,new YamlConstructor() {
+ public Object call(final Constructor self, final Node node) {
+ return self.constructPrivateType(node);
+ }
+ });
+
+ addMultiConstructor("!ruby/object:",new YamlMultiConstructor() {
+ public Object call(final Constructor self, final String pref, final Node node) {
+ return constructRuby(self,pref,node);
+ }
+ });
+ }
+}// JRubyConstructor
+

0 comments on commit dbac74b

Please sign in to comment.