1 28 package org.jvyamlb; 29 30 import java.io.InputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.OutputStream ; 33 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.ArrayList ; 39 40 import org.jruby.util.ByteList; 41 42 47 public class YAML { 48 public static final String DEFAULT_SCALAR_TAG = "tag:yaml.org,2002:str"; 49 public static final String DEFAULT_SEQUENCE_TAG = "tag:yaml.org,2002:seq"; 50 public static final String DEFAULT_MAPPING_TAG = "tag:yaml.org,2002:map"; 51 52 public static final Map ESCAPE_REPLACEMENTS = new HashMap (); 53 54 static { 55 ESCAPE_REPLACEMENTS.put(new Character ('\0'),"0"); 56 ESCAPE_REPLACEMENTS.put(new Character ('\u0007'),"a"); 57 ESCAPE_REPLACEMENTS.put(new Character ('\u0008'),"b"); 58 ESCAPE_REPLACEMENTS.put(new Character ('\u0009'),"t"); 59 ESCAPE_REPLACEMENTS.put(new Character ('\n'),"n"); 60 ESCAPE_REPLACEMENTS.put(new Character ('\u000B'),"v"); 61 ESCAPE_REPLACEMENTS.put(new Character ('\u000C'),"f"); 62 ESCAPE_REPLACEMENTS.put(new Character ('\r'),"r"); 63 ESCAPE_REPLACEMENTS.put(new Character ('\u001B'),"e"); 64 ESCAPE_REPLACEMENTS.put(new Character ('"'),"\""); 65 ESCAPE_REPLACEMENTS.put(new Character ('\\'),"\\"); 66 ESCAPE_REPLACEMENTS.put(new Character ('\u0085'),"N"); 67 ESCAPE_REPLACEMENTS.put(new Character ('\u00A0'),"_"); 68 } 69 public static YAMLConfig config() { 70 return defaultConfig(); 71 } 72 73 public static YAMLConfig defaultConfig() { 74 return new DefaultYAMLConfig(); 75 } 76 77 public static ByteList dump(final Object data) { 78 return dump(data,config()); 79 } 80 81 public static ByteList dump(final Object data, final YAMLFactory fact) { 82 return dump(data,fact, config()); 83 } 84 85 public static ByteList dump(final Object data, final YAMLConfig cfg) { 86 final List lst = new ArrayList (1); 87 lst.add(data); 88 return dumpAll(lst,cfg); 89 } 90 91 public static ByteList dump(final Object data, final YAMLFactory fact, final YAMLConfig cfg) { 92 final List lst = new ArrayList (1); 93 lst.add(data); 94 return dumpAll(lst,fact,cfg); 95 } 96 97 public static ByteList dumpAll(final List data) { 98 return dumpAll(data,config()); 99 } 100 101 public static ByteList dumpAll(final List data, final YAMLFactory fact) { 102 return dumpAll(data,fact,config()); 103 } 104 105 public static ByteList dumpAll(final List data, final YAMLConfig cfg) { 106 final ByteArrayOutputStream swe = new ByteArrayOutputStream (); 107 dumpAll(data,swe,cfg); 108 return new ByteList(swe.toByteArray(),false); 109 } 110 111 public static ByteList dumpAll(final List data, final YAMLFactory fact, final YAMLConfig cfg) { 112 final ByteArrayOutputStream swe = new ByteArrayOutputStream (); 113 dumpAll(data,swe,fact,cfg); 114 return new ByteList(swe.toByteArray(),false); 115 } 116 117 public static void dump(final Object data, final OutputStream output) { 118 dump(data,output,config()); 119 } 120 121 public static void dump(final Object data, final OutputStream output, YAMLFactory fact) { 122 dump(data,output,fact,config()); 123 } 124 125 public static void dump(final Object data, final OutputStream output, final YAMLConfig cfg) { 126 final List lst = new ArrayList (1); 127 lst.add(data); 128 dumpAll(lst,output,cfg); 129 } 130 131 public static void dump(final Object data, final OutputStream output, final YAMLFactory fact, final YAMLConfig cfg) { 132 final List lst = new ArrayList (1); 133 lst.add(data); 134 dumpAll(lst,output,fact,cfg); 135 } 136 137 public static void dumpAll(final List data, final OutputStream output) { 138 dumpAll(data,output,config()); 139 } 140 141 public static void dumpAll(final List data, final OutputStream output, final YAMLFactory fact) { 142 dumpAll(data,output,fact,config()); 143 } 144 145 public static void dumpAll(final List data, final OutputStream output, final YAMLConfig cfg) { 146 dumpAll(data,output,new DefaultYAMLFactory(),cfg); 147 } 148 149 public static void dumpAll(final List data, final OutputStream output, final YAMLFactory fact, final YAMLConfig cfg) { 150 final Serializer s = fact.createSerializer(fact.createEmitter(output,cfg),fact.createResolver(),cfg); 151 try { 152 s.open(); 153 final Representer r = fact.createRepresenter(s,cfg); 154 for(final Iterator iter = data.iterator(); iter.hasNext();) { 155 r.represent(iter.next()); 156 } 157 } catch(final java.io.IOException e) { 158 throw new YAMLException(e); 159 } finally { 160 try { s.close(); } catch(final java.io.IOException e) {} 161 } 162 } 163 164 public static Object load(final ByteList io) { 165 return load(io, new DefaultYAMLFactory(),config()); 166 } 167 168 public static Object load(final InputStream io) { 169 return load(io, new DefaultYAMLFactory(),config()); 170 } 171 172 public static Object load(final ByteList io, final YAMLConfig cfg) { 173 return load(io, new DefaultYAMLFactory(),cfg); 174 } 175 176 public static Object load(final InputStream io, final YAMLConfig cfg) { 177 return load(io, new DefaultYAMLFactory(),cfg); 178 } 179 180 public static Object load(final ByteList io, final YAMLFactory fact, final YAMLConfig cfg) { 181 final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); 182 if(ctor.checkData()) { 183 return ctor.getData(); 184 } else { 185 return null; 186 } 187 } 188 189 public static Object load(final InputStream io, final YAMLFactory fact, final YAMLConfig cfg) { 190 final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); 191 if(ctor.checkData()) { 192 return ctor.getData(); 193 } else { 194 return null; 195 } 196 } 197 198 public static List loadAll(final ByteList io) { 199 return loadAll(io, new DefaultYAMLFactory(),config()); 200 } 201 202 public static List loadAll(final InputStream io) { 203 return loadAll(io, new DefaultYAMLFactory(),config()); 204 } 205 206 public static List loadAll(final ByteList io, final YAMLConfig cfg) { 207 return loadAll(io, new DefaultYAMLFactory(),cfg); 208 } 209 210 public static List loadAll(final InputStream io, final YAMLConfig cfg) { 211 return loadAll(io, new DefaultYAMLFactory(),cfg); 212 } 213 214 public static List loadAll(final ByteList io, final YAMLFactory fact, final YAMLConfig cfg) { 215 final List result = new ArrayList (); 216 final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); 217 while(ctor.checkData()) { 218 result.add(ctor.getData()); 219 } 220 return result; 221 } 222 223 public static List loadAll(final InputStream io, final YAMLFactory fact, final YAMLConfig cfg) { 224 final List result = new ArrayList (); 225 final Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(io),cfg),fact.createResolver())); 226 while(ctor.checkData()) { 227 result.add(ctor.getData()); 228 } 229 return result; 230 } 231 232 public static void main(final String [] args) { 233 System.err.println(YAML.load(new ByteList("%YAML 1.0\n---\n!str str".getBytes()))); 234 } 235 } | Popular Tags |