<commit_before>/**
 * @file xsdwebform.parser.htmltags.js
 * XSD Schema to HTML5 Web Form
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (C) 2017 EEA, Eworx, <NAME>. All rights reserved.
 */

import XSDWebFormParserLog from './xsdwebform.parser.log.js';
import XSDWebFormParserError from './xsdwebform.parser.error.js';

/**
 * Class XSDWebFormParserHTMLTags
 * Parser for XSD Schema Tags 
 * Static
 */
class XSDWebFormParserHTMLTags {
	/**
	 * Class constructor
	 */
	constructor() {
		this.HTML_TYPES = {
			"page": this.parsePage,
			"form": this.parseForm,
			"group": this.parseGroup,
			"item": this.parseItem,
			"input": this.parseInput,
			"text": this.parseText,
			"number": this.parseNumber,
			"positivenumber": this.parseNumberP,
			"date": this.parseDate,
			"select": this.parseSelect,
			"radio": this.parseRadio
		};

		this.XSD_HTML_TYPES = {
			"xs:integer": "number",
			"xs:decimal": "number",
			"NonNegativeIntegerType": "positivenumber",
			"xs:date": "date",
			"DateType": "date",
			"xs:string": "input",
			"xs:boolean": "radio"
		};

		this.HTMLObjects = [];
		this.LabelContentObjects = [];
		this.TextContentObjects = [];
		this.showLog = false;
		this.verbose = false;
	}

	/**
	 * htmlParse - Parse inner XML Document
	 * @param htmlItem
	 * @param xsdItem
	 * @returns HTMLObjects[]
	 */
	htmlParse(htmlItem, xsdItem) {
		// Loop through Tag's childNodes
		for (let i = 0, l = htmlItem.children.length; i < l; i++) {
			if (htmlItem.children[i].type === "element") {
				if (this.showLog) {
					process.stdout.write(`\x1b[0m\x1b[31mHTML => \x1b[0m\x1b[33m${htmlItem.children[i].name}`);
					if (htmlItem.children[i].attr.name) {
						process.stdout.write(` :: \x1b[1m\x1b[33m${htmlItem.children[i].attr.name} \x1b[0m`);
					}
					if (htmlItem.children[i].attr.element) {
						process.stdout.write(` :: \x1b[1m\x1b[33m${htmlItem.children[i].attr.element} \x1b[0m`);
					}
					console.log("");
				}

				try {
					this.parseHTMLItem(htmlItem.children[i], xsdItem);
					this.htmlParse(htmlItem.children[i], xsdItem);
				} catch (err) {
					this.reportError(err);
				}
			}
		}
	}

	/**
	 * parseHTMLItem - Parse HTML Tag
	 * @param item
	 * @param xsdItem
	 */
	parseHTMLItem(item, xsdItem) {
		if ((item.name in this.HTML_TYPES)) {
			(this.HTML_TYPES[item.name])(item, xsdItem, this);
		} else {
			console.log(`\n************* Unknown HTML Tag {${item.name}} *************\n`);
			process.stdout.write('\x07');
		}
	}

