1 28 package org.jvyamlb; 29 30 import java.io.FileInputStream ; 31 import java.io.InputStream ; 32 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import java.util.regex.Pattern ; 40 41 import org.jruby.util.ByteList; 42 43 46 public class ConstructorImpl extends SafeConstructorImpl { 47 private final static Map yamlConstructors = new HashMap (); 48 private final static Map yamlMultiConstructors = new HashMap (); 49 private final static Map yamlMultiRegexps = new HashMap (); 50 public YamlConstructor getYamlConstructor(final Object key) { 51 YamlConstructor mine = (YamlConstructor)yamlConstructors.get(key); 52 if(mine == null) { 53 mine = super.getYamlConstructor(key); 54 } 55 return mine; 56 } 57 58 public YamlMultiConstructor getYamlMultiConstructor(final Object key) { 59 YamlMultiConstructor mine = (YamlMultiConstructor)yamlMultiConstructors.get(key); 60 if(mine == null) { 61 mine = super.getYamlMultiConstructor(key); 62 } 63 return mine; 64 } 65 66 public Pattern getYamlMultiRegexp(final Object key) { 67 Pattern mine = (Pattern )yamlMultiRegexps.get(key); 68 if(mine == null) { 69 mine = super.getYamlMultiRegexp(key); 70 } 71 return mine; 72 } 73 74 public Set getYamlMultiRegexps() { 75 final Set all = new HashSet (super.getYamlMultiRegexps()); 76 all.addAll(yamlMultiRegexps.keySet()); 77 return all; 78 } 79 80 public static void addConstructor(final String tag, final YamlConstructor ctor) { 81 yamlConstructors.put(tag,ctor); 82 } 83 84 public static void addMultiConstructor(final String tagPrefix, final YamlMultiConstructor ctor) { 85 yamlMultiConstructors.put(tagPrefix,ctor); 86 yamlMultiRegexps.put(tagPrefix,Pattern.compile("^"+tagPrefix)); 87 } 88 89 public ConstructorImpl(final Composer composer) { 90 super(composer); 91 } 92 93 public static void main(final String [] args) throws Exception { 94 final String filename = args[0]; 95 System.out.println("Reading of file: \"" + filename + "\""); 96 97 final ByteList input = new ByteList(1024); 98 final InputStream reader = new FileInputStream (filename); 99 byte[] buff = new byte[1024]; 100 int read = 0; 101 while(true) { 102 read = reader.read(buff); 103 input.append(buff,0,read); 104 if(read < 1024) { 105 break; 106 } 107 } 108 reader.close(); 109 final long before = System.currentTimeMillis(); 110 for(int i=0;i<1;i++) { 111 final Constructor ctor = new ConstructorImpl(new ComposerImpl(new ParserImpl(new ScannerImpl(input)),new ResolverImpl())); 112 for(final Iterator iter = ctor.eachDocument();iter.hasNext();iter.next()) { 113 iter.next(); 114 } 116 } 117 final long after = System.currentTimeMillis(); 118 final long time = after-before; 119 final double timeS = (after-before)/1000.0; 120 System.out.println("Walking through the nodes for the file: " + filename + " took " + time + "ms, or " + timeS + " seconds"); 121 } 122 } | Popular Tags |