]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/MARC/Batch.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[working/Evergreen.git] / Open-ILS / web / js / dojo / MARC / Batch.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2009  Equinox Software, Inc.
3  * Mike Rylander <miker@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource["MARC.Batch"]) {
18
19     dojo.require('dojox.xml.parser');
20     dojo.require('MARC.Record');
21
22     dojo._hasResource["MARC.Batch"] = true;
23     dojo.provide("MARC.Batch");
24     dojo.declare('MARC.Batch', null, {
25
26         constructor : function(kwargs) {
27             this.ready = false;
28             this.records = [];
29             this.source = kwargs.source;
30             this.delimiter = kwargs.delimiter
31             this.current_record = 0;
32
33             if (this.source) this.ready = true;
34             if (!this.ready && kwargs.url) this.fetchURL( kwargs.url );
35
36             if (this.ready) this.parse();
37         },
38
39         parse : function () {
40             if (dojo.isObject( this.source )) { // assume an xml collection document
41                 this.source = dojo.query('record', this.source);
42                 this.type = 'xml';
43             } else if (this.source.match(/^\s*</)) { // this is xml text
44                 this.source = dojox.xml.parser.parse( this.source );
45                 this.parse();
46             } else { // must be a marcbreaker doc. split on blank lines
47                 this.source = this.source.split(/^$/);
48                 this.type = 'marcbreaker';
49             }
50         },
51
52         fetchURL : function (u) {
53             var me = this;
54             dojo.xhrGet({
55                 url     : u,
56                 sync    : true,
57                 handleAs: 'text',
58                 load    : function (mrc) {
59                     me.source = mrc;
60                     me.ready = true;
61                 }
62             });
63         },
64
65         next : function () {
66             var chunk = this.source[this.current_record++];
67
68             if (chunk) {
69                 var args = {};
70                 args[this.type] = chunk;
71                 if (this.delimiter) args.delimiter = this.delimiter;
72                 return new MARC.Record(args);
73             }
74
75             return null;
76         }
77
78     });
79 }
80             
81             
82