Skip to content

Commit

Permalink
Implemented loadXML() processing#40
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPrince304 committed Feb 13, 2016
1 parent 2c1368c commit 89eb025
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require('./image/p5.Image');
require('./math/p5.Vector');
require('./io/p5.TableRow');
require('./io/p5.Table');
require('./io/p5.XML');

require('./color/creating_reading');
require('./color/setting');
Expand Down
73 changes: 45 additions & 28 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,35 +685,36 @@ function makeObject(row, headers) {
* in as first argument
* @return {Object} XML object containing data
*/
p5.prototype.loadXML = function(path, callback, errorCallback) {
p5.prototype.loadXML = function (path, callback, errorCallback) {
var ret = document.implementation.createDocument(null, null);
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);

var y;
reqwest({
url: path,
type: 'xml',
crossOrigin: true,
error: function(resp){
// pass to error callback if defined
if (errorCallback) {
errorCallback(resp);
} else { // otherwise log error msg
console.log(resp.statusText);
url: path,
type: 'xml',
crossOrigin: true,
error: function (resp) {
// pass to error callback if defined
if (errorCallback) {
errorCallback(resp);
} else { // otherwise log error msg
console.log(resp.statusText);
}
//p5._friendlyFileLoadError(1,path);
}
//p5._friendlyFileLoadError(1,path);
}
})
.then(function(resp){
var x = resp.documentElement;
ret.appendChild(x);
if (typeof callback !== 'undefined') {
callback(ret);
}
if (decrementPreload && (callback !== decrementPreload)) {
decrementPreload();
}
});
return ret;
})
.then(function (resp) {
var x = resp.documentElement;
ret = parseXML(x)
console.log(ret);
if (typeof callback !== 'undefined') {
callback(ret);
}
if (decrementPreload && (callback !== decrementPreload)) {
decrementPreload();
}
});
return ret;
};

// name clash with window.open
Expand All @@ -722,10 +723,26 @@ p5.prototype.loadXML = function(path, callback, errorCallback) {

// };

p5.prototype.parseXML = function() {
// TODO
throw 'not yet implemented';

p5.prototype.parseXML = function (two) {
var one = new p5.XML(), node = new p5.XML(),i;
if(two.children.length){
for( i = 0; i < two.children.length; i++ ) {
node = parseXML(two.children[i]);
one.addChild(node);
}
one.setName(two.nodeName);
one.setCont(two.textContent);
one.setAttributes(two);
one.setParent();
return one;
}
else {
one.setName(two.nodeName);
one.setCont(two.textContent);
one.setAttributes(two);
return one;
}
};

p5.prototype.selectFolder = function() {
Expand Down
126 changes: 126 additions & 0 deletions src/io/p5.XML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
p5.XML = function () {
this.name = null; //done
this.attributes = {}; //done
this.children = [];
this.parent = null;
this.content = []; //done
};

p5.XML.prototype.setName = function(name) {
this.name = name
}

p5.XML.prototype.setParent = function() {
var i;
for( i = 0; i < this.children.length; i++ ){
this.children[i].parent = this;
}
}

p5.XML.prototype.addChild = function(node) {
this.children.push(node);
}

p5.XML.prototype.setCont = function(content) {
var str;
str = content;
str = str.replace(/\s\s+/g, ',');
str = str.split(',');
this.content = str;
}

p5.XML.prototype.setAttributes = function(node) {
var list = [], i, att = {};
for( i = 0; i < node.attributes.length; i++) {
att[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
}
this.attributes = att;
}

p5.XML.prototype.getParent = function() {
return this.parent;
}

p5.XML.prototype.getName = function() {
return this.name;
}

p5.XML.prototype.hasChildren = function() {
if(this.children)
return true;
else
return false;
}

p5.XML.prototype.listChildren = function() {
var i, arr = [];
for( i = 0; i < this.children.length; i++ ) {
arr.push(this.children[i].name);
}
return arr;
}

p5.XML.prototype.getChildren = function(param) {
if (param) {
var i, arr = [];
for( i = 0; i < this.children.length; i++ ) {
if (this.children[i].name == param) {
arr.push(this.children[i]);
}
}
return arr;
}
else {
return this.children;
}
}

p5.XML.prototype.getChild = function(param) {
if(typeof param == "string") {
return this.children[0];
}
else {
var i;
for( i = 0; i < this.children.length; i++ ) {
if(i == param)
return this.children[i];
}
}
}

p5.XML.prototype.removeChild = function(node) {
var i;
for( i = 0 ; i < this.children.length; i++ ) {
if( this.children[i] == node ) {
delete this.children[i];
}
}
}

p5.XML.prototype.getAttributeCount = function() {
return Object.keys(this.attributes).length;
}

p5.XML.prototype.listAttributes = function() {
return Object.keys(this.attributes);
}

p5.XML.prototype.hasAttribute = function(name) {
var i;
var names = Object.keys(this.attributes);
for( i = 0 ; i < names.length ; i++ ) {
if(name = names[i])
return true;
}
return false;
}

p5.XML.prototype.getContent = function() {
return this.content;
}

p5.XML.prototype.setContent = function( content ) {
if(!this.children.length) {
this.content = content;
}
}

0 comments on commit 89eb025

Please sign in to comment.