KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jvyamlb > YAML


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2007 Ola Bini <ola@ologix.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jvyamlb;
29
30 import java.io.InputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 import org.jruby.util.ByteList;
41
42 /**
43  * The combinatorial explosion class.
44  *
45  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
46  */

47 public class YAML {
48     public static final String JavaDoc DEFAULT_SCALAR_TAG = "tag:yaml.org,2002:str";
49     public static final String JavaDoc DEFAULT_SEQUENCE_TAG = "tag:yaml.org,2002:seq";
50     public static final String JavaDoc DEFAULT_MAPPING_TAG = "tag:yaml.org,2002:map";
51
52     public static final Map JavaDoc ESCAPE_REPLACEMENTS = new HashMap JavaDoc();
53
54     static {
55         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\0'),"0");
56         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u0007'),"a");
57         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u0008'),"b");
58         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u0009'),"t");
59         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\n'),"n");
60         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u000B'),"v");
61         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u000C'),"f");
62         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\r'),"r");
63         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u001B'),"e");
64         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('"'),"\"");
65         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\\'),"\\");
66         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\u0085'),"N");
67         ESCAPE_REPLACEMENTS.put(new Character JavaDoc('\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 JavaDoc data) {
78         return dump(data,config());
79     }
80
81     public static ByteList dump(final Object JavaDoc data, final YAMLFactory fact) {
82         return dump(data,fact, config());
83     }
84
85     public static ByteList dump(final Object JavaDoc data, final YAMLConfig cfg) {
86         final List JavaDoc lst = new ArrayList JavaDoc(1);
87         lst.add(data);
88         return dumpAll(lst,cfg);
89     }
90
91     public static ByteList dump(final Object JavaDoc data, final YAMLFactory fact, final YAMLConfig cfg) {
92         final List JavaDoc lst = new ArrayList JavaDoc(1);
93         lst.add(data);
94         return dumpAll(lst,fact,cfg);
95     }
96
97     public static ByteList dumpAll(final List JavaDoc data) {
98         return dumpAll(data,config());
99     }
100
101     public static ByteList dumpAll(final List JavaDoc data, final YAMLFactory fact) {
102         return dumpAll(data,fact,config());
103     }
104
105     public static ByteList dumpAll(final List JavaDoc data, final YAMLConfig cfg) {
106         final ByteArrayOutputStream JavaDoc swe = new ByteArrayOutputStream JavaDoc();
107         dumpAll(data,swe,cfg);
108         return new ByteList(swe.toByteArray(),false);
109     }
110
111     public static ByteList dumpAll(final List JavaDoc data, final YAMLFactory fact, final YAMLConfig cfg) {
112         final ByteArrayOutputStream JavaDoc swe = new ByteArrayOutputStream JavaDoc();
113         dumpAll(data,swe,fact,cfg);
114         return new ByteList(swe.toByteArray(),false);
115     }
116
117     public static void dump(final Object JavaDoc data, final OutputStream JavaDoc output) {
118         dump(data,output,config());
119     }
120
121     public static void dump(final Object JavaDoc data, final OutputStream JavaDoc output, YAMLFactory fact) {
122         dump(data,output,fact,config());
123     }
124
125     public static void dump(final Object JavaDoc data, final OutputStream JavaDoc output, final YAMLConfig cfg) {
126         final List JavaDoc lst = new ArrayList JavaDoc(1);
127         lst.add(data);
128         dumpAll(lst,output,cfg);
129     }
130
131     public static void dump(final Object JavaDoc data, final OutputStream JavaDoc output, final YAMLFactory fact, final YAMLConfig cfg) {
132         final List JavaDoc lst = new ArrayList JavaDoc(1);
133         lst.add(data);
134         dumpAll(lst,output,fact,cfg);
135     }
136
137     public static void dumpAll(final List JavaDoc data, final OutputStream JavaDoc output) {
138         dumpAll(data,output,config());
139     }
140
141     public static void dumpAll(final List JavaDoc data, final OutputStream JavaDoc output, final YAMLFactory fact) {
142         dumpAll(data,output,fact,config());
143     }
144
145     public static void dumpAll(final List JavaDoc data, final OutputStream JavaDoc output, final YAMLConfig cfg) {
146         dumpAll(data,output,new DefaultYAMLFactory(),cfg);
147     }
148
149     public static void dumpAll(final List JavaDoc data, final OutputStream JavaDoc 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 JavaDoc iter = data.iterator(); iter.hasNext();) {
155                 r.represent(iter.next());
156             }
157         } catch(final java.io.IOException JavaDoc e) {
158             throw new YAMLException(e);
159         } finally {
160             try { s.close(); } catch(final java.io.IOException JavaDoc e) {}
161         }
162     }
163
164     public static Object JavaDoc load(final ByteList io) {
165         return load(io, new DefaultYAMLFactory(),config());
166     }
167
168     public static Object JavaDoc load(final InputStream JavaDoc io) {
169         return load(io, new DefaultYAMLFactory(),config());
170     }
171
172     public static Object JavaDoc load(final ByteList io, final YAMLConfig cfg) {
173         return load(io, new DefaultYAMLFactory(),cfg);
174     }
175
176     public static Object JavaDoc load(final InputStream JavaDoc io, final YAMLConfig cfg) {
177         return load(io, new DefaultYAMLFactory(),cfg);
178     }
179
180     public static Object JavaDoc 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 JavaDoc load(final InputStream JavaDoc 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 JavaDoc loadAll(final ByteList io) {
199         return loadAll(io, new DefaultYAMLFactory(),config());
200     }
201
202     public static List JavaDoc loadAll(final InputStream JavaDoc io) {
203         return loadAll(io, new DefaultYAMLFactory(),config());
204     }
205
206     public static List JavaDoc loadAll(final ByteList io, final YAMLConfig cfg) {
207         return loadAll(io, new DefaultYAMLFactory(),cfg);
208     }
209
210     public static List JavaDoc loadAll(final InputStream JavaDoc io, final YAMLConfig cfg) {
211         return loadAll(io, new DefaultYAMLFactory(),cfg);
212     }
213
214     public static List JavaDoc loadAll(final ByteList io, final YAMLFactory fact, final YAMLConfig cfg) {
215         final List JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc loadAll(final InputStream JavaDoc io, final YAMLFactory fact, final YAMLConfig cfg) {
224         final List JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc[] args) {
233         System.err.println(YAML.load(new ByteList("%YAML 1.0\n---\n!str str".getBytes())));
234     }
235 }// YAML
236
Popular Tags