pax_global_header00006660000000000000000000000064115121361220014504gustar00rootroot0000000000000052 comment=2765b0e004b26537373bf7807c522b36947c3f3b jvyamlb-0.2.5/000077500000000000000000000000001151213612200131545ustar00rootroot00000000000000jvyamlb-0.2.5/CREDITS000066400000000000000000000001331151213612200141710ustar00rootroot00000000000000Credits ------- Kirill Simonov - for writing the original Python code. jvyamlb-0.2.5/LICENSE000066400000000000000000000020731151213612200141630ustar00rootroot00000000000000Copyright (c) 2008 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. jvyamlb-0.2.5/README000066400000000000000000000046171151213612200140440ustar00rootroot00000000000000= JvYAMLb - A pure Java YAML 1.1 loader and dumper Project Contact: Ola Bini For a long time Java have lacked a good YAML loader and dumper with all the features that the SYCK using scripting communities have gotten used to. JvYAMLb aims to rectify this. It's a port from RbYAML by Ola Bini, which was a port of PyYAML3000, written by Kirill Simonov . JvYAMLb is aimed at improving YAML performance in the JRuby project (http://jruby.sourceforge.net), but is also suitable for configuration and data storage in regular Java applications. JvYAMLb supports both 1.0 and 1.1 loading and dumping. == Usage To load a YAML document without any special customization, you can simply import org.jvyaml.YAML and do something like this: Map configuration = (Map)YAML.load(new FileReader("c:/projects/ourSpecificConfig.yml")); or this: List values = (List)YAML.load("--- \n- A\n- b\n- c\n"); Or if you have a YAML document that looks like this: --- !java/object:org.jruby.UserConfig userdir: d:/config/jruby prefs: - pref1.properties - values.properties - foo.xml Load it like this: org.jruby.UserConfig conf = (org.jruby.UserConfig)YAML.load(new FileReader("path.to.the.yaml.file")); conf.getUserdir(); ==> "d:/config/jruby" conf.getPrefs().iterator().next(); ==> "pref1.properties" Dumping is as easy as this: YAML.dump("str"); // -> "--- str\n" This works for complex objects too. If you want to dump to a stream instead of a String just add the stream to the dump command: YAML.dump("str",new FileWriter("test1.yml")); If you need to add parameters to either loading or dumping, just add a YAMLConfig object. For example, to dump with version 1.0 syntax (but not necessarily write a header), you can do it like this: YAML.dump("1.0",YAML.config().version("1.0")); JvYAMLb is very customizable. If you want to use a different Resolver or Constructor for your YAML document, it's very easy to change the behaviour by implementing the YAMLFactory interface. This is in fact the way JRuby returns RubyObjects instead of regular Java objects. A specialized JRubyConstructor is created, and when loading a JRubyYAMLFactory is created that uses this Constructor instead. == More information Visit http://jvyaml.dev.java.net for more information and updated versions == License JvYAMLb is distributed with a MIT license, which can be found in the file LICENSE. jvyamlb-0.2.5/build.xml000066400000000000000000000073171151213612200150050ustar00rootroot00000000000000 JvYAMLb is a Java YAML 1.1 parser and emitter. jvyamlb-0.2.5/default.build.properties000066400000000000000000000006261151213612200200200ustar00rootroot00000000000000# Defaults. To override, create a file called build.properties in # the same directory and put your changes in that. src.dir=src main.src.dir=${src.dir}/main test.src.dir=${src.dir}/test lib.dir=lib build.dir=build classes.dir=${build.dir}/classes jvyamlb.classes.dir=${classes.dir}/jvyaml test.classes.dir=${classes.dir}/test docs.dir=docs api.docs.dir=${docs.dir}/api release.dir=rels javac.version=1.4 jvyamlb-0.2.5/src/000077500000000000000000000000001151213612200137435ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/000077500000000000000000000000001151213612200146675ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/000077500000000000000000000000001151213612200154565ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/000077500000000000000000000000001151213612200171225ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/BaseConstructorImpl.java000066400000000000000000000335471151213612200237430ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import org.jvyamlb.exceptions.ConstructorException; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.LinkNode; import org.jvyamlb.nodes.ScalarNode; import org.jvyamlb.nodes.SequenceNode; import org.jvyamlb.nodes.MappingNode; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class BaseConstructorImpl implements Constructor { 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 Composer composer; private Map recursiveObjects = new HashMap(); public BaseConstructorImpl(final Composer composer) { this.composer = composer; } public boolean checkData() { return composer.checkNode(); } public Object getData() { if(composer.checkNode()) { Node node = composer.getNode(); if(null != node) { return constructDocument(node); } } return null; } private class DocumentIterator implements Iterator { public boolean hasNext() {return checkData();} public Object next() {return getData();} public void remove() {} } public Iterator eachDocument() { return new DocumentIterator(); } public Iterator iterator() { return eachDocument(); } public Object constructDocument(final Node node) { final Object data = constructObject(node); recursiveObjects.clear(); return data; } public static class YamlMultiAdapter implements YamlConstructor { private YamlMultiConstructor ctor; private String prefix; public YamlMultiAdapter(final YamlMultiConstructor ctor, final String prefix) { this.ctor = ctor; this.prefix = prefix; } public Object call(final Constructor self, final Node node) { return ctor.call(self,this.prefix,node); } } public Node getNullNode() { return new ScalarNode("tag:yaml.org,2002:null",null,(char)0); } public Object constructObject(Node node) { if(node == null) { node = getNullNode(); } if(node.getConstructed() != null) { return node.getConstructed(); } if(recursiveObjects.containsKey(node)) { LinkNode n = new LinkNode(); n.setValue(node); return n; // throw new ConstructorException(null,"found recursive node",null); } recursiveObjects.put(node,new ArrayList()); YamlConstructor ctor = getYamlConstructor(node.getTag()); if(ctor == null) { boolean through = true; for(final Iterator iter = getYamlMultiRegexps().iterator();iter.hasNext();) { final String tagPrefix = (String)iter.next(); final Pattern reg = getYamlMultiRegexp(tagPrefix); if(reg.matcher(node.getTag()).find()) { final String tagSuffix = node.getTag().substring(tagPrefix.length()); ctor = new YamlMultiAdapter(getYamlMultiConstructor(tagPrefix),tagSuffix); through = false; break; } } if(through) { final YamlMultiConstructor xctor = getYamlMultiConstructor(null); if(null != xctor) { ctor = new YamlMultiAdapter(xctor,node.getTag()); } else { ctor = getYamlConstructor(null); if(ctor == null) { ctor = CONSTRUCT_PRIMITIVE; } } } } final Object data = ctor.call(this,node); node.setConstructed(data); doRecursionFix(node,data); return data; } public void doRecursionFix(Node node, Object obj) { List ll = (List)recursiveObjects.remove(node); if(null != ll) { for(Iterator iter = ll.iterator();iter.hasNext();) { ((RecursiveFixer)iter.next()).replace(node, obj); } } } public Object constructPrimitive(final Node node) { if(node instanceof ScalarNode) { return constructScalar(node); } else if(node instanceof SequenceNode) { return constructSequence(node); } else if(node instanceof MappingNode) { return constructMapping(node); } else { System.err.println(node.getTag()); } return null; } public Object constructScalar(final Node node) { if(!(node instanceof ScalarNode)) { if(node instanceof MappingNode) { final Map vals = ((Map)node.getValue()); for(final Iterator iter = vals.keySet().iterator();iter.hasNext();) { final Node key = (Node)iter.next(); if("tag:yaml.org,2002:value".equals(key.getTag())) { return constructScalar((Node)vals.get(key)); } } } throw new ConstructorException(null,"expected a scalar node, but found " + node.getClass().getName(),null); } return node.getValue(); } public Object constructPrivateType(final Node node) { Object val = null; if(node.getValue() instanceof Map) { val = constructMapping(node); } else if(node.getValue() instanceof List) { val = constructSequence(node); } else { val = node.getValue().toString(); } return new PrivateType(node.getTag(),val); } public Object constructSequence(final Node node) { if(!(node instanceof SequenceNode)) { throw new ConstructorException(null,"expected a sequence node, but found " + node.getClass().getName(),null); } final List internal = (List)node.getValue(); final List val = new ArrayList(internal.size()); for(final Iterator iter = internal.iterator();iter.hasNext();) { final Object obj = constructObject((Node)iter.next()); if(obj instanceof LinkNode) { final int ix = val.size(); addFixer((Node)(((LinkNode)obj).getValue()), new RecursiveFixer() { public void replace(Node node, Object real) { val.set(ix, real); } }); } val.add(obj); } return val; } public Object constructMapping(final Node node) { if(!(node instanceof MappingNode)) { throw new ConstructorException(null,"expected a mapping node, but found " + node.getClass().getName(),null); } final Map[] mapping = new Map[]{new HashMap()}; List merge = null; final Map val = (Map)node.getValue(); for(final Iterator iter = val.keySet().iterator();iter.hasNext();) { final Node key_v = (Node)iter.next(); final Node value_v = (Node)val.get(key_v); if(key_v.getTag().equals("tag:yaml.org,2002:merge")) { if(merge != null) { throw new ConstructorException("while constructing a mapping", "found duplicate merge key",null); } if(value_v instanceof MappingNode) { merge = new LinkedList(); merge.add(constructMapping(value_v)); } else if(value_v instanceof SequenceNode) { merge = new LinkedList(); final List vals = (List)value_v.getValue(); for(final Iterator sub = vals.iterator();sub.hasNext();) { final Node subnode = (Node)sub.next(); if(!(subnode instanceof MappingNode)) { throw new ConstructorException("while constructing a mapping","expected a mapping for merging, but found " + subnode.getClass().getName(),null); } merge.add(0,constructMapping(subnode)); } } else { throw new ConstructorException("while constructing a mapping","expected a mapping or list of mappings for merging, but found " + value_v.getClass().getName(),null); } } else if(key_v.getTag().equals("tag:yaml.org,2002:value")) { if(mapping[0].containsKey("=")) { throw new ConstructorException("while construction a mapping", "found duplicate value key", null); } mapping[0].put("=",constructObject(value_v)); } else { final Object kk = constructObject(key_v); final Object vv = constructObject(value_v); if(vv instanceof LinkNode) { addFixer((Node)((LinkNode)vv).getValue(), new RecursiveFixer() { public void replace(Node node, Object real) { mapping[0].put(kk, real); } }); } mapping[0].put(kk,vv); } } if(null != merge) { merge.add(mapping[0]); mapping[0] = new HashMap(); for(final Iterator iter = merge.iterator();iter.hasNext();) { mapping[0].putAll((Map)iter.next()); } } return mapping[0]; } public void addFixer(Node node, RecursiveFixer fixer) { List ll = (List)recursiveObjects.get(node); if(ll == null) { ll = new ArrayList(); recursiveObjects.put(node, ll); } ll.add(fixer); } public Object constructPairs(final Node node) { if(!(node instanceof MappingNode)) { throw new ConstructorException(null,"expected a mapping node, but found " + node.getClass().getName(), null); } final List value = new LinkedList(); final Map vals = (Map)node.getValue(); for(final Iterator iter = vals.keySet().iterator();iter.hasNext();) { final Node key = (Node)iter.next(); final Node val = (Node)vals.get(key); value.add(new Object[]{constructObject(key),constructObject(val)}); } return value; } public final static YamlConstructor CONSTRUCT_PRIMITIVE = new YamlConstructor() { public Object call(final Constructor self, final Node node) { return self.constructPrimitive(node); }}; public final static YamlConstructor CONSTRUCT_SCALAR = new YamlConstructor() { public Object call(final Constructor self, final Node node) { return self.constructScalar(node); }}; public final static YamlConstructor CONSTRUCT_PRIVATE = new YamlConstructor() { public Object call(final Constructor self, final Node node) { return self.constructPrivateType(node); }}; public final static YamlConstructor CONSTRUCT_SEQUENCE = new YamlConstructor() { public Object call(final Constructor self, final Node node) { return self.constructSequence(node); }}; public final static YamlConstructor CONSTRUCT_MAPPING = new YamlConstructor() { public Object call(final Constructor self, final Node node) { return self.constructMapping(node); }}; public static void main(final String[] args) throws Exception { final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final ByteList input = new ByteList(1024); final InputStream reader = new FileInputStream(filename); byte[] buff = new byte[1024]; int read = 0; while(true) { read = reader.read(buff); input.append(buff,0,read); if(read < 1024) { break; } } reader.close(); final long before = System.currentTimeMillis(); for(int i=0;i<1;i++) { final Constructor ctor = new BaseConstructorImpl(new ComposerImpl(new ParserImpl(new ScannerImpl(input)),new ResolverImpl())); for(final Iterator iter = ctor.eachDocument();iter.hasNext();) { iter.next(); //System.out.println(iter.next()); } } final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the nodes for the file: " + filename + " took " + time + "ms, or " + timeS + " seconds"); } }// BaseConstructorImpl jvyamlb-0.2.5/src/main/org/jvyamlb/Composer.java000066400000000000000000000005621151213612200215570ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Iterator; import org.jvyamlb.nodes.Node; /** * @author Ola Bini */ public interface Composer { boolean checkNode(); Node getNode(); Iterator eachNode(); Iterator iterator(); }// Composer jvyamlb-0.2.5/src/main/org/jvyamlb/ComposerImpl.java000066400000000000000000000164631151213612200224100ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Map; import org.jvyamlb.exceptions.ComposerException; import org.jvyamlb.events.AliasEvent; import org.jvyamlb.events.Event; import org.jvyamlb.events.NodeEvent; import org.jvyamlb.events.MappingEndEvent; import org.jvyamlb.events.MappingStartEvent; import org.jvyamlb.events.ScalarEvent; import org.jvyamlb.events.SequenceStartEvent; import org.jvyamlb.events.SequenceEndEvent; import org.jvyamlb.events.StreamStartEvent; import org.jvyamlb.events.StreamEndEvent; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.ScalarNode; import org.jvyamlb.nodes.SequenceNode; import org.jvyamlb.nodes.MappingNode; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ComposerImpl implements Composer { protected Parser parser; private Resolver resolver; private Map anchors; public ComposerImpl(final Parser parser, final Resolver resolver) { this.parser = parser; this.resolver = resolver; this.anchors = new HashMap(); } public boolean checkNode() { return !(parser.peekEvent() instanceof StreamEndEvent); } public Node getNode() { return checkNode() ? composeDocument() : (Node)null; } private class NodeIterator implements Iterator { public boolean hasNext() {return checkNode();} public Object next() {return getNode();} public void remove() {} } public Iterator eachNode() { return new NodeIterator(); } public Iterator iterator() { return eachNode(); } public Node composeDocument() { if(parser.peekEvent() instanceof StreamStartEvent) { //Drop STREAM-START event parser.getEvent(); } //Drop DOCUMENT-START event parser.getEvent(); final Node node = composeNode(null,null); //Drop DOCUMENT-END event parser.getEvent(); this.anchors.clear(); return node; } private final static boolean[] FALS = new boolean[]{false}; private final static boolean[] TRU = new boolean[]{true}; protected Node getScalar(final String tag, final ByteList value, final char style, final Event e) { return new ScalarNode(tag,value,style); } protected Node createMapping(final String tag, final Map value, final boolean flowStyle, final Event e) { return new MappingNode(tag,value,flowStyle); } protected void finalizeMapping(final Node node, final Event e) { } protected Node createSequence(final String tag, final List value, final boolean flowStyle, final Event e) { return new SequenceNode(tag,value,flowStyle); } protected void finalizeSequence(final Node node, final Event e) { } protected void composerException(final String when, final String what, final String note, final Event e) { throw new ComposerException(when, what, note); } public Node composeNode(final Node parent, final Object index) { if(parser.peekEvent() instanceof AliasEvent) { final AliasEvent event = (AliasEvent)parser.getEvent(); final String anchor = event.getAnchor(); if(!anchors.containsKey(anchor)) { composerException(null,"found undefined alias " + anchor,null,event); } return (Node)anchors.get(anchor); } final Event event = parser.peekEvent(); String anchor = null; if(event instanceof NodeEvent) { anchor = ((NodeEvent)event).getAnchor(); } resolver.descendResolver(parent,index); Node node = null; if(event instanceof ScalarEvent) { final ScalarEvent ev = (ScalarEvent)parser.getEvent(); String tag = ev.getTag(); if(tag == null || tag.equals("!")) { tag = resolver.resolve(ScalarNode.class,ev.getValue(),ev.getImplicit()); } node = getScalar(tag,ev.getValue(),ev.getStyle(),ev); if(null != anchor) { anchors.put(anchor,node); } } else if(event instanceof SequenceStartEvent) { final SequenceStartEvent start = (SequenceStartEvent)parser.getEvent(); String tag = start.getTag(); if(tag == null || tag.equals("!")) { tag = resolver.resolve(SequenceNode.class,null,start.getImplicit() ? TRU : FALS); } node = createSequence(tag,new ArrayList(),start.getFlowStyle(),start); if(null != anchor) { anchors.put(anchor,node); } int ix = 0; while(!(parser.peekEvent() instanceof SequenceEndEvent)) { ((List)node.getValue()).add(composeNode(node,new Integer(ix++))); } finalizeSequence(node, parser.getEvent()); } else if(event instanceof MappingStartEvent) { final MappingStartEvent start = (MappingStartEvent)parser.getEvent(); String tag = start.getTag(); if(tag == null || tag.equals("!")) { tag = resolver.resolve(MappingNode.class,null, start.getImplicit() ? TRU : FALS); } node = createMapping(tag, new HashMap(), start.getFlowStyle(), start); if(null != anchor) { anchors.put(anchor,node); } while(!(parser.peekEvent() instanceof MappingEndEvent)) { final Event key = parser.peekEvent(); final Node itemKey = composeNode(node,null); if(((Map)node.getValue()).containsKey(itemKey)) { composeNode(node,itemKey); } else { ((Map)node.getValue()).put(itemKey,composeNode(node,itemKey)); } } finalizeMapping(node, parser.getEvent()); } resolver.ascendResolver(); return node; } public static void main(final String[] args) throws Exception { final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final ByteList input = new ByteList(1024); final InputStream reader = new FileInputStream(filename); byte[] buff = new byte[1024]; int read = 0; while(true) { read = reader.read(buff); input.append(buff,0,read); if(read < 1024) { break; } } reader.close(); final long before = System.currentTimeMillis(); for(int i=0;i<1;i++) { final Composer cmp = new ComposerImpl(new ParserImpl(new ScannerImpl(input)),new ResolverImpl()); for(final Iterator iter = cmp.eachNode();iter.hasNext();) { iter.next(); // System.out.println(iter.next()); } } final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the nodes for the file: " + filename + " took " + time + "ms, or " + timeS + " seconds"); } }// ComposerImpl jvyamlb-0.2.5/src/main/org/jvyamlb/Constructor.java000066400000000000000000000022261151213612200223140ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Iterator; import org.jvyamlb.nodes.Node; /** * @author Ola Bini */ public interface Constructor { boolean checkData(); Object getData(); Iterator eachDocument(); Iterator iterator(); Object constructDocument(final Node node); Object constructObject(final Node node); Object constructPrimitive(final Node node); Object constructScalar(final Node node); Object constructPrivateType(final Node node); Object constructSequence(final Node node); Object constructMapping(final Node node); Object constructPairs(final Node node); void doRecursionFix(Node node, Object obj); void addFixer(Node node, RecursiveFixer fixer); interface RecursiveFixer { void replace(final Node node, final Object real); } interface YamlConstructor { Object call(final Constructor self, final Node node); } interface YamlMultiConstructor { Object call(final Constructor self, final String pref, final Node node); } }// Constructor jvyamlb-0.2.5/src/main/org/jvyamlb/ConstructorImpl.java000066400000000000000000000065651151213612200231500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import org.jruby.util.ByteList; import org.jvyamlb.exceptions.ConstructorException; /** * @author Ola Bini */ public class ConstructorImpl extends SafeConstructorImpl { 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) { YamlConstructor mine = (YamlConstructor)yamlConstructors.get(key); if(mine == null) { mine = super.getYamlConstructor(key); } return mine; } public YamlMultiConstructor getYamlMultiConstructor(final Object key) { YamlMultiConstructor mine = (YamlMultiConstructor)yamlMultiConstructors.get(key); if(mine == null) { mine = super.getYamlMultiConstructor(key); } return mine; } public Pattern getYamlMultiRegexp(final Object key) { Pattern mine = (Pattern)yamlMultiRegexps.get(key); if(mine == null) { mine = super.getYamlMultiRegexp(key); } return mine; } public Set getYamlMultiRegexps() { final Set all = new HashSet(super.getYamlMultiRegexps()); all.addAll(yamlMultiRegexps.keySet()); return all; } 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)); } public ConstructorImpl(final Composer composer) { super(composer); } public static void main(final String[] args) throws Exception { final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final ByteList input = new ByteList(1024); final InputStream reader = new FileInputStream(filename); byte[] buff = new byte[1024]; int read = 0; while(true) { read = reader.read(buff); input.append(buff,0,read); if(read < 1024) { break; } } reader.close(); final long before = System.currentTimeMillis(); for(int i=0;i<1;i++) { final Constructor ctor = new ConstructorImpl(new ComposerImpl(new ParserImpl(new ScannerImpl(input)),new ResolverImpl())); for(final Iterator iter = ctor.eachDocument();iter.hasNext();iter.next()) { iter.next(); // System.out.println(iter.next()); } } final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the nodes for the file: " + filename + " took " + time + "ms, or " + timeS + " seconds"); } }// ConstructorImpl jvyamlb-0.2.5/src/main/org/jvyamlb/DefaultYAMLConfig.java000066400000000000000000000061121151213612200231620ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public class DefaultYAMLConfig implements YAMLConfig { private int indent = 2; private boolean useHeader = false; private boolean useVersion = false; private String version = "1.1"; private boolean expStart = true; private boolean expEnd = false; private String format = "id{0,number,000}"; private boolean expTypes = false; private boolean canonical = false; private int bestWidth = 80; private boolean useBlock = false; private boolean useFlow = false; private boolean usePlain = false; private boolean useSingle = false; private boolean useDouble = false; public YAMLConfig indent(final int indent) { this.indent = indent; return this; } public int indent() { return this.indent; } public YAMLConfig useHeader(final boolean useHeader) { this.useHeader = useHeader; return this; } public boolean useHeader() { return this.useHeader; } public YAMLConfig useVersion(final boolean useVersion) { this.useVersion = useVersion; return this; } public boolean useVersion() { return this.useVersion; } public YAMLConfig version(final String version) { this.version = version; return this; } public String version() { return this.version; } public YAMLConfig explicitStart(final boolean expStart) { this.expStart = expStart; return this; } public boolean explicitStart() { return this.expStart; } public YAMLConfig explicitEnd(final boolean expEnd) { this.expEnd = expEnd; return this; } public boolean explicitEnd() { return this.expEnd; } public YAMLConfig anchorFormat(final String format) { this.format = format; return this; } public String anchorFormat() { return this.format; } public YAMLConfig explicitTypes(final boolean expTypes) { this.expTypes = expTypes; return this; } public boolean explicitTypes() { return this.expTypes; } public YAMLConfig canonical(final boolean canonical) { this.canonical = canonical; return this; } public boolean canonical() { return this.canonical; } public YAMLConfig bestWidth(final int bestWidth) { this.bestWidth = bestWidth; return this; } public int bestWidth() { return this.bestWidth; } public YAMLConfig useBlock(final boolean useBlock) { this.useBlock = useBlock; return this; } public boolean useBlock() { return this.useBlock; } public YAMLConfig useFlow(final boolean useFlow) { this.useFlow = useFlow; return this; } public boolean useFlow() { return this.useFlow; } public YAMLConfig usePlain(final boolean usePlain) { this.usePlain = usePlain; return this; } public boolean usePlain() { return this.usePlain; } public YAMLConfig useSingle(final boolean useSingle) { this.useSingle = useSingle; return this; } public boolean useSingle() { return this.useSingle; } public YAMLConfig useDouble(final boolean useDouble) { this.useDouble = useDouble; return this; } public boolean useDouble() { return this.useDouble; } }// DefaultYAMLConfig jvyamlb-0.2.5/src/main/org/jvyamlb/DefaultYAMLFactory.java000066400000000000000000000030431151213612200233640ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.InputStream; import java.io.OutputStream; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class DefaultYAMLFactory implements YAMLFactory { public Scanner createScanner(final ByteList io) { return new ScannerImpl(io); } public Scanner createScanner(final InputStream io) { return new ScannerImpl(io); } public Parser createParser(final Scanner scanner) { return new ParserImpl(scanner); } public Parser createParser(final Scanner scanner, final YAMLConfig cfg) { return new ParserImpl(scanner,cfg); } public Resolver createResolver() { return new ResolverImpl(); } public Composer createComposer(final Parser parser, final Resolver resolver) { return new ComposerImpl(parser,resolver); } public Constructor createConstructor(final Composer composer) { return new ConstructorImpl(composer); } public Emitter createEmitter(final OutputStream output, final YAMLConfig cfg) { return new EmitterImpl(output,cfg); } public Serializer createSerializer(final Emitter emitter, final Resolver resolver, final YAMLConfig cfg) { return new SerializerImpl(emitter,resolver,cfg); } public Representer createRepresenter(final Serializer serializer, final YAMLConfig cfg) { return new RepresenterImpl(serializer,cfg); } }// DefaultYAMLFactory jvyamlb-0.2.5/src/main/org/jvyamlb/Emitter.java000066400000000000000000000005111151213612200213730ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import org.jvyamlb.events.Event; /** * @author Ola Bini */ public interface Emitter { void emit(final Event event) throws IOException; }// Emitter jvyamlb-0.2.5/src/main/org/jvyamlb/EmitterImpl.java000066400000000000000000001600601151213612200222230ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import java.io.OutputStream; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; import java.util.regex.Pattern; import org.jvyamlb.exceptions.EmitterException; import org.jvyamlb.events.Event; import org.jvyamlb.events.StreamStartEvent; import org.jvyamlb.events.StreamEndEvent; import org.jvyamlb.events.DocumentStartEvent; import org.jvyamlb.events.DocumentEndEvent; import org.jvyamlb.events.CollectionStartEvent; import org.jvyamlb.events.CollectionEndEvent; import org.jvyamlb.events.MappingStartEvent; import org.jvyamlb.events.SequenceStartEvent; import org.jvyamlb.events.MappingEndEvent; import org.jvyamlb.events.SequenceEndEvent; import org.jvyamlb.events.AliasEvent; import org.jvyamlb.events.ScalarEvent; import org.jvyamlb.events.NodeEvent; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class EmitterImpl implements Emitter { private static class ScalarAnalysis { public ByteList scalar; public boolean empty; public boolean multiline; public boolean allowFlowPlain; public boolean allowBlockPlain; public boolean allowSingleQuoted; public boolean allowDoubleQuoted; public boolean allowBlock; public boolean specialCharacters; public ScalarAnalysis(final ByteList scalar, final boolean empty, final boolean multiline, final boolean allowFlowPlain, final boolean allowBlockPlain, final boolean allowSingleQuoted, final boolean allowDoubleQuoted, final boolean allowBlock, final boolean specialCharacters) { this.scalar = scalar; this.empty = empty; this.multiline = multiline; this.allowFlowPlain = allowFlowPlain; this.allowBlockPlain = allowBlockPlain; this.allowSingleQuoted = allowSingleQuoted; this.allowDoubleQuoted = allowDoubleQuoted; this.allowBlock = allowBlock; this.specialCharacters = specialCharacters; } } private static interface EmitterState { void expect(final EmitterEnvironment env) throws IOException; } private static final int STREAM_START = 0; private static final int FIRST_DOCUMENT_START = 1; private static final int DOCUMENT_ROOT = 2; private static final int NOTHING = 3; private static final int DOCUMENT_START = 4; private static final int DOCUMENT_END = 5; private static final int FIRST_FLOW_SEQUENCE_ITEM = 6; private static final int FLOW_SEQUENCE_ITEM = 7; private static final int FIRST_FLOW_MAPPING_KEY = 8; private static final int FLOW_MAPPING_SIMPLE_VALUE = 9; private static final int FLOW_MAPPING_VALUE = 10; private static final int FLOW_MAPPING_KEY = 11; private static final int BLOCK_SEQUENCE_ITEM = 12; private static final int FIRST_BLOCK_MAPPING_KEY = 13; private static final int BLOCK_MAPPING_SIMPLE_VALUE = 14; private static final int BLOCK_MAPPING_VALUE = 15; private static final int BLOCK_MAPPING_KEY = 16; private static final int FIRST_BLOCK_SEQUENCE_ITEM = 17; private static final EmitterState[] STATES = new EmitterState[18]; static { STATES[STREAM_START] = new EmitterState() { public void expect(final EmitterEnvironment env) { env.expectStreamStart(); } }; STATES[FIRST_DOCUMENT_START] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectDocumentStart(true); } }; STATES[DOCUMENT_ROOT] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectDocumentRoot(); } }; STATES[NOTHING] = new EmitterState() { public void expect(final EmitterEnvironment env) { env.expectNothing(); } }; STATES[DOCUMENT_START] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectDocumentStart(false); } }; STATES[DOCUMENT_END] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectDocumentEnd(); } }; STATES[FIRST_FLOW_SEQUENCE_ITEM] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFirstFlowSequenceItem(); } }; STATES[FLOW_SEQUENCE_ITEM] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFlowSequenceItem(); } }; STATES[FIRST_FLOW_MAPPING_KEY] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFirstFlowMappingKey(); } }; STATES[FLOW_MAPPING_SIMPLE_VALUE] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFlowMappingSimpleValue(); } }; STATES[FLOW_MAPPING_VALUE] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFlowMappingValue(); } }; STATES[FLOW_MAPPING_KEY] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFlowMappingKey(); } }; STATES[BLOCK_SEQUENCE_ITEM] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectBlockSequenceItem(false); } }; STATES[FIRST_BLOCK_MAPPING_KEY] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectFirstBlockMappingKey(); } }; STATES[BLOCK_MAPPING_SIMPLE_VALUE] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectBlockMappingSimpleValue(); } }; STATES[BLOCK_MAPPING_VALUE] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectBlockMappingValue(); } }; STATES[BLOCK_MAPPING_KEY] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectBlockMappingKey(false); } }; STATES[FIRST_BLOCK_SEQUENCE_ITEM] = new EmitterState() { public void expect(final EmitterEnvironment env) throws IOException { env.expectBlockSequenceItem(true); } }; } private final static Map DEFAULT_TAG_PREFIXES_1_0; private final static Map DEFAULT_TAG_PREFIXES_1_1; static { final Map defInit0 = new HashMap(); defInit0.put("tag:yaml.org,2002:","!"); DEFAULT_TAG_PREFIXES_1_0 = java.util.Collections.unmodifiableMap(defInit0); final Map defInit = new HashMap(); defInit.put("!","!"); defInit.put("tag:yaml.org,2002:","!!"); DEFAULT_TAG_PREFIXES_1_1 = java.util.Collections.unmodifiableMap(defInit); } private OutputStream stream; private YAMLConfig options; private EmitterEnvironment env; public EmitterImpl(final OutputStream stream, final YAMLConfig opts) { this.stream = stream; this.options = opts; this.env = new EmitterEnvironment(); this.env.emitter = this; this.env.canonical = this.options.canonical(); final int propIndent = this.options.indent(); if(propIndent>=2 && propIndent<10) { this.env.bestIndent = propIndent; } final int propWidth = this.options.bestWidth(); if(propWidth != 0 && propWidth > (this.env.bestIndent*2)) { this.env.bestWidth = propWidth; } } public YAMLConfig getOptions() { return options; } public void emit(final Event event) throws IOException { this.env.events.add(event); while(!this.env.needMoreEvents()) { this.env.event = (Event)this.env.events.remove(0); STATES[this.env.state].expect(env); this.env.event = null; } } private static class EmitterEnvironment { public List states = new ArrayList(); public int state = STREAM_START; public List events = new ArrayList(); public Event event; public int flowLevel = 0; public List indents = new ArrayList(); public int indent = -1; public boolean rootContext = false; public boolean sequenceContext = false; public boolean mappingContext = false; public boolean simpleKeyContext = false; public int line = 0; public int column = 0; public boolean whitespace = true; public boolean indentation = true; public boolean canonical = false; public int bestIndent = 2; public int bestWidth = 80; public ByteList bestLinebreak = ByteList.create("\n"); public Map tagPrefixes; public String preparedAnchor; public String preparedTag; public ScalarAnalysis analysis; public char style = 0; public EmitterImpl emitter; public boolean isVersion10 = false; public boolean needMoreEvents() { if(events.isEmpty()) { return true; } event = (Event)events.get(0); if(event instanceof DocumentStartEvent) { return needEvents(1); } else if(event instanceof SequenceStartEvent) { return needEvents(2); } else if(event instanceof MappingStartEvent) { return needEvents(3); } else { return false; } } private boolean needEvents(final int count) { int level = 0; final Iterator iter = events.iterator(); iter.next(); for(;iter.hasNext();) { final Object curr = iter.next(); if(curr instanceof DocumentStartEvent || curr instanceof CollectionStartEvent) { level++; } else if(curr instanceof DocumentEndEvent || curr instanceof CollectionEndEvent) { level--; } else if(curr instanceof StreamEndEvent) { level = -1; } if(level<0) { return false; } } return events.size() < count+1; } private void increaseIndent(final boolean flow, final boolean indentless) { indents.add(0,new Integer(indent)); if(indent == -1) { if(flow) { indent = bestIndent; } else { indent = 0; } } else if(!indentless) { indent += bestIndent; } } public void expectStreamStart() { if(this.event instanceof StreamStartEvent) { emitter.writeStreamStart(); this.state = FIRST_DOCUMENT_START; } else { throw new EmitterException("expected StreamStartEvent, but got " + this.event); } } public void expectNothing() { throw new EmitterException("expecting nothing, but got " + this.event); } public void expectDocumentStart(final boolean first) throws IOException { if(event instanceof DocumentStartEvent) { final DocumentStartEvent ev = (DocumentStartEvent)event; if(first) { if(null != ev.getVersion()) { emitter.writeVersionDirective(prepareVersion(ev.getVersion())); } if((null != ev.getVersion() && ev.getVersion()[1] == 0) || emitter.getOptions().version().equals("1.0")) { isVersion10 = true; tagPrefixes = new HashMap(DEFAULT_TAG_PREFIXES_1_0); } else { tagPrefixes = new HashMap(DEFAULT_TAG_PREFIXES_1_1); } if(null != ev.getTags()) { final Set handles = new TreeSet(); handles.addAll(ev.getTags().keySet()); for(final Iterator iter = handles.iterator();iter.hasNext();) { final String handle = (String)iter.next(); final String prefix = (String)ev.getTags().get(handle); tagPrefixes.put(prefix,handle); final String handleText = prepareTagHandle(handle); final String prefixText = prepareTagPrefix(prefix); emitter.writeTagDirective(handleText,prefixText); } } } final boolean implicit = first && !ev.getExplicit() && !canonical && ev.getVersion() == null && ev.getTags() == null && !checkEmptyDocument(); if(!implicit) { emitter.writeIndent(); emitter.writeIndicator(ByteList.create("--- "),true,true,false); if(canonical) { emitter.writeIndent(); } } state = DOCUMENT_ROOT; } else if(event instanceof StreamEndEvent) { emitter.writeStreamEnd(); state = NOTHING; } else { throw new EmitterException("expected DocumentStartEvent, but got " + event); } } public void expectDocumentRoot() throws IOException { states.add(0,new Integer(DOCUMENT_END)); expectNode(true,false,false,false); } public void expectDocumentEnd() throws IOException { if(event instanceof DocumentEndEvent) { emitter.writeIndent(); if(((DocumentEndEvent)event).getExplicit()) { emitter.writeIndicator(ByteList.create("..."),true,false,false); emitter.writeIndent(); } emitter.flushStream(); state = DOCUMENT_START; } else { throw new EmitterException("expected DocumentEndEvent, but got " + event); } } public void expectFirstFlowSequenceItem() throws IOException { if(event instanceof SequenceEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); flowLevel--; emitter.writeIndicator(ByteList.create("]"),false,false,false); emitter.writeLineBreak(null); emitter.writeLineBreak(null); state = ((Integer)states.remove(0)).intValue(); } else { if(canonical || column > bestWidth) { emitter.writeIndent(); } states.add(0,new Integer(FLOW_SEQUENCE_ITEM)); expectNode(false,true,false,false); } } public void expectFlowSequenceItem() throws IOException { if(event instanceof SequenceEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); flowLevel--; if(canonical) { emitter.writeIndicator(ByteList.create(","),false,false,false); emitter.writeIndent(); } emitter.writeIndicator(ByteList.create("]"),false,false,false); emitter.writeLineBreak(null); emitter.writeLineBreak(null); state = ((Integer)states.remove(0)).intValue(); } else { emitter.writeIndicator(ByteList.create(","),false,false,false); if(canonical || column > bestWidth) { emitter.writeIndent(); } states.add(0,new Integer(FLOW_SEQUENCE_ITEM)); expectNode(false,true,false,false); } } public void expectFirstFlowMappingKey() throws IOException { if(event instanceof MappingEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); flowLevel--; emitter.writeIndicator(ByteList.create("}"),false,false,false); emitter.writeLineBreak(null); emitter.writeLineBreak(null); state = ((Integer)states.remove(0)).intValue(); } else { if(canonical || column > bestWidth) { emitter.writeIndent(); } if(!canonical && checkSimpleKey()) { states.add(0,new Integer(FLOW_MAPPING_SIMPLE_VALUE)); expectNode(false,false,true,true); } else { emitter.writeIndicator(ByteList.create("?"),true,false,false); states.add(0,new Integer(FLOW_MAPPING_VALUE)); expectNode(false,false,true,false); } } } public void expectFlowMappingSimpleValue() throws IOException { emitter.writeIndicator(ByteList.create(": "),false,true,false); states.add(0,new Integer(FLOW_MAPPING_KEY)); expectNode(false,false,true,false); } public void expectFlowMappingValue() throws IOException { if(canonical || column > bestWidth) { emitter.writeIndent(); } emitter.writeIndicator(ByteList.create(": "),false,true,false); states.add(0,new Integer(FLOW_MAPPING_KEY)); expectNode(false,false,true,false); } public void expectFlowMappingKey() throws IOException { if(event instanceof MappingEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); flowLevel--; if(canonical) { emitter.writeIndicator(ByteList.create(","),false,false,false); emitter.writeIndent(); } emitter.writeIndicator(ByteList.create("}"),false,false,false); emitter.writeLineBreak(null); emitter.writeLineBreak(null); state = ((Integer)states.remove(0)).intValue(); } else { emitter.writeIndicator(ByteList.create(","),false,false,false); if(canonical || column > bestWidth) { emitter.writeIndent(); } if(!canonical && checkSimpleKey()) { states.add(0,new Integer(FLOW_MAPPING_SIMPLE_VALUE)); expectNode(false,false,true,true); } else { emitter.writeIndicator(ByteList.create("?"),true,false,false); states.add(0,new Integer(FLOW_MAPPING_VALUE)); expectNode(false,false,true,false); } } } public void expectBlockSequenceItem(final boolean first) throws IOException { if(!first && event instanceof SequenceEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); state = ((Integer)states.remove(0)).intValue(); } else { emitter.writeIndent(); emitter.writeIndicator(ByteList.create("-"),true,false,true); states.add(0,new Integer(BLOCK_SEQUENCE_ITEM)); expectNode(false,true,false,false); } } public void expectFirstBlockMappingKey() throws IOException { expectBlockMappingKey(true); } public void expectBlockMappingSimpleValue() throws IOException { emitter.writeIndicator(ByteList.create(": "),false,true,false); states.add(0,new Integer(BLOCK_MAPPING_KEY)); expectNode(false,false,true,false); } public void expectBlockMappingValue() throws IOException { emitter.writeIndent(); emitter.writeIndicator(ByteList.create(": "),true,true,true); states.add(0,new Integer(BLOCK_MAPPING_KEY)); expectNode(false,false,true,false); } public void expectBlockMappingKey(final boolean first) throws IOException { if(!first && event instanceof MappingEndEvent) { indent = ((Integer)indents.remove(0)).intValue(); state = ((Integer)states.remove(0)).intValue(); } else { emitter.writeIndent(); if(checkSimpleKey()) { states.add(0,new Integer(BLOCK_MAPPING_SIMPLE_VALUE)); expectNode(false,false,true,true); } else { emitter.writeIndicator(ByteList.create("?"),true,false,true); states.add(0,new Integer(BLOCK_MAPPING_VALUE)); expectNode(false,false,true,false); } } } private void expectNode(final boolean root, final boolean sequence, final boolean mapping, final boolean simpleKey) throws IOException { rootContext = root; sequenceContext = sequence; mappingContext = mapping; simpleKeyContext = simpleKey; if(event instanceof AliasEvent) { expectAlias(); } else if(event instanceof ScalarEvent || event instanceof CollectionStartEvent) { processAnchor(ByteList.create("&")); processTag(); if(event instanceof ScalarEvent) { expectScalar(); } else if(event instanceof SequenceStartEvent) { if(flowLevel != 0 || canonical || ((SequenceStartEvent)event).getFlowStyle() || checkEmptySequence()) { expectFlowSequence(); } else { expectBlockSequence(); } } else if(event instanceof MappingStartEvent) { if(flowLevel != 0 || canonical || ((MappingStartEvent)event).getFlowStyle() || checkEmptyMapping()) { expectFlowMapping(); } else { expectBlockMapping(); } } } else { throw new EmitterException("expected NodeEvent, but got " + event); } } private void expectAlias() throws IOException { if(((NodeEvent)event).getAnchor() == null) { throw new EmitterException("anchor is not specified for alias"); } processAnchor(ByteList.create("*")); state = ((Integer)states.remove(0)).intValue(); } private void expectScalar() throws IOException { increaseIndent(true,false); processScalar(); indent = ((Integer)indents.remove(0)).intValue(); state = ((Integer)states.remove(0)).intValue(); } private void expectFlowSequence() throws IOException { emitter.writeIndicator(ByteList.create("["),true,true,false); flowLevel++; increaseIndent(true,false); state = FIRST_FLOW_SEQUENCE_ITEM; } private void expectBlockSequence() throws IOException { increaseIndent(false, mappingContext && !indentation); state = FIRST_BLOCK_SEQUENCE_ITEM; } private void expectFlowMapping() throws IOException { emitter.writeIndicator(ByteList.create("{"),true,true,false); flowLevel++; increaseIndent(true,false); state = FIRST_FLOW_MAPPING_KEY; } private void expectBlockMapping() throws IOException { increaseIndent(false,false); state = FIRST_BLOCK_MAPPING_KEY; } private boolean checkEmptySequence() { return event instanceof SequenceStartEvent && !events.isEmpty() && events.get(0) instanceof SequenceEndEvent; } private boolean checkEmptyMapping() { return event instanceof MappingStartEvent && !events.isEmpty() && events.get(0) instanceof MappingEndEvent; } private boolean checkEmptyDocument() { if(!(event instanceof DocumentStartEvent) || events.isEmpty()) { return false; } final Event ev = (Event)events.get(0); return ev instanceof ScalarEvent && ((ScalarEvent)ev).getAnchor() == null && ((ScalarEvent)ev).getTag() == null && ((ScalarEvent)ev).getImplicit() != null && ((ScalarEvent)ev).getValue().realSize == 0; } private boolean checkSimpleKey() { int length = 0; if(event instanceof NodeEvent && null != ((NodeEvent)event).getAnchor()) { if(null == preparedAnchor) { preparedAnchor = prepareAnchor(((NodeEvent)event).getAnchor()); } length += preparedAnchor.length(); } String tag = null; if(event instanceof ScalarEvent) { tag = ((ScalarEvent)event).getTag(); } else if(event instanceof CollectionStartEvent) { tag = ((CollectionStartEvent)event).getTag(); } if(tag != null) { if(null == preparedTag) { preparedTag = emitter.prepareTag(tag); } length += preparedTag.length(); } if(event instanceof ScalarEvent) { if(null == analysis) { analysis = analyzeScalar(((ScalarEvent)event).getValue()); length += analysis.scalar.length(); } } return (length < 128 && (event instanceof AliasEvent || (event instanceof ScalarEvent && !analysis.multiline))); } private void processAnchor(final ByteList indicator) throws IOException { final NodeEvent ev = (NodeEvent)event; if(null == ev.getAnchor()) { preparedAnchor = null; return; } if(null == preparedAnchor) { preparedAnchor = prepareAnchor(ev.getAnchor()); } if(preparedAnchor != null && !"".equals(preparedAnchor)) { indicator.append(preparedAnchor.getBytes()); if(ev instanceof CollectionStartEvent) { indentation = true; } emitter.writeIndicator(indicator,true,false,true); } preparedAnchor = null; } private void processTag() throws IOException { String tag = null; if(event instanceof ScalarEvent) { final ScalarEvent ev = (ScalarEvent)event; tag = ev.getTag(); if(style == 0) { style = chooseScalarStyle(); } if(((!canonical || tag == null) && ((0 == style && ev.getImplicit()[0]) || (0 != style && ev.getImplicit()[1])))) { preparedTag = null; return; } if(ev.getImplicit()[0] && null == tag) { tag = "!"; preparedTag = null; } } else { final CollectionStartEvent ev = (CollectionStartEvent)event; tag = ev.getTag(); if((!canonical || tag == null) && ev.getImplicit()) { preparedTag = null; return; } indentation = true; } if(tag == null) { throw new EmitterException("tag is not specified"); } if(null == preparedTag) { preparedTag = emitter.prepareTag(tag); } if(preparedTag != null && !"".equals(preparedTag)) { emitter.writeIndicator(ByteList.create(preparedTag),true,false,true); emitter.writeSpace(); } preparedTag = null; } private char chooseScalarStyle() { final ScalarEvent ev = (ScalarEvent)event; if(null == analysis) { analysis = analyzeScalar(ev.getValue()); } if(ev.getStyle() == '"' || this.canonical || (analysis.empty && ev.getTag().equals("tag:yaml.org,2002:str"))) { return '"'; } // if(ev.getStyle() == 0 && ev.getImplicit()[0]) { if(ev.getStyle() == 0) { if(!(simpleKeyContext && (analysis.empty || analysis.multiline)) && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { return 0; } } if(ev.getStyle() == 0 && ev.getImplicit()[0] && (!(simpleKeyContext && (analysis.empty || analysis.multiline)) && (flowLevel!=0 && analysis.allowFlowPlain || (flowLevel == 0 && analysis.allowBlockPlain)))) { return 0; } if((ev.getStyle() == '|' || ev.getStyle() == '>') && flowLevel == 0 && analysis.allowBlock) { return '\''; } if((ev.getStyle() == 0 || ev.getStyle() == '\'') && (analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline))) { return '\''; } if(analysis.multiline && !FIRST_SPACE.matcher(ev.getValue()).find() && !analysis.specialCharacters) { return '|'; } return '"'; } private void processScalar() throws IOException { final ScalarEvent ev = (ScalarEvent)event; if(null == analysis) { analysis = analyzeScalar(ev.getValue()); } if(0 == style) { style = chooseScalarStyle(); } final boolean split = !simpleKeyContext; if(style == '"') { emitter.writeDoubleQuoted(analysis.scalar,split); } else if(style == '\'') { emitter.writeSingleQuoted(analysis.scalar,split); } else if(style == '>') { emitter.writeFolded(analysis.scalar); } else if(style == '|') { emitter.writeLiteral(analysis.scalar); } else { emitter.writePlain(analysis.scalar,split); } analysis = null; style = 0; } } void writeStreamStart() { } void writeStreamEnd() throws IOException { flushStream(); } void writeIndicator(final ByteList indicator, final boolean needWhitespace, final boolean whitespace, final boolean indentation) throws IOException { ByteList data = indicator; if(!(env.whitespace || !needWhitespace)) { data.prepend((byte)' '); } env.whitespace = whitespace; env.indentation = env.indentation && indentation; env.column += data.length(); stream.write(data.bytes,0,data.realSize); } void writeIndent() throws IOException { int indent = 0; if(env.indent != -1) { indent = env.indent; } if(!env.indentation || env.column > indent || (env.column == indent && !env.whitespace)) { writeLineBreak(null); } if(env.column < indent) { env.whitespace = true; final ByteList data = new ByteList(); for(int i=0,j=(indent-env.column);i= ending) && (env.column+(ending-start)) > env.bestWidth && split) { if(start < ending) { data = (ByteList)text.subSequence(start,ending); data.append(' '); data.append('\\'); start = ending+1; } else { data = ByteList.create("\\"); } env.column += data.length(); stream.write(data.bytes,0,data.realSize); writeIndent(); env.whitespace = false; env.indentation = false; if(start < (text.length()+1) && text.charAt(start) == ' ') { data = ByteList.create("\\"); stream.write(data.bytes,0,data.realSize); } } ending += 1; } writeIndicator(ByteList.create("\""),false,false,false); } void writeSingleQuoted(final ByteList text, final boolean split) throws IOException { writeIndicator(ByteList.create("'"),true,false,false); boolean spaces = false; boolean breaks = false; int start=0,ending=0; char ceh = 0; ByteList data = null; while(ending <= text.length()) { ceh = 0; if(ending < text.length()) { ceh = text.charAt(ending); } if(spaces) { if(ceh == 0 || ceh != 32) { if(start+1 == ending && env.column > env.bestWidth && split && start != 0 && ending != text.length()) { writeIndent(); } else { data = (ByteList)text.subSequence(start,ending); env.column += data.length(); stream.write(data.bytes,0,data.realSize); } start = ending; } } else if(breaks) { if(ceh == 0 || !('\n' == ceh)) { data = (ByteList)text.subSequence(start,ending); for(int i=0,j=data.length();i" + chomp), true, false, false); writeIndent(); boolean leadingSpace = false; boolean spaces = false; boolean breaks = false; int start=0,ending=0; ByteList data = null; while(ending <= text.length()) { char ceh = 0; if(ending < text.length()) { ceh = text.charAt(ending); } if(breaks) { if(ceh == 0 || !('\n' == ceh)) { if(!leadingSpace && ceh != 0 && ceh != ' ' && text.charAt(start) == '\n') { writeLineBreak(null); } leadingSpace = ceh == ' '; data = (ByteList)text.subSequence(start,ending); for(int i=0,j=data.length();i env.bestWidth) { writeIndent(); } else { data = (ByteList)text.subSequence(start,ending); env.column += data.length(); stream.write(data.bytes,0,data.realSize); } start = ending; } } else { if(ceh == 0 || ' ' == ceh || '\n' == ceh) { data = (ByteList)text.subSequence(start,ending); stream.write(data.bytes,0,data.realSize); if(ceh == 0) { writeLineBreak(null); } start = ending; } } if(ceh != 0) { breaks = '\n' == ceh; spaces = ceh == ' '; } ending++; } } void writeLiteral(final ByteList text) throws IOException { String chomp = determineChomp(text); writeIndicator(ByteList.create("|" + chomp), true, false, false); writeIndent(); boolean breaks = false; int start=0,ending=0; ByteList data = null; while(ending <= text.length()) { char ceh = 0; if(ending < text.length()) { ceh = text.charAt(ending); } if(breaks) { if(ceh == 0 || !('\n' == ceh)) { data = (ByteList)text.subSequence(start,ending); for(int i=0,j=data.length();i env.bestWidth && split) { writeIndent(); env.whitespace = false; env.indentation = false; } else { data = new ByteList(text, start, ending-start); env.column += data.length(); stream.write(data.bytes,0,data.realSize); } start = ending; } } else if(breaks) { if(ceh != '\n') { if((text.bytes[start] & 0xFF) == '\n') { writeLineBreak(null); } data = new ByteList(text, start, ending-start); for(int i=0,j=data.length();i"; } } private final static Pattern DOC_INDIC = Pattern.compile("^(---|\\.\\.\\.)"); private final static Pattern FIRST_SPACE = Pattern.compile("(^|\n) "); private final static String NULL_BL_T_LINEBR = "\0 \t\r\n"; private final static String SPECIAL_INDIC = "#,[]{}#&*!|>'\"%@`"; private final static String FLOW_INDIC = ",?[]{}"; static ScalarAnalysis analyzeScalar(final ByteList scalar) { if(scalar == null || scalar.realSize == 0) { return new ScalarAnalysis(scalar,true,false,false,true,true,true,false,false); } boolean blockIndicators = false; boolean flowIndicators = false; boolean lineBreaks = false; boolean specialCharacters = false; // Whitespaces. boolean inlineSpaces = false; // non-space space+ non-space boolean inlineBreaks = false; // non-space break+ non-space boolean leadingSpaces = false; // ^ space+ (non-space | $) boolean leadingBreaks = false; // ^ break+ (non-space | $) boolean trailingSpaces = false; // (^ | non-space) space+ $ boolean trailingBreaks = false; // (^ | non-space) break+ $ boolean inlineBreaksSpaces = false; // non-space break+ space+ non-space boolean mixedBreaksSpaces = false; // anything else if(DOC_INDIC.matcher(scalar).matches()) { blockIndicators = true; flowIndicators = true; } boolean preceededBySpace = true; boolean followedBySpace = scalar.length() == 1 || NULL_BL_T_LINEBR.indexOf(scalar.charAt(1)) != -1; boolean spaces = false; boolean breaks = false; boolean mixed = false; boolean leading = false; int index = 0; while(index < scalar.length()) { char ceh = scalar.charAt(index); if(index == 0) { if(SPECIAL_INDIC.indexOf(ceh) != -1) { flowIndicators = true; blockIndicators = true; } if(ceh == '?' || ceh == ':') { flowIndicators = true; if(followedBySpace) { blockIndicators = true; } } if(ceh == '-' && followedBySpace) { flowIndicators = true; blockIndicators = true; } } else { if(FLOW_INDIC.indexOf(ceh) != -1) { flowIndicators = true; } if(ceh == ':') { flowIndicators = true; if(followedBySpace) { blockIndicators = true; } } if(ceh == '#' && preceededBySpace) { flowIndicators = true; blockIndicators = true; } } if(ceh == '\n') { lineBreaks = true; } if(!(ceh == '\n' || ('\u0020' <= ceh && ceh <= '\u007E'))) { specialCharacters = true; } if(' ' == ceh || '\n' == ceh) { if(spaces && breaks) { if(ceh != ' ') { mixed = true; } } else if(spaces) { if(ceh != ' ') { breaks = true; mixed = true; } } else if(breaks) { if(ceh == ' ') { spaces = true; } } else { leading = (index == 0); if(ceh == ' ') { spaces = true; } else { breaks = true; } } } else if(spaces || breaks) { if(leading) { if(spaces && breaks) { mixedBreaksSpaces = true; } else if(spaces) { leadingSpaces = true; } else if(breaks) { leadingBreaks = true; } } else { if(mixed) { mixedBreaksSpaces = true; } else if(spaces && breaks) { inlineBreaksSpaces = true; } else if(spaces) { inlineSpaces = true; } else if(breaks) { inlineBreaks = true; } } spaces = breaks = mixed = leading = false; } if((spaces || breaks) && (index == scalar.length()-1)) { if(spaces && breaks) { mixedBreaksSpaces = true; } else if(spaces) { trailingSpaces = true; if(leading) { leadingSpaces = true; } } else if(breaks) { trailingBreaks = true; if(leading) { leadingBreaks = true; } } spaces = breaks = mixed = leading = false; } index++; preceededBySpace = NULL_BL_T_LINEBR.indexOf(ceh) != -1; followedBySpace = index+1 >= scalar.length() || NULL_BL_T_LINEBR.indexOf(scalar.charAt(index+1)) != -1; } boolean allowFlowPlain = true; boolean allowBlockPlain = true; boolean allowSingleQuoted = true; boolean allowDoubleQuoted = true; boolean allowBlock = true; if(leadingSpaces || leadingBreaks || trailingSpaces) { allowFlowPlain = allowBlockPlain = allowBlock = allowSingleQuoted = false; } if(trailingBreaks) { allowFlowPlain = allowBlockPlain = false; } if(inlineBreaksSpaces) { allowFlowPlain = allowBlockPlain = allowSingleQuoted = false; } if(mixedBreaksSpaces || specialCharacters) { allowFlowPlain = allowBlockPlain = allowSingleQuoted = allowBlock = false; } if(inlineBreaks) { allowFlowPlain = allowBlockPlain = allowSingleQuoted = false; } if(trailingBreaks) { allowSingleQuoted = false; } if(lineBreaks) { allowFlowPlain = allowBlockPlain = false; } if(flowIndicators) { allowFlowPlain = false; } if(blockIndicators) { allowBlockPlain = false; } return new ScalarAnalysis(scalar,false,lineBreaks,allowFlowPlain,allowBlockPlain,allowSingleQuoted,allowDoubleQuoted,allowBlock,specialCharacters); } static String determineChomp(final ByteList text) { char ceh = ' '; char ceh2 = ' '; if(text.realSize > 0) { ceh = (char)(text.bytes[text.realSize-1] & 0xFF); if(text.realSize > 1) { ceh2 = (char)(text.bytes[text.realSize-2] & 0xFF); } } return (ceh == '\n') ? ((ceh2 == '\n') ? "+" : "") : "-"; } public static void main(final String[] args) throws IOException { final String filename = args[0]; // filename to test against System.out.println("File contents:"); final BufferedInputStream read = new BufferedInputStream(new FileInputStream(filename)); int last = -1; while((last = read.read()) != -1) { System.out.print((char)last); } read.close(); System.out.println("--------------------------------"); final Emitter emitter = new EmitterImpl(System.out,YAML.config()); final Parser pars = new ParserImpl(new ScannerImpl(new FileInputStream(filename))); for(final Iterator iter = pars.eachEvent();iter.hasNext();) { emitter.emit((Event)iter.next()); } } }// EmitterImpl jvyamlb-0.2.5/src/main/org/jvyamlb/Parser.java000066400000000000000000000007251151213612200212250ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Iterator; import org.jvyamlb.events.Event; /** * @author Ola Bini */ public interface Parser { boolean checkEvent(final Class[] choices); Event peekEvent(); Event getEvent(); Iterator eachEvent(); Iterator iterator(); void parseStream(); Event parseStreamNext(); }// Parser jvyamlb-0.2.5/src/main/org/jvyamlb/ParserImpl.java000066400000000000000000001161241151213612200220500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.InputStream; import java.io.FileInputStream; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import org.jvyamlb.exceptions.ParserException; import org.jvyamlb.events.*; import org.jvyamlb.tokens.*; import org.jvyamlb.util.IntStack; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ParserImpl implements Parser { // Memnonics for the production table private final static int P_STREAM = 0; private final static int P_STREAM_START = 1; // TERMINAL private final static int P_STREAM_END = 2; // TERMINAL private final static int P_IMPLICIT_DOCUMENT = 3; private final static int P_EXPLICIT_DOCUMENT = 4; private final static int P_DOCUMENT_START = 5; private final static int P_DOCUMENT_START_IMPLICIT = 6; private final static int P_DOCUMENT_END = 7; private final static int P_BLOCK_NODE = 8; private final static int P_BLOCK_CONTENT = 9; private final static int P_PROPERTIES = 10; private final static int P_PROPERTIES_END = 11; private final static int P_FLOW_CONTENT = 12; private final static int P_BLOCK_SEQUENCE = 13; private final static int P_BLOCK_MAPPING = 14; private final static int P_FLOW_SEQUENCE = 15; private final static int P_FLOW_MAPPING = 16; private final static int P_SCALAR = 17; private final static int P_BLOCK_SEQUENCE_ENTRY = 18; private final static int P_BLOCK_MAPPING_ENTRY = 19; private final static int P_BLOCK_MAPPING_ENTRY_VALUE = 20; private final static int P_BLOCK_NODE_OR_INDENTLESS_SEQUENCE = 21; private final static int P_BLOCK_SEQUENCE_START = 22; private final static int P_BLOCK_SEQUENCE_END = 23; private final static int P_BLOCK_MAPPING_START = 24; private final static int P_BLOCK_MAPPING_END = 25; private final static int P_INDENTLESS_BLOCK_SEQUENCE = 26; private final static int P_BLOCK_INDENTLESS_SEQUENCE_START = 27; private final static int P_INDENTLESS_BLOCK_SEQUENCE_ENTRY = 28; private final static int P_BLOCK_INDENTLESS_SEQUENCE_END = 29; private final static int P_FLOW_SEQUENCE_START = 30; private final static int P_FLOW_SEQUENCE_ENTRY = 31; private final static int P_FLOW_SEQUENCE_END = 32; private final static int P_FLOW_MAPPING_START = 33; private final static int P_FLOW_MAPPING_ENTRY = 34; private final static int P_FLOW_MAPPING_END = 35; private final static int P_FLOW_INTERNAL_MAPPING_START = 36; private final static int P_FLOW_INTERNAL_CONTENT = 37; private final static int P_FLOW_INTERNAL_VALUE = 38; private final static int P_FLOW_INTERNAL_MAPPING_END = 39; private final static int P_FLOW_ENTRY_MARKER = 40; private final static int P_FLOW_NODE = 41; private final static int P_FLOW_MAPPING_INTERNAL_CONTENT = 42; private final static int P_FLOW_MAPPING_INTERNAL_VALUE = 43; private final static int P_ALIAS = 44; private final static int P_EMPTY_SCALAR = 45; private final static String[] productionNames = new String[46]; static { productionNames[0]="P_STREAM"; productionNames[1]="P_STREAM_START"; productionNames[2]="P_STREAM_END"; productionNames[3]="P_IMPLICIT_DOCUMENT"; productionNames[4]="P_EXPLICIT_DOCUMENT"; productionNames[5]="P_DOCUMENT_START"; productionNames[6]="P_DOCUMENT_START_IMPLICIT"; productionNames[7]="P_DOCUMENT_END"; productionNames[8]="P_BLOCK_NODE"; productionNames[9]="P_BLOCK_CONTENT"; productionNames[10]="P_PROPERTIES"; productionNames[11]="P_PROPERTIES_END"; productionNames[12]="P_FLOW_CONTENT"; productionNames[13]="P_BLOCK_SEQUENCE"; productionNames[14]="P_BLOCK_MAPPING"; productionNames[15]="P_FLOW_SEQUENCE"; productionNames[16]="P_FLOW_MAPPING"; productionNames[17]="P_SCALAR"; productionNames[18]="P_BLOCK_SEQUENCE_ENTRY"; productionNames[19]="P_BLOCK_MAPPING_ENTRY"; productionNames[20]="P_BLOCK_MAPPING_ENTRY_VALUE"; productionNames[21]="P_BLOCK_NODE_OR_INDENTLESS_SEQUENCE"; productionNames[22]="P_BLOCK_SEQUENCE_START"; productionNames[23]="P_BLOCK_SEQUENCE_END"; productionNames[24]="P_BLOCK_MAPPING_START"; productionNames[25]="P_BLOCK_MAPPING_END"; productionNames[26]="P_INDENTLESS_BLOCK_SEQUENCE"; productionNames[27]="P_BLOCK_INDENTLESS_SEQUENCE_START"; productionNames[28]="P_INDENTLESS_BLOCK_SEQUENCE_ENTRY"; productionNames[29]="P_BLOCK_INDENTLESS_SEQUENCE_END"; productionNames[30]="P_FLOW_SEQUENCE_START"; productionNames[31]="P_FLOW_SEQUENCE_ENTRY"; productionNames[32]="P_FLOW_SEQUENCE_END"; productionNames[33]="P_FLOW_MAPPING_START"; productionNames[34]="P_FLOW_MAPPING_ENTRY"; productionNames[35]="P_FLOW_MAPPING_END"; productionNames[36]="P_FLOW_INTERNAL_MAPPING_START"; productionNames[37]="P_FLOW_INTERNAL_CONTENT"; productionNames[38]="P_FLOW_INTERNAL_VALUE"; productionNames[39]="P_FLOW_INTERNAL_MAPPING_END"; productionNames[40]="P_FLOW_ENTRY_MARKER"; productionNames[41]="P_FLOW_NODE"; productionNames[42]="P_FLOW_MAPPING_INTERNAL_CONTENT"; productionNames[43]="P_FLOW_MAPPING_INTERNAL_VALUE"; productionNames[44]="P_ALIAS"; productionNames[45]="P_EMPTY_SCALAR"; } private final static DocumentEndEvent DOCUMENT_END_TRUE = new DocumentEndEvent(true); private final static DocumentEndEvent DOCUMENT_END_FALSE = new DocumentEndEvent(false); private final static MappingEndEvent MAPPING_END = new MappingEndEvent(); private final static SequenceEndEvent SEQUENCE_END = new SequenceEndEvent(); private final static StreamEndEvent STREAM_END = new StreamEndEvent(); private final static StreamStartEvent STREAM_START = new StreamStartEvent(); protected static class ProductionEnvironment { private List tags; private List anchors; private List tagTokens; private List anchorTokens; private Map tagHandles; private int[] yamlVersion; private int[] defaultYamlVersion; public ProductionEnvironment(final YAMLConfig cfg) { this.tags = new LinkedList(); this.anchors = new LinkedList(); this.tagTokens = new LinkedList(); this.anchorTokens = new LinkedList(); this.tagHandles = new HashMap(); this.yamlVersion = null; this.defaultYamlVersion = new int[2]; this.defaultYamlVersion[0] = Integer.parseInt(cfg.version().substring(0,cfg.version().indexOf('.'))); this.defaultYamlVersion[1] = Integer.parseInt(cfg.version().substring(cfg.version().indexOf('.')+1)); } public List getTags() { return this.tags; } public List getAnchors() { return this.anchors; } public List getTagTokens() { return this.tagTokens; } public List getAnchorTokens() { return this.anchorTokens; } public Map getTagHandles() { return this.tagHandles; } public int[] getYamlVersion() { return this.yamlVersion; } public int[] getFinalYamlVersion() { if(null == this.yamlVersion) { return this.defaultYamlVersion; } return this.yamlVersion; } public void setYamlVersion(final int[] yamlVersion) { this.yamlVersion = yamlVersion; } protected StreamStartEvent getStreamStart(final Token t) { return STREAM_START; } protected StreamEndEvent getStreamEnd(final Token t) { return STREAM_END; } protected DocumentStartEvent getDocumentStart(final boolean explicit, final int[] version, final Map tags, final Token t) { return new DocumentStartEvent(explicit, version, tags); } protected DocumentEndEvent getDocumentEndImplicit(final Token t) { return DOCUMENT_END_FALSE; } protected DocumentEndEvent getDocumentEndExplicit(final Token t) { return DOCUMENT_END_TRUE; } protected ScalarEvent getScalar(final String anchor, final String tag, final boolean[] implicit, final ByteList value, final char style, final Token t, final Token anchorT, final Token tagT) { return new ScalarEvent(anchor, tag, implicit, value, style); } protected MappingStartEvent getMappingStart(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Token t, final Token anchorT, final Token tagT) { return new MappingStartEvent(anchor, tag, implicit, flowStyle); } protected MappingEndEvent getMappingEnd(final Token t) { return MAPPING_END; } protected SequenceStartEvent getSequenceStart(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Token t, final Token anchorT, final Token tagT) { return new SequenceStartEvent(anchor, tag, implicit, flowStyle); } protected SequenceEndEvent getSequenceEnd(final Token t) { return SEQUENCE_END; } protected AliasEvent getAlias(final String value, final Token t) { return new AliasEvent(value); } protected void setEmptyToken(Token t) { } protected Token getEmptyToken(Scanner scanner) { return scanner.peekToken(); } protected void parserException(final String when, final String what, final String note, final Token t) { throw new ParserException(when, what, note); } public Event produce(final int current, final IntStack parseStack, final Scanner scanner) { switch(current) { case P_STREAM: { parseStack.push(P_STREAM_END); parseStack.push(P_EXPLICIT_DOCUMENT); parseStack.push(P_IMPLICIT_DOCUMENT); parseStack.push(P_STREAM_START); return null; } case P_STREAM_START: { return getStreamStart(scanner.getToken()); } case P_STREAM_END: { return getStreamEnd(scanner.getToken()); } case P_IMPLICIT_DOCUMENT: { final Token curr = scanner.peekToken(); if(!(curr instanceof DirectiveToken || curr instanceof DocumentStartToken || curr instanceof StreamEndToken)) { parseStack.push(P_DOCUMENT_END); parseStack.push(P_BLOCK_NODE); parseStack.push(P_DOCUMENT_START_IMPLICIT); } return null; } case P_EXPLICIT_DOCUMENT: { if(!(scanner.peekToken() instanceof StreamEndToken)) { parseStack.push(P_EXPLICIT_DOCUMENT); parseStack.push(P_DOCUMENT_END); parseStack.push(P_BLOCK_NODE); parseStack.push(P_DOCUMENT_START); } return null; } case P_DOCUMENT_START: { Token tok = scanner.peekToken(); final Object[] directives = processDirectives(this,scanner); if(!(scanner.peekToken() instanceof DocumentStartToken)) { parserException(null,"expected '', but found " + tok.getClass().getName(),null,scanner.peekToken()); } scanner.getToken(); return getDocumentStart(true,(int[])directives[0],(Map)directives[1], tok); } case P_DOCUMENT_START_IMPLICIT: { Token tok = scanner.peekToken(); final Object[] directives = processDirectives(this,scanner); return getDocumentStart(false,(int[])directives[0],(Map)directives[1], tok); } case P_DOCUMENT_END: { Token tok = scanner.peekToken(); boolean explicit = false; while(scanner.peekToken() instanceof DocumentEndToken) { tok = scanner.getToken(); explicit = true; } return explicit ? getDocumentEndExplicit(tok) : getDocumentEndImplicit(tok); } case P_BLOCK_NODE: { final Token curr = scanner.peekToken(); if(curr instanceof DirectiveToken || curr instanceof DocumentStartToken || curr instanceof DocumentEndToken || curr instanceof StreamEndToken) { parseStack.push(P_EMPTY_SCALAR); } else { if(curr instanceof AliasToken) { parseStack.push(P_ALIAS); } else { parseStack.push(P_PROPERTIES_END); parseStack.push(P_BLOCK_CONTENT); parseStack.push(P_PROPERTIES); } } return null; } case P_BLOCK_CONTENT: { final Token tok = scanner.peekToken(); if(tok instanceof BlockSequenceStartToken) { parseStack.push(P_BLOCK_SEQUENCE); } else if(tok instanceof BlockMappingStartToken) { parseStack.push(P_BLOCK_MAPPING); } else if(tok instanceof FlowSequenceStartToken) { parseStack.push(P_FLOW_SEQUENCE); } else if(tok instanceof FlowMappingStartToken) { parseStack.push(P_FLOW_MAPPING); } else if(tok instanceof ScalarToken) { parseStack.push(P_SCALAR); } else { // Part of solution for JRUBY-718 boolean[] implicit = new boolean[]{false,false}; return getScalar((String)this.getAnchors().get(0),(String)this.getTags().get(0),implicit,new ByteList(new byte[0],false),'\'', tok, (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } return null; } case P_PROPERTIES: { String anchor = null; Object tag = null; Token anchorToken = null; Token tagToken = null; if(scanner.peekToken() instanceof AnchorToken) { anchorToken = scanner.getToken(); anchor = ((AnchorToken)anchorToken).getValue(); if(scanner.peekToken() instanceof TagToken) { tagToken = scanner.getToken(); tag = ((TagToken)tagToken).getValue(); } } else if(scanner.peekToken() instanceof TagToken) { tagToken = scanner.getToken(); tag = ((TagToken)tagToken).getValue(); if(scanner.peekToken() instanceof AnchorToken) { anchorToken = scanner.getToken(); anchor = ((AnchorToken)anchorToken).getValue(); } } if(tag != null && !tag.equals("!")) { final String handle = ScannerImpl.into(((ByteList[])tag)[0]); String suffix = ScannerImpl.into(((ByteList[])tag)[1]); int ix = -1; if((ix = suffix.indexOf("^")) != -1) { suffix = suffix.substring(0,ix) + suffix.substring(ix+1); } if(handle != null) { if(!this.getTagHandles().containsKey(handle)) { parserException("while parsing a node","found undefined tag handle " + handle,null,tagToken); } if((ix = suffix.indexOf("/")) != -1) { String before = suffix.substring(0,ix); String after = suffix.substring(ix+1); if(ONLY_WORD.matcher(before).matches()) { tag = "tag:" + before + ".yaml.org,2002:" + after; } else { if(before.startsWith("tag:")) { tag = before + ":" + after; } else { tag = "tag:" + before + ":" + after; } } } else { tag = ((String)this.getTagHandles().get(handle)) + suffix; } } else { tag = suffix; } } this.getAnchors().add(0,anchor); this.getTags().add(0,tag); this.getAnchorTokens().add(0,anchorToken); this.getTagTokens().add(0,tagToken); return null; } case P_PROPERTIES_END: { this.getAnchors().remove(0); this.getTags().remove(0); this.getAnchorTokens().remove(0); this.getTagTokens().remove(0); return null; } case P_FLOW_CONTENT: { final Token tok = scanner.peekToken(); if(tok instanceof FlowSequenceStartToken) { parseStack.push(P_FLOW_SEQUENCE); } else if(tok instanceof FlowMappingStartToken) { parseStack.push(P_FLOW_MAPPING); } else if(tok instanceof ScalarToken) { parseStack.push(P_SCALAR); } else { parserException("while scanning a flow node","expected the node content, but found " + tok.getClass().getName(),null,tok); } return null; } case P_BLOCK_SEQUENCE: { parseStack.push(P_BLOCK_SEQUENCE_END); parseStack.push(P_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_BLOCK_SEQUENCE_START); return null; } case P_BLOCK_MAPPING: { parseStack.push(P_BLOCK_MAPPING_END); parseStack.push(P_BLOCK_MAPPING_ENTRY); parseStack.push(P_BLOCK_MAPPING_START); return null; } case P_FLOW_SEQUENCE: { parseStack.push(P_FLOW_SEQUENCE_END); parseStack.push(P_FLOW_SEQUENCE_ENTRY); parseStack.push(P_FLOW_SEQUENCE_START); return null; } case P_FLOW_MAPPING: { parseStack.push(P_FLOW_MAPPING_END); parseStack.push(P_FLOW_MAPPING_ENTRY); parseStack.push(P_FLOW_MAPPING_START); return null; } case P_SCALAR: { final ScalarToken tok = (ScalarToken)scanner.getToken(); boolean[] implicit = null; if((tok.getPlain() && this.getTags().get(0) == null) || "!".equals(this.getTags().get(0))) { implicit = new boolean[]{true,false}; } else if(this.getTags().get(0) == null) { implicit = new boolean[]{false,true}; } else { implicit = new boolean[]{false,false}; } return getScalar((String)this.getAnchors().get(0),(String)this.getTags().get(0),implicit,tok.getValue(),tok.getStyle(), tok, (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_BLOCK_SEQUENCE_ENTRY: { if(scanner.peekToken() instanceof BlockEntryToken) { scanner.getToken(); if(!(scanner.peekToken() instanceof BlockEntryToken || scanner.peekToken() instanceof BlockEndToken)) { parseStack.push(P_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_BLOCK_NODE); } else { parseStack.push(P_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_EMPTY_SCALAR); } } return null; } case P_BLOCK_MAPPING_ENTRY: { if(scanner.peekToken() instanceof KeyToken || scanner.peekToken() instanceof ValueToken) { if(scanner.peekToken() instanceof KeyToken) { scanner.getToken(); final Token curr = scanner.peekToken(); if(!(curr instanceof KeyToken || curr instanceof ValueToken || curr instanceof BlockEndToken)) { parseStack.push(P_BLOCK_MAPPING_ENTRY); parseStack.push(P_BLOCK_MAPPING_ENTRY_VALUE); parseStack.push(P_BLOCK_NODE_OR_INDENTLESS_SEQUENCE); } else { parseStack.push(P_BLOCK_MAPPING_ENTRY); parseStack.push(P_BLOCK_MAPPING_ENTRY_VALUE); parseStack.push(P_EMPTY_SCALAR); } } else { parseStack.push(P_BLOCK_MAPPING_ENTRY); parseStack.push(P_BLOCK_MAPPING_ENTRY_VALUE); parseStack.push(P_EMPTY_SCALAR); } } return null; } case P_BLOCK_MAPPING_ENTRY_VALUE: { if(scanner.peekToken() instanceof KeyToken || scanner.peekToken() instanceof ValueToken) { if(scanner.peekToken() instanceof ValueToken) { final Token value = scanner.getToken(); final Token curr = scanner.peekToken(); if(!(curr instanceof KeyToken || curr instanceof ValueToken || curr instanceof BlockEndToken)) { if(curr instanceof ScalarToken && scanner.peekToken(1) instanceof BlockEntryToken) { // System.err.println("warning, possibly invalid YAML, eating token: " + curr); scanner.getToken(); } parseStack.push(P_BLOCK_NODE_OR_INDENTLESS_SEQUENCE); } else { setEmptyToken(value); parseStack.push(P_EMPTY_SCALAR); } } else { parseStack.push(P_EMPTY_SCALAR); } } return null; } case P_BLOCK_NODE_OR_INDENTLESS_SEQUENCE: { if(scanner.peekToken() instanceof AliasToken) { parseStack.push(P_ALIAS); } else { if(scanner.peekToken() instanceof BlockEntryToken) { parseStack.push(P_INDENTLESS_BLOCK_SEQUENCE); parseStack.push(P_PROPERTIES); } else { parseStack.push(P_BLOCK_CONTENT); parseStack.push(P_PROPERTIES); } } return null; } case P_BLOCK_SEQUENCE_START: { final boolean implicit = this.getTags().get(0) == null || this.getTags().get(0).equals("!"); return getSequenceStart((String)this.getAnchors().get(0), (String)this.getTags().get(0), implicit,false,scanner.getToken(), (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_BLOCK_SEQUENCE_END: { Token tok = null; if(!(scanner.peekToken() instanceof BlockEndToken)) { tok = scanner.peekToken(); parserException("while scanning a block collection","expected , but found " + tok.getClass().getName(),null,tok); } return getSequenceEnd(scanner.getToken()); } case P_BLOCK_MAPPING_START: { final boolean implicit = this.getTags().get(0) == null || this.getTags().get(0).equals("!"); return getMappingStart((String)this.getAnchors().get(0), (String)this.getTags().get(0), implicit,false,scanner.getToken(), (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_BLOCK_MAPPING_END: { Token tok = null; if(!(scanner.peekToken() instanceof BlockEndToken)) { tok = scanner.peekToken(); parserException("while scanning a block mapping","expected , but found " + tok.getClass().getName(),null,tok); } return getMappingEnd(scanner.getToken()); } case P_INDENTLESS_BLOCK_SEQUENCE: { parseStack.push(P_BLOCK_INDENTLESS_SEQUENCE_END); parseStack.push(P_INDENTLESS_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_BLOCK_INDENTLESS_SEQUENCE_START); return null; } case P_BLOCK_INDENTLESS_SEQUENCE_START: { final boolean implicit = this.getTags().get(0) == null || this.getTags().get(0).equals("!"); return getSequenceStart((String)this.getAnchors().get(0), (String)this.getTags().get(0), implicit, false, scanner.peekToken(), (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_INDENTLESS_BLOCK_SEQUENCE_ENTRY: { if(scanner.peekToken() instanceof BlockEntryToken) { scanner.getToken(); final Token curr = scanner.peekToken(); if(!(curr instanceof BlockEntryToken || curr instanceof KeyToken || curr instanceof ValueToken || curr instanceof BlockEndToken)) { parseStack.push(P_INDENTLESS_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_BLOCK_NODE); } else { parseStack.push(P_INDENTLESS_BLOCK_SEQUENCE_ENTRY); parseStack.push(P_EMPTY_SCALAR); } } return null; } case P_BLOCK_INDENTLESS_SEQUENCE_END: { return getSequenceEnd(scanner.peekToken()); } case P_FLOW_SEQUENCE_START: { final boolean implicit = this.getTags().get(0) == null || this.getTags().get(0).equals("!"); return getSequenceStart((String)this.getAnchors().get(0), (String)this.getTags().get(0), implicit,true,scanner.getToken(), (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_FLOW_SEQUENCE_ENTRY: { if(!(scanner.peekToken() instanceof FlowSequenceEndToken)) { if(scanner.peekToken() instanceof KeyToken) { parseStack.push(P_FLOW_SEQUENCE_ENTRY); parseStack.push(P_FLOW_ENTRY_MARKER); parseStack.push(P_FLOW_INTERNAL_MAPPING_END); parseStack.push(P_FLOW_INTERNAL_VALUE); parseStack.push(P_FLOW_INTERNAL_CONTENT); parseStack.push(P_FLOW_INTERNAL_MAPPING_START); } else { parseStack.push(P_FLOW_SEQUENCE_ENTRY); parseStack.push(P_FLOW_NODE); parseStack.push(P_FLOW_ENTRY_MARKER); } } return null; } case P_FLOW_SEQUENCE_END: { return getSequenceEnd(scanner.getToken()); } case P_FLOW_MAPPING_START: { final boolean implicit = this.getTags().get(0) == null || this.getTags().get(0).equals("!"); return getMappingStart((String)this.getAnchors().get(0), (String)this.getTags().get(0), implicit,true, scanner.getToken(), (Token)this.getAnchorTokens().get(0), (Token)this.getTagTokens().get(0)); } case P_FLOW_MAPPING_ENTRY: { if(!(scanner.peekToken() instanceof FlowMappingEndToken)) { if(scanner.peekToken() instanceof KeyToken) { parseStack.push(P_FLOW_MAPPING_ENTRY); parseStack.push(P_FLOW_ENTRY_MARKER); parseStack.push(P_FLOW_MAPPING_INTERNAL_VALUE); parseStack.push(P_FLOW_MAPPING_INTERNAL_CONTENT); } else { parseStack.push(P_FLOW_MAPPING_ENTRY); parseStack.push(P_EMPTY_SCALAR); parseStack.push(P_FLOW_NODE); parseStack.push(P_FLOW_ENTRY_MARKER); } } return null; } case P_FLOW_MAPPING_END: { return getMappingEnd(scanner.getToken()); } case P_FLOW_INTERNAL_MAPPING_START: { return getMappingStart(null,null,true,true, scanner.getToken(), null, null); } case P_FLOW_INTERNAL_CONTENT: { final Token curr = scanner.peekToken(); if(!(curr instanceof ValueToken || curr instanceof FlowEntryToken || curr instanceof FlowSequenceEndToken)) { parseStack.push(P_FLOW_NODE); } else { parseStack.push(P_EMPTY_SCALAR); } return null; } case P_FLOW_INTERNAL_VALUE: { if(scanner.peekToken() instanceof ValueToken) { scanner.getToken(); if(!((scanner.peekToken() instanceof FlowEntryToken) || (scanner.peekToken() instanceof FlowSequenceEndToken))) { parseStack.push(P_FLOW_NODE); } else { parseStack.push(P_EMPTY_SCALAR); } } else { parseStack.push(P_EMPTY_SCALAR); } return null; } case P_FLOW_INTERNAL_MAPPING_END: { return getMappingEnd(scanner.peekToken()); } case P_FLOW_ENTRY_MARKER: { if(scanner.peekToken() instanceof FlowEntryToken) { scanner.getToken(); } return null; } case P_FLOW_NODE: { if(scanner.peekToken() instanceof AliasToken) { parseStack.push(P_ALIAS); } else { parseStack.push(P_PROPERTIES_END); parseStack.push(P_FLOW_CONTENT); parseStack.push(P_PROPERTIES); } return null; } case P_FLOW_MAPPING_INTERNAL_CONTENT: { final Token curr = scanner.peekToken(); if(!(curr instanceof ValueToken || curr instanceof FlowEntryToken || curr instanceof FlowMappingEndToken)) { scanner.getToken(); parseStack.push(P_FLOW_NODE); } else { parseStack.push(P_EMPTY_SCALAR); } return null; } case P_FLOW_MAPPING_INTERNAL_VALUE: { if(scanner.peekToken() instanceof ValueToken) { scanner.getToken(); if(!(scanner.peekToken() instanceof FlowEntryToken || scanner.peekToken() instanceof FlowMappingEndToken)) { parseStack.push(P_FLOW_NODE); } else { parseStack.push(P_EMPTY_SCALAR); } } else { parseStack.push(P_EMPTY_SCALAR); } return null; } case P_ALIAS: { final AliasToken tok = (AliasToken)scanner.getToken(); return getAlias(tok.getValue(), tok); } case P_EMPTY_SCALAR: { return getScalar(null,null,new boolean[]{true,false},new ByteList(ByteList.NULL_ARRAY),(char)0, getEmptyToken(scanner), null, null); } } return null; } } private final static Map DEFAULT_TAGS_1_0 = new HashMap(); private final static Map DEFAULT_TAGS_1_1 = new HashMap(); static { DEFAULT_TAGS_1_0.put("!","tag:yaml.org,2002:"); DEFAULT_TAGS_1_0.put("!!",""); DEFAULT_TAGS_1_1.put("!","!"); DEFAULT_TAGS_1_1.put("!!","tag:yaml.org,2002:"); } private final static Pattern ONLY_WORD = Pattern.compile("^\\w+$"); private static Object[] processDirectives(final ProductionEnvironment env, final Scanner scanner) { while(scanner.peekToken() instanceof DirectiveToken) { final DirectiveToken tok = (DirectiveToken)scanner.getToken(); if(tok.getName().equals("YAML")) { if(env.getYamlVersion() != null) { env.parserException(null,"found duplicate YAML directive",null,tok); } final int major = Integer.parseInt(tok.getValue()[0]); final int minor = Integer.parseInt(tok.getValue()[1]); if(major != 1) { env.parserException(null,"found incompatible YAML document (version 1.* is required)",null,tok); } env.setYamlVersion(new int[]{major,minor}); } else if(tok.getName().equals("TAG")) { final String handle = tok.getValue()[0]; final String prefix = tok.getValue()[1]; if(env.getTagHandles().containsKey(handle)) { env.parserException(null,"duplicate tag handle " + handle,null,tok); } env.getTagHandles().put(handle,prefix); } } Object[] value = new Object[2]; value[0] = env.getFinalYamlVersion(); if(!env.getTagHandles().isEmpty()) { value[1] = new HashMap(env.getTagHandles()); } final Map baseTags = ((int[])value[0])[1] == 0 ? DEFAULT_TAGS_1_0 : DEFAULT_TAGS_1_1; for(final Iterator iter = baseTags.keySet().iterator();iter.hasNext();) { final Object key = iter.next(); if(!env.getTagHandles().containsKey(key)) { env.getTagHandles().put(key,baseTags.get(key)); } } return value; } protected Scanner scanner = null; private YAMLConfig cfg = null; public ParserImpl(final Scanner scanner) { this(scanner, YAML.config()); } public ParserImpl(final Scanner scanner, final YAMLConfig cfg) { this.scanner = scanner; this.cfg = cfg; } private Event currentEvent = null; public boolean checkEvent(final Class[] choices) { parseStream(); if(this.currentEvent == null) { this.currentEvent = parseStreamNext(); } if(this.currentEvent != null) { if(choices.length == 0) { return true; } for(int i=0,j=choices.length;iOla Bini */ public class Position { public static class Range { public final Position start; public final Position end; public Range(final Position start) { this(start, start); } public Range(final Position start, final Position end) { this.start = start; this.end = end; } public Range withStart(final Position start) { return new Range(start, this.end); } public Range withEnd(final Position end) { return new Range(this.start, end); } public boolean equals(Object other) { boolean res = this == other; if(!res && (other instanceof Range)) { Range o = (Range)other; res = this.start.equals(o.start) && this.end.equals(o.end); } return res; } public String toString() { return "#"; } } public final int line; public final int column; public final int offset; public Position(final int line, final int column, final int offset) { this.line = line; this.column = column; this.offset = offset; } public Position withLine(final int line) { return new Position(line, this.column, this.offset); } public Position withColumn(final int column) { return new Position(this.line, column, this.offset); } public Position withOffset(final int offset) { return new Position(this.line, this.column, offset); } public boolean equals(Object other) { boolean res = this == other; if(!res && (other instanceof Position)) { Position o = (Position)other; res = this.line == o.line && this.column == o.column && this.offset == o.offset; } return res; } public String toString() { return "[" + line + ":" + column + "(" + offset + ")]"; } }// Position jvyamlb-0.2.5/src/main/org/jvyamlb/Positionable.java000066400000000000000000000004321151213612200224140ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public interface Positionable { Position getPosition(); Position.Range getRange(); }// Positionable jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningComposer.java000066400000000000000000000004141151213612200237760ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public interface PositioningComposer extends Composer, Positionable { }// PositioningComposer jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningComposerImpl.java000066400000000000000000000042641151213612200246270ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import org.jvyamlb.nodes.PositionedScalarNode; import org.jvyamlb.nodes.PositionedMappingNode; import org.jvyamlb.nodes.PositionedSequenceNode; import org.jvyamlb.nodes.Node; import org.jvyamlb.exceptions.PositionedComposerException; import org.jruby.util.ByteList; import org.jvyamlb.events.Event; import java.util.Map; import java.util.List; /** * @author Ola Bini */ public class PositioningComposerImpl extends ComposerImpl implements PositioningComposer { public PositioningComposerImpl(final PositioningParser parser, final Resolver resolver) { super(parser, resolver); } public Position getPosition() { return ((PositioningParser)parser).getPosition(); } public Position.Range getRange() { return ((PositioningParser)parser).getRange(); } protected Node getScalar(final String tag, final ByteList value, final char style, final Event e) { return new PositionedScalarNode(tag,value,style, ((Positionable)e).getRange()); } protected Node createMapping(final String tag, final Map value, final boolean flowStyle, final Event e) { return new PositionedMappingNode(tag,value,flowStyle, new Position.Range(((Positionable)e).getPosition())); } protected void finalizeMapping(final Node node, final Event e) { ((PositionedMappingNode)node).setRange(((Positionable)node).getRange().withEnd(((Positionable)e).getRange().end)); } protected Node createSequence(final String tag, final List value, final boolean flowStyle, final Event e) { return new PositionedSequenceNode(tag,value,flowStyle, new Position.Range(((Positionable)e).getPosition())); } protected void finalizeSequence(final Node node, final Event e) { ((PositionedSequenceNode)node).setRange(((Positionable)node).getRange().withEnd(((Positionable)e).getRange().end)); } protected void composerException(final String when, final String what, final String note, final Event e) { throw new PositionedComposerException(when, what, note, ((Positionable)e).getRange()); } } jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningParser.java000066400000000000000000000004061151213612200234440ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public interface PositioningParser extends Parser, Positionable { }// PositioningParser jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningParserImpl.java000066400000000000000000000163461151213612200243000ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import org.jvyamlb.events.Event; import org.jvyamlb.events.PositionedStreamEndEvent; import org.jvyamlb.events.PositionedStreamStartEvent; import org.jvyamlb.tokens.Token; import org.jvyamlb.events.StreamStartEvent; import org.jvyamlb.events.StreamEndEvent; import org.jvyamlb.events.DocumentStartEvent; import org.jvyamlb.events.PositionedDocumentStartEvent; import org.jvyamlb.events.PositionedDocumentEndEvent; import java.util.Map; import org.jvyamlb.events.DocumentEndEvent; import org.jvyamlb.events.PositionedDocumentEndEvent; import org.jvyamlb.events.ScalarEvent; import org.jvyamlb.events.PositionedScalarEvent; import org.jruby.util.ByteList; import org.jvyamlb.events.MappingStartEvent; import org.jvyamlb.events.MappingEndEvent; import org.jvyamlb.events.PositionedMappingStartEvent; import org.jvyamlb.events.PositionedMappingEndEvent; import org.jvyamlb.events.SequenceStartEvent; import org.jvyamlb.events.SequenceEndEvent; import org.jvyamlb.events.PositionedSequenceStartEvent; import org.jvyamlb.events.PositionedSequenceEndEvent; import org.jvyamlb.events.PositionedAliasEvent; import org.jvyamlb.events.AliasEvent; import org.jvyamlb.exceptions.PositionedParserException; import org.jvyamlb.util.IntStack; import org.jvyamlb.tokens.FlowSequenceEndToken; import org.jvyamlb.tokens.FlowMappingEndToken; /** * @author Ola Bini */ public class PositioningParserImpl extends ParserImpl implements PositioningParser { protected static class PositioningProductionEnvironment extends ProductionEnvironment { public PositioningProductionEnvironment(final YAMLConfig cfg) { super(cfg); } protected StreamStartEvent getStreamStart(final Token t) { return new PositionedStreamStartEvent(((Positionable)t).getRange()); } protected StreamEndEvent getStreamEnd(final Token t) { return new PositionedStreamEndEvent(((Positionable)t).getRange()); } protected DocumentStartEvent getDocumentStart(final boolean explicit, final int[] version, final Map tags, final Token t) { return new PositionedDocumentStartEvent(explicit, version, tags, new Position.Range(((Positionable)t).getPosition())); } protected DocumentEndEvent getDocumentEndImplicit(final Token t) { return new PositionedDocumentEndEvent(false, new Position.Range(((Positionable)t).getPosition())); } protected DocumentEndEvent getDocumentEndExplicit(final Token t) { return new PositionedDocumentEndEvent(true, new Position.Range(((Positionable)t).getPosition())); } protected ScalarEvent getScalar(final String anchor, final String tag, final boolean[] implicit, final ByteList value, final char style, final Token t, final Token anchorT, final Token tagT) { Position.Range range = ((Positionable)t).getRange(); if(null != anchorT && ((Positionable)anchorT).getRange().start.offset < range.start.offset) { range = range.withStart(((Positionable)anchorT).getRange().start); } if(null != tagT && ((Positionable)tagT).getRange().start.offset < range.start.offset) { range = range.withStart(((Positionable)tagT).getRange().start); } return new PositionedScalarEvent(anchor, tag, implicit, value, style, range); } protected MappingStartEvent getMappingStart(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Token t, final Token anchorT, final Token tagT) { Position position = ((Positionable)t).getPosition(); if(null != anchorT && ((Positionable)anchorT).getRange().start.offset < position.offset) { position = ((Positionable)anchorT).getRange().start; } if(null != tagT && ((Positionable)tagT).getRange().start.offset < position.offset) { position = ((Positionable)tagT).getRange().start; } return new PositionedMappingStartEvent(anchor, tag, implicit, flowStyle, new Position.Range(position)); } protected MappingEndEvent getMappingEnd(final Token t) { Positionable p = (Positionable)this.last; if(p == null || t instanceof FlowMappingEndToken) { p = (Positionable)t; } return new PositionedMappingEndEvent(new Position.Range(p.getRange().end)); } protected SequenceStartEvent getSequenceStart(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Token t, final Token anchorT, final Token tagT) { Position position = ((Positionable)t).getPosition(); if(null != anchorT && ((Positionable)anchorT).getRange().start.offset < position.offset) { position = ((Positionable)anchorT).getRange().start; } if(null != tagT && ((Positionable)tagT).getRange().start.offset < position.offset) { position = ((Positionable)tagT).getRange().start; } return new PositionedSequenceStartEvent(anchor, tag, implicit, flowStyle, new Position.Range(position)); } protected SequenceEndEvent getSequenceEnd(final Token t) { Positionable p = (Positionable)this.last; if(p == null || t instanceof FlowSequenceEndToken) { p = (Positionable)t; } return new PositionedSequenceEndEvent(new Position.Range(p.getRange().end)); } protected AliasEvent getAlias(final String value, final Token t) { return new PositionedAliasEvent(value, ((Positionable)t).getRange()); } protected void parserException(final String when, final String what, final String note, final Token t) { throw new PositionedParserException(when, what, note, ((Positionable)t).getRange()); } private Token emptyToken = null; protected void setEmptyToken(Token t) { this.emptyToken = t; } protected Token getEmptyToken(Scanner scanner) { if(emptyToken != null) { Token ret = emptyToken; emptyToken = null; return ret; } return scanner.peekToken(); } private Event last; public Event produce(final int current, final IntStack parseStack, final Scanner scanner) { Event ev = super.produce(current, parseStack, scanner); if(ev != null) { last = ev; } return ev; } } public PositioningParserImpl(final PositioningScanner scanner) { super(scanner); } public PositioningParserImpl(final PositioningScanner scanner, final YAMLConfig cfg) { super(scanner, cfg); } public Position getPosition() { return ((PositioningScanner)scanner).getPosition(); } public Position.Range getRange() { return new Position.Range(((PositioningScanner)scanner).getPosition()); } protected ProductionEnvironment getEnvironment(YAMLConfig cfg) { return new PositioningProductionEnvironment(cfg); } } jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningScanner.java000066400000000000000000000004111151213612200235750ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public interface PositioningScanner extends Scanner, Positionable { }// PositioningScanner jvyamlb-0.2.5/src/main/org/jvyamlb/PositioningScannerImpl.java000066400000000000000000000203221151213612200244220ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.jruby.util.ByteList; import org.jvyamlb.tokens.AliasToken; import org.jvyamlb.tokens.AnchorToken; import org.jvyamlb.tokens.BlockEndToken; import org.jvyamlb.tokens.BlockEntryToken; import org.jvyamlb.tokens.BlockMappingStartToken; import org.jvyamlb.tokens.BlockSequenceStartToken; import org.jvyamlb.tokens.DirectiveToken; import org.jvyamlb.tokens.DocumentEndToken; import org.jvyamlb.tokens.DocumentStartToken; import org.jvyamlb.tokens.FlowEntryToken; import org.jvyamlb.tokens.FlowMappingEndToken; import org.jvyamlb.tokens.FlowMappingStartToken; import org.jvyamlb.tokens.FlowSequenceEndToken; import org.jvyamlb.tokens.FlowSequenceStartToken; import org.jvyamlb.tokens.KeyToken; import org.jvyamlb.tokens.PositionedDocumentEndToken; import org.jvyamlb.tokens.PositionedDocumentStartToken; import org.jvyamlb.tokens.PositionedScalarToken; import org.jvyamlb.tokens.PositionedStreamEndToken; import org.jvyamlb.tokens.PositionedStreamStartToken; import org.jvyamlb.tokens.ScalarToken; import org.jvyamlb.tokens.StreamEndToken; import org.jvyamlb.tokens.StreamStartToken; import org.jvyamlb.tokens.TagToken; import org.jvyamlb.tokens.Token; import org.jvyamlb.tokens.ValueToken; import org.jvyamlb.tokens.PositionedBlockMappingStartToken; import org.jvyamlb.tokens.PositionedBlockEndToken; import org.jvyamlb.tokens.PositionedKeyToken; import org.jvyamlb.tokens.PositionedValueToken; import org.jvyamlb.tokens.PositionedBlockSequenceStartToken; import org.jvyamlb.tokens.PositionedBlockEntryToken; import org.jvyamlb.tokens.PositionedFlowMappingStartToken; import org.jvyamlb.tokens.PositionedFlowMappingEndToken; import org.jvyamlb.tokens.PositionedFlowSequenceStartToken; import org.jvyamlb.tokens.PositionedFlowSequenceEndToken; import org.jvyamlb.tokens.PositionedFlowEntryToken; import org.jvyamlb.tokens.PositionedTagToken; import org.jvyamlb.tokens.PositionedAliasToken; import org.jvyamlb.tokens.PositionedAnchorToken; import org.jvyamlb.tokens.PositionedDirectiveToken; import org.jvyamlb.exceptions.PositionedScannerException; /** * @author Ola Bini */ public class PositioningScannerImpl extends ScannerImpl implements PositioningScanner { public PositioningScannerImpl(final InputStream stream) { super(stream); } public PositioningScannerImpl(final ByteList stream) { super(stream); } public PositioningScannerImpl(final String stream) { super(stream); } private int line = 0; private int offset = 0; private List started = new ArrayList(); private Position possible = null; protected void forward() { final int lastPointer = pointer; super.forward(); // Forwarding one character always sets the column to zero on // a new line, or sets column to a non zero value if(column == 0) { line++; } offset += (pointer - lastPointer); } protected void forward(final int length) { // This implementation is not as efficient as it could // be. This makes it simpler to reason about new lines, // though, so it is worth it. for(int i = 0; i < length; i++) { forward(); } } public Position getPosition() { return new Position(line, column, offset); } public Position getStartPosition() { return (Position)started.remove(0); } public Position.Range getRange() { return new Position.Range(getStartPosition(), getPosition()); } protected void startingItem() { started.add(0, getPosition()); } protected void possibleEnd() { this.possible = getPosition(); } protected StreamStartToken getStreamStart() { return new PositionedStreamStartToken(new Position.Range(getPosition())); } protected StreamEndToken getStreamEnd() { return new PositionedStreamEndToken(new Position.Range(getPosition())); } protected DocumentStartToken getDocumentStart() { final Position p = getPosition(); return new PositionedDocumentStartToken(new Position.Range(p, p.withOffset(p.offset + 3).withColumn(p.column + 3))); } protected DocumentEndToken getDocumentEnd() { final Position p = getPosition(); return new PositionedDocumentEndToken(new Position.Range(p, p.withOffset(p.offset + 3).withColumn(p.column + 3))); } protected BlockEndToken getBlockEnd() { return new PositionedBlockEndToken(new Position.Range(getPosition())); } protected BlockSequenceStartToken getBlockSequenceStart() { return new PositionedBlockSequenceStartToken(new Position.Range(getPosition())); } protected BlockEntryToken getBlockEntry() { return new PositionedBlockEntryToken(new Position.Range(getPosition())); } protected KeyToken getKey() { return new PositionedKeyToken(new Position.Range(getPosition())); } protected KeyToken getKey(SimpleKey key) { return new PositionedKeyToken(new Position.Range(key.getPosition())); } protected ValueToken getValue() { return new PositionedValueToken(new Position.Range(getPosition())); } protected BlockMappingStartToken getBlockMappingStart() { return new PositionedBlockMappingStartToken(new Position.Range(getPosition())); } protected BlockMappingStartToken getBlockMappingStart(SimpleKey key) { return new PositionedBlockMappingStartToken(new Position.Range(key.getPosition())); } protected FlowSequenceStartToken getFlowSequenceStart() { return new PositionedFlowSequenceStartToken(new Position.Range(getPosition())); } protected FlowMappingStartToken getFlowMappingStart() { return new PositionedFlowMappingStartToken(new Position.Range(getPosition())); } protected FlowSequenceEndToken getFlowSequenceEnd() { return new PositionedFlowSequenceEndToken(new Position.Range(getPosition())); } protected FlowMappingEndToken getFlowMappingEnd() { return new PositionedFlowMappingEndToken(new Position.Range(getPosition())); } protected FlowEntryToken getFlowEntry() { return new PositionedFlowEntryToken(new Position.Range(getPosition())); } protected TagToken getTag(final ByteList[] args) { return new PositionedTagToken(args, new Position.Range(getStartPosition(), getPosition())); } protected AliasToken getAlias() { return new PositionedAliasToken(new Position.Range(getPosition())); } protected AnchorToken getAnchor() { return new PositionedAnchorToken(new Position.Range(getPosition())); } protected Token finalizeAnchor(Token t) { if(t instanceof PositionedAliasToken) { return new PositionedAliasToken((String)((AliasToken)t).getValue(), new Position.Range(((PositionedAliasToken)t).getPosition(), getPosition())); } else if(t instanceof PositionedAnchorToken) { return new PositionedAnchorToken((String)((AnchorToken)t).getValue(), new Position.Range(((PositionedAnchorToken)t).getPosition(), getPosition())); } return t; } protected DirectiveToken getDirective(String name, String[] value) { return new PositionedDirectiveToken(name, value, new Position.Range(getStartPosition(), getPosition())); } protected ScalarToken getScalar(ByteList value, boolean plain, char style) { Position p = possible; if(p == null) { p = getPosition(); } else { possible = null; } return new PositionedScalarToken(value, plain, style, new Position.Range(getStartPosition(), p)); } protected SimpleKey getSimpleKey(final int tokenNumber, final boolean required, final int index, final int line, final int column) { final Position p = getPosition(); return new SimpleKey(tokenNumber, required, p.offset, p.line, column); } protected void scannerException(String when, String what, String note) { throw new PositionedScannerException(when, what, note, new Position.Range(getPosition())); } }// PositioningScannerImpl jvyamlb-0.2.5/src/main/org/jvyamlb/PrivateType.java000066400000000000000000000007621151213612200222460ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public class PrivateType { private String tag; private Object value; public PrivateType(final String tag, final Object value) { this.tag = tag; this.value = value; } public String toString() { return "#"; } }// PrivateType jvyamlb-0.2.5/src/main/org/jvyamlb/Representer.java000066400000000000000000000012731151213612200222660ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import java.util.List; import java.util.Map; import org.jvyamlb.nodes.Node; import org.jruby.util.ByteList; /** * @author Ola Bini */ public interface Representer { void represent(final Object data) throws IOException; Node scalar(final String tag, final ByteList value, char style) throws IOException; Node seq(final String tag, final List sequence, final boolean flowStyle) throws IOException; Node map(final String tag, final Map mapping, final boolean flowStyle) throws IOException; }// Representer jvyamlb-0.2.5/src/main/org/jvyamlb/RepresenterImpl.java000066400000000000000000000354071151213612200231160ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Method; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Iterator; import java.util.Map; import java.util.IdentityHashMap; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import java.util.Set; import java.util.Date; import java.util.Calendar; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.LinkNode; import org.jvyamlb.nodes.CollectionNode; import org.jvyamlb.nodes.MappingNode; import org.jvyamlb.nodes.ScalarNode; import org.jvyamlb.nodes.SequenceNode; import org.jvyamlb.util.IntHashMap; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class RepresenterImpl implements Representer { private final Serializer serializer; private final char defaultStyle; private final Map representedObjects; private final Map links; public RepresenterImpl(final Serializer serializer, final YAMLConfig opts) { this.serializer = serializer; this.defaultStyle = opts.useDouble() ? '"' : (opts.useSingle() ? '\'' : 0); this.representedObjects = new IdentityHashMap(); this.links = new IdentityHashMap(); } protected Node representData(final Object data) throws IOException { Node node = null; boolean ignoreAlias = ignoreAliases(data); if(!ignoreAlias) { if(this.representedObjects.containsKey(data)) { node = (Node)this.representedObjects.get(data); if(null == node) { node = new LinkNode(); List ll = (List)links.get(data); if(ll == null) { ll = new ArrayList(); links.put(data,ll); } ll.add(node); } return node; } this.representedObjects.put(data,null); } node = getNodeCreatorFor(data).toYamlNode(this); if(!ignoreAlias) { this.representedObjects.put(data,node); List ll = (List)this.links.remove(data); if(ll != null) { for(Iterator iter = ll.iterator();iter.hasNext();) { ((LinkNode)iter.next()).setAnchor(node); } } } return node; } public Node scalar(final String tag, final ByteList value, char style) throws IOException { return representScalar(tag,value,style); } public Node representScalar(final String tag, final ByteList value, char style) throws IOException { char realStyle = style == 0 ? this.defaultStyle : style; return new ScalarNode(tag,value,style); } public Node seq(final String tag, final List sequence, final boolean flowStyle) throws IOException { return representSequence(tag,sequence,flowStyle); } public Node representSequence(final String tag, final List sequence, final boolean flowStyle) throws IOException { List value = new ArrayList(sequence.size()); for(final Iterator iter = sequence.iterator();iter.hasNext();) { value.add(representData(iter.next())); } return new SequenceNode(tag,value,flowStyle); } public Node map(final String tag, final Map mapping, final boolean flowStyle) throws IOException { return representMapping(tag,mapping,flowStyle); } public Node representMapping(final String tag, final Map mapping, final boolean flowStyle) throws IOException { Map value = new HashMap(); final Iterator iter = mapping.entrySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); value.put(representData(entry.getKey()),representData(entry.getValue())); } return new MappingNode(tag,value,flowStyle); } public void represent(final Object data) throws IOException { Node node = representData(data); this.serializer.serialize(node); this.representedObjects.clear(); } protected boolean ignoreAliases(final Object data) { return false; } protected YAMLNodeCreator getNodeCreatorFor(final Object data) { if(data instanceof YAMLNodeCreator) { return (YAMLNodeCreator)data; } else if(data instanceof Map) { return new MappingYAMLNodeCreator(data); } else if(data instanceof List) { return new SequenceYAMLNodeCreator(data); } else if(data instanceof Set) { return new SetYAMLNodeCreator(data); } else if(data instanceof Date) { return new DateYAMLNodeCreator(data); } else if(data instanceof String) { return new StringYAMLNodeCreator(data); } else if(data instanceof ByteList) { return new ByteListYAMLNodeCreator(data); } else if(data instanceof Number) { return new NumberYAMLNodeCreator(data); } else if(data instanceof Boolean) { return new ScalarYAMLNodeCreator("tag:yaml.org,2002:bool",data); } else if(data == null) { return new ScalarYAMLNodeCreator("tag:yaml.org,2002:null",""); } else if(data.getClass().isArray()) { return new ArrayYAMLNodeCreator(data); } else { // Fallback, handles JavaBeans and other return new JavaBeanYAMLNodeCreator(data); } } public static class DateYAMLNodeCreator implements YAMLNodeCreator { private final Date data; public DateYAMLNodeCreator(final Object data) { this.data = (Date)data; } public String taguri() { return "tag:yaml.org,2002:timestamp"; } private static DateFormat dateOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); private static DateFormat dateOutputUsec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z"); public Node toYamlNode(final Representer representer) throws IOException { final Calendar c = Calendar.getInstance(); c.setTime(data); String out = null; if(c.get(Calendar.MILLISECOND) != 0) { out = dateOutputUsec.format(data); } else { out = dateOutput.format(data); } out = out.substring(0, 23) + ":" + out.substring(23); return representer.scalar(taguri(), ByteList.create(out), (char)0); } } public static class SetYAMLNodeCreator implements YAMLNodeCreator { private final Set data; public SetYAMLNodeCreator(final Object data) { this.data = (Set)data; } public String taguri() { return "tag:yaml.org,2002:set"; } public Node toYamlNode(final Representer representer) throws IOException { final Map entries = new HashMap(); for(final Iterator iter = data.iterator();iter.hasNext();) { entries.put(iter.next(),null); } return representer.map(taguri(), entries, false); } } public static class ArrayYAMLNodeCreator implements YAMLNodeCreator { private final Object data; public ArrayYAMLNodeCreator(final Object data) { this.data = data; } public String taguri() { return "tag:yaml.org,2002:seq"; } public Node toYamlNode(final Representer representer) throws IOException { final int l = java.lang.reflect.Array.getLength(data); final List lst = new ArrayList(l); for(int i=0;iOla Bini */ public interface Resolver { void descendResolver(final Node currentNode, final Object currentIndex); void ascendResolver(); boolean checkResolverPrefix(final int depth, final List path, final Class kind, final Node currentNode, final Object currentIndex); String resolve(final Class kind, final ByteList value, final boolean[] implicit); }// Resolver jvyamlb-0.2.5/src/main/org/jvyamlb/ResolverImpl.java000066400000000000000000000211701151213612200224110ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.LinkedList; import java.util.HashMap; import java.util.Map; import org.jvyamlb.exceptions.ResolverException; import org.jvyamlb.nodes.MappingNode; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.ScalarNode; import org.jvyamlb.nodes.SequenceNode; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ResolverImpl implements Resolver { private final static Map yamlPathResolvers = new HashMap(); private final static ResolverScanner SCANNER = new ResolverScanner(); private List resolverExactPaths = new LinkedList(); private List resolverPrefixPaths = new LinkedList(); public static void addPathResolver(final String tag, final List path, final Class kind) { final List newPath = new LinkedList(); Object nodeCheck=null; Object indexCheck=null; for(final Iterator iter = path.iterator();iter.hasNext();) { final Object element = iter.next(); if(element instanceof List) { final List eList = (List)element; if(eList.size() == 2) { nodeCheck = eList.get(0); indexCheck = eList.get(1); } else if(eList.size() == 1) { nodeCheck = eList.get(0); indexCheck = Boolean.TRUE; } else { throw new ResolverException("Invalid path element: " + element); } } else { nodeCheck = null; indexCheck = element; } if(nodeCheck instanceof String || nodeCheck instanceof ByteList) { nodeCheck = ScalarNode.class; } else if(nodeCheck instanceof List) { nodeCheck = SequenceNode.class; } else if(nodeCheck instanceof Map) { nodeCheck = MappingNode.class; } else if(null != nodeCheck && !ScalarNode.class.equals(nodeCheck) && !SequenceNode.class.equals(nodeCheck) && !MappingNode.class.equals(nodeCheck)) { throw new ResolverException("Invalid node checker: " + nodeCheck); } if(!(indexCheck instanceof String || nodeCheck instanceof ByteList || indexCheck instanceof Integer) && null != indexCheck) { throw new ResolverException("Invalid index checker: " + indexCheck); } newPath.add(new Object[]{nodeCheck,indexCheck}); } Class newKind = null; if(String.class.equals(kind) || ByteList.class.equals(kind)) { newKind = ScalarNode.class; } else if(List.class.equals(kind)) { newKind = SequenceNode.class; } else if(Map.class.equals(kind)) { newKind = MappingNode.class; } else if(kind != null && !ScalarNode.class.equals(kind) && !SequenceNode.class.equals(kind) && !MappingNode.class.equals(kind)) { throw new ResolverException("Invalid node kind: " + kind); } else { newKind = kind; } final List x = new ArrayList(1); x.add(newPath); final List y = new ArrayList(2); y.add(x); y.add(kind); yamlPathResolvers.put(y,tag); } public void descendResolver(final Node currentNode, final Object currentIndex) { final Map exactPaths = new HashMap(); final List prefixPaths = new LinkedList(); if(null != currentNode) { final int depth = resolverPrefixPaths.size(); for(final Iterator iter = ((List)resolverPrefixPaths.get(0)).iterator();iter.hasNext();) { final Object[] obj = (Object[])iter.next(); final List path = (List)obj[0]; if(checkResolverPrefix(depth,path,(Class)obj[1],currentNode,currentIndex)) { if(path.size() > depth) { prefixPaths.add(new Object[] {path,obj[1]}); } else { final List resPath = new ArrayList(2); resPath.add(path); resPath.add(obj[1]); exactPaths.put(obj[1],yamlPathResolvers.get(resPath)); } } } } else { for(final Iterator iter = yamlPathResolvers.keySet().iterator();iter.hasNext();) { final List key = (List)iter.next(); final List path = (List)key.get(0); final Class kind = (Class)key.get(1); if(null == path) { exactPaths.put(kind,yamlPathResolvers.get(key)); } else { prefixPaths.add(key); } } } resolverExactPaths.add(0,exactPaths); resolverPrefixPaths.add(0,prefixPaths); } public void ascendResolver() { resolverExactPaths.remove(0); resolverPrefixPaths.remove(0); } public boolean checkResolverPrefix(final int depth, final List path, final Class kind, final Node currentNode, final Object currentIndex) { final Object[] check = (Object[])path.get(depth-1); final Object nodeCheck = check[0]; final Object indexCheck = check[1]; if(nodeCheck instanceof String) { if(!currentNode.getTag().equals(nodeCheck)) { return false; } } else if(null != nodeCheck) { if(!((Class)nodeCheck).isInstance(currentNode)) { return false; } } if(indexCheck == Boolean.TRUE && currentIndex != null) { return false; } if(indexCheck == Boolean.FALSE && currentIndex == null) { return false; } if(indexCheck instanceof String) { if(!(currentIndex instanceof ScalarNode && indexCheck.equals(((ScalarNode)currentIndex).getValue()))) { return false; } } else if(indexCheck instanceof ByteList) { if(!(currentIndex instanceof ScalarNode && indexCheck.equals(((ScalarNode)currentIndex).getValue()))) { return false; } } else if(indexCheck instanceof Integer) { if(!currentIndex.equals(indexCheck)) { return false; } } return true; } public String resolve(final Class kind, final ByteList value, final boolean[] implicit) { List resolvers = null; if(kind.equals(ScalarNode.class) && implicit[0]) { String resolv = SCANNER.recognize(value); if(resolv != null) { return resolv; } } final Map exactPaths = (Map)resolverExactPaths.get(0); if(exactPaths.containsKey(kind)) { return (String)exactPaths.get(kind); } if(exactPaths.containsKey(null)) { return (String)exactPaths.get(null); } if(kind.equals(ScalarNode.class)) { return YAML.DEFAULT_SCALAR_TAG; } else if(kind.equals(SequenceNode.class)) { return YAML.DEFAULT_SEQUENCE_TAG; } else if(kind.equals(MappingNode.class)) { return YAML.DEFAULT_MAPPING_TAG; } return null; } private static ByteList s(String se){ return new ByteList(se.getBytes()); } public static void main(String[] args) { ByteList[] strings = {s("yes"), s("NO"), s("booooooooooooooooooooooooooooooooooooooooooooooool"), s("false"),s(""), s("~"),s("~a"), s("<<"), s("10.1"), s("10000000000003435345.2324E+13"), s(".23"), s(".nan"), s("null"), s("124233333333333333"), s("0b030323"), s("+0b0111111011010101"), s("0xaafffdf"), s("2005-05-03"), s("2005-05-03a"), s(".nana"), s("2005-03-05T05:23:22"), s("="), s("= "), s("=a")}; boolean[] implicit = new boolean[]{true,true}; Resolver res = new ResolverImpl(); res.descendResolver(null,null); Class s = ScalarNode.class; final long before = System.currentTimeMillis(); final int NUM = 100000; for(int j=0;j 0 ) { int _lower = _keys; int _mid; int _upper = _keys + _klen - 1; while (true) { if ( _upper < _lower ) break; _mid = _lower + ((_upper-_lower) >> 1); if ( data[p] < _resolver_scanner_trans_keys[_mid] ) _upper = _mid - 1; else if ( data[p] > _resolver_scanner_trans_keys[_mid] ) _lower = _mid + 1; else { _trans += (_mid - _keys); break _match; } } _keys += _klen; _trans += _klen; } _klen = _resolver_scanner_range_lengths[cs]; if ( _klen > 0 ) { int _lower = _keys; int _mid; int _upper = _keys + (_klen<<1) - 2; while (true) { if ( _upper < _lower ) break; _mid = _lower + (((_upper-_lower) >> 1) & ~1); if ( data[p] < _resolver_scanner_trans_keys[_mid] ) _upper = _mid - 2; else if ( data[p] > _resolver_scanner_trans_keys[_mid+1] ) _lower = _mid + 2; else { _trans += ((_mid - _keys)>>1); break _match; } } _trans += _klen; } } while (false); _trans = _resolver_scanner_indicies[_trans]; cs = _resolver_scanner_trans_targs_wi[_trans]; } while (false); if ( cs == 0 ) break _resume; if ( ++p == pe ) break _resume; } } } } // line 76 "src/org/jvyamlb/resolver_scanner.rl" // line 452 "src/org/jvyamlb/ResolverScanner.java" int _acts = _resolver_scanner_eof_actions[cs]; int _nacts = (int) _resolver_scanner_actions[_acts++]; while ( _nacts-- > 0 ) { switch ( _resolver_scanner_actions[_acts++] ) { case 0: // line 10 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:bool"; } break; case 1: // line 11 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:merge"; } break; case 2: // line 12 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:null"; } break; case 3: // line 13 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:timestamp#ymd"; } break; case 4: // line 14 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:timestamp"; } break; case 5: // line 15 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:value"; } break; case 6: // line 16 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:float"; } break; case 7: // line 17 "src/org/jvyamlb/resolver_scanner.rl" { tag = "tag:yaml.org,2002:int"; } break; // line 489 "src/org/jvyamlb/ResolverScanner.java" } } // line 78 "src/org/jvyamlb/resolver_scanner.rl" return tag; } public static void main(String[] args) { ByteList b = new ByteList(78); b.append(args[0].getBytes()); /* for(int i=0;iOla Bini */ public class SafeConstructorImpl extends BaseConstructorImpl { 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) { YamlConstructor mine = (YamlConstructor)yamlConstructors.get(key); if(mine == null) { mine = super.getYamlConstructor(key); } return mine; } public YamlMultiConstructor getYamlMultiConstructor(final Object key) { YamlMultiConstructor mine = (YamlMultiConstructor)yamlMultiConstructors.get(key); if(mine == null) { mine = super.getYamlMultiConstructor(key); } return mine; } public Pattern getYamlMultiRegexp(final Object key) { Pattern mine = (Pattern)yamlMultiRegexps.get(key); if(mine == null) { mine = super.getYamlMultiRegexp(key); } return mine; } public Set getYamlMultiRegexps() { final Set all = new HashSet(super.getYamlMultiRegexps()); all.addAll(yamlMultiRegexps.keySet()); return all; } 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)); } public SafeConstructorImpl(final Composer composer) { super(composer); } private static ByteList into(String v) { return new ByteList(v.getBytes(),false); } private final static Map BOOL_VALUES = new HashMap(); static { BOOL_VALUES.put(into("yes"),Boolean.TRUE); BOOL_VALUES.put(into("Yes"),Boolean.TRUE); BOOL_VALUES.put(into("YES"),Boolean.TRUE); BOOL_VALUES.put(into("no"),Boolean.FALSE); BOOL_VALUES.put(into("No"),Boolean.FALSE); BOOL_VALUES.put(into("NO"),Boolean.FALSE); BOOL_VALUES.put(into("true"),Boolean.TRUE); BOOL_VALUES.put(into("True"),Boolean.TRUE); BOOL_VALUES.put(into("TRUE"),Boolean.TRUE); BOOL_VALUES.put(into("false"),Boolean.FALSE); BOOL_VALUES.put(into("False"),Boolean.FALSE); BOOL_VALUES.put(into("FALSE"),Boolean.FALSE); BOOL_VALUES.put(into("on"),Boolean.TRUE); BOOL_VALUES.put(into("On"),Boolean.TRUE); BOOL_VALUES.put(into("ON"),Boolean.TRUE); BOOL_VALUES.put(into("off"),Boolean.FALSE); BOOL_VALUES.put(into("Off"),Boolean.FALSE); BOOL_VALUES.put(into("OFF"),Boolean.FALSE); } public static Object constructYamlNull(final Constructor ctor, final Node node) { return null; } public static Object constructYamlBool(final Constructor ctor, final Node node) { final Object val = ctor.constructScalar(node); return BOOL_VALUES.get(val); } public static Object constructYamlOmap(final Constructor ctor, final Node node) { return ctor.constructPairs(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 ((Map)ctor.constructMapping(node)).keySet(); } public static Object constructYamlStr(final Constructor ctor, final Node node) { final ByteList value = (ByteList)ctor.constructScalar(node); return value.length() == 0 ? (ByteList)null : value; } public static Object constructYamlSeq(final Constructor ctor, final Node node) { return ctor.constructSequence(node); } public static Object constructYamlMap(final Constructor ctor, final Node node) { return ctor.constructMapping(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); } private final static Pattern TIMESTAMP_REGEXP = Pattern.compile("^(-?[0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)(?:(?:[Tt]|[ \t]+)([0-9][0-9]?):([0-9][0-9]):([0-9][0-9])(?:\\.([0-9]*))?(?:[ \t]*(Z|([-+][0-9][0-9]?)(?::([0-9][0-9])?)?)))?$"); public final static Pattern YMD_REGEXP = Pattern.compile("^(-?[0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)$"); public static Object constructYamlTimestamp(final Constructor ctor, final Node node) { Matcher match = YMD_REGEXP.matcher(node.getValue().toString()); if(match.matches()) { final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); DateTime dt = new DateTime(0,1,1,0,0,0,0); if(year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); } if(month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if(day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } return new Object[]{dt}; } match = TIMESTAMP_REGEXP.matcher(node.getValue().toString()); if(!match.matches()) { return new Object[]{ctor.constructPrivateType(node)}; } final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); final String hour_s = match.group(4); final String min_s = match.group(5); final String sec_s = match.group(6); final String fract_s = match.group(7); final String utc = match.group(8); final String timezoneh_s = match.group(9); final String timezonem_s = match.group(10); int usec = 0; if(fract_s != null) { usec = Integer.parseInt(fract_s); if(usec != 0) { while(10*usec < 1000) { usec *= 10; } } } DateTime dt = new DateTime(0,1,1,0,0,0,0); if("Z".equalsIgnoreCase(utc)) { dt = dt.withZone(DateTimeZone.forID("Etc/GMT")); } else { if(timezoneh_s != null || timezonem_s != null) { int zone = 0; int sign = +1; if(timezoneh_s != null) { if(timezoneh_s.startsWith("-")) { sign = -1; } zone += Integer.parseInt(timezoneh_s.substring(1))*3600000; } if(timezonem_s != null) { zone += Integer.parseInt(timezonem_s)*60000; } dt = dt.withZone(DateTimeZone.forOffsetMillis(sign*zone)); } } if(year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); } if(month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if(day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } if(hour_s != null) { dt = dt.withHourOfDay(Integer.parseInt(hour_s)); } if(min_s != null) { dt = dt.withMinuteOfHour(Integer.parseInt(min_s)); } if(sec_s != null) { dt = dt.withSecondOfMinute(Integer.parseInt(sec_s)); } dt = dt.withMillisOfSecond(usec/1000); return new Object[]{dt, new Integer(usec%1000)}; } public static Object constructYamlInt(final Constructor ctor, final Node node) { String value = ctor.constructScalar(node).toString().replaceAll("_","").replaceAll(",","");; int sign = +1; char first = value.charAt(0); if(first == '-') { sign = -1; value = value.substring(1); } else if(first == '+') { value = value.substring(1); } int base = 10; if(value.equals("0")) { return new Long(0); } else if(value.startsWith("0b")) { value = value.substring(2); base = 2; } else if(value.startsWith("0x")) { value = value.substring(2); base = 16; } else if(value.startsWith("0")) { value = value.substring(1); base = 8; } else if(value.indexOf(':') != -1) { final String[] digits = value.split(":"); int bes = 1; int val = 0; for(int i=0,j=digits.length;iOla Bini */ public class SafeRepresenterImpl extends RepresenterImpl { public SafeRepresenterImpl(final Serializer serializer, final YAMLConfig opts) { super(serializer,opts); } protected boolean ignoreAliases(final Object data) { return data == null || data instanceof String || data instanceof ByteList || data instanceof Boolean || data instanceof Integer || data instanceof Float || data instanceof Double; } }// SafeRepresenterImpl jvyamlb-0.2.5/src/main/org/jvyamlb/Scanner.java000066400000000000000000000007021151213612200213550ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Iterator; import org.jvyamlb.tokens.Token; /** * @author Ola Bini */ public interface Scanner { boolean checkToken(final Class[] choices); Token peekToken(); Token peekToken(int index); Token getToken(); Iterator eachToken(); Iterator iterator(); }// Scanner jvyamlb-0.2.5/src/main/org/jvyamlb/ScannerImpl.java000066400000000000000000001620531151213612200222070ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.jvyamlb.exceptions.ScannerException; import org.jvyamlb.exceptions.YAMLException; import org.jruby.util.ByteList; import org.jvyamlb.tokens.AliasToken; import org.jvyamlb.tokens.AnchorToken; import org.jvyamlb.tokens.DirectiveToken; import org.jvyamlb.tokens.FlowMappingStartToken; import org.jvyamlb.tokens.FlowSequenceStartToken; import org.jvyamlb.tokens.FlowEntryToken; import org.jvyamlb.tokens.KeyToken; import org.jvyamlb.tokens.ScalarToken; import org.jvyamlb.tokens.TagToken; import org.jvyamlb.tokens.Token; import org.jvyamlb.tokens.ValueToken; import org.jvyamlb.tokens.StreamStartToken; import org.jvyamlb.tokens.StreamEndToken; import org.jvyamlb.tokens.DocumentStartToken; import org.jvyamlb.tokens.BlockMappingStartToken; import org.jvyamlb.tokens.BlockEntryToken; import org.jvyamlb.tokens.BlockSequenceStartToken; import org.jvyamlb.tokens.BlockEndToken; import org.jvyamlb.tokens.FlowSequenceEndToken; import org.jvyamlb.tokens.FlowMappingEndToken; import org.jvyamlb.tokens.DocumentEndToken; /** *

