Skip to content

Commit

Permalink
Implemented loadXML() functionality processing#40
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPrince304 committed Feb 13, 2016
1 parent 32e1e3a commit caf8ef7
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 7 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
36 changes: 29 additions & 7 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,22 +654,44 @@ function makeObject(row, headers) {
return ret;
}

/*global parseXML */
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;
}
};

/**
* Reads the contents of a file and creates an XML object with its values.
* If the name of the file is used as the parameter, as in the above example,
* the file must be located in the sketch directory/folder.
* <br><br>
*
* Alternatively, the file maybe be loaded from anywhere on the local
* computer using an absolute path (something that starts with / on Unix and
* Linux, or a drive letter on Windows), or the filename parameter can be a
* URL for a file found on a network.
* <br><br>
*
* This method is asynchronous, meaning it may not finish before the next
* line in your sketch is executed. Calling loadXML() inside preload()
* guarantees to complete the operation before setup() and draw() are called.
* <br><br>
* Outside of preload(), you may supply a callback function to handle the
* object:
*
* <p>Outside of preload(), you may supply a callback function to handle the
* object:</p>
*
* @method loadXML
* @param {String} filename name of the file or URL to load
Expand All @@ -684,7 +706,6 @@ function makeObject(row, headers) {
p5.prototype.loadXML = function (path, callback, errorCallback) {
var ret = document.implementation.createDocument(null, null);
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);

reqwest({
url: path,
type: 'xml',
Expand All @@ -701,7 +722,8 @@ p5.prototype.loadXML = function (path, callback, errorCallback) {
})
.then(function (resp) {
var x = resp.documentElement;
ret.appendChild(x);
ret = parseXML(x);
console.log(ret);
if (typeof callback !== 'undefined') {
callback(ret);
}
Expand Down
136 changes: 136 additions & 0 deletions src/io/p5.XML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

'use strict';

var p5 = require('../core/core');


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 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 caf8ef7

Please sign in to comment.