summaryrefslogtreecommitdiff
path: root/com/jeremyfa/yaml/Yaml.js
blob: 3f9dfec594b2cf9e6ad9d10801853748b288bc01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * sfYaml offers convenience methods to load and dump YAML.
 *
 * @package		symfony
 * @subpackage yaml
 * @author		 Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version		SVN: $Id: sfYaml.class.php 8988 2008-05-15 20:24:26Z fabien $
 */
 
var Yaml = function(){};
Yaml.prototype =
{
	spec: '1.2',

	/**
	 * Sets the YAML specification version to use.
	 *
	 * @param string version The YAML specification version
	 */
	setSpecVersion: function(version /* String */)
	{
		if ( version != '1.1' && version != '1.2' )
		{
			throw new InvalidArgumentException('Version '+version+' of the YAML specifications is not supported');
		}

		this.spec = version;
	},

	/**
	 * Gets the YAML specification version to use.
	 *
	 * @return string The YAML specification version
	 */
	getSpecVersion: function()
	{
		return this.spec;
	},

	/**
	 * Loads YAML into a JS representation.
	 *
	 * The load method, when supplied with a YAML stream (file),
	 * will do its best to convert YAML in a file into a JS representation.
	 *
	 *	Usage:
	 *	<code>
	 *	 obj = yaml.loadFile('config.yml');
	 *	</code>
	 *
	 * @param string input Path of YAML file or string containing YAML
	 *
	 * @return array The YAML converted to a JS representation
	 *
	 * @throws InvalidArgumentException If the YAML is not valid
	 */
	loadFile: function(file /* String */, callback /* Function */)
	{
		if ( callback == undefined )
		{
			input = this.getFileContents(file);
			return this.load(input);
		}
		
		this.getFileContents(file, function(data)
		{
			callback(new Yaml().load(data));
		});
	},

	/**
	 * Loads YAML into a JS representation.
	 *
	 * The load method, when supplied with a YAML stream (string),
	 * will do its best to convert YAML in a file into a JS representation.
	 *
	 *	Usage:
	 *	<code>
	 *	 obj = yaml.load(...);
	 *	</code>
	 *
	 * @param string input Path of YAML file or string containing YAML
	 *
	 * @return array The YAML converted to a JS representation
	 *
	 * @throws InvalidArgumentException If the YAML is not valid
	 */
	load: function(input /* String */)
	{
		var yaml = new YamlParser();
		var ret = null;

		try
		{
			ret = yaml.parse(input);
		}
		catch ( e )
		{
			if ( e.name != undefined && e.name.toString == "TypeError" ) throw e;
			throw 'Syntax error: '+e.message;
			//throw new InvalidArgumentException(e.name+' ('+e.lineNumber+') '+e.message);
		}

		return ret;
	},

	/**
	 * Dumps a JS representation to a YAML string.
	 *
	 * The dump method, when supplied with an array, will do its best
	 * to convert the array into friendly YAML.
	 *
	 * @param array	 array JS representation
	 * @param integer inline The level where you switch to inline YAML
	 *
	 * @return string A YAML string representing the original JS representation
	 */
	dump: function(array, inline)
	{
		if ( inline == undefined ) inline = 2;

		yaml = new YamlDumper();

		return yaml.dump(array, inline);
	},
	
	getXHR: function()
	{
		if ( window.XMLHttpRequest )
			return new XMLHttpRequest();
		 
		if ( window.ActiveXObject )
		{
			var names = [
			"Msxml2.XMLHTTP.6.0",
			"Msxml2.XMLHTTP.3.0",
			"Msxml2.XMLHTTP",
			"Microsoft.XMLHTTP"
			];
			
			for ( var i = 0; i < 4; i++ )
			{
				try{ return new ActiveXObject(names[i]); }
				catch(e){}
			}
		}
		return null;
	},
	
	getFileContents: function(file, callback)
	{
		var request = this.getXHR();
		
		// Sync
		if ( callback == undefined )
		{
			request.open('GET', file, false);                  
			request.send(null);

			if ( request.status == 200 || request.status == 0 )
				return request.responseText;
			
			return null;
		}
		
		// Async
		request.onreadystatechange = function()
		{
			if ( request.readyState == 4 )
				if ( request.status == 200 || request.status == 0 )
					callback(request.responseText);
				else
					callback(null);
		};
		request.open('GET', file, true);                  
		request.send(null);
	}
};

var YAML =
{
	encode: function(input)
	{
		return new Yaml().dump(input);
	},
	
	decode: function(input)
	{
		return new Yaml().load(input);
	},
	
	load: function(file, callback)
	{
		return new Yaml().loadFile(file, callback);
	}
};

if ( typeof(InvalidArgumentException) == 'undefined' )
	InvalidArgumentException = function(message)
	{
		this.name = 'InvalidArgumentException';
		this.message = message;
	};