A Java implementation of the RbYAML scanner.

* * @author Ola Bini */ public class ScannerImpl implements Scanner { private final static byte[] EMPTY = new byte[0]; private final static byte[] NN = new byte[]{'\n'}; private final static ByteList BANG = new ByteList(new byte[]{'!'},false); private final static ByteList SPACE = new ByteList(new byte[]{' '},false); private final static boolean[] ALL_FALSE = new boolean[256]; private final static boolean[] ALL_TRUE = new boolean[256]; private final static boolean[] LINEBR = new boolean[256]; private final static boolean[] NULL_BL_LINEBR = new boolean[256]; private final static boolean[] NULL_BL_T_LINEBR = new boolean[256]; private final static boolean[] NULL_OR_LINEBR = new boolean[256]; private final static boolean[] FULL_LINEBR = new boolean[256]; private final static boolean[] BLANK_OR_LINEBR = new boolean[256]; private final static boolean[] S4 = new boolean[256]; private final static boolean[] ALPHA = new boolean[256]; private final static boolean[] DIGIT = new boolean[256]; private final static boolean[] HEXA = new boolean[256]; private final static boolean[] STRANGE_CHAR = new boolean[256]; private final static int[] RN = new int[]{'\r','\n'}; private final static boolean[] BLANK_T = new boolean[256]; private final static boolean[] SPACES_AND_STUFF = new boolean[256]; private final static boolean[] DOUBLE_ESC = new boolean[256]; private final static boolean[] NON_ALPHA_OR_NUM = new boolean[256]; private final static boolean[] NON_PRINTABLE = new boolean[256]; private final static boolean[] STUPID_CHAR = new boolean[256]; private final static boolean[] R_FLOWZERO = NULL_BL_T_LINEBR; private final static boolean[] R_FLOWZERO1 = new boolean[256]; private final static boolean[] R_FLOWNONZERO = new boolean[256]; private final static byte[] ESCAPE_REPLACEMENTS = new byte[256]; private final static boolean[] IS_ESCAPE_REPLACEMENT = new boolean[256]; private final static Map ESCAPE_CODES = new HashMap(); private final static boolean[] CHOMPING = new boolean[256]; static { CHOMPING['+'] = true; CHOMPING['-'] = true; CHOMPING['\n'] = true; CHOMPING['\r'] = true; CHOMPING['0'] = true; CHOMPING['1'] = true; CHOMPING['2'] = true; CHOMPING['3'] = true; CHOMPING['4'] = true; CHOMPING['5'] = true; CHOMPING['6'] = true; CHOMPING['7'] = true; CHOMPING['8'] = true; CHOMPING['9'] = true; CHOMPING['#'] = true; CHOMPING[' '] = true; Arrays.fill(ALL_TRUE,true); LINEBR['\n'] = true; NULL_BL_LINEBR['\0'] = true; NULL_BL_LINEBR[' '] = true; NULL_BL_LINEBR['\r'] = true; NULL_BL_LINEBR['\n'] = true; NULL_BL_T_LINEBR['\0'] = true; NULL_BL_T_LINEBR[' '] = true; NULL_BL_T_LINEBR['\t'] = true; NULL_BL_T_LINEBR['\r'] = true; NULL_BL_T_LINEBR['\n'] = true; NULL_OR_LINEBR['\0'] = true; NULL_OR_LINEBR['\r'] = true; NULL_OR_LINEBR['\n'] = true; FULL_LINEBR['\r'] = true; FULL_LINEBR['\n'] = true; BLANK_OR_LINEBR[' '] = true; BLANK_OR_LINEBR['\r'] = true; BLANK_OR_LINEBR['\n'] = true; S4['\0'] = true; S4[' '] = true; S4['\t'] = true; S4['\r'] = true; S4['\n'] = true; S4['['] = true; S4[']'] = true; S4['{'] = true; S4['}'] = true; for(char c = 'a'; c<='z'; c++) { ALPHA[c] = true; STRANGE_CHAR[c] = true; } for(char c = 'A'; c<='Z'; c++) { ALPHA[c] = true; STRANGE_CHAR[c] = true; } for(char c = '0'; c<='9'; c++) { ALPHA[c] = true; STRANGE_CHAR[c] = true; HEXA[c] = true; DIGIT[c] = true; } for(char c = 'a'; c<='f'; c++) { HEXA[c] = true; } for(char c = 'A'; c<='F'; c++) { HEXA[c] = true; } ALPHA['-'] = true; ALPHA['_'] = true; STRANGE_CHAR['-'] = true; STRANGE_CHAR['_'] = true; STRANGE_CHAR['['] = true; STRANGE_CHAR[']'] = true; STRANGE_CHAR['('] = true; STRANGE_CHAR[')'] = true; STRANGE_CHAR['\''] = true; STRANGE_CHAR[';'] = true; STRANGE_CHAR['/'] = true; STRANGE_CHAR['?'] = true; STRANGE_CHAR[':'] = true; STRANGE_CHAR['@'] = true; STRANGE_CHAR['&'] = true; STRANGE_CHAR['='] = true; STRANGE_CHAR['+'] = true; STRANGE_CHAR['$'] = true; STRANGE_CHAR[','] = true; STRANGE_CHAR['.'] = true; STRANGE_CHAR['!'] = true; STRANGE_CHAR['~'] = true; STRANGE_CHAR['*'] = true; STRANGE_CHAR['%'] = true; STRANGE_CHAR['^'] = true; STRANGE_CHAR['#'] = true; BLANK_T[' '] = true; BLANK_T['\t'] = true; SPACES_AND_STUFF['\0'] = true; SPACES_AND_STUFF[' '] = true; SPACES_AND_STUFF['\t'] = true; SPACES_AND_STUFF['\r'] = true; SPACES_AND_STUFF['\n'] = true; SPACES_AND_STUFF['\\'] = true; SPACES_AND_STUFF['\''] = true; SPACES_AND_STUFF['"'] = true; DOUBLE_ESC['\\'] = true; DOUBLE_ESC['"'] = true; NON_ALPHA_OR_NUM['\0'] = true; NON_ALPHA_OR_NUM[' '] = true; NON_ALPHA_OR_NUM['\t'] = true; NON_ALPHA_OR_NUM['\r'] = true; NON_ALPHA_OR_NUM['\n'] = true; NON_ALPHA_OR_NUM['?'] = true; NON_ALPHA_OR_NUM[':'] = true; NON_ALPHA_OR_NUM[','] = true; NON_ALPHA_OR_NUM[']'] = true; NON_ALPHA_OR_NUM['}'] = true; NON_ALPHA_OR_NUM['%'] = true; NON_ALPHA_OR_NUM['@'] = true; NON_ALPHA_OR_NUM['`'] = true; Arrays.fill(ESCAPE_REPLACEMENTS,(byte)0); ESCAPE_REPLACEMENTS['0'] = 0; ESCAPE_REPLACEMENTS['a'] = 7; ESCAPE_REPLACEMENTS['b'] = 8; ESCAPE_REPLACEMENTS['t'] = 9; ESCAPE_REPLACEMENTS['\t'] = 9; ESCAPE_REPLACEMENTS['n'] = 10; ESCAPE_REPLACEMENTS['v'] = 11; ESCAPE_REPLACEMENTS['f'] = 12; ESCAPE_REPLACEMENTS['r'] = 13; ESCAPE_REPLACEMENTS['e'] = 27; // ESCAPE_REPLACEMENTS[' '] = 32; ESCAPE_REPLACEMENTS['"'] = (byte)'"'; ESCAPE_REPLACEMENTS['\\'] = (byte)'\\'; ESCAPE_REPLACEMENTS['N'] = (byte)133; ESCAPE_REPLACEMENTS['_'] = (byte)160; IS_ESCAPE_REPLACEMENT['0'] = true; IS_ESCAPE_REPLACEMENT['a'] = true; IS_ESCAPE_REPLACEMENT['b'] = true; IS_ESCAPE_REPLACEMENT['t'] = true; IS_ESCAPE_REPLACEMENT['\t'] = true; IS_ESCAPE_REPLACEMENT['n'] = true; IS_ESCAPE_REPLACEMENT['v'] = true; IS_ESCAPE_REPLACEMENT['f'] = true; IS_ESCAPE_REPLACEMENT['r'] = true; IS_ESCAPE_REPLACEMENT['e'] = true; // IS_ESCAPE_REPLACEMENT[' '] = true; IS_ESCAPE_REPLACEMENT['"'] = true; IS_ESCAPE_REPLACEMENT['\\'] = true; IS_ESCAPE_REPLACEMENT['N'] = true; IS_ESCAPE_REPLACEMENT['_'] = true; ESCAPE_CODES.put(new Character('x'),new Integer(2)); ESCAPE_CODES.put(new Character('u'),new Integer(4)); ESCAPE_CODES.put(new Character('U'),new Integer(8)); Arrays.fill(STUPID_CHAR,true); STUPID_CHAR['\0'] = false; STUPID_CHAR[' '] = false; STUPID_CHAR['\t'] = false; STUPID_CHAR['\r'] = false; STUPID_CHAR['\n'] = false; STUPID_CHAR['-'] = false; STUPID_CHAR['?'] = false; STUPID_CHAR[':'] = false; STUPID_CHAR['['] = false; STUPID_CHAR[']'] = false; STUPID_CHAR['{'] = false; STUPID_CHAR['#'] = false; STUPID_CHAR['!'] = false; STUPID_CHAR['\''] = false; STUPID_CHAR['"'] = false; R_FLOWZERO1[':'] = true; R_FLOWNONZERO['\0'] = true; R_FLOWNONZERO[' '] = true; R_FLOWNONZERO['\t'] = true; R_FLOWNONZERO['\r'] = true; R_FLOWNONZERO['\n'] = true; R_FLOWNONZERO['['] = true; R_FLOWNONZERO[']'] = true; R_FLOWNONZERO['{'] = true; R_FLOWNONZERO['}'] = true; R_FLOWNONZERO[','] = true; R_FLOWNONZERO[':'] = true; R_FLOWNONZERO['?'] = true; } private boolean done = false; private int flowLevel = 0; private int tokensTaken = 0; private int indent = -1; private boolean allowSimpleKey = true; private boolean eof = true; protected int column = 0; protected int pointer = 0; private ByteList buffer; private InputStream stream; private List tokens; private List indents; private Map possibleSimpleKeys; private boolean docStart = false; public ScannerImpl(final InputStream stream) { this.stream = stream; this.eof = false; this.buffer = new ByteList(100); this.tokens = new LinkedList(); this.indents = new LinkedList(); this.possibleSimpleKeys = new TreeMap(); fetchStreamStart(); } public ScannerImpl(final ByteList stream) { this.buffer = new ByteList(stream.bytes,stream.begin,stream.realSize); this.stream = null; this.tokens = new LinkedList(); this.indents = new LinkedList(); this.possibleSimpleKeys = new TreeMap(); fetchStreamStart(); } public ScannerImpl(final String stream) { try { this.buffer = new ByteList(ByteList.plain(stream),false); } catch(Exception e) { throw new RuntimeException(e.getMessage()); } this.stream = null; this.tokens = new LinkedList(); this.indents = new LinkedList(); this.possibleSimpleKeys = new TreeMap(); fetchStreamStart(); } private void update(final int length, final boolean reset) { if(!eof && reset) { this.buffer.delete(0,this.pointer); this.pointer = 0; } while(this.buffer.realSize < (this.pointer+length)) { byte[] rawData = ByteList.NULL_ARRAY; int converted = -2; if(!this.eof) { byte[] data = new byte[1024]; try { converted = this.stream.read(data); } catch(final IOException ioe) { throw new YAMLException(ioe); } if(converted == -1) { this.eof = true; } else { rawData = data; } } if(this.eof) { this.buffer.append('\0'); break; } else { checkPrintable(rawData,converted); this.buffer.append(rawData,0,converted); } } } protected void yamlException(String message) { throw new YAMLException(message); } protected void scannerException(String when, String what, String note) { throw new ScannerException(when, what, note); } private void checkPrintable(final byte[] b, final int len) { for(int i=0;i= this.buffer.realSize) { update(len, reset); } return true; } private char peek() { ensure(1,false); return (char)((char)(buffer.bytes[this.pointer]) & 0xFF); } private char peek(final int index) { ensure(index+1,false); return (char)((char)this.buffer.bytes[this.pointer + index] & 0xFF); } protected void forward() { ensure(2,true); final char ch1 = (char)((int)this.buffer.bytes[this.pointer++] & 0xFF); if(ch1 == '\n' || (ch1 == '\r' && (((int)this.buffer.bytes[this.pointer] & 0xFF) != '\n'))) { this.possibleSimpleKeys.clear(); this.column = 0; } else { this.column++; } } protected void forward(final int length) { ensure(length+1,true); int ch = 0; for(int i=0;i': if(this.flowLevel == 0 && CHOMPING[peek(1)]) { return fetchFolded(); } break; } //TODO: this is probably incorrect... if(STUPID_CHAR[this.buffer.bytes[this.pointer]&0xFF] || (ensure(1,false) && (this.buffer.bytes[this.pointer] == '-' || this.buffer.bytes[this.pointer] == '?' || this.buffer.bytes[this.pointer] == ':') && !NULL_BL_T_LINEBR[this.buffer.bytes[this.pointer+1]&0xFF])) { return fetchPlain(); } scannerException("while scanning for the next token","found character " + ch + "(" + (int)ch + ") that cannot start any token",null); return null; } protected StreamStartToken getStreamStart() { return Token.STREAM_START; } protected StreamEndToken getStreamEnd() { return Token.STREAM_END; } protected DocumentStartToken getDocumentStart() { return Token.DOCUMENT_START; } protected DocumentEndToken getDocumentEnd() { return Token.DOCUMENT_END; } protected BlockEndToken getBlockEnd() { return Token.BLOCK_END; } protected BlockSequenceStartToken getBlockSequenceStart() { return Token.BLOCK_SEQUENCE_START; } protected BlockEntryToken getBlockEntry() { return Token.BLOCK_ENTRY; } protected KeyToken getKey() { return Token.KEY; } protected KeyToken getKey(SimpleKey key) { return Token.KEY; } protected ValueToken getValue() { return Token.VALUE; } protected BlockMappingStartToken getBlockMappingStart() { return Token.BLOCK_MAPPING_START; } protected BlockMappingStartToken getBlockMappingStart(SimpleKey key) { return Token.BLOCK_MAPPING_START; } protected FlowSequenceStartToken getFlowSequenceStart() { return Token.FLOW_SEQUENCE_START; } protected FlowMappingStartToken getFlowMappingStart() { return Token.FLOW_MAPPING_START; } protected FlowSequenceEndToken getFlowSequenceEnd() { return Token.FLOW_SEQUENCE_END; } protected FlowMappingEndToken getFlowMappingEnd() { return Token.FLOW_MAPPING_END; } protected FlowEntryToken getFlowEntry() { return Token.FLOW_ENTRY; } protected TagToken getTag(final ByteList[] args) { return new TagToken(args); } protected AliasToken getAlias() { return new AliasToken(); } protected AnchorToken getAnchor() { return new AnchorToken(); } protected Token finalizeAnchor(Token t) { return t; } protected DirectiveToken getDirective(String name, String[] value) { return new DirectiveToken(name, value); } protected ScalarToken getScalar(ByteList value, boolean plain, char style) { return new ScalarToken(value, plain, style); } private Token fetchStreamStart() { this.docStart = true; Token t = getStreamStart(); addToken(t); return t; } private Token fetchStreamEnd() { unwindIndent(-1); this.allowSimpleKey = false; this.possibleSimpleKeys = new TreeMap(); Token t = getStreamEnd(); addToken(t); this.done = true; this.docStart = false; return t; } private void scanToNextToken() { for(;;) { char ch; while((ch=peek()) == ' ' || ch=='\t') { forward(); } if(ch == '#') { forward(); while(!NULL_OR_LINEBR[peek()]) { forward(); } } if(scanLineBreak().length != 0 ) { if(this.flowLevel == 0) { this.allowSimpleKey = true; } } else { break; } } } private byte[] scanLineBreak() { // Transforms: // '\r\n' : '\n' // '\r' : '\n' // '\n' : '\n' // '\x85' : '\n' // default : '' final int val = peek(); if(FULL_LINEBR[val]) { ensure(2,false); if(RN[0] == buffer.bytes[this.pointer] && RN[1] == buffer.bytes[this.pointer+1]) { forward(2); } else { forward(); } return NN; } else { return EMPTY; } } private void unwindIndent(final int col) { if(this.flowLevel != 0) { return; } while(this.indent > col) { this.indent = ((Integer)(this.indents.remove(0))).intValue(); addToken(getBlockEnd()); } } private Token fetchDocumentStart() { this.docStart = false; return fetchDocumentIndicator(getDocumentStart()); } private Token fetchDocumentIndicator(final Token tok) { unwindIndent(-1); removePossibleSimpleKey(); this.allowSimpleKey = false; forward(3); addToken(tok); return tok; } private Token fetchBlockEntry() { this.docStart = false; if(this.flowLevel == 0) { if(!this.allowSimpleKey) { scannerException(null,"sequence entries are not allowed here",null); } if(addIndent(this.column)) { addToken(getBlockSequenceStart()); } } this.allowSimpleKey = true; removePossibleSimpleKey(); Token t = getBlockEntry(); forward(); addToken(t); return t; } private boolean addIndent(final int col) { if(this.indent < col) { this.indents.add(0,new Integer(this.indent)); this.indent = col; return true; } return false; } private Token fetchTag() { this.docStart = false; savePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanTag(); addToken(tok); return tok; } private void removePossibleSimpleKey() { SimpleKey key = (SimpleKey)this.possibleSimpleKeys.remove(new Integer(this.flowLevel)); if(key != null) { if(key.isRequired()) { scannerException("while scanning a simple key","could not find expected ':'",null); } } } protected SimpleKey getSimpleKey(final int tokenNumber, final boolean required, final int index, final int line, final int column) { return new SimpleKey(tokenNumber, required, index, line, column); } private void savePossibleSimpleKey() { if(this.allowSimpleKey) { this.removePossibleSimpleKey(); this.possibleSimpleKeys.put(new Integer(this.flowLevel), getSimpleKey(this.tokensTaken+this.tokens.size(), (this.flowLevel == 0) && this.indent == this.column,-1,-1,this.column)); } } private Token scanTag() { startingItem(); char ch = peek(1); ByteList handle = null; ByteList suffix = null; if(ch == '<') { forward(2); suffix = scanTagUri("tag"); if(peek() != '>') { scannerException("while scanning a tag","expected '>', but found "+ peek() + "(" + (int)peek() + ")",null); } forward(); } else if(NULL_BL_T_LINEBR[ch]) { suffix = BANG; forward(); } else { int length = 1; boolean useHandle = false; while(!NULL_BL_T_LINEBR[ch]) { if(ch == '!') { useHandle = true; break; } length++; ch = peek(length); } handle = BANG; if(useHandle) { handle = scanTagHandle("tag"); } else { handle = BANG; forward(); } suffix = scanTagUri("tag"); } if(!NULL_BL_LINEBR[peek()]) { scannerException("while scanning a tag","expected ' ', but found " + peek() + "(" + (int)peek() + ")",null); } return getTag(new ByteList[] {handle,suffix}); } private ByteList scanTagUri(final String name) { final ByteList chunks = new ByteList(10); int length = 0; char ch = peek(length); while(STRANGE_CHAR[ch]) { if('%' == ch) { ensure(length,false); chunks.append(this.buffer.bytes,this.pointer,length); length = 0; chunks.append(scanUriEscapes(name)); } else { length++; } ch = peek(length); } if(length != 0) { ensure(length,false); chunks.append(this.buffer.bytes,this.pointer,length); forward(length); } if(chunks.length() == 0) { scannerException("while scanning a " + name,"expected URI, but found " + ch + "(" + (int)ch + ")",null); } return chunks; } private ByteList scanTagHandle(final String name) { char ch = peek(); if(ch != '!') { scannerException("while scanning a " + name,"expected '!', but found " + ch + "(" + (int)ch + ")",null); } int length = 1; ch = peek(length); if(ch != ' ') { while(ALPHA[ch]) { length++; ch = peek(length); } if('!' != ch) { forward(length); scannerException("while scanning a " + name,"expected '!', but found " + ch + "(" + ((int)ch) + ")",null); } length++; } ensure(length,false); final ByteList value = new ByteList(this.buffer.bytes,this.pointer,length,false); forward(length); return value; } private ByteList scanUriEscapes(final String name) { final ByteList bytes = new ByteList(); while(peek() == '%') { forward(); try { ensure(2,false); bytes.append(Integer.parseInt(new String(ByteList.plain(this.buffer.bytes,this.pointer,2)),16)); } catch(final NumberFormatException nfe) { scannerException("while scanning a " + name,"expected URI escape sequence of 2 hexadecimal numbers, but found " + peek(1) + "(" + ((int)peek(1)) + ") and "+ peek(2) + "(" + ((int)peek(2)) + ")",null); } forward(2); } return bytes; } private Token fetchPlain() { this.docStart = false; savePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanPlain(); addToken(tok); return tok; } protected void startingItem() { } protected void possibleEnd() { } private Token scanPlain() { startingItem(); final ByteList chunks = new ByteList(7); final int ind = this.indent+1; ByteList spaces = new ByteList(0); boolean f_nzero = true; boolean[] r_check = R_FLOWNONZERO; boolean[] r_check2 = ALL_FALSE; boolean[] r_check3 = ALL_FALSE; if(this.flowLevel == 0) { f_nzero = false; r_check = R_FLOWZERO; r_check2 = R_FLOWZERO1; r_check3 = R_FLOWZERO; } while(peek() != '#') { int length = 0; int i = 0; for(;;i++) { ensure(i+2,false); if(r_check[this.buffer.bytes[this.pointer+i]&0xFF] || (r_check2[this.buffer.bytes[this.pointer+i]&0xFF] && r_check3[this.buffer.bytes[this.pointer+i+1]&0xFF])) { length = i; final char ch = peek(length); if(!(f_nzero && ch == ':' && !S4[peek(length+1)])) { break; } } } if(length == 0) { break; } this.allowSimpleKey = false; chunks.append(spaces); ensure(length,false); chunks.append(this.buffer.bytes,this.pointer,length); forward(length); possibleEnd(); spaces = scanPlainSpaces(ind); if(spaces == null || (this.flowLevel == 0 && this.column < ind)) { break; } } return getScalar(chunks,true, (char)0); } private int nextPossibleSimpleKey() { for(final Iterator iter = this.possibleSimpleKeys.values().iterator();iter.hasNext();) { final SimpleKey key = (SimpleKey)iter.next(); if(key.getTokenNumber() > 0) { return key.getTokenNumber(); } } return -1; } private ByteList scanPlainSpaces(final int indent) { final ByteList chunks = new ByteList(); int length = 0; while(peek(length) == ' ') { length++; } final byte[] whitespaces = new byte[length]; Arrays.fill(whitespaces,(byte)' '); forward(length); char ch = peek(); if(FULL_LINEBR[ch]) { final byte[] lineBreak = scanLineBreak(); this.allowSimpleKey = true; if(isEndOrStart()) { return new ByteList(0); } final ByteList breaks = new ByteList(); while(BLANK_OR_LINEBR[peek()]) { if(' ' == peek()) { forward(); } else { breaks.append(scanLineBreak()); if(isEndOrStart()) { return new ByteList(0); } } } if(!(lineBreak.length == 1 && lineBreak[0] == '\n')) { chunks.append(lineBreak); } else if(breaks == null || breaks.realSize == 0) { chunks.append(SPACE); } chunks.append(breaks); } else { chunks.append(whitespaces); } return chunks; } private Token fetchSingle() { return fetchFlowScalar('\''); } private Token fetchDouble() { return fetchFlowScalar('"'); } private Token fetchFlowScalar(final char style) { this.docStart = false; savePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanFlowScalar(style); addToken(tok); return tok; } private Token scanFlowScalar(final char style) { startingItem(); final boolean dbl = style == '"'; final ByteList chunks = new ByteList(); final char quote = peek(); forward(); chunks.append(scanFlowScalarNonSpaces(dbl)); while(peek() != quote) { chunks.append(scanFlowScalarSpaces()); chunks.append(scanFlowScalarNonSpaces(dbl)); } forward(); return getScalar(chunks,false,style); } private final static byte[] HEXA_VALUES = new byte[256]; static { Arrays.fill(HEXA_VALUES,(byte)-1); HEXA_VALUES['0'] = 0; HEXA_VALUES['1'] = 1; HEXA_VALUES['2'] = 2; HEXA_VALUES['3'] = 3; HEXA_VALUES['4'] = 4; HEXA_VALUES['5'] = 5; HEXA_VALUES['6'] = 6; HEXA_VALUES['7'] = 7; HEXA_VALUES['8'] = 8; HEXA_VALUES['9'] = 9; HEXA_VALUES['A'] = 10; HEXA_VALUES['B'] = 11; HEXA_VALUES['C'] = 12; HEXA_VALUES['D'] = 13; HEXA_VALUES['E'] = 14; HEXA_VALUES['F'] = 15; HEXA_VALUES['a'] = 10; HEXA_VALUES['b'] = 11; HEXA_VALUES['c'] = 12; HEXA_VALUES['d'] = 13; HEXA_VALUES['e'] = 14; HEXA_VALUES['f'] = 15; } private ByteList parseHexa(int length) { ensure(length,false); ByteList chunks = new ByteList(length/2); for(int i=0;i'); } private Token fetchBlockScalar(final char style) { this.docStart = false; this.allowSimpleKey = true; this.removePossibleSimpleKey(); final Token tok = scanBlockScalar(style); addToken(tok); return tok; } private Token scanBlockScalar(final char style) { startingItem(); final boolean folded = style == '>'; final ByteList chunks = new ByteList(); forward(); final Object[] chompi = scanBlockScalarIndicators(); final Boolean chomping = (Boolean)chompi[0]; final int increment = ((Integer)chompi[1]).intValue(); boolean sameLine = scanBlockScalarIgnoredLine(); int minIndent = this.indent+1; if(minIndent < 0) { minIndent = 0; } ByteList breaks = null; int maxIndent = 0; int ind = 0; if(sameLine) { final boolean leadingNonSpace = !BLANK_T[peek()]; int length = 0; while(!NULL_OR_LINEBR[peek(length)]) { length++; } ensure(length,false); chunks.append(this.buffer.bytes,this.pointer,length); forward(length); } if(increment == -1) { final Object[] brme = scanBlockScalarIndentation(); breaks = (ByteList)brme[0]; maxIndent = ((Integer)brme[1]).intValue(); if(minIndent > maxIndent) { ind = minIndent; } else { ind = maxIndent; } } else { ind = minIndent + increment - 1; breaks = scanBlockScalarBreaks(ind); } byte[] lineBreak = ByteList.NULL_ARRAY; while(this.column == ind && peek() != '\0') { chunks.append(breaks); final boolean leadingNonSpace = !BLANK_T[peek()]; int length = 0; while(!NULL_OR_LINEBR[peek(length)]) { length++; } ensure(length,false); chunks.append(this.buffer.bytes,this.pointer,length); forward(length); lineBreak = scanLineBreak(); breaks = scanBlockScalarBreaks(ind); if(this.column == ind && peek() != '\0') { if(folded && lineBreak.length == 1 && lineBreak[0] == '\n' && leadingNonSpace && !BLANK_T[peek()]) { if(breaks.length() == 0) { chunks.append(SPACE); } } else { chunks.append(lineBreak); } } else { break; } } if(chomping != Boolean.FALSE) { chunks.append(lineBreak); } if(chomping == Boolean.TRUE) { chunks.append(breaks); } return getScalar(chunks,false,style); } private ByteList scanBlockScalarBreaks(final int indent) { final ByteList chunks = new ByteList(); while(this.column < indent && peek() == ' ') { forward(); } while(FULL_LINEBR[peek()]) { chunks.append(scanLineBreak()); while(this.column < indent && peek() == ' ') { forward(); } } return chunks; } private Object[] scanBlockScalarIndentation() { final ByteList chunks = new ByteList(); int maxIndent = 0; while(BLANK_OR_LINEBR[peek()]) { if(peek() != ' ') { chunks.append(scanLineBreak()); } else { forward(); if(this.column > maxIndent) { maxIndent = column; } } } return new Object[] {chunks,new Integer(maxIndent)}; } private Object[] scanBlockScalarIndicators() { Boolean chomping = null; int increment = -1; char ch = peek(); if(ch == '-' || ch == '+') { chomping = ch == '+' ? Boolean.TRUE : Boolean.FALSE; forward(); ch = peek(); if(DIGIT[ch]) { increment = ch-'0'; if(increment == 0) { scannerException("while scanning a block scalar","expected indentation indicator in the range 1-9, but found 0",null); } forward(); } } else if(DIGIT[ch]) { increment = ch-'0'; if(increment == 0) { scannerException("while scanning a block scalar","expected indentation indicator in the range 1-9, but found 0",null); } forward(); ch = peek(); if(ch == '-' || ch == '+') { chomping = ch == '+' ? Boolean.TRUE : Boolean.FALSE; forward(); } } if(!NULL_BL_LINEBR[peek()]) { scannerException("while scanning a block scalar","expected chomping or indentation indicators, but found " + peek() + "(" + ((int)peek()) + ")",null); } return new Object[] {chomping,new Integer(increment)}; } private boolean scanBlockScalarIgnoredLine() { boolean same = true; while(peek() == ' ') { forward(); } if(peek() == '#') { while(!NULL_OR_LINEBR[peek()]) { forward(); } same = false; } if(NULL_OR_LINEBR[peek()]) { scanLineBreak(); return false; } return same; } private Token fetchDirective() { unwindIndent(-1); removePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanDirective(); addToken(tok); return tok; } private Token fetchKey() { if(this.flowLevel == 0) { if(!this.allowSimpleKey) { scannerException(null,"mapping keys are not allowed here",null); } if(addIndent(this.column)) { addToken(getBlockMappingStart()); } } this.allowSimpleKey = this.flowLevel == 0; removePossibleSimpleKey(); forward(); Token t = getKey(); addToken(t); return t; } private Token fetchAlias() { savePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanAnchor(getAlias()); addToken(tok); return tok; } private Token fetchAnchor() { savePossibleSimpleKey(); this.allowSimpleKey = false; final Token tok = scanAnchor(getAnchor()); addToken(tok); return tok; } private Token scanDirective() { startingItem(); forward(); final String name = scanDirectiveName(); String[] value = null; if(name.equals("YAML")) { value = scanYamlDirectiveValue(); } else if(name.equals("TAG")) { value = scanTagDirectiveValue(); } else { while(!NULL_OR_LINEBR[peek()]) { forward(); } } Token t = getDirective(name,value); scanDirectiveIgnoredLine(); return t; } private String scanDirectiveName() { int length = 0; char ch = peek(length); boolean zlen = true; while(ALPHA[ch]) { zlen = false; length++; ch = peek(length); } if(zlen) { scannerException("while scanning a directive","expected alphabetic or numeric character, but found " + ch + "(" + ((int)ch) + ")",null); } String value = null; try { ensure(length,false); value = new String(this.buffer.bytes,this.pointer,length,"ISO8859-1"); } catch(Exception e) { } forward(length); if(!NULL_BL_LINEBR[peek()]) { scannerException("while scanning a directive","expected alphabetic or numeric character, but found " + ch + "(" + ((int)ch) + ")",null); } return value; } private byte[] scanDirectiveIgnoredLine() { while(peek() == ' ') { forward(); } if(peek() == '"') { while(!NULL_OR_LINEBR[peek()]) { forward(); } } final char ch = peek(); if(!NULL_OR_LINEBR[ch]) { scannerException("while scanning a directive","expected a comment or a line break, but found " + peek() + "(" + ((int)peek()) + ")",null); } return scanLineBreak(); } private Token scanAnchor(final Token tok) { final char indicator = peek(); final String name = indicator == '*' ? "alias" : "anchor"; forward(); int length = 0; while(ALPHA[peek(length)]) { length++; } if(length == 0) { scannerException("while scanning an " + name,"expected alphabetic or numeric character, but found something else...",null); } String value = null; try { ensure(length,false); value = new String(this.buffer.bytes,this.pointer,length,"ISO8859-1"); } catch(Exception e) { } forward(length); if(!NON_ALPHA_OR_NUM[peek()]) { scannerException("while scanning an " + name,"expected alphabetic or numeric character, but found "+ peek() + "(" + ((int)peek()) + ")",null); } tok.setValue(value); return finalizeAnchor(tok); } private String[] scanYamlDirectiveValue() { while(peek() == ' ') { forward(); } final String major = scanYamlDirectiveNumber(); if(peek() != '.') { scannerException("while scanning a directive","expected a digit or '.', but found " + peek() + "(" + ((int)peek()) + ")",null); } forward(); final String minor = scanYamlDirectiveNumber(); if(!NULL_BL_LINEBR[peek()]) { scannerException("while scanning a directive","expected a digit or ' ', but found " + peek() + "(" + ((int)peek()) + ")",null); } return new String[] {major,minor}; } private String scanYamlDirectiveNumber() { final char ch = peek(); if(!Character.isDigit(ch)) { scannerException("while scanning a directive","expected a digit, but found " + ch + "(" + ((int)ch) + ")",null); } int length = 0; StringBuffer sb = new StringBuffer(); while(Character.isDigit(peek(length))) { sb.append(peek(length)); length++; } forward(length); return sb.toString(); } public static String into(ByteList b) { try { return new String(b.bytes,0,b.realSize,"ISO8859-1"); } catch(Exception e) { return null; // Shouldn't happen } } private String[] scanTagDirectiveValue() { while(peek() == ' ') { forward(); } final String handle = into(scanTagDirectiveHandle()); while(peek() == ' ') { forward(); } final String prefix = into(scanTagDirectivePrefix()); return new String[] {handle,prefix}; } private ByteList scanTagDirectiveHandle() { final ByteList value = scanTagHandle("directive"); if(peek() != ' ') { scannerException("while scanning a directive","expected ' ', but found " + peek() + "(" + ((int)peek()) + ")",null); } return value; } private ByteList scanTagDirectivePrefix() { final ByteList value = scanTagUri("directive"); if(!NULL_BL_LINEBR[peek()]) { scannerException("while scanning a directive","expected ' ', but found " + peek() + "(" + ((int)peek()) + ")",null); } return value; } /* private final static Pattern NON_PRINTABLE = Pattern.compile("[^\u0009\n\r\u0020-\u007E\u0085\u00A0-\u00FF]"); private final static Pattern R_FLOWZERO = Pattern.compile("[\0 \t\r\n\u0085]|(:[\0 \t\r\n\u0085])"); private final static Pattern R_FLOWNONZERO = Pattern.compile("[\0 \t\r\n\u0085\\[\\]{},:?]"); private final static Pattern LINE_BR_REG = Pattern.compile("[\n\u0085]|(?:\r[^\n])"); private final static Pattern END_OR_START = Pattern.compile("^(---|\\.\\.\\.)[\0 \t\r\n\u0085]$"); private final static Pattern ENDING = Pattern.compile("^---[\0 \t\r\n\u0085]$"); private final static Pattern START = Pattern.compile("^\\.\\.\\.[\0 \t\r\n\u0085]$"); private final static Pattern BEG = Pattern.compile("^([^\0 \t\r\n\u0085\\-?:,\\[\\]{}#&*!|>'\"%@]|([\\-?:][^\0 \t\r\n\u0085]))"); public static void main(final String[] args) throws Exception { // final String test1 = "--- \nA: b\nc: 3.14\n"; final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final StringBuffer input = new StringBuffer(); final Reader reader = new FileReader(filename); char[] buff = new char[1024]; int read = 0; while(true) { read = reader.read(buff); input.append(buff,0,read); if(read < 1024) { break; } } reader.close(); final String str = input.toString(); final long before = System.currentTimeMillis(); int tokens = 0; for(int i=0;i<1;i++) { final Scanner sce2 = new ScannerImpl(str); for(final Iterator iter = sce2.eachToken();iter.hasNext();) { tokens++;iter.next(); // System.out.println(iter.next()); } } final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the " + tokens + " tokens took " + time + "ms, or " + timeS + " seconds"); } */ public static void main(final String[] args) throws Exception { final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final ByteList input = new ByteList(1024); final InputStream reader = new FileInputStream(filename); byte[] buff = new byte[1024]; int read = 0; while(true) { read = reader.read(buff); input.append(buff,0,read); if(read < 1024) { break; } } reader.close(); final long before = System.currentTimeMillis(); int tokens = 0; for(int i=0;i<1;i++) { final Scanner sce2 = new ScannerImpl(input); for(final Iterator iter = sce2.eachToken();iter.hasNext();) { tokens++;//iter.next(); System.out.println(iter.next()); } } final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the " + tokens + " tokens took " + time + "ms, or " + timeS + " seconds"); } public static void tmain(final String[] args) throws Exception { final String filename = args[0]; System.out.println("Reading of file: \"" + filename + "\""); final InputStream reader = new FileInputStream(filename); final long before = System.currentTimeMillis(); int tokens = 0; for(int i=0;i<1;i++) { final Scanner sce2 = new ScannerImpl(reader); for(final Iterator iter = sce2.eachToken();iter.hasNext();) { tokens++;iter.next(); //System.out.println(iter.next()); } } reader.close(); final long after = System.currentTimeMillis(); final long time = after-before; final double timeS = (after-before)/1000.0; System.out.println("Walking through the " + tokens + " tokens took " + time + "ms, or " + timeS + " seconds"); } }// Scanner jvyamlb-0.2.5/src/main/org/jvyamlb/Serializer.java000066400000000000000000000006311151213612200220760ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import org.jvyamlb.nodes.Node; /** * @author Ola Bini */ public interface Serializer { void open() throws IOException; void close() throws IOException; void serialize(final Node node) throws IOException; }// Serializer jvyamlb-0.2.5/src/main/org/jvyamlb/SerializerImpl.java000066400000000000000000000203101151213612200227140ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import java.text.MessageFormat; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.IdentityHashMap; import java.util.HashMap; import java.util.Set; import java.util.HashSet; import org.jvyamlb.events.AliasEvent; import org.jvyamlb.events.DocumentEndEvent; import org.jvyamlb.events.DocumentStartEvent; import org.jvyamlb.events.ScalarEvent; import org.jvyamlb.events.MappingEndEvent; import org.jvyamlb.events.MappingStartEvent; import org.jvyamlb.events.SequenceEndEvent; import org.jvyamlb.events.SequenceStartEvent; import org.jvyamlb.events.StreamEndEvent; import org.jvyamlb.events.StreamStartEvent; import org.jvyamlb.exceptions.SerializerException; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.LinkNode; import org.jvyamlb.nodes.CollectionNode; import org.jvyamlb.nodes.MappingNode; import org.jvyamlb.nodes.ScalarNode; import org.jvyamlb.nodes.SequenceNode; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class SerializerImpl implements Serializer { private Emitter emitter; private Resolver resolver; private YAMLConfig options; private boolean useExplicitStart; private boolean useExplicitEnd; private int[] useVersion; private boolean useTags; private String anchorTemplate; private Map serializedNodes; private Map anchors; private int lastAnchorId; private boolean closed; private boolean opened; public SerializerImpl(final Emitter emitter, final Resolver resolver, final YAMLConfig opts) { this.emitter = emitter; this.resolver = resolver; this.options = opts; this.useExplicitStart = opts.explicitStart(); this.useExplicitEnd = opts.explicitEnd(); int[] version = new int[2]; if(opts.useVersion()) { final String v1 = opts.version(); final int index = v1.indexOf('.'); version[0] = Integer.parseInt(v1.substring(0,index)); version[1] = Integer.parseInt(v1.substring(index+1)); } else { version = null; } this.useVersion = version; this.useTags = opts.useHeader(); this.anchorTemplate = opts.anchorFormat() == null ? "id{0,number,000}" : opts.anchorFormat(); this.serializedNodes = new IdentityHashMap(); this.anchors = new IdentityHashMap(); this.lastAnchorId = 0; this.closed = false; this.opened = false; } protected boolean ignoreAnchor(final Node node) { return false; } public void open() throws IOException { if(!closed && !opened) { this.emitter.emit(new StreamStartEvent()); this.opened = true; } else if(closed) { throw new SerializerException("serializer is closed"); } else { throw new SerializerException("serializer is already opened"); } } public void close() throws IOException { if(!opened) { throw new SerializerException("serializer is not opened"); } else if(!closed) { this.emitter.emit(new StreamEndEvent()); this.closed = true; this.opened = false; } } public void serialize(final Node node) throws IOException { if(!this.closed && !this.opened) { throw new SerializerException("serializer is not opened"); } else if(this.closed) { throw new SerializerException("serializer is closed"); } this.emitter.emit(new DocumentStartEvent(this.useExplicitStart,this.useVersion,null)); anchorNode(node); serializeNode(node,null,null); this.emitter.emit(new DocumentEndEvent(this.useExplicitEnd)); this.serializedNodes = new IdentityHashMap(); this.anchors = new IdentityHashMap(); this.lastAnchorId = 0; } private void anchorNode(Node node) { while(node instanceof LinkNode) { node = ((LinkNode)node).getAnchor(); } if(!ignoreAnchor(node)) { if(this.anchors.containsKey(node)) { String anchor = (String)this.anchors.get(node); if(null == anchor) { anchor = generateAnchor(node); this.anchors.put(node,anchor); } } else { this.anchors.put(node,null); if(node instanceof SequenceNode) { for(final Iterator iter = ((List)node.getValue()).iterator();iter.hasNext();) { anchorNode((Node)iter.next()); } } else if(node instanceof MappingNode) { final Map value = (Map)node.getValue(); for(final Iterator iter = value.entrySet().iterator();iter.hasNext();) { final Map.Entry me = (Map.Entry)iter.next(); anchorNode((Node)me.getKey()); anchorNode((Node)me.getValue()); } } } } } private String generateAnchor(final Node node) { this.lastAnchorId++; return new MessageFormat(this.anchorTemplate).format(new Object[]{new Integer(this.lastAnchorId)}); } private void serializeNode(Node node, final Node parent, final Object index) throws IOException { while(node instanceof LinkNode) { node = ((LinkNode)node).getAnchor(); } String tAlias = (String)this.anchors.get(node); if(this.serializedNodes.containsKey(node) && tAlias != null) { this.emitter.emit(new AliasEvent(tAlias)); } else { this.serializedNodes.put(node,null); this.resolver.descendResolver(parent,index); if(node instanceof ScalarNode) { final String detectedTag = this.resolver.resolve(ScalarNode.class,(ByteList)node.getValue(),new boolean[]{true,false}); final String defaultTag = this.resolver.resolve(ScalarNode.class,(ByteList)node.getValue(),new boolean[]{false,true}); final boolean[] implicit = new boolean[] {false,false}; if(!options.explicitTypes()) { implicit[0] = node.getTag().equals(detectedTag); implicit[1] = node.getTag().equals(defaultTag); } char style = ((ScalarNode)node).getStyle(); if(!implicit[0] && implicit[1] && node.getTag().equals(YAML.DEFAULT_SCALAR_TAG)) { style = '"'; } this.emitter.emit(new ScalarEvent(tAlias,node.getTag(),implicit,(ByteList)node.getValue(),style)); } else if(node instanceof SequenceNode) { final boolean implicit = !options.explicitTypes() && (node.getTag().equals(this.resolver.resolve(SequenceNode.class,null,new boolean[]{true,true}))); this.emitter.emit(new SequenceStartEvent(tAlias,node.getTag(),implicit,((CollectionNode)node).getFlowStyle())); int ix = 0; for(final Iterator iter = ((List)node.getValue()).iterator();iter.hasNext();) { serializeNode((Node)iter.next(),node,new Integer(ix++)); } this.emitter.emit(new SequenceEndEvent()); } else if(node instanceof MappingNode) { final boolean implicit = !options.explicitTypes() && (node.getTag().equals(this.resolver.resolve(MappingNode.class,null,new boolean[]{true,true}))); this.emitter.emit(new MappingStartEvent(tAlias,node.getTag(),implicit,((CollectionNode)node).getFlowStyle())); final Map value = (Map)node.getValue(); for(final Iterator iter = value.entrySet().iterator();iter.hasNext();) { final Map.Entry entry = (Map.Entry)iter.next(); final Node key = (Node)entry.getKey(); final Node val = (Node)entry.getValue(); serializeNode(key,node,null); serializeNode(val,node,key); } this.emitter.emit(new MappingEndEvent()); } } } }// SerializerImpl jvyamlb-0.2.5/src/main/org/jvyamlb/SimpleKey.java000066400000000000000000000016251151213612200216730ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ class SimpleKey { private int tokenNumber; private boolean required; private int index; private int line; private int column; public SimpleKey(final int tokenNumber, final boolean required, final int index, final int line, final int column) { this.tokenNumber = tokenNumber; this.required = required; this.index = index; this.line = line; this.column = column; } public boolean isRequired() { return required; } public int getTokenNumber() { return this.tokenNumber; } public int getColumn() { return this.column; } public Position getPosition() { return new Position(line, column, index); } }// SimpleKey jvyamlb-0.2.5/src/main/org/jvyamlb/YAML.java000066400000000000000000000167411151213612200205400ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import org.jvyamlb.exceptions.YAMLException; import org.jruby.util.ByteList; /** * The combinatorial explosion class. * * @author Ola Bini */ public class YAML { public static final String DEFAULT_SCALAR_TAG = "tag:yaml.org,2002:str"; public static final String DEFAULT_SEQUENCE_TAG = "tag:yaml.org,2002:seq"; public static final String DEFAULT_MAPPING_TAG = "tag:yaml.org,2002:map"; public static final Map ESCAPE_REPLACEMENTS = new HashMap(); static { ESCAPE_REPLACEMENTS.put(new Character('\0'),"0"); ESCAPE_REPLACEMENTS.put(new Character('\u0007'),"a"); ESCAPE_REPLACEMENTS.put(new Character('\u0008'),"b"); ESCAPE_REPLACEMENTS.put(new Character('\u0009'),"t"); ESCAPE_REPLACEMENTS.put(new Character('\n'),"n"); ESCAPE_REPLACEMENTS.put(new Character('\u000B'),"v"); ESCAPE_REPLACEMENTS.put(new Character('\u000C'),"f"); ESCAPE_REPLACEMENTS.put(new Character('\r'),"r"); ESCAPE_REPLACEMENTS.put(new Character('\u001B'),"e"); ESCAPE_REPLACEMENTS.put(new Character('"'),"\""); ESCAPE_REPLACEMENTS.put(new Character('\\'),"\\"); ESCAPE_REPLACEMENTS.put(new Character('\u00A0'),"_"); } public static YAMLConfig config() { return defaultConfig(); } public static YAMLConfig defaultConfig() { return new DefaultYAMLConfig(); } public static ByteList dump(final Object data) { return dump(data,config()); } public static ByteList dump(final Object data, final YAMLFactory fact) { return dump(data,fact, config()); } public static ByteList dump(final Object data, final YAMLConfig cfg) { final List lst = new ArrayList(1); lst.add(data); return dumpAll(lst,cfg); } public static ByteList dump(final Object data, final YAMLFactory fact, final YAMLConfig cfg) { final List lst = new ArrayList(1); lst.add(data); return dumpAll(lst,fact,cfg); } public static ByteList dumpAll(final List data) { return dumpAll(data,config()); } public static ByteList dumpAll(final List data, final YAMLFactory fact) { return dumpAll(data,fact,config()); } public static ByteList dumpAll(final List data, final YAMLConfig cfg) { final ByteArrayOutputStream swe = new ByteArrayOutputStream(); dumpAll(data,swe,cfg); return new ByteList(swe.toByteArray(),false); } public static ByteList dumpAll(final List data, final YAMLFactory fact, final YAMLConfig cfg) { final ByteArrayOutputStream swe = new ByteArrayOutputStream(); dumpAll(data,swe,fact,cfg); return new ByteList(swe.toByteArray(),false); } public static void dump(final Object data, final OutputStream output) { dump(data,output,config()); } public static void dump(final Object data, final OutputStream output, YAMLFactory fact) { dump(data,output,fact,config()); } public static void dump(final Object data, final OutputStream output, final YAMLConfig cfg) { final List lst = new ArrayList(1); lst.add(data); dumpAll(lst,output,cfg); } public static void dump(final Object data, final OutputStream output, final YAMLFactory fact, final YAMLConfig cfg) { final List lst = new ArrayList(1); lst.add(data); dumpAll(lst,output,fact,cfg); } public static void dumpAll(final List data, final OutputStream output) { dumpAll(data,output,config()); } public static void dumpAll(final List data, final OutputStream output, final YAMLFactory fact) { dumpAll(data,output,fact,config()); } public static void dumpAll(final List data, final OutputStream output, final YAMLConfig cfg) { dumpAll(data,output,new DefaultYAMLFactory(),cfg); } public static void dumpAll(final List data, final OutputStream output, final YAMLFactory fact, final YAMLConfig cfg) { final Serializer s = fact.createSerializer(fact.createEmitter(output,cfg),fact.createResolver(),cfg); try { s.open(); final Representer r = fact.createRepresenter(s,cfg); for(final Iterator iter = data.iterator(); iter.hasNext();) { r.represent(iter.next()); } } catch(final java.io.IOException e) { throw new YAMLException(e); } finally { try { s.close(); } catch(final java.io.IOException e) {} } } public static Object load(final ByteList io) { return load(io, new DefaultYAMLFactory(),config()); } public static Object load(final InputStream io) { return load(io, new DefaultYAMLFactory(),config()); } public static Object load(final ByteList io, final YAMLConfig cfg) { return load(io, new DefaultYAMLFactory(),cfg); } public static Object load(final InputStream io, final YAMLConfig cfg) { return load(io, new DefaultYAMLFactory(),cfg); } public static Object load(final ByteList io, final YAMLFactory fact, final YAMLConfig cfg) { final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); if(ctor.checkData()) { return ctor.getData(); } else { return null; } } public static Object load(final InputStream io, final YAMLFactory fact, final YAMLConfig cfg) { final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); if(ctor.checkData()) { return ctor.getData(); } else { return null; } } public static List loadAll(final ByteList io) { return loadAll(io, new DefaultYAMLFactory(),config()); } public static List loadAll(final InputStream io) { return loadAll(io, new DefaultYAMLFactory(),config()); } public static List loadAll(final ByteList io, final YAMLConfig cfg) { return loadAll(io, new DefaultYAMLFactory(),cfg); } public static List loadAll(final InputStream io, final YAMLConfig cfg) { return loadAll(io, new DefaultYAMLFactory(),cfg); } public static List loadAll(final ByteList io, final YAMLFactory fact, final YAMLConfig cfg) { final List result = new ArrayList(); final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); while(ctor.checkData()) { result.add(ctor.getData()); } return result; } public static List loadAll(final InputStream io, final YAMLFactory fact, final YAMLConfig cfg) { final List result = new ArrayList(); final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); while(ctor.checkData()) { result.add(ctor.getData()); } return result; } public static void main(final String[] args) { System.err.println(YAML.load(new ByteList("%YAML 1.0\n---\n!str str".getBytes()))); } }// YAML jvyamlb-0.2.5/src/main/org/jvyamlb/YAMLConfig.java000066400000000000000000000024651151213612200216640ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; /** * @author Ola Bini */ public interface YAMLConfig { YAMLConfig indent(final int indent); int indent(); YAMLConfig useHeader(final boolean useHeader); boolean useHeader(); YAMLConfig useVersion(final boolean useVersion); boolean useVersion(); YAMLConfig version(final String version); String version(); YAMLConfig explicitStart(final boolean expStart); boolean explicitStart(); YAMLConfig explicitEnd(final boolean expEnd); boolean explicitEnd(); YAMLConfig anchorFormat(final String format); String anchorFormat(); YAMLConfig explicitTypes(final boolean expTypes); boolean explicitTypes(); YAMLConfig canonical(final boolean canonical); boolean canonical(); YAMLConfig bestWidth(final int bestWidth); int bestWidth(); YAMLConfig useBlock(final boolean useBlock); boolean useBlock(); YAMLConfig useFlow(final boolean useFlow); boolean useFlow(); YAMLConfig usePlain(final boolean usePlain); boolean usePlain(); YAMLConfig useSingle(final boolean useSingle); boolean useSingle(); YAMLConfig useDouble(final boolean useDouble); boolean useDouble(); }// YAMLConfig jvyamlb-0.2.5/src/main/org/jvyamlb/YAMLFactory.java000066400000000000000000000016741151213612200220670ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.InputStream; import java.io.OutputStream; import org.jruby.util.ByteList; /** * @author Ola Bini */ public interface YAMLFactory { Scanner createScanner(final ByteList io); Scanner createScanner(final InputStream io); Parser createParser(final Scanner scanner); Parser createParser(final Scanner scanner, final YAMLConfig cfg); Resolver createResolver(); Composer createComposer(final Parser parser, final Resolver resolver); Constructor createConstructor(final Composer composer); Emitter createEmitter(final OutputStream output, final YAMLConfig cfg); Serializer createSerializer(final Emitter emitter, final Resolver resolver, final YAMLConfig cfg); Representer createRepresenter(final Serializer serializer, final YAMLConfig cfg); }// YAMLFactory jvyamlb-0.2.5/src/main/org/jvyamlb/YAMLNodeCreator.java000066400000000000000000000005701151213612200226570ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.io.IOException; import org.jvyamlb.nodes.Node; /** * @author Ola Bini */ public interface YAMLNodeCreator { String taguri(); Node toYamlNode(Representer representer) throws IOException; }// YAMLNodeCreator jvyamlb-0.2.5/src/main/org/jvyamlb/events/000077500000000000000000000000001151213612200204265ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/events/AliasEvent.java000066400000000000000000000004721151213612200233270ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class AliasEvent extends NodeEvent { public AliasEvent(final String anchor) { super(anchor); } }// AliasEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/CollectionEndEvent.java000066400000000000000000000004051151213612200250140ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public abstract class CollectionEndEvent extends Event { }// CollectionEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/CollectionStartEvent.java000066400000000000000000000014471151213612200254120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public abstract class CollectionStartEvent extends NodeEvent { private String tag; private boolean implicit; private boolean flowStyle; public CollectionStartEvent(final String anchor, final String tag, final boolean implicit, final boolean flowStyle) { super(anchor); this.tag = tag; this.implicit = implicit; this.flowStyle = flowStyle; } public String getTag() { return this.tag; } public boolean getImplicit() { return this.implicit; } public boolean getFlowStyle() { return this.flowStyle; } }// CollectionStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/DocumentEndEvent.java000066400000000000000000000006701151213612200245030ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class DocumentEndEvent extends Event { private boolean explicit; public DocumentEndEvent(final boolean explicit) { this.explicit = explicit; } public boolean getExplicit() { return explicit; } }// DocumentEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/DocumentStartEvent.java000066400000000000000000000013331151213612200250670ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import java.util.Map; /** * @author Ola Bini */ public class DocumentStartEvent extends Event { private boolean explicit; private int[] version; private Map tags; public DocumentStartEvent(final boolean explicit, final int[] version, final Map tags) { this.explicit = explicit; this.version = version; this.tags = tags; } public boolean getExplicit() { return explicit; } public int[] getVersion() { return version; } public Map getTags() { return tags; } }// DocumentStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/Event.java000066400000000000000000000004711151213612200223540ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public abstract class Event { public String toString() { return "#<" + this.getClass().getName() + ">"; } }// Event jvyamlb-0.2.5/src/main/org/jvyamlb/events/MappingEndEvent.java000066400000000000000000000004031151213612200243120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class MappingEndEvent extends CollectionEndEvent { }// MappingEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/MappingStartEvent.java000066400000000000000000000011111151213612200246760ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class MappingStartEvent extends CollectionStartEvent { public MappingStartEvent(final String anchor, final String tag, final boolean implicit, final boolean flowStyle) { super(anchor,tag,implicit,flowStyle); } public String toString() { return "#<" + this.getClass().getName() + " anchor=\"" + getAnchor() + "\" tag=\"" + getTag() + "\">"; } }// MappingStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/NodeEvent.java000066400000000000000000000006411151213612200231610ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public abstract class NodeEvent extends Event { private String anchor; public NodeEvent(final String anchor) { this.anchor = anchor; } public String getAnchor() { return this.anchor; } }// NodeEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedAliasEvent.java000066400000000000000000000022651151213612200253670ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedAliasEvent extends AliasEvent implements Positionable { private Position.Range range; public PositionedAliasEvent(final String value, final Position.Range range) { super(value); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedAliasEvent)) { PositionedAliasEvent o = (PositionedAliasEvent)other; ret = this.getAnchor().equals(o.getAnchor()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value="+getAnchor()+" range=" + getRange() + ">"; } }// PositionedAliasEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedDocumentEndEvent.java000066400000000000000000000024131151213612200265360ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedDocumentEndEvent extends DocumentEndEvent implements Positionable { private Position.Range range; public PositionedDocumentEndEvent(final boolean explicit, final Position.Range range) { super(explicit); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedDocumentEndEvent)) { PositionedDocumentEndEvent o = (PositionedDocumentEndEvent)other; ret = this.getExplicit() == o.getExplicit() && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " explicit="+getExplicit()+" range=" + getRange() + ">"; } }// PositionedDocumentEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedDocumentStartEvent.java000066400000000000000000000031521151213612200271260ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; import org.jvyamlb.Position; import java.util.Arrays; import org.jvyamlb.Positionable; import java.util.Map; /** * @author Ola Bini */ public class PositionedDocumentStartEvent extends DocumentStartEvent implements Positionable { private Position.Range range; public PositionedDocumentStartEvent(final boolean explicit, final int[] version, final Map tags, final Position.Range range) { super(explicit, version, tags); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedDocumentStartEvent)) { PositionedDocumentStartEvent o = (PositionedDocumentStartEvent)other; ret = this.getExplicit() == o.getExplicit() && Arrays.equals(this.getVersion(), o.getVersion()) && (null == this.getTags() ? null == o.getTags() : this.getTags().equals(o.getTags())) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " explicit="+getExplicit()+" version="+getVersion()[0]+"."+getVersion()[1]+" tags="+getTags()+" range=" + getRange() + ">"; } }// PositionedDocumentStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedMappingEndEvent.java000066400000000000000000000017561151213612200263640ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedMappingEndEvent extends MappingEndEvent implements Positionable { private Position.Range range; public PositionedMappingEndEvent(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedMappingEndEvent) && this.getRange().equals(((PositionedMappingEndEvent)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedMappingEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedMappingStartEvent.java000066400000000000000000000032741151213612200267500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedMappingStartEvent extends MappingStartEvent implements Positionable { private Position.Range range; public PositionedMappingStartEvent(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Position.Range range) { super(anchor, tag, implicit, flowStyle); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedMappingStartEvent)) { PositionedMappingStartEvent o = (PositionedMappingStartEvent)other; ret = this.getImplicit() == o.getImplicit() && this.getFlowStyle() == o.getFlowStyle() && (null == this.getAnchor() ? null == o.getAnchor() : this.getAnchor().equals(o.getAnchor())) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " anchor="+getAnchor()+" tag="+getTag()+" implicit="+getImplicit()+" flowStyle="+getFlowStyle()+" range=" + getRange() + ">"; } }// PositionedMappingStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedScalarEvent.java000066400000000000000000000032431151213612200255400ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; import java.util.Arrays; /** * @author Ola Bini */ public class PositionedScalarEvent extends ScalarEvent implements Positionable { private Position.Range range; public PositionedScalarEvent(final String anchor, final String tag, final boolean[] implicit, final ByteList value, final char style, final Position.Range range) { super(anchor, tag, implicit, value, style); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedScalarEvent)) { PositionedScalarEvent o = (PositionedScalarEvent)other; ret = this.getValue().equals(o.getValue()) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getStyle() == o.getStyle() && Arrays.equals(this.getImplicit(), o.getImplicit()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + getValue() + "\" tag="+getTag()+" style="+getStyle()+" implicit="+getImplicit()[0]+","+getImplicit()[1]+" range=" + getRange() + ">"; } }// PositionedScalarEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedSequenceEndEvent.java000066400000000000000000000017641151213612200265400ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedSequenceEndEvent extends SequenceEndEvent implements Positionable { private Position.Range range; public PositionedSequenceEndEvent(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedSequenceEndEvent) && this.getRange().equals(((PositionedSequenceEndEvent)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedSequenceEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedSequenceStartEvent.java000066400000000000000000000033031151213612200271160ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedSequenceStartEvent extends SequenceStartEvent implements Positionable { private Position.Range range; public PositionedSequenceStartEvent(final String anchor, final String tag, final boolean implicit, final boolean flowStyle, final Position.Range range) { super(anchor, tag, implicit, flowStyle); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedSequenceStartEvent)) { PositionedSequenceStartEvent o = (PositionedSequenceStartEvent)other; ret = this.getImplicit() == o.getImplicit() && this.getFlowStyle() == o.getFlowStyle() && (null == this.getAnchor() ? null == o.getAnchor() : this.getAnchor().equals(o.getAnchor())) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " anchor="+getAnchor()+" tag="+getTag()+" implicit="+getImplicit()+" flowStyle="+getFlowStyle()+" range=" + getRange() + ">"; } }// PositionedSequenceStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedStreamEndEvent.java000066400000000000000000000017501151213612200262160ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedStreamEndEvent extends StreamEndEvent implements Positionable { private Position.Range range; public PositionedStreamEndEvent(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedStreamEndEvent) && this.getRange().equals(((PositionedStreamEndEvent)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedStreamEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/PositionedStreamStartEvent.java000066400000000000000000000017641151213612200266120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedStreamStartEvent extends StreamStartEvent implements Positionable { private Position.Range range; public PositionedStreamStartEvent(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedStreamStartEvent) && this.getRange().equals(((PositionedStreamStartEvent)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedStreamStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/ScalarEvent.java000066400000000000000000000020411151213612200234750ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ScalarEvent extends NodeEvent { private String tag; private char style; private ByteList value; private boolean[] implicit; public ScalarEvent(final String anchor, final String tag, final boolean[] implicit, final ByteList value, final char style) { super(anchor); this.tag = tag; this.implicit = implicit; this.value = value; this.style = style; } public String getTag() { return this.tag; } public char getStyle() { return this.style; } public ByteList getValue() { return this.value; } public boolean[] getImplicit() { return this.implicit; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + new String(value.bytes()) + "\">"; } }// ScalarEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/SequenceEndEvent.java000066400000000000000000000004051151213612200244710ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class SequenceEndEvent extends CollectionEndEvent { }// SequenceEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/SequenceStartEvent.java000066400000000000000000000006701151213612200250640ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class SequenceStartEvent extends CollectionStartEvent { public SequenceStartEvent(final String anchor, final String tag, final boolean implicit, final boolean flowStyle) { super(anchor,tag,implicit,flowStyle); } }// SequenceStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/StreamEndEvent.java000066400000000000000000000003641151213612200241600ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class StreamEndEvent extends Event { }// StreamEndEvent jvyamlb-0.2.5/src/main/org/jvyamlb/events/StreamStartEvent.java000066400000000000000000000003701151213612200245440ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.events; /** * @author Ola Bini */ public class StreamStartEvent extends Event { }// StreamStartEvent jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/000077500000000000000000000000001151213612200213035ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/ComposerException.java000066400000000000000000000021201151213612200256070ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class ComposerException extends YAMLException { private String when; private String what; private String note; public ComposerException(final String when, final String what, final String note) { super("ComposerException " + when + " we had this " + what); this.when = when; this.what = what; this.note = note; } public ComposerException(final Throwable thr) { super(thr); } public String toString() { final StringBuffer lines = new StringBuffer(); if(this.when != null) { lines.append(this.when).append("\n"); } if(this.what != null) { lines.append(this.what).append("\n"); } if(this.note != null) { lines.append(this.note).append("\n"); } lines.append(super.toString()); return lines.toString(); } }// ComposerException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/ConstructorException.java000066400000000000000000000021371151213612200263550ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class ConstructorException extends YAMLException { private String when; private String what; private String note; public ConstructorException(final String when, final String what, final String note) { super("ConstructorException " + when + " we had this " + what); this.when = when; this.what = what; this.note = note; } public ConstructorException(final Throwable thr) { super(thr); } public String toString() { final StringBuffer lines = new StringBuffer(); if(this.when != null) { lines.append(this.when).append("\n"); } if(this.what != null) { lines.append(this.what).append("\n"); } if(this.note != null) { lines.append(this.note).append("\n"); } lines.append(super.toString()); return lines.toString(); } }// ConstructorException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/EmitterException.java000066400000000000000000000006341151213612200254410ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class EmitterException extends YAMLException { public EmitterException(final String msg) { super(msg); } public EmitterException(final Throwable thr) { super(thr); } }// EmitterException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/ParserException.java000066400000000000000000000021061151213612200252600ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class ParserException extends YAMLException { private String when; private String what; private String note; public ParserException(final String when, final String what, final String note) { super("ParserException " + when + " we had this " + what); this.when = when; this.what = what; this.note = note; } public ParserException(final Throwable thr) { super(thr); } public String toString() { final StringBuffer lines = new StringBuffer(); if(this.when != null) { lines.append(this.when).append("\n"); } if(this.what != null) { lines.append(this.what).append("\n"); } if(this.note != null) { lines.append(this.note).append("\n"); } lines.append(super.toString()); return lines.toString(); } }// ParserException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/PositionedComposerException.java000066400000000000000000000022161151213612200276530ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedComposerException extends ComposerException implements Positionable { private Position.Range range; public PositionedComposerException(final String when, final String what, final String note, final Position.Range range) { super(when, what, note); assert range != null; this.range = range; } public PositionedComposerException(final Throwable thr, final Position.Range range) { super(thr); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange(){ return range; } public String toString() { final StringBuffer lines = new StringBuffer(); lines.append(super.toString()); lines.append(" at ").append(getPosition()); return lines.toString(); } }// PositionedComposerException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/PositionedParserException.java000066400000000000000000000022041151213612200273150ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedParserException extends ParserException implements Positionable { private Position.Range range; public PositionedParserException(final String when, final String what, final String note, final Position.Range range) { super(when, what, note); assert range != null; this.range = range; } public PositionedParserException(final Throwable thr, final Position.Range range) { super(thr); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange(){ return range; } public String toString() { final StringBuffer lines = new StringBuffer(); lines.append(super.toString()); lines.append(" at ").append(getPosition()); return lines.toString(); } }// PositionedParserException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/PositionedScannerException.java000066400000000000000000000022111151213612200274500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedScannerException extends ScannerException implements Positionable { private Position.Range range; public PositionedScannerException(final String when, final String what, final String note, final Position.Range range) { super(when, what, note); assert range != null; this.range = range; } public PositionedScannerException(final Throwable thr, final Position.Range range) { super(thr); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange(){ return range; } public String toString() { final StringBuffer lines = new StringBuffer(); lines.append(super.toString()); lines.append(" at ").append(getPosition()); return lines.toString(); } }// PositionedScannerException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/RepresenterException.java000066400000000000000000000006541151213612200263300ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class RepresenterException extends YAMLException { public RepresenterException(final String msg) { super(msg); } public RepresenterException(final Throwable thr) { super(thr); } }// RepresenterException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/ResolverException.java000066400000000000000000000006401151213612200256260ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class ResolverException extends YAMLException { public ResolverException(final String msg) { super(msg); } public ResolverException(final Throwable thr) { super(thr); } }// ResolverException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/ScannerException.java000066400000000000000000000021501151213612200254140ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini * @version $Revision: 1.1 $ */ public class ScannerException extends YAMLException { private String when; private String what; private String note; public ScannerException(final String when, final String what, final String note) { super("ScannerException " + when + " we had this " + what); this.when = when; this.what = what; this.note = note; } public ScannerException(final Throwable thr) { super(thr); } public String toString() { final StringBuffer lines = new StringBuffer(); if(this.when != null) { lines.append(this.when).append("\n"); } if(this.what != null) { lines.append(this.what).append("\n"); } if(this.note != null) { lines.append(this.note).append("\n"); } lines.append(super.toString()); return lines.toString(); } }// ScannerException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/SerializerException.java000066400000000000000000000006501151213612200261370ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class SerializerException extends YAMLException { public SerializerException(final String msg) { super(msg); } public SerializerException(final Throwable thr) { super(thr); } }// SerializerException jvyamlb-0.2.5/src/main/org/jvyamlb/exceptions/YAMLException.java000066400000000000000000000006371151213612200245750ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.exceptions; /** * @author Ola Bini */ public class YAMLException extends RuntimeException { public YAMLException(final String message) { super(message); } public YAMLException(final Throwable cause) { super(cause); } }// YAMLException jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/000077500000000000000000000000001151213612200202325ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/CollectionNode.java000066400000000000000000000007651151213612200240060ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; /** * @author Ola Bini */ public class CollectionNode extends Node { private boolean flowStyle; public CollectionNode(final String tag, final Object value, final boolean flowStyle) { super(tag,value); this.flowStyle = flowStyle; } public boolean getFlowStyle() { return flowStyle; } }// CollectionNode jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/LinkNode.java000066400000000000000000000006561151213612200226070ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; /** * @author Ola Bini */ public class LinkNode extends Node { public LinkNode() { super(null,null); } public void setAnchor(Node anchor) { setValue(anchor); } public Node getAnchor() { return (Node)getValue(); } }// LinkNode jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/MappingNode.java000066400000000000000000000006141151213612200232770ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import java.util.Map; /** * @author Ola Bini */ public class MappingNode extends CollectionNode { public MappingNode(final String tag, final Map value, final boolean flowStyle) { super(tag,value,flowStyle); } }// MappingNode jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/Node.java000066400000000000000000000027601151213612200217670ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; /** * @author Ola Bini */ public abstract class Node { private String tag; private Object value; private int hash = -1; private Object constructed; public Node(final String tag, final Object value) { this.tag = tag; this.value = value; } public Object getConstructed() { return constructed; } public void setConstructed(Object constructed) { this.constructed = constructed; } public String getTag() { return this.tag; } public Object getValue() { return this.value; } public void setValue(Object v) { this.value = v; } public int hashCode() { if(hash == -1) { hash = 3; hash += (null == tag) ? 0 : 3*tag.hashCode(); hash += (null == value) ? 0 : 3*value.hashCode(); } return hash; } public boolean equals(final Object oth) { if(oth instanceof Node) { final Node nod = (Node)oth; return ((this.tag != null) ? this.tag.equals(nod.tag) : nod.tag == null) && ((this.value != null) ? this.value.equals(nod.value) : nod.value == null); } return false; } public String toString() { return "#<" + this.getClass().getName() + " (tag=" + getTag() + ", value=" + getValue()+")>"; } }// Node jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/PositionedMappingNode.java000066400000000000000000000030571151213612200253410ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import org.jvyamlb.Position; import org.jvyamlb.Positionable; import java.util.Map; /** * @author Ola Bini */ public class PositionedMappingNode extends MappingNode implements Positionable { private Position.Range range; public PositionedMappingNode(final String tag, final Map value, final boolean flowStyle, final Position.Range range) { super(tag, value, flowStyle); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public void setRange(Position.Range range) { this.range = range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedMappingNode)) { PositionedMappingNode o = (PositionedMappingNode)other; ret = this.getValue().equals(o.getValue()) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getFlowStyle() == o.getFlowStyle() && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + getValue() + "\" tag="+getTag()+" flowStyle="+getFlowStyle()+" range=" + getRange() + ">"; } }// PositionedMappingEvent jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/PositionedScalarNode.java000066400000000000000000000027111151213612200251470ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedScalarNode extends ScalarNode implements Positionable { private Position.Range range; public PositionedScalarNode(final String tag, final ByteList value, final char style, final Position.Range range) { super(tag, value, style); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedScalarNode)) { PositionedScalarNode o = (PositionedScalarNode)other; ret = this.getValue().equals(o.getValue()) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getStyle() == o.getStyle() && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + getValue() + "\" tag="+getTag()+" style="+getStyle()+" range=" + getRange() + ">"; } }// PositionedScalarEvent jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/PositionedSequenceNode.java000066400000000000000000000030701151213612200255110ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import org.jvyamlb.Position; import org.jvyamlb.Positionable; import java.util.List; /** * @author Ola Bini */ public class PositionedSequenceNode extends SequenceNode implements Positionable { private Position.Range range; public PositionedSequenceNode(final String tag, final List value, final boolean flowStyle, final Position.Range range) { super(tag, value, flowStyle); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public void setRange(Position.Range range) { this.range = range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedSequenceNode)) { PositionedSequenceNode o = (PositionedSequenceNode)other; ret = this.getValue().equals(o.getValue()) && (null == this.getTag() ? null == o.getTag() : this.getTag().equals(o.getTag())) && this.getFlowStyle() == o.getFlowStyle() && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + getValue() + "\" tag="+getTag()+" flowStyle="+getFlowStyle()+" range=" + getRange() + ">"; } }// PositionedSequenceEvent jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/ScalarNode.java000066400000000000000000000007531151213612200231150ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ScalarNode extends Node { private char style; public ScalarNode(final String tag, final ByteList value, final char style) { super(tag,value); this.style = style; } public char getStyle() { return style; } }// ScalarNode jvyamlb-0.2.5/src/main/org/jvyamlb/nodes/SequenceNode.java000066400000000000000000000006211151213612200234520ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.nodes; import java.util.List; /** * @author Ola Bini */ public class SequenceNode extends CollectionNode { public SequenceNode(final String tag, final List value, final boolean flowStyle) { super(tag,value,flowStyle); } }// SequenceNode jvyamlb-0.2.5/src/main/org/jvyamlb/resolver_scanner.rl000066400000000000000000000055761151213612200230500ustar00rootroot00000000000000 package org.jvyamlb; import org.jruby.util.ByteList; public class ResolverScanner { %%{ machine resolver_scanner; action bool_tag { tag = "tag:yaml.org,2002:bool"; } action merge_tag { tag = "tag:yaml.org,2002:merge"; } action null_tag { tag = "tag:yaml.org,2002:null"; } action timestamp_ymd_tag { tag = "tag:yaml.org,2002:timestamp#ymd"; } action timestamp_tag { tag = "tag:yaml.org,2002:timestamp"; } action value_tag { tag = "tag:yaml.org,2002:value"; } action float_tag { tag = "tag:yaml.org,2002:float"; } action int_tag { tag = "tag:yaml.org,2002:int"; } Bool = ("yes" | "Yes" | "YES" | "no" | "No" | "NO" | "true" | "True" | "TRUE" | "false" | "False" | "FALSE" | "on" | "On" | "ON" | "off" | "Off" | "OFF") %/bool_tag; Merge = "<<" %/merge_tag; Value = "=" %/value_tag; Null = ("~" | "null" | "Null" | "null" | "NULL" | " ") %/null_tag; digitF = digit | ","; digit2 = digit | "_" | ","; sign = "-" | "+"; timestampFract = "." digit*; timestampZone = [ \t]* ("Z" | (sign digit{1,2} ( ":" digit{2} )?)); TimestampYMD = digit{4} ("-" digit{2}){2} %/timestamp_ymd_tag; Timestamp = digit{4} ("-" digit{1,2}){2} ([Tt] | [ \t]+) digit{1,2} ":" digit{2} ":" digit{2} timestampFract? timestampZone %/timestamp_tag; exp = [eE] sign digit+; Float = ((sign? ((digitF+ "." digit* exp?) | ((digitF+)? "." digit+ exp?) | (digit+ (":" [0-5]? digit)+ "." digit*) | "." ("inf" | "Inf" | "INF"))) | ("." ("nan" | "NaN" | "NAN"))) %/float_tag; binaryInt = "0b" [0-1_]+; octalInt = "0" [0-7_]+; decimalInt = "0" | [1-9]digit2* (":" [0-5]? digit)*; hexaInt = "0x" [0-9a-fA-F_,]+; Int = sign? (binaryInt | octalInt | decimalInt | hexaInt) %/int_tag; Scalar = Bool | Null | Int | Float | Merge | Value | Timestamp | TimestampYMD; main := Scalar; }%% %% write data nofinal; public String recognize(ByteList list) { String tag = null; int cs; int act; int have = 0; int nread = 0; int p=list.begin; int pe = p+list.realSize; int tokstart = -1; int tokend = -1; byte[] data = list.bytes; if(pe == 0) { data = new byte[]{(byte)'~'}; pe = 1; } %% write init; %% write exec; %% write eof; return tag; } public static void main(String[] args) { ByteList b = new ByteList(78); b.append(args[0].getBytes()); /* for(int i=0;iOla Bini */ public class AliasToken extends Token { private String value; public AliasToken() { super(); } public AliasToken(final String value) { this.value = value; } public void setValue(final Object value) { this.value = (String)value; } public String getValue() { return this.value; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + value + "\">"; } }// AliasToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/AnchorToken.java000066400000000000000000000010451151213612200235030ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class AnchorToken extends Token { private String value; public AnchorToken() { super(); } public AnchorToken(final String value) { this.value = value; } public void setValue(final Object value) { this.value = (String)value; } public String getValue() { return this.value; } }// AnchorToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/BlockEndToken.java000066400000000000000000000003621151213612200237530ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class BlockEndToken extends Token { }// BlockEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/BlockEntryToken.java000066400000000000000000000003661151213612200243520ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class BlockEntryToken extends Token { }// BlockEntryToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/BlockMappingStartToken.java000066400000000000000000000004041151213612200256530ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class BlockMappingStartToken extends Token { }// BlockMappingStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/BlockSequenceStartToken.java000066400000000000000000000004061151213612200260320ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class BlockSequenceStartToken extends Token { }// BlockSequenceStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/DirectiveToken.java000066400000000000000000000010551151213612200242100ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class DirectiveToken extends Token { private String name; private String[] value; public DirectiveToken(final String name, final String[] value) { this.name = name; this.value = value; } public String getName() { return this.name; } public String[] getValue() { return this.value; } }// DirectiveToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/DocumentEndToken.java000066400000000000000000000003701151213612200244760ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class DocumentEndToken extends Token { }// DocumentEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/DocumentStartToken.java000066400000000000000000000003741151213612200250710ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class DocumentStartToken extends Token { }// DocumentStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/FlowEntryToken.java000066400000000000000000000003641151213612200242250ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class FlowEntryToken extends Token { }// FlowEntryToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/FlowMappingEndToken.java000066400000000000000000000003761151213612200251510ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class FlowMappingEndToken extends Token { }// FlowMappingEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/FlowMappingStartToken.java000066400000000000000000000004021151213612200255260ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class FlowMappingStartToken extends Token { }// FlowMappingStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/FlowSequenceEndToken.java000066400000000000000000000004001151213612200253120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class FlowSequenceEndToken extends Token { }// FlowSequenceEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/FlowSequenceStartToken.java000066400000000000000000000004041151213612200257050ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class FlowSequenceStartToken extends Token { }// FlowSequenceStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/KeyToken.java000066400000000000000000000003501151213612200230170ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class KeyToken extends Token { }// KeyToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedAliasToken.java000066400000000000000000000025471151213612200253700ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedAliasToken extends AliasToken implements Positionable { private Position.Range range; public PositionedAliasToken(final Position.Range range) { super(); assert range != null; this.range = range; } public PositionedAliasToken(final String value, final Position.Range range) { super(value); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedAliasToken)) { PositionedAliasToken o = (PositionedAliasToken)other; ret = this.getValue().equals(o.getValue()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\""+getValue()+"\" range=" + getRange() + ">"; } }// PositionedAliasToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedAnchorToken.java000066400000000000000000000025571151213612200255520ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedAnchorToken extends AnchorToken implements Positionable { private Position.Range range; public PositionedAnchorToken(final Position.Range range) { super(); assert range != null; this.range = range; } public PositionedAnchorToken(final String value, final Position.Range range) { super(value); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedAnchorToken)) { PositionedAnchorToken o = (PositionedAnchorToken)other; ret = this.getValue().equals(o.getValue()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\""+getValue()+"\" range=" + getRange() + ">"; } }// PositionedAnchorToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedBlockEndToken.java000066400000000000000000000017301151213612200260110ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedBlockEndToken extends BlockEndToken implements Positionable { private Position.Range range; public PositionedBlockEndToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedBlockEndToken) && this.getRange().equals(((PositionedBlockEndToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedBlockEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedBlockEntryToken.java000066400000000000000000000017441151213612200264110ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedBlockEntryToken extends BlockEntryToken implements Positionable { private Position.Range range; public PositionedBlockEntryToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedBlockEntryToken) && this.getRange().equals(((PositionedBlockEntryToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedBlockEntryToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedBlockMappingStartToken.java000066400000000000000000000020161151213612200277120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedBlockMappingStartToken extends BlockMappingStartToken implements Positionable { private Position.Range range; public PositionedBlockMappingStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedBlockMappingStartToken) && this.getRange().equals(((PositionedBlockMappingStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedBlockMappingStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedBlockSequenceStartToken.java000066400000000000000000000020241151213612200300660ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedBlockSequenceStartToken extends BlockSequenceStartToken implements Positionable { private Position.Range range; public PositionedBlockSequenceStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedBlockSequenceStartToken) && this.getRange().equals(((PositionedBlockSequenceStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedBlockSequenceStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedDirectiveToken.java000066400000000000000000000025421151213612200262500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; import org.jvyamlb.Position; import java.util.Arrays; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedDirectiveToken extends DirectiveToken implements Positionable { private Position.Range range; public PositionedDirectiveToken(final String name, final String[] value, final Position.Range range) { super(name, value); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedDirectiveToken)) { PositionedDirectiveToken o = (PositionedDirectiveToken)other; ret = this.getName().equals(o.getName()) && Arrays.equals(this.getValue(),o.getValue()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " name=\""+getName()+"\" range=" + getRange() + ">"; } }// PositionedDirectiveToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedDocumentEndToken.java000066400000000000000000000017521151213612200265410ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedDocumentEndToken extends DocumentEndToken implements Positionable { private Position.Range range; public PositionedDocumentEndToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedDocumentEndToken) && this.getRange().equals(((PositionedDocumentEndToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedDocumentEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedDocumentStartToken.java000066400000000000000000000017661151213612200271350ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedDocumentStartToken extends DocumentStartToken implements Positionable { private Position.Range range; public PositionedDocumentStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedDocumentStartToken) && this.getRange().equals(((PositionedDocumentStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedDocumentStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedFlowEntryToken.java000066400000000000000000000017361151213612200262670ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedFlowEntryToken extends FlowEntryToken implements Positionable { private Position.Range range; public PositionedFlowEntryToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedFlowEntryToken) && this.getRange().equals(((PositionedFlowEntryToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedFlowEntryToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedFlowMappingEndToken.java000066400000000000000000000017741151213612200272120ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedFlowMappingEndToken extends FlowMappingEndToken implements Positionable { private Position.Range range; public PositionedFlowMappingEndToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedFlowMappingEndToken) && this.getRange().equals(((PositionedFlowMappingEndToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedFlowMappingEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedFlowMappingStartToken.java000066400000000000000000000020101151213612200275610ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedFlowMappingStartToken extends FlowMappingStartToken implements Positionable { private Position.Range range; public PositionedFlowMappingStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedFlowMappingStartToken) && this.getRange().equals(((PositionedFlowMappingStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedFlowMappingStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedFlowSequenceEndToken.java000066400000000000000000000020021151213612200273500ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedFlowSequenceEndToken extends FlowSequenceEndToken implements Positionable { private Position.Range range; public PositionedFlowSequenceEndToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedFlowSequenceEndToken) && this.getRange().equals(((PositionedFlowSequenceEndToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedFlowSequenceEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedFlowSequenceStartToken.java000066400000000000000000000020161151213612200277440ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedFlowSequenceStartToken extends FlowSequenceStartToken implements Positionable { private Position.Range range; public PositionedFlowSequenceStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedFlowSequenceStartToken) && this.getRange().equals(((PositionedFlowSequenceStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedFlowSequenceStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedKeyToken.java000066400000000000000000000016721151213612200250650ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedKeyToken extends KeyToken implements Positionable { private Position.Range range; public PositionedKeyToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedKeyToken) && this.getRange().equals(((PositionedKeyToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedKeyToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedScalarToken.java000066400000000000000000000030561151213612200255400ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedScalarToken extends ScalarToken implements Positionable { private Position.Range range; public PositionedScalarToken(final ByteList value, final boolean plain, final Position.Range range) { this(value,plain,(char)0, range); } public PositionedScalarToken(final ByteList value, final boolean plain, final char style, final Position.Range range) { super(value, plain, style); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedScalarToken)) { PositionedScalarToken o = (PositionedScalarToken)other; ret = this.getValue().equals(o.getValue()) && this.getPlain() == o.getPlain() && this.getStyle() == o.getStyle() && this.getRange().equals(o.getRange()); } return ret; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + new String(getValue().bytes()) + "\" range=" + getRange() + ">"; } }// PositionedScalarToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedStreamEndToken.java000066400000000000000000000017441151213612200262170ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedStreamEndToken extends StreamEndToken implements Positionable { private Position.Range range; public PositionedStreamEndToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedStreamEndToken) && this.getRange().equals(((PositionedStreamEndToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedStreamEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedStreamStartToken.java000066400000000000000000000017641151213612200266100ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedStreamStartToken extends StreamStartToken implements Positionable { private Position.Range range; public PositionedStreamStartToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedStreamStartToken) && this.getRange().equals(((PositionedStreamStartToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " position=" + getPosition() + ">"; } }// PositionedStreamStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedTagToken.java000066400000000000000000000027201151213612200250430ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; import org.jvyamlb.Position; import java.util.Arrays; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedTagToken extends TagToken implements Positionable { private Position.Range range; public PositionedTagToken(final ByteList[] value, final Position.Range range) { super(value); assert range != null; this.range = range; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { boolean ret = this == other; if(!ret && (other instanceof PositionedTagToken)) { PositionedTagToken o = (PositionedTagToken)other; ret = Arrays.equals(this.getValue(), o.getValue()) && this.getRange().equals(o.getRange()); } return ret; } public String toString() { StringBuffer sb = new StringBuffer(); String sep = ""; for(int i=0;i"; } }// PositionedScalarToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/PositionedValueToken.java000066400000000000000000000017061151213612200254070ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jvyamlb.Position; import org.jvyamlb.Positionable; /** * @author Ola Bini */ public class PositionedValueToken extends ValueToken implements Positionable { private Position.Range range; public PositionedValueToken(Position.Range r) { assert r != null; this.range = r; } public Position getPosition() { return range.start; } public Position.Range getRange() { return range; } public boolean equals(Object other) { return this == other || ((other instanceof PositionedValueToken) && this.getRange().equals(((PositionedValueToken)other).getRange())); } public String toString() { return "#<" + this.getClass().getName() + " range=" + getRange() + ">"; } }// PositionedValueToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/ScalarToken.java000066400000000000000000000017121151213612200234770ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class ScalarToken extends Token { private ByteList value; private boolean plain; private char style; public ScalarToken(final ByteList value, final boolean plain) { this(value,plain,(char)0); } public ScalarToken(final ByteList value, final boolean plain, final char style) { this.value = value; this.plain = plain; this.style = style; } public boolean getPlain() { return this.plain; } public ByteList getValue() { return this.value; } public char getStyle() { return this.style; } public String toString() { return "#<" + this.getClass().getName() + " value=\"" + new String(value.bytes()) + "\">"; } }// ScalarToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/StreamEndToken.java000066400000000000000000000005471151213612200241610ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class StreamEndToken extends Token { public boolean equals(Object other) { return this == other || (other instanceof StreamEndToken); } }// StreamEndToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/StreamStartToken.java000066400000000000000000000005551151213612200245470ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class StreamStartToken extends Token { public boolean equals(Object other) { return this == other || (other instanceof StreamStartToken); } }// StreamStartToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/TagToken.java000066400000000000000000000007001151213612200230010ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class TagToken extends Token { private ByteList[] value; public TagToken(final ByteList[] value) { this.value = value; } public ByteList[] getValue() { return this.value; } }// TagToken jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/Token.java000066400000000000000000000031411151213612200223470ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public abstract class Token { public final static DocumentStartToken DOCUMENT_START = new DocumentStartToken(); public final static DocumentEndToken DOCUMENT_END = new DocumentEndToken(); public final static BlockMappingStartToken BLOCK_MAPPING_START = new BlockMappingStartToken(); public final static BlockSequenceStartToken BLOCK_SEQUENCE_START = new BlockSequenceStartToken(); public final static BlockEntryToken BLOCK_ENTRY = new BlockEntryToken(); public final static BlockEndToken BLOCK_END = new BlockEndToken(); public final static FlowEntryToken FLOW_ENTRY = new FlowEntryToken(); public final static FlowMappingEndToken FLOW_MAPPING_END = new FlowMappingEndToken(); public final static FlowMappingStartToken FLOW_MAPPING_START = new FlowMappingStartToken(); public final static FlowSequenceEndToken FLOW_SEQUENCE_END = new FlowSequenceEndToken(); public final static FlowSequenceStartToken FLOW_SEQUENCE_START = new FlowSequenceStartToken(); public final static KeyToken KEY = new KeyToken(); public final static ValueToken VALUE = new ValueToken(); public final static StreamEndToken STREAM_END = new StreamEndToken(); public final static StreamStartToken STREAM_START = new StreamStartToken(); public Token() { } public void setValue(final Object value) { } public String toString() { return "#<" + this.getClass().getName() + ">"; } }// Token jvyamlb-0.2.5/src/main/org/jvyamlb/tokens/ValueToken.java000066400000000000000000000003541151213612200233470ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.tokens; /** * @author Ola Bini */ public class ValueToken extends Token { }// ValueToken jvyamlb-0.2.5/src/main/org/jvyamlb/util/000077500000000000000000000000001151213612200200775ustar00rootroot00000000000000jvyamlb-0.2.5/src/main/org/jvyamlb/util/Base64Coder.java000066400000000000000000000076651151213612200227610ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.util; /** * @author Ola Bini */ public class Base64Coder { // Mapping table from 6-bit nibbles to Base64 characters. private final static char[] map1 = new char[64]; static { int i=0; for(char c='A'; c<='Z'; c++) map1[i++] = c; for(char c='a'; c<='z'; c++) map1[i++] = c; for(char c='0'; c<='9'; c++) map1[i++] = c; map1[i++] = '+'; map1[i++] = '/'; } // Mapping table from Base64 characters to 6-bit nibbles. private final static byte[] map2 = new byte[128]; static { for(int i=0; i>> 2; int o1 = ((i0 & 3) << 4) | (i1 >>> 4); int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6); int o3 = i2 & 0x3F; out[op++] = map1[o0]; out[op++] = map1[o1]; out[op] = op < oDataLen ? map1[o2] : '='; op++; out[op] = op < oDataLen ? map1[o3] : '='; op++; } return out; } /** * Decodes Base64 data. * No blanks or line breaks are allowed within the Base64 encoded data. * @param in a character array containing the Base64 encoded data. * @return An array containing the decoded data bytes. * @throws IllegalArgumentException if the input is not valid Base64 encoded data. */ public static byte[] decode(final byte[] in) { int iLen = in.length; if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4."); while (iLen > 0 && in[iLen-1] == '=') iLen--; int oLen = (iLen*3) / 4; byte[] out = new byte[oLen]; int ip = 0; int op = 0; while (ip < iLen) { int i0 = in[ip++]; int i1 = in[ip++]; int i2 = ip < iLen ? in[ip++] : 'A'; int i3 = ip < iLen ? in[ip++] : 'A'; if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) throw new IllegalArgumentException ("Illegal character in Base64 encoded data."); int b0 = map2[i0]; int b1 = map2[i1]; int b2 = map2[i2]; int b3 = map2[i3]; if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) throw new IllegalArgumentException ("Illegal character in Base64 encoded data."); int o0 = ( b0 <<2) | (b1>>>4); int o1 = ((b1 & 0xf)<<4) | (b2>>>2); int o2 = ((b2 & 3)<<6) | b3; out[op++] = (byte)o0; if (op 0;) { for (Entry e = tab[i]; e != null; e = e.next) { if (e.value.equals(value)) { return true; } } } return false; } public boolean containsValue(Object value) { return contains(value); } public boolean containsKey(int key) { Entry tab[] = table; int hash = key; int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.hash == hash) { return true; } } return false; } public Object get(int key) { Entry tab[] = table; int hash = key; int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.hash == hash) { return e.value; } } return null; } protected void rehash() { int oldCapacity = table.length; Entry oldMap[] = table; int newCapacity = oldCapacity * 2 + 1; Entry newMap[] = new Entry[newCapacity]; threshold = (int) (newCapacity * loadFactor); table = newMap; for (int i = oldCapacity; i-- > 0;) { for (Entry old = oldMap[i]; old != null;) { Entry e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = newMap[index]; newMap[index] = e; } } } Entry getEntry(int key) { Entry tab[] = table; int hash = key; int index = (hash & 0x7FFFFFFF) % tab.length; for(Entry e = tab[index]; e != null; e = e.next) { if (e.hash == hash) { return e; } } return null; } public Object put(int key, Object value) { // Makes sure the key is not already in the hashtable. Entry tab[] = table; int hash = key; int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.hash == hash) { Object old = e.value; e.value = value; return old; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. Entry e = new Entry(hash, key, value, tab[index]); tab[index] = e; count++; return null; } public Object remove(int key) { Entry tab[] = table; int hash = key; int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) { if (e.hash == hash) { if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; Object oldValue = e.value; e.value = null; return oldValue; } } return null; } public synchronized void clear() { Entry tab[] = table; for (int index = tab.length; --index >= 0;) { tab[index] = null; } count = 0; } private abstract class HashIterator implements Iterator { Entry next; // next entry to return int index; // current slot Entry current; // current entry HashIterator() { Entry[] t = table; int i = t.length; Entry n = null; if(count != 0) { // advance to first entry while (i > 0 && (n = t[--i]) == null) { ; } } next = n; index = i; } public boolean hasNext() { return next != null; } Entry nextEntry() { Entry e = next; if(e == null) { throw new NoSuchElementException(); } Entry n = e.next; Entry[] t = table; int i = index; while(n == null && i > 0) { n = t[--i]; } index = i; next = n; return current = e; } public void remove() { throw new UnsupportedOperationException(); } } private class ValueIterator extends HashIterator { public Object next() { return nextEntry().value; } } private class KeyIterator extends HashIterator { public Object next() { return new Integer(nextEntry().key); } } private class EntryIterator extends HashIterator { public Object next() { return nextEntry(); } } Iterator newKeyIterator() { return new KeyIterator(); } Iterator newValueIterator() { return new ValueIterator(); } Iterator newEntryIterator() { return new EntryIterator(); } private transient Set entrySet = null; public Set keySet() { Set ks = keySet; return (ks != null ? ks : (keySet = new KeySet())); } private class KeySet extends AbstractSet { public Iterator iterator() { return newKeyIterator(); } public int size() { return count; } public boolean contains(Object o) { if(o instanceof Number) { return containsKey(((Number)o).intValue()); } return false; } public boolean remove(Object o) { throw new UnsupportedOperationException(); } public void clear() { IntHashMap.this.clear(); } } public Collection values() { Collection vs = values; return (vs != null ? vs : (values = new Values())); } private class Values extends AbstractCollection { public Iterator iterator() { return newValueIterator(); } public int size() { return count; } public boolean contains(Object o) { return containsValue(o); } public void clear() { IntHashMap.this.clear(); } } public Set entrySet() { Set es = entrySet; return (es != null ? es : (entrySet = new EntrySet())); } private class EntrySet extends AbstractSet { public Iterator iterator() { return newEntryIterator(); } public boolean contains(Object o) { if (!(o instanceof Entry)) { return false; } Entry e = (Entry)o; Entry candidate = getEntry(e.key); return candidate != null && candidate.equals(e); } public boolean remove(Object o) { throw new UnsupportedOperationException(); } public int size() { return count; } public void clear() { IntHashMap.this.clear(); } } } jvyamlb-0.2.5/src/main/org/jvyamlb/util/IntStack.java000066400000000000000000000024071151213612200224650ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb.util; /** * @author Ola Bini */ public class IntStack { private int[] values; private int capacity; private int index; public IntStack() { this(27); } public IntStack(int size) { this.values = new int[size]; this.capacity = size-1; this.index = -1; } public boolean isEmpty() { return index == -1; } public int size() { return index + 1; } public int get(int ix) { if(ix < 0 || ix > index) { throw new IndexOutOfBoundsException("Index out of bounds: " + ix); } return values[ix]; } public int pop() { if(index == -1) { throw new IllegalStateException("Can't pop from an empty stack"); } return values[index--]; } public void push(int value) { values[++index] = value; if(index == capacity) { int[] newValues = new int[this.values.length*2]; System.arraycopy(values,0,newValues,0,this.values.length); capacity = newValues.length - 1; values = newValues; } } }// IntStack jvyamlb-0.2.5/src/test/000077500000000000000000000000001151213612200147225ustar00rootroot00000000000000jvyamlb-0.2.5/src/test/org/000077500000000000000000000000001151213612200155115ustar00rootroot00000000000000jvyamlb-0.2.5/src/test/org/jvyamlb/000077500000000000000000000000001151213612200171555ustar00rootroot00000000000000jvyamlb-0.2.5/src/test/org/jvyamlb/PositionTest.java000066400000000000000000000006301151213612200224630ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import junit.framework.TestCase; /** * * @author Ola Bini */ public class PositionTest extends TestCase { public PositionTest(String name) { super(name); } public void testTrue() { } // TODO: implement }// PositionTest jvyamlb-0.2.5/src/test/org/jvyamlb/PositioningComposerImplTest.java000066400000000000000000000063461151213612200255250ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.jruby.util.ByteList; import org.jvyamlb.nodes.Node; import org.jvyamlb.nodes.PositionedScalarNode; import org.jvyamlb.nodes.PositionedMappingNode; import org.jvyamlb.nodes.PositionedSequenceNode; import java.util.HashMap; import java.util.Map; import org.jvyamlb.exceptions.ComposerException; import org.jvyamlb.exceptions.PositionedComposerException; /** * @author Ola Bini */ public class PositioningComposerImplTest extends YAMLTestCase { public PositioningComposerImplTest(String name) { super(name); } protected Node getDocument(String input) { Composer composer = new PositioningComposerImpl(new PositioningParserImpl(new PositioningScannerImpl(input)), new ResolverImpl()); return composer.getNode(); } public void testThatEmptyComposerGeneratesPositionedEnvelope() { Node expected = null; Node node = getDocument(""); assertEquals(expected, node); } public void testThatSimpleScalarGeneratesCorrectPositioning() throws Exception { Node expected = new PositionedScalarNode("tag:yaml.org,2002:str", s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1))); Node node = getDocument("a"); assertEquals(expected, node); } public void testThatAKeyValuePairGetsCorrectPositioning() throws Exception { Map value = new HashMap(); value.put( new PositionedScalarNode("tag:yaml.org,2002:str", s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1))), new PositionedScalarNode("tag:yaml.org,2002:str", s("b"), (char)0, new Position.Range(new Position(0,3,3), new Position(0,4,4))) ); Node expected = new PositionedMappingNode("tag:yaml.org,2002:map", value, false, new Position.Range(new Position(0,0,0), new Position(0,4,4))); Node node = getDocument("a: b"); assertEquals(expected, node); } public void testThatASimpleSequenceGetsCorrectPositioning() throws Exception { List value = new ArrayList(); value.add( new PositionedScalarNode("tag:yaml.org,2002:str", s("a"), (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3))) ); Node expected = new PositionedSequenceNode("tag:yaml.org,2002:seq", value, false, new Position.Range(new Position(0,0,0), new Position(0,3,3))); Node node = getDocument("- a"); assertEquals(expected, node); } public void testComposerExceptionIncludesPositioningInformation() throws Exception { try { getDocument(" *foobar"); assertTrue("composing should throw an exception", false); } catch(ComposerException e) { assertTrue("Exception should be instance of PositionedComposerException", e instanceof PositionedComposerException); assertEquals("Position should be correct for exception", new Position.Range(new Position(0,1,1),new Position(0,8,8)), ((PositionedComposerException)e).getRange()); } } } jvyamlb-0.2.5/src/test/org/jvyamlb/PositioningParserImplTest.java000066400000000000000000000732511151213612200251710ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.jruby.util.ByteList; import org.jvyamlb.events.PositionedStreamStartEvent; import org.jvyamlb.events.PositionedStreamEndEvent; import org.jvyamlb.events.PositionedDocumentStartEvent; import org.jvyamlb.events.PositionedDocumentEndEvent; import org.jvyamlb.events.PositionedScalarEvent; import org.jvyamlb.events.PositionedMappingStartEvent; import org.jvyamlb.events.PositionedMappingEndEvent; import org.jvyamlb.events.PositionedSequenceStartEvent; import org.jvyamlb.events.PositionedSequenceEndEvent; import org.jvyamlb.events.PositionedAliasEvent; import org.jvyamlb.exceptions.ParserException; import org.jvyamlb.exceptions.PositionedParserException; /** * @author Ola Bini */ public class PositioningParserImplTest extends YAMLTestCase { public PositioningParserImplTest(String name) { super(name); } protected List getParse(String input) { Parser parser = new PositioningParserImpl(new PositioningScannerImpl(input)); List output = new ArrayList(); for(Iterator iter = parser.iterator(); iter.hasNext();) { output.add(iter.next()); } return output; } public void testThatEmptyParserGeneratesPositionedEnvelope() { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,0,0)))); List events = getParse(""); assertEquals(expected, events); } public void testThatSimpleScalarGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,1,1)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,1,1)))); List events = getParse("a"); assertEquals(expected, events); } public void testThatComplicatedeScalarGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(true, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{false,true}, s("abcdefafgsdfgsdfg sfgdfsg fdsgfgsdf"), '"', new Position.Range(new Position(0,4,4), new Position(2,10,41)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(2,10,41)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(2,10,41)))); List events = getParse("--- \"abcdefafgsdfgsdfg\n"+ "sfgdfsg\n"+ "fdsgfgsdf\""); assertEquals(expected, events); } public void testThatAKeyValuePairGetsCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,3,3), new Position(0,4,4)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(0,4,4)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,4,4)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,4,4)))); List events = getParse("a: b"); assertEquals(expected, events); } public void testThatTwoKeyValuePairsGetsCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,3,3), new Position(0,4,4)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("c"), (char)0, new Position.Range(new Position(1,0,5), new Position(1,1,6)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("d"), (char)0, new Position.Range(new Position(1,4,9), new Position(1,5,10)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(1,5,10)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(1,5,10)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(1,5,10)))); List events = getParse("a: b\n"+ "c: d"); assertEquals(expected, events); } public void testThatAOneItemSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,3,3)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,3,3)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,3,3)))); List events = getParse("- a"); assertEquals(expected, events); } public void testThatMoreThanOneItemSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(1,4,8), new Position(1,5,9)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("c"), (char)0, new Position.Range(new Position(2,2,12), new Position(2,3,13)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(2,3,13)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(2,3,13)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(2,3,13)))); List events = getParse("- a\n"+ "- b\n"+ "- c"); assertEquals(expected, events); } public void testThatListedSequenesWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedSequenceStartEvent(null, null, true, false, new Position.Range(new Position(1,2,6)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("foo"), (char)0, new Position.Range(new Position(1,4,8), new Position(1,7,11)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("bar"), (char)0, new Position.Range(new Position(2,4,16), new Position(2,7,19)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(2,7,19)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(3,2,22), new Position(3,3,23)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(3,3,23)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(3,3,23)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(3,3,23)))); List events = getParse("- a\n"+ "- - foo\n"+ " - bar\n"+ "- b"); assertEquals(expected, events); } public void testThatSimpleFlowMappingWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, null, true, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,4,4), new Position(0,5,5)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(0,6,6)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,7,7)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,7,7)))); List events = getParse("{a: b }"); assertEquals(expected, events); } public void testThatSimpleFlowSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, null, true, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,2,2)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,3,3)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,3,3)))); List events = getParse("[a]"); assertEquals(expected, events); } public void testThatFlowSequenceWithMoreThanOneElementWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, null, true, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,4,4), new Position(0,5,5)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,6,6)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,7,7)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,7,7)))); List events = getParse("[a, b ]"); assertEquals(expected, events); } public void testScalarWithTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, "!str", new boolean[]{false,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,6,6)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,6,6)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,6,6)))); List events = getParse("!str a"); assertEquals(expected, events); } public void testScalarWithAnchor() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent("blad", null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,7,7)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,7,7)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,7,7)))); List events = getParse("&blad a"); assertEquals(expected, events); } public void testScalarWithAnchorAndTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent("blad", "!str", new boolean[]{false,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,12,12)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,12,12)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,12,12)))); List events = getParse("&blad !str a"); assertEquals(expected, events); events = getParse("!str &blad a"); assertEquals(expected, events); } public void testAlias() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedAliasEvent("blad", new Position.Range(new Position(0,0,0), new Position(0,5,5)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,5,5)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,5,5)))); List events = getParse("*blad"); assertEquals(expected, events); } public void testSequenceWithTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent(null, "!seq", false, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,6,6), new Position(0,7,7)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,7,7)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,8,8)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,8,8)))); List events = getParse("!seq [a]"); assertEquals(expected, events); } public void testSequenceWithAnchor() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent("blad", null, true, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,7,7), new Position(0,8,8)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,8,8)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,9,9)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,9,9)))); List events = getParse("&blad [a]"); assertEquals(expected, events); } public void testSequenceWithAnchorAndTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedSequenceStartEvent("blad", "!seq", false, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,12,12), new Position(0,13,13)))); expected.add(new PositionedSequenceEndEvent(new Position.Range(new Position(0,13,13)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,14,14)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,14,14)))); List events = getParse("&blad !seq [a]"); assertEquals(expected, events); events = getParse("!seq &blad [a]"); assertEquals(expected, events); } public void testMappingWithTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, "!map", false, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,6,6), new Position(0,7,7)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,9,9), new Position(0,10,10)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(0,10,10)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,11,11)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,11,11)))); List events = getParse("!map {a: b}"); assertEquals(expected, events); } public void testMappingWithAnchor() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent("blad", null, true, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,7,7), new Position(0,8,8)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,10,10), new Position(0,11,11)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(0,11,11)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,12,12)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,12,12)))); List events = getParse("&blad {a: b}"); assertEquals(expected, events); } public void testMappingWithAnchorAndTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent("blad", "!map", false, true, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,12,12), new Position(0,13,13)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("b"), (char)0, new Position.Range(new Position(0,15,15), new Position(0,16,16)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(0,16,16)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,17,17)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,17,17)))); List events = getParse("&blad !map {a: b}"); assertEquals(expected, events); events = getParse("!map &blad {a: b}"); assertEquals(expected, events); } public void testLiteralScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{false,true}, s("abc\nfoobar"), '|', new Position.Range(new Position(0,0,0), new Position(2,7,14)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(2,7,14)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(2,7,14)))); List events = getParse("|\n"+ " abc\n"+ " foobar"); assertEquals(expected, events); } public void testFoldedScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{false,true}, s("abc foobar"), '>', new Position.Range(new Position(0,0,0), new Position(2,7,14)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(2,7,14)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(2,7,14)))); List events = getParse(">\n"+ " abc\n"+ " foobar"); assertEquals(expected, events); } public void testSingleQuotedScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{false,true}, s("abc"), '\'', new Position.Range(new Position(0,0,0), new Position(0,5,5)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(0,5,5)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(0,5,5)))); List events = getParse("'abc'"); assertEquals(expected, events); } public void testParserExceptionIncludesPositioningInformation() throws Exception { try { getParse("*foobar bla"); assertTrue("parsing should throw an exception", false); } catch(ParserException e) { assertTrue("Exception should be instance of PositionedParserException", e instanceof PositionedParserException); assertEquals("Position should be correct for exception", new Position.Range(new Position(0,8,8),new Position(0,11,11)), ((PositionedParserException)e).getRange()); } } public void testCommentBeforeSecondKeyWithBlankFirst() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("a"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s(""), (char)0, new Position.Range(new Position(0,2,2), new Position(0,2,2)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("c"), (char)0, new Position.Range(new Position(2,0,9), new Position(2,1,10)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("d"), (char)0, new Position.Range(new Position(2,3,12), new Position(2,4,13)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(2,4,13)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(2,4,13)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(2,4,13)))); List events = getParse("a: \n#foo\nc: d"); assertEquals(expected, events); } public void testCommentBeforeSecondKeyWithBlankFirstAndSomeMore() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartEvent(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartEvent(false, new int[]{1,1}, null, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedMappingStartEvent(null, null, true, false, new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("foo"), (char)0, new Position.Range(new Position(0,0,0), new Position(0,3,3)))); expected.add(new PositionedMappingStartEvent(null, null, true, false, new Position.Range(new Position(1,2,7)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("inner"), (char)0, new Position.Range(new Position(1,2,7), new Position(1,7,12)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("val"), (char)0, new Position.Range(new Position(1,9,14), new Position(1,12,17)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(1,12,17)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("bar"), (char)0, new Position.Range(new Position(4,0,27), new Position(4,3,30)))); expected.add(new PositionedScalarEvent(null, null, new boolean[]{true,false}, s("baz"), (char)0, new Position.Range(new Position(4,5,32), new Position(4,8,35)))); expected.add(new PositionedMappingEndEvent(new Position.Range(new Position(4,8,35)))); expected.add(new PositionedDocumentEndEvent(false, new Position.Range(new Position(4,8,35)))); expected.add(new PositionedStreamEndEvent(new Position.Range(new Position(4,8,35)))); List events = getParse("foo:\n" + " inner: val\n" + "\n" + "# Hello\n" + "bar: baz"); assertEquals(expected, events); } } jvyamlb-0.2.5/src/test/org/jvyamlb/PositioningScannerImplTest.java000066400000000000000000000626761151213612200253370ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.jvyamlb.tokens.PositionedDocumentEndToken; import org.jvyamlb.tokens.PositionedDocumentStartToken; import org.jvyamlb.tokens.PositionedScalarToken; import org.jvyamlb.tokens.PositionedStreamStartToken; import org.jvyamlb.tokens.PositionedStreamEndToken; import org.jvyamlb.tokens.PositionedBlockMappingStartToken; import org.jvyamlb.tokens.PositionedBlockEndToken; import org.jvyamlb.tokens.PositionedKeyToken; import org.jvyamlb.tokens.PositionedValueToken; import org.jvyamlb.tokens.PositionedBlockSequenceStartToken; import org.jvyamlb.tokens.PositionedBlockEntryToken; import org.jvyamlb.tokens.PositionedFlowMappingStartToken; import org.jvyamlb.tokens.PositionedFlowMappingEndToken; import org.jvyamlb.tokens.PositionedFlowSequenceStartToken; import org.jvyamlb.tokens.PositionedFlowSequenceEndToken; import org.jvyamlb.tokens.PositionedFlowEntryToken; import org.jvyamlb.tokens.PositionedTagToken; import org.jvyamlb.tokens.PositionedAliasToken; import org.jvyamlb.tokens.PositionedAnchorToken; import org.jvyamlb.tokens.PositionedDirectiveToken; import org.jruby.util.ByteList; import org.jvyamlb.exceptions.ScannerException; import org.jvyamlb.exceptions.PositionedScannerException; /** * @author Ola Bini */ public class PositioningScannerImplTest extends YAMLTestCase { public PositioningScannerImplTest(String name) { super(name); } protected List getScan(String input) { Scanner s = new PositioningScannerImpl(input); List output = new ArrayList(); for(Iterator iter = s.iterator(); iter.hasNext();) { output.add(iter.next()); } return output; } public void testThatEmptyScannerGeneratesPositionedEnvelope() { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,0,0)))); List tokens = getScan(""); assertEquals(expected, tokens); } public void testThatSimpleScalarGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,0,0), new Position(0, 1, 1)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,1,1)))); List tokens = getScan("a"); assertEquals(expected, tokens); } public void testThatSimpleScalarWithSpaceGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,2,2)))); List tokens = getScan("a "); assertEquals(expected, tokens); } public void testThatSimpleScalarWithNewlineGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(1,0,2)))); List tokens = getScan("a\n"); assertEquals(expected, tokens); } public void testThatComplicatedeScalarGeneratesCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDocumentStartToken(new Position.Range(new Position(0,0,0),new Position(0,3,3)))); expected.add(new PositionedScalarToken(s("abcdefafgsdfgsdfg sfgdfsg fdsgfgsdf"), false, '"', new Position.Range(new Position(0,4,4), new Position(2,10,41)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(2,10,41)))); List tokens = getScan("--- \"abcdefafgsdfgsdfg\n"+ "sfgdfsg\n"+ "fdsgfgsdf\""); assertEquals(expected, tokens); } public void testThatAKeyValuePairGetsCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockMappingStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedKeyToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedValueToken( new Position.Range(new Position(0,2,2)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(0,3,3), new Position(0,4,4)))); expected.add(new PositionedBlockEndToken( new Position.Range(new Position(0,4,4)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(0,4,4)))); List tokens = getScan("a: b"); assertEquals(expected, tokens); } public void testThatTwoKeyValuePairsGetsCorrectPositioning() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockMappingStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedKeyToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,0,0), new Position(0,1,1)))); expected.add(new PositionedValueToken( new Position.Range(new Position(0,2,2)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(0,3,3), new Position(0,4,4)))); expected.add(new PositionedKeyToken( new Position.Range(new Position(1,0,5)))); expected.add(new PositionedScalarToken(s("c"), true, (char)0, new Position.Range(new Position(1,0,5), new Position(1,1,6)))); expected.add(new PositionedValueToken( new Position.Range(new Position(1,2,7)))); expected.add(new PositionedScalarToken(s("d"), true, (char)0, new Position.Range(new Position(1,4,9), new Position(1,5,10)))); expected.add(new PositionedBlockEndToken( new Position.Range(new Position(1,5,10)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(1,5,10)))); List tokens = getScan("a: b\n"+ "c: d"); assertEquals(expected, tokens); } public void testThatAOneItemSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockSequenceStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedBlockEndToken( new Position.Range(new Position(0,3,3)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(0,3,3)))); List tokens = getScan("- a"); assertEquals(expected, tokens); } public void testThatMoreThanOneItemSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockSequenceStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(1,0,4)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(1,4,8), new Position(1,5,9)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(2,0,10)))); expected.add(new PositionedScalarToken(s("c"), true, (char)0, new Position.Range(new Position(2,2,12), new Position(2,3,13)))); expected.add(new PositionedBlockEndToken( new Position.Range(new Position(2,3,13)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(2,3,13)))); List tokens = getScan("- a\n"+ "- b\n"+ "- c"); assertEquals(expected, tokens); } public void testThatListedSequenesWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockSequenceStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,2,2), new Position(0,3,3)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(1,0,4)))); expected.add(new PositionedBlockSequenceStartToken( new Position.Range(new Position(1,2,6)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(1,2,6)))); expected.add(new PositionedScalarToken(s("foo"), true, (char)0,new Position.Range(new Position(1,4,8), new Position(1,7,11)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(2,2,14)))); expected.add(new PositionedScalarToken(s("bar"), true, (char)0,new Position.Range(new Position(2,4,16), new Position(2,7,19)))); // This is a bit unintuitive - a block ending is on the new line afterwards. expected.add(new PositionedBlockEndToken( new Position.Range(new Position(3,0,20)))); expected.add(new PositionedBlockEntryToken( new Position.Range(new Position(3,0,20)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(3,2,22), new Position(3,3,23)))); expected.add(new PositionedBlockEndToken( new Position.Range(new Position(3,3,23)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(3,3,23)))); List tokens = getScan("- a\n"+ "- - foo\n"+ " - bar\n"+ "- b"); assertEquals(expected, tokens); } public void testThatSimpleFlowMappingWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedFlowMappingStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedKeyToken( new Position.Range(new Position(0,1,1)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedValueToken( new Position.Range(new Position(0,3,3)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(0,4,4), new Position(0,5,5)))); expected.add(new PositionedFlowMappingEndToken( new Position.Range(new Position(0,5,5)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(0,6,6)))); List tokens = getScan("{a: b}"); assertEquals(expected, tokens); } public void testThatSimpleFlowSequenceWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedFlowSequenceStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedFlowSequenceEndToken( new Position.Range(new Position(0,2,2)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(0,3,3)))); List tokens = getScan("[a]"); assertEquals(expected, tokens); } public void testThatFlowSequenceWithMoreThanOneElementWorks() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedFlowSequenceStartToken( new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,1,1), new Position(0,2,2)))); expected.add(new PositionedFlowEntryToken( new Position.Range(new Position(0,3,3)))); expected.add(new PositionedScalarToken(s("b"), true, (char)0, new Position.Range(new Position(0,4,4), new Position(0,5,5)))); expected.add(new PositionedFlowSequenceEndToken( new Position.Range(new Position(0,6,6)))); expected.add(new PositionedStreamEndToken( new Position.Range(new Position(0,7,7)))); List tokens = getScan("[a, b ]"); assertEquals(expected, tokens); } public void testScalarWithTag() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedTagToken(new ByteList[]{s("!"), s("str")}, new Position.Range(new Position(0,0,0),new Position(0,4,4)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,5,5), new Position(0, 6, 6)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,6,6)))); List tokens = getScan("!str a"); assertEquals(expected, tokens); } public void testScalarWithAlias() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedAliasToken("foobar", new Position.Range(new Position(0,0,0), new Position(0,7,7)))); expected.add(new PositionedScalarToken(s("a"), true, (char)0, new Position.Range(new Position(0,8,8), new Position(0,9,9)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,9,9)))); List tokens = getScan("*foobar a"); assertEquals(expected, tokens); } public void testAnchor() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedAnchorToken("foobar", new Position.Range(new Position(0,0,0), new Position(0,7,7)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,7,7)))); List tokens = getScan("&foobar"); assertEquals(expected, tokens); } public void testDirective() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDirectiveToken("YAML", new String[]{"1","0"}, new Position.Range(new Position(0,0,0), new Position(0,9,9)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,9,9)))); List tokens = getScan("%YAML 1.0"); assertEquals(expected, tokens); } public void testDoubleDirective() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDirectiveToken("YAML", new String[]{"1","0"}, new Position.Range(new Position(0,0,0), new Position(0,9,9)))); expected.add(new PositionedDirectiveToken("TAG", new String[]{"!","abc"}, new Position.Range(new Position(1,0,10), new Position(1,10,20)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(2,0,21)))); List tokens = getScan("%YAML 1.0\n"+ "%TAG ! abc\n"); assertEquals(expected, tokens); } public void testDoubleDirectiveWithoutNewlineEnd() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedDirectiveToken("YAML", new String[]{"1","0"}, new Position.Range(new Position(0,0,0), new Position(0,9,9)))); expected.add(new PositionedDirectiveToken("TAG", new String[]{"!","abc"}, new Position.Range(new Position(1,0,10), new Position(1,10,20)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(1,10,20)))); List tokens = getScan("%YAML 1.0\n"+ "%TAG ! abc"); assertEquals(expected, tokens); } public void testLiteralScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("abc\nfoobar"), false, '|', new Position.Range(new Position(0,0,0), new Position(2,7,14)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(2,7,14)))); List tokens = getScan("|\n"+ " abc\n"+ " foobar"); assertEquals(expected, tokens); } public void testFoldedScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("abc foobar"), false, '>', new Position.Range(new Position(0,0,0), new Position(2,7,14)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(2,7,14)))); List tokens = getScan(">\n"+ " abc\n"+ " foobar"); assertEquals(expected, tokens); } public void testSingleQuotedScalar() throws Exception { List expected = new ArrayList(); expected.add(new PositionedStreamStartToken(new Position.Range(new Position(0,0,0)))); expected.add(new PositionedScalarToken(s("abc"), false, '\'', new Position.Range(new Position(0,0,0), new Position(0,5,5)))); expected.add(new PositionedStreamEndToken(new Position.Range(new Position(0,5,5)))); List tokens = getScan("'abc'"); assertEquals(expected, tokens); } public void testScannerExceptionIncludesPositioningInformation() throws Exception { try { getScan("!Ola Bini */ public class RoundtripTest extends YAMLTestCase { public RoundtripTest(final String name) { super(name); } public void testSeveralStrings() throws Exception { assertRoundtrip("C VW\u0085\u000B\u00D1XU\u00E6"); assertRoundtrip("\n8 xwKmjHG"); assertRoundtrip("1jq[\205qIB\ns"); assertRoundtrip("\rj\230fso\304\nEE"); assertRoundtrip("ks]qkYM\2073Un\317\nL\346Yp\204 CKMfFcRDFZ\u000BMNk\302fQDROla Bini */ public class SimpleLoadTest extends YAMLTestCase { public SimpleLoadTest(final String name) { super(name); } public void testStrings() throws Exception { assertLoad(s("str"),"!!str str"); assertLoad(s("str"),"--- str"); assertLoad(s("str"),"---\nstr"); assertLoad(s("str"),"--- \nstr"); assertLoad(s("str"),"--- \n str"); assertLoad(s("str"),"str"); assertLoad(s("str")," str"); assertLoad(s("str"),"\nstr"); assertLoad(s("str"),"\n str"); assertLoad(s("str"),"\"str\""); assertLoad(s("str"),"'str'"); assertLoad(s("str")," --- 'str'"); assertLoad(s("1.0"),"!!str 1.0"); assertLoad10(s("str"),"!str str"); assertLoad10(s("1.0"),"!str 1.0"); } public void testArrays() throws Exception { List expected = new ArrayList(); expected.add(s("a")); expected.add(s("b")); expected.add(s("c")); assertLoad(expected, "--- \n- a\n- b\n- c\n"); assertLoad(expected, "--- [a, b, c]"); assertLoad(expected, "[a, b, c]"); } public void testEmpty() throws Exception { assertLoad(null, "---\n!!str"); // this doesn't match Ruby's way of doing things assertLoad10(null, "---\n!str"); } public void testStrange() throws Exception { assertLoad(s("---"), "--- ---\n"); assertLoad(s("---"), "---"); } public void testShared() throws Exception { ByteList inp = s("abcde"); inp.begin = 2; inp.realSize = 3; assertLoad(s("cde"),inp); } public void testDate() throws Exception { assertEquals(ByteList.class, ((Map)load("{a: 2007-01-01 01:12:34}")).get(s("a")).getClass()); assertEquals(ByteList.class, load("2007-01-01 01:12:34").getClass()); assertEquals(ByteList.class, load("2007-01-01 01:12:34.0").getClass()); assertEquals(DateTime.class, load("2007-01-01 01:12:34 +00:00").getClass()); assertEquals(DateTime.class, load("2007-01-01 01:12:34.0 +00:00").getClass()); assertEquals(ByteList.class, ((Map)load("{a: 2007-01-01 01:12:34}")).get(s("a")).getClass()); } public void testObjectIdentity() throws Exception { List val = (List)load("---\n- foo\n- foo\n- [foo]\n- [foo]\n- {foo: foo}\n- {foo: foo}\n"); assertNotSame(val.get(0), val.get(1)); assertNotSame(val.get(2), val.get(3)); assertNotSame(val.get(4), val.get(5)); } public void testStrangeNesting() throws Exception { Map expected = new HashMap(); Map internal = new HashMap(); internal.put(s("bar"), null); expected.put(s("foo"), internal); assertLoad(expected, "---\nfoo: { bar }\n"); expected = new HashMap(); List internal2 = new ArrayList(); internal2.add(s("a")); expected.put(s("default"), internal2); expected = new HashMap(); internal = new HashMap(); internal.put(s("bar"),null); internal.put(s("qux"),null); expected.put(s("foo"), internal); assertLoad(expected, "---\nfoo: {bar, qux}"); // This looks like bad YAML to me... But MRI supports it // assertLoad(expected, "---\ndefault: -\n- a\n"); } public void testStrangeCharacters() throws Exception { assertLoad(s(",a"), "--- \n,a"); Map expected = new HashMap(); expected.put(s("foobar"), s(">= 123")); assertLoad(expected, "foobar: >= 123"); expected = new HashMap(); expected.put(s("foo"), s("bar")); assertLoad(expected, "---\nfoo: \tbar"); expected = new HashMap(); expected.put(s("foobar"), s("|= 567")); assertLoad(expected, "foobar: |= 567"); } public void testAtSign() throws Exception { Map expected = new HashMap(); expected.put(s("foo"), s("@bar")); assertLoad(expected, "foo: @bar"); } public void testSymbols() throws Exception { assertLoad(s(":a"), "--- \n:a"); List expected = new ArrayList(); expected.add(s(":a")); assertLoad(expected, "--- \n[:a]"); Map expected2 = new HashMap(); expected2.put(s(":year"), s(":last")); expected2.put(s(":month"), s(":jan")); assertLoad(expected2, "--- \n{:year: :last, :month: :jan}"); expected2 = new HashMap(); List l = new ArrayList(); l.add(s(":year")); expected2.put(s("order"), l); assertLoad(expected2, "--- \norder: [:year]"); } public void testLoadOfAsterisk() throws Exception { assertLoad(s("*.rb"), "--- \n*.rb"); assertLoad(s("*.rb"), "--- \n'*.rb'"); assertLoad(s("&.rb"), "--- \n&.rb"); assertLoad(s("&.rb"), "--- \n'&.rb'"); try { assertLoad(null, "--- \n*r.b"); assertTrue(false); } catch(ScannerException e) { assertTrue(true); } try { assertLoad(null, "--- \n&r.b"); assertTrue(false); } catch(ScannerException e) { assertTrue(true); } } public void testSimpleFailure() throws Exception { try { assertLoad(null, "--- -\nh"); } catch(AssertionFailedError e) { assertTrue("should fail on faulty YAML: " + e, false); } catch(ScannerException e) { assertTrue(true); } } public void testEmptyDocument() throws Exception { assertLoad(null, "---\n"); assertLoad(null, "--- \n"); assertLoad(s("---"), "---"); } }// SimpleLoadTest jvyamlb-0.2.5/src/test/org/jvyamlb/TestBean.java000066400000000000000000000033131151213612200215250ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import org.joda.time.DateTime; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class TestBean { private ByteList name; private int age; private DateTime born; public TestBean() { } public TestBean(final ByteList name, final int age, final DateTime born) { this.name = name; this.age = age; this.born = born; } public ByteList getName() { return this.name; } public int getAge() { return age; } public DateTime getBorn() { return born; } public void setName(final ByteList name) { this.name = name; } public void setAge(final int age) { this.age = age; } public void setBorn(final DateTime born) { this.born = born; } public boolean equals(final Object other) { boolean ret = this == other; if(!ret && other instanceof TestBean) { TestBean o = (TestBean)other; ret = this.name == null ? o.name == null : this.name.equals(o.name) && this.age == o.age && this.born == null ? o.born == null : this.born.equals(o.born); } return ret; } public int hashCode() { int val = 3; val += 3 * (name == null ? 0 : name.hashCode()); val += 3 * age; val += 3 * (born == null ? 0 : born.hashCode()); return val; } public String toString() { return "#"; } }// TestBean jvyamlb-0.2.5/src/test/org/jvyamlb/TestBean2.java000066400000000000000000000024751151213612200216170ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class TestBean2 { private ByteList name; private int age; public TestBean2() { } public TestBean2(final ByteList name, final int age) { this.name = name; this.age = age; } public ByteList getName() { return this.name; } public int getAge() { return age; } public void setName(final ByteList name) { this.name = name; } public void setAge(final int age) { this.age = age; } public boolean equals(final Object other) { boolean ret = this == other; if(!ret && other instanceof TestBean2) { TestBean2 o = (TestBean2)other; ret = this.name == null ? o.name == null : this.name.equals(o.name) && this.age == o.age; } return ret; } public int hashCode() { int val = 3; val += 3 * (name == null ? 0 : name.hashCode()); val += 3 * age; return val; } public String toString() { return "#"; } }// TestBean2 jvyamlb-0.2.5/src/test/org/jvyamlb/YAMLDumpTest.java000066400000000000000000000052471151213612200222600ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.ArrayList; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class YAMLDumpTest extends YAMLTestCase { public YAMLDumpTest(final String name) { super(name); } public void testBasicStringDump() { assertEquals(ByteList.create("--- str\n"), YAML.dump(ByteList.create("str"))); } public void testBasicHashDump() { Map ex = new HashMap(); ex.put(ByteList.create("a"),ByteList.create("b")); assertEquals(ByteList.create("--- \na: b\n"), YAML.dump(ex)); } public void testBasicListDump() { List ex = new ArrayList(); ex.add(ByteList.create("a")); ex.add(ByteList.create("b")); ex.add(ByteList.create("c")); assertEquals(ByteList.create("--- \n- a\n- b\n- c\n"), YAML.dump(ex)); } public void testVersionDumps() { assertEquals(ByteList.create("--- !!int 1\n"), YAML.dump(new Integer(1),YAML.config().explicitTypes(true))); assertEquals(ByteList.create("--- !int 1\n"), YAML.dump(new Integer(1),YAML.config().version("1.0").explicitTypes(true))); } public void testMoreScalars() { assertEquals(ByteList.create("--- \"1.0\"\n"), YAML.dump(ByteList.create("1.0"))); } public void testPrefersQuotesToExplicitTag() { assertEquals(ByteList.create("--- \"30\"\n"), YAML.dump(ByteList.create("30"))); } public void testEmptyList() { assertEquals(ByteList.create("--- []\n\n"), YAML.dump(new ArrayList())); } public void testEmptyMap() { assertEquals(ByteList.create("--- {}\n\n"), YAML.dump(new HashMap())); } public void testEmptyListAsKey() { Map m = new HashMap(); m.put(new ArrayList(), ""); assertEquals(ByteList.create("--- \n? []\n\n: \"\"\n"), YAML.dump(m)); } public void testEmptyMapAsKey() { Map m = new HashMap(); m.put(new HashMap(), ""); assertEquals(ByteList.create("--- \n? {}\n\n: \"\"\n"), YAML.dump(m)); } public void testDumpJavaBean() { final TestBean2 toDump = new TestBean2(ByteList.create("Ola Bini"), 24); Object v = YAML.dump(toDump); String s = v.toString(); assertTrue("something is wrong with: \"" + v + "\"", ByteList.create("--- !java/object:org.jvyamlb.TestBean2 \nname: Ola Bini\nage: 24\n").equals(v) || ByteList.create("--- !java/object:org.jvyamlb.TestBean2 \nage: 24\nname: Ola Bini\n").equals(v) ); } }// YAMLDumpTest jvyamlb-0.2.5/src/test/org/jvyamlb/YAMLLoadTest.java000066400000000000000000000110031151213612200222150ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.List; import org.jvyamlb.exceptions.ParserException; import org.jruby.util.ByteList; /** * @author Ola Bini */ public class YAMLLoadTest extends YAMLTestCase { public YAMLLoadTest(final String name) { super(name); } public void testBasicStringScalarLoad() throws Exception { ByteList str = s("str"); assertLoad(str,"--- str"); assertLoad(str,"---\nstr"); assertLoad(str,"--- \nstr"); assertLoad(str,"--- \n str"); assertLoad(str,"str"); assertLoad(str," str"); assertLoad(str,"\nstr"); assertLoad(str,"\n str"); assertLoad(str,"\"str\""); assertLoad(str,"'str'"); assertLoad(s("\u00fc"),"---\n\"\\xC3\\xBC\""); } public void testBasicIntegerScalarLoad() throws Exception { assertLoad(new Long(47),"47"); assertLoad(new Long(0),"0"); assertLoad(new Long(-1),"-1"); } public void testBlockMappingLoad() throws Exception { Map expected = new HashMap(); expected.put(s("a"),s("b")); expected.put(s("c"),s("d")); assertLoad(expected,"a: b\nc: d"); assertLoad(expected,"c: d\na: b\n"); } public void testFlowMappingLoad() throws Exception { Map expected = new HashMap(); expected.put(s("a"),s("b")); expected.put(s("c"),s("d")); assertLoad(expected,"{a: b, c: d}"); assertLoad(expected,"{c: d,\na: b}"); } public void testInternalChar() throws Exception { Map expected = new HashMap(); expected.put(s("bad_sample"),s("something:(")); assertLoad(expected,"--- \nbad_sample: something:(\n"); } public void testBuiltinTag() throws Exception { assertLoad(s("str"),"!!str str"); assertLoad(s("str"),"%YAML 1.1\n---\n!!str str"); assertLoad(s("str"),"%YAML 1.0\n---\n!str str"); assertLoad10(s("str"),"---\n!str str"); assertLoad10(new Long(123),"---\n!int 123"); assertLoad10(new Long(123),"%YAML 1.1\n---\n!!int 123"); } public void testDirectives() throws Exception { assertLoad(s("str"),"%YAML 1.1\n--- !!str str"); assertLoad(s("str"),"%YAML 1.1\n%TAG !yaml! tag:yaml.org,2002:\n--- !yaml!str str"); try { YAML.load(s("%YAML 1.1\n%YAML 1.1\n--- !!str str")); fail("should throw exception when repeating directive"); } catch(final ParserException e) { assertTrue(true); } } public void testJavaBeanLoad() throws Exception { org.joda.time.DateTime dt = new org.joda.time.DateTime(1982,5,3,0,0,0,0); final TestBean expected = new TestBean(s("Ola Bini"), 24, dt); assertLoad(expected, "--- !java/object:org.jvyamlb.TestBean\nname: Ola Bini\nage: 24\nborn: 1982-05-03\n"); } // JRUBY-2754 public void testJavaBeanLoadRefs() throws Exception { org.joda.time.DateTime dt = new org.joda.time.DateTime(1982,5,3,0,0,0,0); final TestBean bean = new TestBean(s("Ola Bini"), 24, dt); List result = (List)YAML.load(s("--- \n- &id001 !java/object:org.jvyamlb.TestBean\n name: Ola Bini\n age: 24\n born: 1982-05-03\n- *id001")); assertEquals(bean, result.get(0)); assertSame(result.get(0), result.get(1)); } public void testFlowSequenceWithFlowMappingLoad() throws Exception { List expectedOuter = new ArrayList(); Map expectedInner = new HashMap(); expectedInner.put(s("a"), s("b")); expectedOuter.add(expectedInner); assertLoad(expectedOuter, "[{a: b}]"); } public void testNestedFlowMappingLoad() throws Exception { Map expectedInner = new HashMap(); expectedInner.put(s("b"), s("c")); Map expectedOuter = new HashMap(); expectedOuter.put(s("a"), expectedInner); assertLoad(expectedOuter, "{a: {b: c}}"); } public void testNestedFlowSequenceFlowMappingLoad() throws Exception { List expectedOuterList = new ArrayList(); Map expectedInnerMap = new HashMap(); expectedInnerMap.put(s("b"), s("c")); Map expectedOuterMap = new HashMap(); expectedOuterMap.put(s("a"), expectedInnerMap); expectedOuterList.add(expectedOuterMap); assertLoad(expectedOuterList, "[{a: {b: c}}]"); } }// YAMLLoadTest jvyamlb-0.2.5/src/test/org/jvyamlb/YAMLTestCase.java000066400000000000000000000033541151213612200222230ustar00rootroot00000000000000/* * See LICENSE file in distribution for copyright and licensing information. */ package org.jvyamlb; import junit.framework.TestCase; import org.jruby.util.ByteList; /** * * @author Ola Bini */ public abstract class YAMLTestCase extends TestCase { public YAMLTestCase(final String name) { super(name); } protected ByteList s(String st) throws Exception { return new ByteList(st.getBytes("UTF-8")); } public void assertLoad(Object expected, String str) throws Exception { assertEquals(expected,YAML.load(s(str))); } public Object load(String str) throws Exception { return YAML.load(s(str)); } public void assertLoad(Object expected, ByteList str) throws Exception { assertEquals(expected,YAML.load(str)); } public void assertLoad10(Object expected, String str) throws Exception { assertEquals(expected,YAML.load(s(str),YAML.config().version("1.0"))); } public void assertRoundtrip(String value) throws Exception { assertEquals(s(value),YAML.load(YAML.dump(s(value)))); } public void assertRoundtrip(ByteList value) throws Exception { try { assertEquals(value,YAML.load(YAML.dump(value))); } catch(Exception e) { System.err.println("bytelist[" + value.begin + ", realSize: " + value.realSize + "]"); for(int i = 0; i < value.realSize; i++) { System.err.println("[" + i + "]= " + value.bytes[value.begin+i]); } System.err.println("for: \"" + value + "\" -- of len: " + value.realSize); System.err.println("out: \"" + YAML.dump(value) + "\""); throw e; } } }// YAMLTestCase