001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2007 Ola Bini <ola@ologix.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jvyamlb;
028:
029: import java.io.InputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.OutputStream;
032:
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.HashMap;
036: import java.util.List;
037: import java.util.ArrayList;
038:
039: import org.jruby.util.ByteList;
040:
041: /**
042: * The combinatorial explosion class.
043: *
044: * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
045: */
046: public class YAML {
047: public static final String DEFAULT_SCALAR_TAG = "tag:yaml.org,2002:str";
048: public static final String DEFAULT_SEQUENCE_TAG = "tag:yaml.org,2002:seq";
049: public static final String DEFAULT_MAPPING_TAG = "tag:yaml.org,2002:map";
050:
051: public static final Map ESCAPE_REPLACEMENTS = new HashMap();
052:
053: static {
054: ESCAPE_REPLACEMENTS.put(new Character('\0'), "0");
055: ESCAPE_REPLACEMENTS.put(new Character('\u0007'), "a");
056: ESCAPE_REPLACEMENTS.put(new Character('\u0008'), "b");
057: ESCAPE_REPLACEMENTS.put(new Character('\u0009'), "t");
058: ESCAPE_REPLACEMENTS.put(new Character('\n'), "n");
059: ESCAPE_REPLACEMENTS.put(new Character('\u000B'), "v");
060: ESCAPE_REPLACEMENTS.put(new Character('\u000C'), "f");
061: ESCAPE_REPLACEMENTS.put(new Character('\r'), "r");
062: ESCAPE_REPLACEMENTS.put(new Character('\u001B'), "e");
063: ESCAPE_REPLACEMENTS.put(new Character('"'), "\"");
064: ESCAPE_REPLACEMENTS.put(new Character('\\'), "\\");
065: ESCAPE_REPLACEMENTS.put(new Character('\u0085'), "N");
066: ESCAPE_REPLACEMENTS.put(new Character('\u00A0'), "_");
067: }
068:
069: public static YAMLConfig config() {
070: return defaultConfig();
071: }
072:
073: public static YAMLConfig defaultConfig() {
074: return new DefaultYAMLConfig();
075: }
076:
077: public static ByteList dump(final Object data) {
078: return dump(data, config());
079: }
080:
081: public static ByteList dump(final Object data,
082: final YAMLFactory fact) {
083: return dump(data, fact, config());
084: }
085:
086: public static ByteList dump(final Object data, final YAMLConfig cfg) {
087: final List lst = new ArrayList(1);
088: lst.add(data);
089: return dumpAll(lst, cfg);
090: }
091:
092: public static ByteList dump(final Object data,
093: final YAMLFactory fact, final YAMLConfig cfg) {
094: final List lst = new ArrayList(1);
095: lst.add(data);
096: return dumpAll(lst, fact, cfg);
097: }
098:
099: public static ByteList dumpAll(final List data) {
100: return dumpAll(data, config());
101: }
102:
103: public static ByteList dumpAll(final List data,
104: final YAMLFactory fact) {
105: return dumpAll(data, fact, config());
106: }
107:
108: public static ByteList dumpAll(final List data, final YAMLConfig cfg) {
109: final ByteArrayOutputStream swe = new ByteArrayOutputStream();
110: dumpAll(data, swe, cfg);
111: return new ByteList(swe.toByteArray(), false);
112: }
113:
114: public static ByteList dumpAll(final List data,
115: final YAMLFactory fact, final YAMLConfig cfg) {
116: final ByteArrayOutputStream swe = new ByteArrayOutputStream();
117: dumpAll(data, swe, fact, cfg);
118: return new ByteList(swe.toByteArray(), false);
119: }
120:
121: public static void dump(final Object data, final OutputStream output) {
122: dump(data, output, config());
123: }
124:
125: public static void dump(final Object data,
126: final OutputStream output, YAMLFactory fact) {
127: dump(data, output, fact, config());
128: }
129:
130: public static void dump(final Object data,
131: final OutputStream output, final YAMLConfig cfg) {
132: final List lst = new ArrayList(1);
133: lst.add(data);
134: dumpAll(lst, output, cfg);
135: }
136:
137: public static void dump(final Object data,
138: final OutputStream output, final YAMLFactory fact,
139: final YAMLConfig cfg) {
140: final List lst = new ArrayList(1);
141: lst.add(data);
142: dumpAll(lst, output, fact, cfg);
143: }
144:
145: public static void dumpAll(final List data,
146: final OutputStream output) {
147: dumpAll(data, output, config());
148: }
149:
150: public static void dumpAll(final List data,
151: final OutputStream output, final YAMLFactory fact) {
152: dumpAll(data, output, fact, config());
153: }
154:
155: public static void dumpAll(final List data,
156: final OutputStream output, final YAMLConfig cfg) {
157: dumpAll(data, output, new DefaultYAMLFactory(), cfg);
158: }
159:
160: public static void dumpAll(final List data,
161: final OutputStream output, final YAMLFactory fact,
162: final YAMLConfig cfg) {
163: final Serializer s = fact.createSerializer(fact.createEmitter(
164: output, cfg), fact.createResolver(), cfg);
165: try {
166: s.open();
167: final Representer r = fact.createRepresenter(s, cfg);
168: for (final Iterator iter = data.iterator(); iter.hasNext();) {
169: r.represent(iter.next());
170: }
171: } catch (final java.io.IOException e) {
172: throw new YAMLException(e);
173: } finally {
174: try {
175: s.close();
176: } catch (final java.io.IOException e) {
177: }
178: }
179: }
180:
181: public static Object load(final ByteList io) {
182: return load(io, new DefaultYAMLFactory(), config());
183: }
184:
185: public static Object load(final InputStream io) {
186: return load(io, new DefaultYAMLFactory(), config());
187: }
188:
189: public static Object load(final ByteList io, final YAMLConfig cfg) {
190: return load(io, new DefaultYAMLFactory(), cfg);
191: }
192:
193: public static Object load(final InputStream io, final YAMLConfig cfg) {
194: return load(io, new DefaultYAMLFactory(), cfg);
195: }
196:
197: public static Object load(final ByteList io,
198: final YAMLFactory fact, final YAMLConfig cfg) {
199: final Constructor ctor = fact
200: .createConstructor(fact.createComposer(fact
201: .createParser(fact.createScanner(io), cfg),
202: fact.createResolver()));
203: if (ctor.checkData()) {
204: return ctor.getData();
205: } else {
206: return null;
207: }
208: }
209:
210: public static Object load(final InputStream io,
211: final YAMLFactory fact, final YAMLConfig cfg) {
212: final Constructor ctor = fact
213: .createConstructor(fact.createComposer(fact
214: .createParser(fact.createScanner(io), cfg),
215: fact.createResolver()));
216: if (ctor.checkData()) {
217: return ctor.getData();
218: } else {
219: return null;
220: }
221: }
222:
223: public static List loadAll(final ByteList io) {
224: return loadAll(io, new DefaultYAMLFactory(), config());
225: }
226:
227: public static List loadAll(final InputStream io) {
228: return loadAll(io, new DefaultYAMLFactory(), config());
229: }
230:
231: public static List loadAll(final ByteList io, final YAMLConfig cfg) {
232: return loadAll(io, new DefaultYAMLFactory(), cfg);
233: }
234:
235: public static List loadAll(final InputStream io,
236: final YAMLConfig cfg) {
237: return loadAll(io, new DefaultYAMLFactory(), cfg);
238: }
239:
240: public static List loadAll(final ByteList io,
241: final YAMLFactory fact, final YAMLConfig cfg) {
242: final List result = new ArrayList();
243: final Constructor ctor = fact
244: .createConstructor(fact.createComposer(fact
245: .createParser(fact.createScanner(io), cfg),
246: fact.createResolver()));
247: while (ctor.checkData()) {
248: result.add(ctor.getData());
249: }
250: return result;
251: }
252:
253: public static List loadAll(final InputStream io,
254: final YAMLFactory fact, final YAMLConfig cfg) {
255: final List result = new ArrayList();
256: final Constructor ctor = fact
257: .createConstructor(fact.createComposer(fact
258: .createParser(fact.createScanner(io), cfg),
259: fact.createResolver()));
260: while (ctor.checkData()) {
261: result.add(ctor.getData());
262: }
263: return result;
264: }
265:
266: public static void main(final String[] args) {
267: System.err.println(YAML.load(new ByteList(
268: "%YAML 1.0\n---\n!str str".getBytes())));
269: }
270: }// YAML
|