Archive for mars, 2010
V 0.3
by admin on mar.01, 2010, under Non classé
I had more time those last days to take account of this project.
Working on it I just realize that this simple work of traduction is not just as simple as it looks.
The problem comes most in the fact that JAVA and Javascript are really different languages in the way most developpers are using them.
When I talk using Javascript as an object language with my foreigners, they are just looking at me like saying « What a fool » ^^.
And I must admit that it doesn’ help…
Well this is the advanced version of this translation work :
function XMLElement(inFullName, inNamespace, inSystemID, inLineNr){ // No line number defined var NO_LINE = -1; //The parent element. var parent = new Array(); // The attributes of the element. var attributes = new Array(); // The child elements. var children = new Array(); //The name of the element. var name = null; // The full name of the element. var fullName = null; // The namespace URI. var namespace = null; // The content of the element. var content = null; // The system ID of the source data where this element is located. var systemID = null; // The line in the source data where this element starts. var lineNr = null; //Checks the attribute value function prCheckValue(inAttribute, type){ var result = false; if(inAttribute != null && typeof inAttribute == type){ result = true; } return result; } // Sets the fullName attribute function prSetFullName(inFullName){ if(prCheckValue(inFullName, "string")){ fullName = inFullName; } } // Sets the name space attribute function prSetNameSpace(inNamespace){ if(prCheckValue(inNamespace, "string")){ namespace = inNamespace; } } // Sets the system ID attribute function prSetSystemID(inSystemID){ if(prCheckValue(inSystemID, "string")){ systemID = inSystemID; } } // Sets the line number function prSetLineNr(inLineNr){ if(prCheckValue(inLineNr, "number")){ lineNr = inLineNr; } } // Sets the line number function prSetName(inName){ if(prCheckValue(inName, "string")){ name = inName; } } // Sets the content function prSetContent(inContent){ if(prCheckValue(inContent, "string")){ content = inContent; } } // Sets the parent function prSetParent(inParent){ if(prCheckValue(inParent, "XMLElement")){ parent = inParent; } } //Init method with 4 arguments function prInitXmlElement(inFullName, inNameSpace, inSystemID, inLineNr){ //attributes = new Array(); //children = new Array(); prSetFullName(inFullName); if(inNameSpace == null){ prSetName(inFullName); }else{ var index = inFullName.indexOf(':'); if (index >= 0) { prSetName(fullName.substring(index + 1)); } else { prSetName(fullName); } } prSetNameSpace(inNameSpace); prSetLineNr(inLineNr); prSetSystemID(inSystemID); } // Manage the number of parameters given switch(arguments.length){ case 0: prInitXmlElement("", "", "", NO_LINE); break; case 4: prInitXmlElement(inFullName, inNamespace, inSystemID, inLineNr); break; } // Creates an element to be used for #PCDATA content. this.createPCDataElement = function createPCDataElement(){ return new XMLElement(); } // Creates an empty element. //@param fullName the full name of the element // @param namespace the namespace URI. // this.createElement = function createElement( fullName, namespace) { //return new XMLElement(fullName, namespace); return new XMLElement(fullName, namespace, "", NO_LINE); } /** * Returns the parent element. This method returns null for the root * element. */ this.getParent = function getParent() { return parent; } /** * Returns the full name (i.e. the name including an eventual namespace * prefix) of the element. * * @return the name, or null if the element only contains #PCDATA. */ this.getName = function getName() { return fullName; } /** * Returns the name of the element. * * @return the name, or null if the element only contains #PCDATA. */ this.getLocalName = function getLocalName() { return name; } /** * Returns the namespace of the element. * * @return the namespace, or null if no namespace is associated with the * element. */ this.getNamespace = function getNamespace() { return namespace; } /** * Sets the full name. This method also sets the short name and clears the * namespace URI. * */ this.setName = function setName(inFullName, inNameSpace) { switch(arguments.length){ case 1 : prSetName(inFullName); prSetFullName(inFullName); prSetNameSpace(null); break; case 2 : var index = inFullName.indexOf(':'); if ((inNameSpace == null) || (index < 0)) { prSetName(inFullName); } else { prSetName(inFullName.substring(index + 1)); } prSetFullName(inFullName); prSetNameSpace(inNameSpace); break; } } /** * Adds a child element. * * @param child the non-null child to add. */ this.addChild = function addChild(child){ if(child == null){ alert("child must not be null"); } if(child.getLocalName() == null && children.length > 0){ var lastChild = children[children.length-1]; if(lastChild.getLocalName() == null){ lastChild.setContent(lastChild.getContent() + child.getContent()); return; } } child.setParent(this); } /** *Sets the parent */ this.setParent = function setParent(inParent){ prSetParent(inParent); } /** *Gets the parent * */ this.getParent = function getParent(){ return parent; } /** * Return the #PCDATA content of the element. If the element has a * combination of #PCDATA content and child elements, the #PCDATA * sections can be retrieved as unnamed child objects. In this case, * this method returns null. * * @return the content. */ this.getContent = function getContent() { return content; } /** * Sets the #PCDATA content. It is an error to call this method with a * non-null value if there are child objects. * * @param content the (possibly null) content. */ this.setContent = function setContent(String inContent) { prSetContent(inContent); } } // tests var xmlElement = new XMLElement("fullName", "nameSpace", "SystemId", 5); var xml1 = xmlElement.createPCDataElement(); var xml2 = xmlElement.createElement("fullNameXml1", "namespaceXml1"); xmlElement.setName("NameTOTO"); alert(xmlElement.getName()); alert(xmlElement.getLocalName()); alert(xmlElement.getNamespace()); xmlElement.setName("NameTOTO2", "totallySpicy"); alert(xmlElement.getName()); alert(xmlElement.getLocalName()); alert(xmlElement.getNamespace()); xmlElement.addChild(new XMLElement());
I hope that when this will be over I will have time to make some demos of the js processing possibilities.