	/**
	 * parsePage - Parse Page Info
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parsePage(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);
		sender.HTML_FORM_TITLE = "{{'formtitle' | translate}}";
	}

	/**
	 * parseForm - Parse Form Tag
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parseForm(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);

		let formEnd = "<button type=\"submit\" class=\"submitbutton btn btn-primary\">{{'submitform' | translate}}</button>";
		let formNum = sender.HTMLObjects.length + 1;
		var formObject = {
			name: 'form' + formNum ,
			action: item.attr.action,
			tag: 'form',
			append: formEnd,
			attrs: {
				name: 'form' + formNum,
				id: 'form' + formNum,
				'ew-action': item.attr.action,
				method: 'post',
				class: 'ewform medium-12',
				'ng-submit': `submit(form${formNum})`
			},
			groups: [],
			tagToHtml: XSDWebFormParserHTMLTags.tagToHtml
		};
		sender.HTMLObjects.push({
			type: "form",
			itemObject: formObject
		});
	}

	/**
	 * parseGroup - Parse Group Tag
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parseGroup(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);

		let xsdGroupTag, xsdGroupProperties;
		try {
			xsdGroupTag = sender.getXSDComplexByGroupTag(item.attr.element, xsdItem, sender);
		} catch (ex) {
			sender.reportError(`Can not find "${item.attr.element}" element in XSD`);
		}

		try {
			xsdGroupProperties = ( xsdItem.childNamed("xs:element").childNamed("xs:complexType").childNamed("xs:sequence") 
						|| xsdItem.childNamed("xs:element").childNamed("xs:complexType").childNamed("xs:all") 
						).childWithAttribute("type", item.attr.element, xsdItem) ;
		} catch (ex) {
			sender.reportError(`Can not find type="${item.attr.element}" element in XSD root element`, item);
		}

		if (!xsdGroupProperties) {
			try {
				xsdGroupProperties = sender.getItemByName(item.attr.element, xsdItem).childNamed("xs:complexType").childNamed("xs:sequence") 
							|| sender.getItemByName(item.attr.element, xsdItem).childNamed("xs:complexType").childNamed("xs:all");
			} catch (ex) {
				sender.reportError(`Can not find name="${item.attr.element}" element in XSD root element`, item);
			}			
		}


		let groupStart= '', groupEnd = '', canAddRows = 0;
		if (xsdGroupProperties.attr.maxOccurs) {
			let maxOccurs = '';
			if (isNaN(xsdGroupProperties.attr.maxOccurs))  {
				if ( xsdGroupProperties.attr.maxOccurs === 'unbounded')  {
					maxOccurs = '0';
				}
			} else {
				maxOccurs = xsdGroupProperties.attr.maxOccurs;<commit_msg>Updated xsd parsing<commit_after>/**
 * @file xsdwebform.parser.htmltags.js
 * XSD Schema to HTML5 Web Form
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (C) 2017 EEA, Eworx, George Bouris. All rights reserved.
 */

import XSDWebFormParserLog from './xsdwebform.parser.log.js';
import XSDWebFormParserError from './xsdwebform.parser.error.js';

/**
 * Class XSDWebFormParserHTMLTags
 * Parser for XSD Schema Tags 
 * Static
 */
class XSDWebFormParserHTMLTags {
	/**
	 * Class constructor
	 */
	constructor() {
		this.HTML_TYPES = {
			"page": this.parsePage,
			"form": this.parseForm,
			"group": this.parseGroup,
			"item": this.parseItem,
			"input": this.parseInput,
			"text": this.parseText,
			"number": this.parseNumber,
			"positivenumber": this.parseNumberP,
			"date": this.parseDate,
			"select": this.parseSelect,
			"radio": this.parseRadio
		};

		this.XSD_HTML_TYPES = {
			"xs:integer": "number",
			"xs:decimal": "number",
			"NonNegativeIntegerType": "positivenumber",
			"xs:date": "date",
			"DateType": "date",
			"xs:string": "input",
			"xs:boolean": "radio"
		};

		this.HTMLObjects = [];
		this.LabelContentObjects = [];
		this.TextContentObjects = [];
		this.showLog = false;
		this.verbose = false;
	}

	/**
	 * htmlParse - Parse inner XML Document
	 * @param htmlItem
	 * @param xsdItem
	 * @returns HTMLObjects[]
	 */
	htmlParse(htmlItem, xsdItem) {
		// Loop through Tag's childNodes
		for (let i = 0, l = htmlItem.children.length; i < l; i++) {
			if (htmlItem.children[i].type === "element") {
				if (this.showLog) {
					process.stdout.write(`\x1b[0m\x1b[31mHTML => \x1b[0m\x1b[33m${htmlItem.children[i].name}`);
					if (htmlItem.children[i].attr.name) {
						process.stdout.write(` :: \x1b[1m\x1b[33m${htmlItem.children[i].attr.name} \x1b[0m`);
					}
					if (htmlItem.children[i].attr.element) {
						process.stdout.write(` :: \x1b[1m\x1b[33m${htmlItem.children[i].attr.element} \x1b[0m`);
					}
					console.log("");
				}

				try {
					this.parseHTMLItem(htmlItem.children[i], xsdItem);
					this.htmlParse(htmlItem.children[i], xsdItem);
				} catch (err) {
					this.reportError(err);
				}
			}
		}
	}

