Browse files

support for loading xml with repetitions

  • Loading branch information...
1 parent 295b525 commit 6ff05681ed70b588aa9f563efa7b34732a5280a0 rlafitte.fa7@gmail.com committed Jan 19, 2011
Showing with 176 additions and 8 deletions.
  1. +1 −1 index.html
  2. +118 −0 sample_data.xml
  3. +57 −7 xsdForm.js
View
2 index.html
@@ -32,7 +32,7 @@
<form onsubmit="generateXml('sample.xsd', this.processed_xml); return false">
<H1>The Form</H1>
<div id="xsdform_container" ></div>
- <input type="submit">
+ <input type="submit" />
<H1>The Output</H1>
<textarea name="processed_xml" rows="20" cols="100"></textarea>
</form>
View
118 sample_data.xml
@@ -0,0 +1,118 @@
+<formCadernoA xmlns="test:ns">
+ <identificacao>
+ <codigo>12345</codigo>
+ <codigoNaoTem>false</codigoNaoTem>
+ <data>2011-03-29</data>
+ <orgaoCadastrador>slkjgfjk</orgaoCadastrador>
+ <test>dlksskl</test>
+ <entrevistador>klasfklasgfkl</entrevistador>
+ <titularBeneficiario>kjkljkadadf</titularBeneficiario>
+ <nomeComunidade>klajkfd jklekd</nomeComunidade>
+ <tipoDemanda>Não Informado</tipoDemanda>
+ <cartao>Não Informado</cartao>
+ </identificacao>
+ <enderecoImovel>
+ <logradouro>asdfmkajlasdf sdfsdkls swdd</logradouro>
+ <numero>9</numero>
+ <complemento>klslsnl 2</complemento>
+ <localizacaoCartografica>654</localizacaoCartografica>
+ <bairro>kdkfn ksslks</bairro>
+ <telefone>88889966</telefone>
+ <informante>Não Informado</informante>
+ <observacoes>oskfmf fkeeododfmdf ossmkmlaaam ddcsg</observacoes>
+ </enderecoImovel>
+ <caracteristicasImovel>
+ <localizacao>
+ <quadraloteada>true</quadraloteada>
+ <leitoDeRua>true</leitoDeRua>
+ <praca>true</praca>
+ <areaVerde>false</areaVerde>
+ <terroParaEquipamentoComunitario>false</terroParaEquipamentoComunitario>
+ <outro>false</outro>
+ <localizacaoOutro>23423r</localizacaoOutro>
+ </localizacao>
+ <localizacao>
+ <quadraloteada>false</quadraloteada>
+ <leitoDeRua>false</leitoDeRua>
+ <praca>false</praca>
+ <areaVerde>false</areaVerde>
+ <terroParaEquipamentoComunitario>true</terroParaEquipamentoComunitario>
+ <outro>true</outro>
+ <localizacaoOutro>tedffd</localizacaoOutro>
+ </localizacao>
+ <areaPreservacao>Não Informado</areaPreservacao>
+ <tipologiaUso>Não Informado</tipologiaUso>
+ <tipoServico>231332123kjh jhgj</tipoServico>
+ <compartimentosMoradia>
+ <qtdQuartos>1</qtdQuartos>
+ <qtdSalas>2</qtdSalas>
+ <qtdCozinhas>2</qtdCozinhas>
+ <qtdBanheiros>2</qtdBanheiros>
+ <qtdQuintais>2</qtdQuintais>
+ <qtdUotros>2</qtdUotros>
+ <localizacaoOutro>teste</localizacaoOutro>
+ </compartimentosMoradia>
+ <tipoMoradia>Invadida</tipoMoradia>
+ <numMatricula>123456</numMatricula>
+ <cartorio>awaasdfa</cartorio>
+ <zona>78</zona>
+ <nomeProprietario>rwgfafg afgasggsfg fssfsg</nomeProprietario>
+ <valor>321345646.44</valor>
+ <terreno>3222333.123545</terreno>
+ <enderecoProprietario>lçjasdgf</enderecoProprietario>
+ <telefoneProprietario>33332222</telefoneProprietario>
+ <tipologiaConstrucao>Taipa</tipologiaConstrucao>
+ <tipoCobertura>Plástico/Madeira</tipoCobertura>
+ <tipoPiso>Barro Batido</tipoPiso>
+ <revestimentoParede>Com Reboco</revestimentoParede>
+ <casaEmSituacaRisco>Sim</casaEmSituacaRisco>
+ <tipoRisco>
+ <alagamento>true</alagamento>
+ <inundacao>true</inundacao>
+ <deslizamento>false</deslizamento>
+ <viaFerrea>false</viaFerrea>
+ <linhaAltaTensao>false</linhaAltaTensao>
+ <outro>false</outro>
+ <tipoRiscoOutro>asdfasdf</tipoRiscoOutro>
+ </tipoRisco>
+ <tipoRisco>
+ <alagamento>false</alagamento>
+ <inundacao>false</inundacao>
+ <deslizamento>false</deslizamento>
+ <viaFerrea>false</viaFerrea>
+ <linhaAltaTensao>true</linhaAltaTensao>
+ <outro>true</outro>
+ <tipoRiscoOutro>dfghsrhsg</tipoRiscoOutro>
+ </tipoRisco>
+ </caracteristicasImovel>
+ <infraestrutura>
+ <redeDeAgua>Não</redeDeAgua>
+ <abastecimentoAguaPublicoPrivado>Privado</abastecimentoAguaPublicoPrivado>
+ <abastecimentoAgua>
+ <tipoAbastecimentoAgua>Não Informado</tipoAbastecimentoAgua>
+ </abastecimentoAgua>
+ <redeColetaEsgoto>Não Informado</redeColetaEsgoto>
+ <esgotamentoSanitario>Não Informado</esgotamentoSanitario>
+ <esgotamentoSanitario2>
+ <tipo>Não Informado</tipo>
+ </esgotamentoSanitario2>
+ <tipoLigacaoRedeEletrica>Não Informado</tipoLigacaoRedeEletrica>
+ <tipoPavimentacao>Não Informado</tipoPavimentacao>
+ <tipoDrenagem>
+ <galeriaSubterranea>true</galeriaSubterranea>
+ <sarjeta>true</sarjeta>
+ <cursoDaguaCanalizado>true</cursoDaguaCanalizado>
+ <cursoDaguaNaoCanalizado>true</cursoDaguaNaoCanalizado>
+ <outro>false</outro>
+ </tipoDrenagem>
+ <tipoServicoTelefonicoPredominante>Não Informado</tipoServicoTelefonicoPredominante>
+ <coletaSistematicaLixo>Não Informado</coletaSistematicaLixo>
+ <destinoLixo>
+ <sistemaColeta>true</sistemaColeta>
+ <sarjeta>true</sarjeta>
+ <cursoDaguaCanalizado>true</cursoDaguaCanalizado>
+ <cursoDaguaNaoCanalizado>true</cursoDaguaNaoCanalizado>
+ <outro>false</outro>
+ </destinoLixo>
+ </infraestrutura>
+</formCadernoA>
View
64 xsdForm.js
@@ -225,6 +225,7 @@ function generateFormFromNode(tagRaiz, xmlNode, namePattern) {
function generateXmlFromNode(odoc, namespace, tagRaiz, xmlNode, namePattern) {
var type = getValueAttributeByName(xmlNode, "type");
var minOccurs = getValueAttributeByName(xmlNode, "minOccurs");
+ var maxOccurs = getValueAttributeByName(xmlNode, "maxOccurs");
if (minOccurs == null) {minOccurs = 1}
if (type != null && static_type(type)) {
// pre-defined types
@@ -244,14 +245,14 @@ function generateXmlFromNode(odoc, namespace, tagRaiz, xmlNode, namePattern) {
var inner = tagRaiz.childNodes[i];
if (inner.nodeType == 1 && inner.nodeName == 'xs:complexType' &&
getValueAttributeByName(inner, "name") == type) {
- return generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, inner, namePattern, getValueAttributeByName(xmlNode, "name"));
+ return generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, inner, namePattern, getValueAttributeByName(xmlNode, "name"), minOccurs, maxOccurs);
}
}
} else {
// inline type definition
for (var i = 0; i < xmlNode.childNodes.length; i++) {
if (xmlNode.childNodes[i].nodeType == 1 && xmlNode.childNodes[i].nodeName == 'xs:complexType') {
- return generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, xmlNode.childNodes[i], namePattern, getValueAttributeByName(xmlNode, "name"));
+ return generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, xmlNode.childNodes[i], namePattern, getValueAttributeByName(xmlNode, "name"), minOccurs, maxOccurs);
} else if (xmlNode.childNodes[i].nodeType == 1 && xmlNode.childNodes[i].nodeName == 'xs:simpleType') {
return generateXmlFromSimpleTypeNode(odoc, namespace, tagRaiz, namePattern, getValueAttributeByName(xmlNode, "name"), minOccurs);
}
@@ -299,6 +300,11 @@ function generateFormFromComplexTypeNode(tagRaiz, xmlNode, namePattern, name, la
}
}
+ var getCurrentCount = function() {
+ return currentCount;
+ }
+ divRepeat.xsdFormCurrentCount = getCurrentCount;
+
var onclickAdd = function() {
if (maxOccurs == "unbounded" || currentCount < maxOccurs) {
var html = generateFormFromComplexTypeNodeNoRepeat(tagRaiz, xmlNode, namePattern+"__"+currentCount, name, "Item "+(currentCount+1));
@@ -308,6 +314,7 @@ function generateFormFromComplexTypeNode(tagRaiz, xmlNode, namePattern, name, la
refreshEnableDisable();
}
buttonAdd.onclick = onclickAdd;
+ divRepeat.addRepeat = onclickAdd;
var onclickDel = function() {
if (currentCount > minOccurs) {
@@ -317,6 +324,7 @@ function generateFormFromComplexTypeNode(tagRaiz, xmlNode, namePattern, name, la
refreshEnableDisable();
}
buttonDel.onclick = onclickDel;
+ divRepeat.delRepeat = onclickDel;
for (var i = 0; i < minOccurs; i++) {
onclickAdd();
@@ -361,7 +369,22 @@ function generateFormFromComplexTypeNodeNoRepeat(tagRaiz, xmlNode, namePattern,
}
-function generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, xmlNode, namePattern, name) {
+function generateXmlFromComplexTypeNode(odoc, namespace, tagRaiz, xmlNode, namePattern, name, minOccurs, maxOccurs) {
+ if (maxOccurs != null && maxOccurs != "1") {
+ var id = namePattern+"__"+name;
+ var divRepeat = getById(id);
+ var count = divRepeat.xsdFormCurrentCount();
+ var frag = odoc.createDocumentFragment();
+ for (var i = 0; i < count; i++) {
+ frag.appendChild(generateXmlFromComplexTypeNodeNoRepeat(odoc, namespace, tagRaiz, xmlNode, namePattern+"__"+i, name));
+ }
+ return frag;
+ } else {
+ return generateXmlFromComplexTypeNodeNoRepeat(odoc, namespace, tagRaiz, xmlNode, namePattern, name);
+ }
+}
+
+function generateXmlFromComplexTypeNodeNoRepeat(odoc, namespace, tagRaiz, xmlNode, namePattern, name) {
// gerar o fieldset com o legend e os conteudos...
var tag = odoc.createElementNS(namespace, name);
@@ -771,10 +794,36 @@ function getFormFromNode(namePattern, xml) {
for (var i = 0; i < xml.childNodes.length; i++) {
xmlNode = xml;
if (xml.childNodes[i].nodeType == 1) {
- xmlNode = getNodeByTagName(xmlNode,xml.childNodes[i].nodeName);
+ xmlNode = xml.childNodes[i];
name = namePattern + "__" + xmlNode.nodeName;
- if (xmlNode.childNodes.length > 1) {
- getFormFromNode(name, xmlNode);
+
+ if (xmlNode.childNodes.length > 1 ||
+ (xmlNode.childNodes.length == 1 &&
+ xmlNode.childNodes[0].nodeType == 1)) {
+ var fieldset = getById(name);
+ var mycounter = 0;
+ if(fieldset.getAttribute('class')=="xsdForm__repeat") {
+ var j = i;
+ for (j = i; j < xml.childNodes.length; j++) {
+ if (xml.childNodes[j].nodeType == 1) {
+ if (xml.childNodes[j].nodeName == xmlNode.nodeName) {
+ if (mycounter >= fieldset.xsdFormCurrentCount()) {
+ fieldset.addRepeat();
+ }
+ getFormFromNode(namePattern+"__"+mycounter+"__"+xmlNode.nodeName, xml.childNodes[j]);
+ mycounter++;
+ } else {
+ i = j - 1;
+ break;
+ }
+ }
+ }
+ if (j == xml.childNodes.length) {
+ i = j;
+ }
+ } else {
+ getFormFromNode(name, xmlNode);
+ }
} else if (xmlNode.childNodes.length == 1) {
insereValor(name,getText(xmlNode));
}
@@ -783,10 +832,11 @@ function getFormFromNode(namePattern, xml) {
}
function insereValor(nameField,valor) {
+
if ((getById(nameField).nodeName == "INPUT" && getById(nameField).type == "text") || getById(nameField).nodeName == "SELECT" || getById(nameField).nodeName == "TEXTAREA") {
getById(nameField).value = valor;
} else if (getById(nameField).type == "checkbox") {
- if (valor == 1) {
+ if (valor == 1 || valor == "true") {
getById(nameField).checked = true;
} else {
getById(nameField).checked = false;

0 comments on commit 6ff0568

Please sign in to comment.