	/**
	 * parseHTMLItem - Parse HTML Tag
	 * @param item
	 * @param xsdItem
	 */
	parseHTMLItem(item, xsdItem) {
		if ((item.name in this.HTML_TYPES)) {
			(this.HTML_TYPES[item.name])(item, xsdItem, this);
		} else {
			console.log(`\n************* Unknown HTML Tag {${item.name}} *************\n`);
			process.stdout.write('\x07');
		}
	}

	/**
	 * parsePage - Parse Page Info
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parsePage(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);
		sender.HTML_FORM_TITLE = "{{'formtitle' | translate}}";
	}

	/**
	 * parseForm - Parse Form Tag
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parseForm(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);

		let formEnd = "<button type=\"submit\" class=\"submitbutton btn btn-primary\">{{'submitform' | translate}}</button>";
		let formNum = sender.HTMLObjects.length + 1;
		var formObject = {
			name: 'form' + formNum ,
			action: item.attr.action,
			tag: 'form',
			append: formEnd,
			attrs: {
				name: 'form' + formNum,
				id: 'form' + formNum,
				'ew-action': item.attr.action,
				method: 'post',
				class: 'ewform medium-12',
				'ng-submit': `submit(form${formNum})`
			},
			groups: [],
			tagToHtml: XSDWebFormParserHTMLTags.tagToHtml
		};
		sender.HTMLObjects.push({
			type: "form",
			itemObject: formObject
		});
	}

	/**
	 * parseGroup - Parse Group Tag
	 * @param item
	 * @param xsdItem
	 * @param sender
	 */
	parseGroup(item, xsdItem, sender) {
		XSDWebFormParserLog.logHtmlTag(item.name, sender);

		let xsdGroupTag, xsdGroupProperties;
		try {
			xsdGroupTag = sender.getXSDComplexByGroupTag(item.attr.element, xsdItem, sender);
		} catch (ex) {
			sender.reportError(`Can not find "${item.attr.element}" element in XSD`);
		}

		try {
			xsdGroupProperties = ( xsdItem.childNamed("xs:element").childNamed("xs:complexType").childNamed("xs:sequence") 
						|| xsdItem.childNamed("xs:element").childNamed("xs:complexType").childNamed("xs:all") 
						).childWithAttribute("type", item.attr.element, xsdItem) 
						|| sender.getItemByName(item.attr.element, xsdItem).childNamed("xs:complexType").childNamed("xs:sequence") 
						|| sender.getItemByName(item.attr.element, xsdItem).childNamed("xs:complexType").childNamed("xs:all");
		} catch (ex) {
			sender.reportError(`Can not find type="${item.attr.element}" or  name="${item.attr.element}" element in XSD root element`, item);
		}

		let groupStart= '', groupEnd = '', canAddRows = 0;
		if (xsdGroupProperties.attr.maxOccurs) {
			let maxOccurs = '';
			if (isNaN(xsdGroupProperties.attr.maxOccurs))  {
				if ( xsdGroupProperties.attr.maxOccurs === 'unbounded')  {
					maxOccurs = '0';
				}
			} else {
				maxOccurs = xsdGroupProperties.attr.maxOccurs;
{'fraction_non_alphanumeric': 0.17069051167411822, 'fraction_numerical': 0.0073522106308991555, 'mean_word_length': 3.619550252409362, 'pattern_counts': {'":': 36, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '234057', 'n_tokens_mistral': 4126, 'n_tokens_neox': 3558, 'n_words': 658}