comete_blog

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.

Leave a Comment more...

0.2 version

by admin on fév.02, 2010, under Non classé

The actual 0.2 version of the project is not situated in the state of the code but in the understanding of the all real XmlElement.java class.

Taking account of exams, other projects and enterprise work and my memory, this should be done within the 2 next week if god helps (him or anyone else).

The actual problem is to allow several constructors because if we take a look at the java file, this one has several. And basically this is not possible in javascript. So i had the iddea of counting each parameter and define an init method that will take care of the several possibilities.

There is the REAL java class that must be translated and studied :

/* XMLElement.java                                                 NanoXML/Java
 *
 * This file is part of NanoXML 2 for Java.
 * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 */

package processing.xml;

import java.io.*;
import java.util.*;

import processing.core.PApplet;

/**
 * XMLElement is an XML element. This is the base class used for the
 * Processing XML library, representing a single node of an XML tree.
 *
 * This code is based on a modified version of NanoXML by Marc De Scheemaecker.
 *
 * @author Marc De Scheemaecker
 * @author processing.org
 */
public class XMLElement implements Serializable {

    /**
     * No line number defined.
     */
    public static final int NO_LINE = -1;

    /**
     * The parent element.
     */
    private XMLElement parent;

    /**
     * The attributes of the element.
     */
    private Vector attributes;

    /**
     * The child elements.
     */
    private Vector children;

    /**
     * The name of the element.
     */
    private String name;

    /**
     * The full name of the element.
     */
    private String fullName;

    /**
     * The namespace URI.
     */
    private String namespace;

    /**
     * The content of the element.
     */
    private String content;

    /**
     * The system ID of the source data where this element is located.
     */
    private String systemID;

    /**
     * The line in the source data where this element starts.
     */
    private int lineNr;

    /**
     * Creates an empty element to be used for #PCDATA content.
     */
    public XMLElement() {
        this(null, null, null, NO_LINE);
    }

    protected void set(String fullName,
                           String namespace,
                           String systemID,
                           int lineNr) {
        this.fullName = fullName;
        if (namespace == null) {
                this.name = fullName;
        } else {
                int index = fullName.indexOf(':');
                if (index >= 0) {
                        this.name = fullName.substring(index + 1);
                } else {
                        this.name = fullName;
                }
        }
        this.namespace = namespace;
        this.lineNr = lineNr;
        this.systemID = systemID;
    }

    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     */
//    public XMLElement(String fullName) {
//        this(fullName, null, null, NO_LINE);
//    }

    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     * @param systemID the system ID of the XML data where the element starts.
     * @param lineNr   the line in the XML data where the element starts.
     */
//    public XMLElement(String fullName,
//                      String systemID,
//                      int    lineNr) {
//        this(fullName, null, systemID, lineNr);
//    }

    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     */
//    public XMLElement(String fullName,
//                      String namespace) {
//        this(fullName, namespace, null, NO_LINE);
//    }

    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     * @param systemID  the system ID of the XML data where the element starts.
     * @param lineNr    the line in the XML data where the element starts.
     */
    public XMLElement(String fullName,
                      String namespace,
                      String systemID,
                      int    lineNr) {
        this.attributes = new Vector();
        this.children = new Vector(8);
        this.fullName = fullName;
        if (namespace == null) {
            this.name = fullName;
        } else {
            int index = fullName.indexOf(':');
            if (index >= 0) {
                this.name = fullName.substring(index + 1);
            } else {
                this.name = fullName;
            }
        }
        this.namespace = namespace;
        this.content = null;
        this.lineNr = lineNr;
        this.systemID = systemID;
        this.parent = null;
    }

    /**
     * Begin parsing XML data passed in from a PApplet. This code
     * wraps exception handling, for more advanced exception handling,
     * use the constructor that takes a Reader or InputStream.
     * @author processing.org
     * @param filename
     * @param parent
     */
    public XMLElement(PApplet parent, String filename) {
        this();
        parseFromReader(parent.createReader(filename));
    }

    public XMLElement(Reader r) {
        this();
        parseFromReader(r);
    }

    public XMLElement(String xml) {
        this();
        parseFromReader(new StringReader(xml));
    }

    protected void parseFromReader(Reader r) {
        try {
            StdXMLParser parser = new StdXMLParser();
            parser.setBuilder(new StdXMLBuilder(this));
            parser.setValidator(new XMLValidator());
            parser.setReader(new StdXMLReader(r));
            //System.out.println(parser.parse().getName());
            /*XMLElement xm = (XMLElement)*/ parser.parse();
            //System.out.println("xm name is " + xm.getName());
            //System.out.println(xm + " " + this);
            //parser.parse();
        } catch (XMLException e) {
            e.printStackTrace();
        }
    }

//    static public XMLElement parse(Reader r) {
//        try {
//              StdXMLParser parser = new StdXMLParser();
//              parser.setBuilder(new StdXMLBuilder());
//              parser.setValidator(new XMLValidator());
//              parser.setReader(new StdXMLReader(r));
//              return (XMLElement) parser.parse();
//              } catch (XMLException e) {
//                      e.printStackTrace();
//                      return null;
//              }
//    }

    /**
     * Creates an element to be used for #PCDATA content.
     */
    public XMLElement createPCDataElement() {
        return new XMLElement();
    }

    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     */
//    public XMLElement createElement(String fullName) {
//        return new XMLElement(fullName);
//    }

    /**
     * Creates an empty element.
     *
     * @param fullName the name of the element.
     * @param systemID the system ID of the XML data where the element starts.
     * @param lineNr   the line in the XML data where the element starts.
     */
//    public XMLElement createElement(String fullName,
//                                    String systemID,
//                                    int lineNr) {
//        //return new XMLElement(fullName, systemID, lineNr);
//      return new XMLElement(fullName, null, systemID, lineNr);
//    }

    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     */
    public XMLElement createElement(String fullName,
                                    String namespace) {
        //return new XMLElement(fullName, namespace);
        return new XMLElement(fullName, namespace, null, NO_LINE);
    }

    /**
     * Creates an empty element.
     *
     * @param fullName  the full name of the element
     * @param namespace the namespace URI.
     * @param systemID  the system ID of the XML data where the element starts.
     * @param lineNr    the line in the XML data where the element starts.
     */
    public XMLElement createElement(String fullName,
                                     String namespace,
                                     String systemID,
                                     int    lineNr) {
        return new XMLElement(fullName, namespace, systemID, lineNr);
    }

    /**
     * Cleans up the object when it's destroyed.
     */
    protected void finalize() throws Throwable {
        this.attributes.clear();
        this.attributes = null;
        this.children = null;
        this.fullName = null;
        this.name = null;
        this.namespace = null;
        this.content = null;
        this.systemID = null;
        this.parent = null;
        super.finalize();
    }

    /**
     * Returns the parent element. This method returns null for the root
     * element.
     */
    public XMLElement getParent() {
        return this.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.
     */
    public String getName() {
        return this.fullName;
    }

    /**
     * Returns the name of the element.
     *
     * @return the name, or null if the element only contains #PCDATA.
     */
    public String getLocalName() {
        return this.name;
    }

    /**
     * Returns the namespace of the element.
     *
     * @return the namespace, or null if no namespace is associated with the
     *         element.
     */
    public String getNamespace() {
        return this.namespace;
    }

    /**
     * Sets the full name. This method also sets the short name and clears the
     * namespace URI.
     *
     * @param name the non-null name.
     */
    public void setName(String name) {
        this.name = name;
        this.fullName = name;
        this.namespace = null;
    }

    /**
     * Sets the name.
     *
     * @param fullName  the non-null full name.
     * @param namespace the namespace URI, which may be null.
     */
    public void setName(String fullName, String namespace) {
        int index = fullName.indexOf(':');
        if ((namespace == null) || (index < 0)) {
            this.name = fullName;
        } else {
            this.name = fullName.substring(index + 1);
        }
        this.fullName = fullName;
        this.namespace = namespace;
    }

    /**
     * Adds a child element.
     *
     * @param child the non-null child to add.
     */
    public void addChild(XMLElement child) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        if ((child.getLocalName() == null) && (! this.children.isEmpty())) {
            XMLElement lastChild = (XMLElement) this.children.lastElement();

            if (lastChild.getLocalName() == null) {
                lastChild.setContent(lastChild.getContent()
                                     + child.getContent());
                return;
            }
        }
        ((XMLElement)child).parent = this;
        this.children.addElement(child);
    }

    /**
     * Inserts a child element.
     *
     * @param child the non-null child to add.
     * @param index where to put the child.
     */
    public void insertChild(XMLElement child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        if ((child.getLocalName() == null) && (! this.children.isEmpty())) {
            XMLElement lastChild = (XMLElement) this.children.lastElement();
            if (lastChild.getLocalName() == null) {
                lastChild.setContent(lastChild.getContent()
                                     + child.getContent());
                return;
            }
        }
        ((XMLElement) child).parent = this;
        this.children.insertElementAt(child, index);
    }

    /**
     * Removes a child element.
     *
     * @param child the non-null child to remove.
     */
    public void removeChild(XMLElement child) {
        if (child == null) {
            throw new IllegalArgumentException("child must not be null");
        }
        this.children.removeElement(child);
    }

    /**
     * Removes the child located at a certain index.
     *
     * @param index the index of the child, where the first child has index 0.
     */
    public void removeChildAtIndex(int index) {
        this.children.removeElementAt(index);
    }

    /**
     * Returns an enumeration of all child elements.
     *
     * @return the non-null enumeration
     */
    public Enumeration enumerateChildren() {
        return this.children.elements();
    }

    /**
     * Returns whether the element is a leaf element.
     *
     * @return true if the element has no children.
     */
    public boolean isLeaf() {
        return this.children.isEmpty();
    }

    /**
     * Returns whether the element has children.
     *
     * @return true if the element has children.
     */
    public boolean hasChildren() {
        return (! this.children.isEmpty());
    }

    /**
     * Returns the number of children.
     *
     * @return the count.
     */
    public int getChildCount() {
        return this.children.size();
    }

    /**
     * Returns a vector containing all the child elements.
     *
     * @return the vector.
     */
//    public Vector getChildren() {
//        return this.children;
//    }

    /**
     * Returns an array containing all the child elements.
     */
    public XMLElement[] getChildren() {
        int childCount = getChildCount();
        XMLElement[] kids = new XMLElement[childCount];
        children.copyInto(kids);
        return kids;
    }

    /**
     * Quick accessor for an element at a particular index.
     * @author processing.org
     */
    public XMLElement getChild(int which) {
        return (XMLElement) children.elementAt(which);
    }

    /**
     * Get a child by its name or path.
     * @param name element name or path/to/element
     * @return the element
     * @author processing.org
     */
    public XMLElement getChild(String name) {
        if (name.indexOf('/') != -1) {
            return getChildRecursive(PApplet.split(name, '/'), 0);
        }
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            XMLElement kid = getChild(i);
            if (kid.getName().equals(name)) {
                return kid;
            }
        }
        return null;
    }

    /**
     * Internal helper function for getChild(String).
     * @param items result of splitting the query on slashes
     * @param offset where in the items[] array we're currently looking
     * @return matching element or null if no match
     * @author processing.org
     */
    protected XMLElement getChildRecursive(String[] items, int offset) {
        // if it's a number, do an index instead
        if (Character.isDigit(items[offset].charAt(0))) {
            XMLElement kid = getChild(Integer.parseInt(items[offset]));
            if (offset == items.length-1) {
                return kid;
            } else {
                return kid.getChildRecursive(items, offset+1);
            }
        }
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            XMLElement kid = getChild(i);
            if (kid.getName().equals(items[offset])) {
                if (offset == items.length-1) {
                    return kid;
                } else {
                    return kid.getChildRecursive(items, offset+1);
                }
            }
        }
        return null;
    }

    /**
     * Returns the child at a specific index.
     *
     * @param index the index of the child
     *
     * @return the non-null child
     *
     * @throws java.lang.ArrayIndexOutOfBoundsException
     *          if the index is out of bounds.
     */
    public XMLElement getChildAtIndex(int index)
    throws ArrayIndexOutOfBoundsException {
        return (XMLElement) this.children.elementAt(index);
    }

    /**
     * Searches a child element.
     *
     * @param name the full name of the child to search for.
     *
     * @return the child element, or null if no such child was found.
     */
//    public XMLElement getFirstChildNamed(String name) {
//        Enumeration enum = this.children.elements();
//        while (enum.hasMoreElements()) {
//            XMLElement child = (XMLElement) enum.nextElement();
//            String childName = child.getFullName();
//            if ((childName != null) && childName.equals(name)) {
//                return child;
//            }
//        }
//        return null;
//    }

    /**
     * Searches a child element.
     *
     * @param name      the name of the child to search for.
     * @param namespace the namespace, which may be null.
     *
     * @return the child element, or null if no such child was found.
     */
//    public XMLElement getFirstChildNamed(String name,
//                                          String namespace) {
//        Enumeration enum = this.children.elements();
//        while (enum.hasMoreElements()) {
//            XMLElement child = (XMLElement) enum.nextElement();
//            String str = child.getName();
//            boolean found = (str != null) && (str.equals(name));
//            str = child.getNamespace();
//            if (str == null) {
//                found &= (name == null);
//            } else {
//                found &= str.equals(namespace);
//            }
//            if (found) {
//                return child;
//            }
//        }
//        return null;
//    }

    /**
     * Get any children that match this name or path. Similar to getChild(),
     * but will grab multiple matches rather than only the first.
     * @param name element name or path/to/element
     * @return array of child elements that match
     * @author processing.org
     */
    public XMLElement[] getChildren(String name) {
        if (name.indexOf('/') != -1) {
                return getChildrenRecursive(PApplet.split(name, '/'), 0);
        }
        // if it's a number, do an index instead
        // (returns a single element array, since this will be a single match
        if (Character.isDigit(name.charAt(0))) {
                return new XMLElement[] { getChild(Integer.parseInt(name)) };
        }
        int childCount = getChildCount();
        XMLElement[] matches = new XMLElement[childCount];
        int matchCount = 0;
        for (int i = 0; i < childCount; i++) {
                XMLElement kid = getChild(i);
                if (kid.getName().equals(name)) {
                        matches[matchCount++] = kid;
                }
        }
        return (XMLElement[]) PApplet.subset(matches, 0, matchCount);
    }

    protected XMLElement[] getChildrenRecursive(String[] items, int offset) {
        if (offset == items.length-1) {
                return getChildren(items[offset]);
        }
        XMLElement[] matches = getChildren(items[offset]);
        XMLElement[] outgoing = new XMLElement[0];
        for (int i = 0; i < matches.length; i++) {
                XMLElement[] kidMatches = matches[i].getChildrenRecursive(items, offset+1);
                outgoing = (XMLElement[]) PApplet.concat(outgoing, kidMatches);
        }
        return outgoing;
    }

    /**
     * Returns a vector of all child elements named name.
     *
     * @param name the full name of the children to search for.
     *
     * @return the non-null vector of child elements.
     */
//    public Vector getChildrenNamed(String name) {
//        Vector result = new Vector(this.children.size());
//        Enumeration enum = this.children.elements();
//        while (enum.hasMoreElements()) {
//            XMLElement child = (XMLElement) enum.nextElement();
//            String childName = child.getFullName();
//            if ((childName != null) && childName.equals(name)) {
//                result.addElement(child);
//            }
//        }
//        return result;
//    }

    /**
     * Returns a vector of all child elements named name.
     *
     * @param name      the name of the children to search for.
     * @param namespace the namespace, which may be null.
     *
     * @return the non-null vector of child elements.
     */
//    public Vector getChildrenNamed(String name,
//                                   String namespace) {
//        Vector result = new Vector(this.children.size());
//        Enumeration enum = this.children.elements();
//        while (enum.hasMoreElements()) {
//            XMLElement child = (XMLElement) enum.nextElement();
//            String str = child.getName();
//            boolean found = (str != null) && (str.equals(name));
//            str = child.getNamespace();
//            if (str == null) {
//                found &= (name == null);
//            } else {
//                found &= str.equals(namespace);
//            }
//
//            if (found) {
//                result.addElement(child);
//            }
//        }
//        return result;
//    }

    /**
     * Searches an attribute.
     *
     * @param fullName the non-null full name of the attribute.
     *
     * @return the attribute, or null if the attribute does not exist.
     */
    private XMLAttribute findAttribute(String fullName) {
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            if (attr.getFullName().equals(fullName)) {
                return attr;
            }
        }
        return null;
    }

    /**
     * Searches an attribute.
     *
     * @param name the non-null short name of the attribute.
     * @param namespace the name space, which may be null.
     *
     * @return the attribute, or null if the attribute does not exist.
     */
    private XMLAttribute findAttribute(String name,
                                       String namespace) {
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            boolean found = attr.getName().equals(name);
            if (namespace == null) {
                found &= (attr.getNamespace() == null);
            } else {
                found &= namespace.equals(attr.getNamespace());
            }

            if (found) {
                return attr;
            }
        }
        return null;
    }

    /**
     * Returns the number of attributes.
     */
    public int getAttributeCount() {
        return this.attributes.size();
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     *
     * @return the value, or null if the attribute does not exist.
     */
    public String getAttribute(String name) {
        return this.getAttribute(name, null);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public String getAttribute(String name,
                               String defaultValue) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return defaultValue;
        } else {
            return attr.getValue();
        }
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public String getAttribute(String name,
                               String namespace,
                               String defaultValue) {
        XMLAttribute attr = this.findAttribute(name, namespace);
        if (attr == null) {
            return defaultValue;
        } else {
            return attr.getValue();
        }
    }

    public String getStringAttribute(String name) {
        return getAttribute(name);
    }

    public String getStringAttribute(String name, String defaultValue) {
        return getAttribute(name, defaultValue);
    }

    public String getStringAttribute(String name,
                                     String namespace,
                                     String defaultValue) {
        return getAttribute(name, namespace, defaultValue);
    }

    public int getIntAttribute(String name) {
        return getIntAttribute(name, 0);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public int getIntAttribute(String name,
                               int defaultValue) {
        String value = this.getAttribute(name, Integer.toString(defaultValue));
        return Integer.parseInt(value);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public int getIntAttribute(String name,
                               String namespace,
                               int defaultValue) {
        String value = this.getAttribute(name, namespace,
                                         Integer.toString(defaultValue));
        return Integer.parseInt(value);
    }

    public float getFloatAttribute(String name) {
        return getFloatAttribute(name, 0);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public float getFloatAttribute(String name,
                                   float defaultValue) {
        String value = this.getAttribute(name, Float.toString(defaultValue));
        return Float.parseFloat(value);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public float getFloatAttribute(String name,
                                   String namespace,
                                   float defaultValue) {
        String value = this.getAttribute(name, namespace,
                                         Float.toString(defaultValue));
        return Float.parseFloat(value);
    }

    public double getDoubleAttribute(String name) {
        return getDoubleAttribute(name, 0);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public double getDoubleAttribute(String name,
                                     double defaultValue) {
        String value = this.getAttribute(name, Double.toString(defaultValue));
        return Double.parseDouble(value);
    }

    /**
     * Returns the value of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     * @param defaultValue the default value of the attribute.
     *
     * @return the value, or defaultValue if the attribute does not exist.
     */
    public double getDoubleAttribute(String name,
                                     String namespace,
                                     double defaultValue) {
        String value = this.getAttribute(name, namespace,
                                         Double.toString(defaultValue));
        return Double.parseDouble(value);
    }

    /**
     * Returns the type of an attribute.
     *
     * @param name the non-null full name of the attribute.
     *
     * @return the type, or null if the attribute does not exist.
     */
    public String getAttributeType(String name) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return null;
        } else {
            return attr.getType();
        }
    }

    /**
     * Returns the namespace of an attribute.
     *
     * @param name the non-null full name of the attribute.
     *
     * @return the namespace, or null if there is none associated.
     */
    public String getAttributeNamespace(String name) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            return null;
        } else {
            return attr.getNamespace();
        }
    }

    /**
     * Returns the type of an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI, which may be null.
     *
     * @return the type, or null if the attribute does not exist.
     */
    public String getAttributeType(String name,
                                   String namespace) {
        XMLAttribute attr = this.findAttribute(name, namespace);
        if (attr == null) {
            return null;
        } else {
            return attr.getType();
        }
    }

    /**
     * Sets an attribute.
     *
     * @param name the non-null full name of the attribute.
     * @param value the non-null value of the attribute.
     */
    public void setAttribute(String name,
                             String value) {
        XMLAttribute attr = this.findAttribute(name);
        if (attr == null) {
            attr = new XMLAttribute(name, name, null, value, "CDATA");
            this.attributes.addElement(attr);
        } else {
            attr.setValue(value);
        }
    }

    /**
     * Sets an attribute.
     *
     * @param fullName the non-null full name of the attribute.
     * @param namespace the namespace URI of the attribute, which may be null.
     * @param value the non-null value of the attribute.
     */
    public void setAttribute(String fullName,
                             String namespace,
                             String value) {
        int index = fullName.indexOf(':');
        String vorname = fullName.substring(index + 1);
        XMLAttribute attr = this.findAttribute(vorname, namespace);
        if (attr == null) {
            attr = new XMLAttribute(fullName, vorname, namespace, value, "CDATA");
            this.attributes.addElement(attr);
        } else {
            attr.setValue(value);
        }
    }

    /**
     * Removes an attribute.
     *
     * @param name the non-null name of the attribute.
     */
    public void removeAttribute(String name) {
        for (int i = 0; i < this.attributes.size(); i++) {
            XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i);
            if (attr.getFullName().equals(name)) {
                this.attributes.removeElementAt(i);
                return;
            }
        }
    }

    /**
     * Removes an attribute.
     *
     * @param name the non-null name of the attribute.
     * @param namespace the namespace URI of the attribute, which may be null.
     */
    public void removeAttribute(String name,
                                String namespace) {
        for (int i = 0; i < this.attributes.size(); i++) {
            XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i);
            boolean found = attr.getName().equals(name);
            if (namespace == null) {
                found &= (attr.getNamespace() == null);
            } else {
                found &= attr.getNamespace().equals(namespace);
            }

            if (found) {
                this.attributes.removeElementAt(i);
                return;
            }
        }
    }

    /**
     * Returns an enumeration of all attribute names.
     *
     * @return the non-null enumeration.
     */
    public Enumeration enumerateAttributeNames() {
        Vector result = new Vector();
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            result.addElement(attr.getFullName());
        }
        return result.elements();
    }

    /**
     * Returns whether an attribute exists.
     *
     * @return true if the attribute exists.
     */
    public boolean hasAttribute(String name) {
        return this.findAttribute(name) != null;
    }

    /**
     * Returns whether an attribute exists.
     *
     * @return true if the attribute exists.
     */
    public boolean hasAttribute(String name,
                                String namespace) {
        return this.findAttribute(name, namespace) != null;
    }

    /**
     * Returns all attributes as a Properties object.
     *
     * @return the non-null set.
     */
    public Properties getAttributes() {
        Properties result = new Properties();
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            result.put(attr.getFullName(), attr.getValue());
        }
        return result;
    }

    /**
     * Returns all attributes in a specific namespace as a Properties object.
     *
     * @param namespace the namespace URI of the attributes, which may be null.
     *
     * @return the non-null set.
     */
    public Properties getAttributesInNamespace(String namespace) {
        Properties result = new Properties();
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            if (namespace == null) {
                if (attr.getNamespace() == null) {
                    result.put(attr.getName(), attr.getValue());
                }
            } else {
                if (namespace.equals(attr.getNamespace())) {
                    result.put(attr.getName(), attr.getValue());
                }
            }
        }
        return result;
    }

    /**
     * Returns the system ID of the data where the element started.
     *
     * @return the system ID, or null if unknown.
     *
     * @see #getLineNr
     */
    public String getSystemID() {
        return this.systemID;
    }

    /**
     * Returns the line number in the data where the element started.
     *
     * @return the line number, or NO_LINE if unknown.
     *
     * @see #NO_LINE
     * @see #getSystemID
     */
    public int getLineNr() {
        return this.lineNr;
    }

    /**
     * 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.
     */
    public String getContent() {
        return this.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.
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Returns true if the element equals another element.
     *
     * @param rawElement the element to compare to
     */
    public boolean equals(Object rawElement) {
        try {
            return this.equalsXMLElement((XMLElement) rawElement);
        } catch (ClassCastException e) {
            return false;
        }
    }

    /**
     * Returns true if the element equals another element.
     *
     * @param rawElement the element to compare to
     */
    public boolean equalsXMLElement(XMLElement rawElement) {
        if (! this.name.equals(rawElement.getLocalName())) {
            return false;
        }
        if (this.attributes.size() != rawElement.getAttributeCount()) {
            return false;
        }
        Enumeration en = this.attributes.elements();
        while (en.hasMoreElements()) {
            XMLAttribute attr = (XMLAttribute) en.nextElement();
            if (! rawElement.hasAttribute(attr.getName(), attr.getNamespace())) {
                return false;
            }
            String value = rawElement.getAttribute(attr.getName(),
                                            attr.getNamespace(),
                                            null);
            if (! attr.getValue().equals(value)) {
                return false;
            }
            String type = rawElement.getAttributeType(attr.getName(),
                                               attr.getNamespace());
            if (! attr.getType().equals(type)) {
                return false;
            }
        }
        if (this.children.size() != rawElement.getChildCount()) {
            return false;
        }
        for (int i = 0; i < this.children.size(); i++) {
            XMLElement child1 = this.getChildAtIndex(i);
            XMLElement child2 = rawElement.getChildAtIndex(i);

            if (! child1.equalsXMLElement(child2)) {
                return false;
            }
        }
        return true;
    }

    public String toString() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos);
        XMLWriter writer = new XMLWriter(osw);
        try {
            writer.write(this, true, 2, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toString();
    }
}

And there is the actual state of this javascript translation :

		function XMLElement(inFullName, inNamespace, inSystemID, inLineNr){

// No line number defined
var NO_LINE = -1;
//The parent element.
var parent;
// The attributes of the element.
var attributes;
// The child elements.
var children;
//The name of the element.
var name;
// The full name of the element.
var fullName;
// The namespace URI.
var namespace;
// The content of the element.
var content;
// The system ID of the source data where this element is located.
var systemID;
// The line in the source data where this element starts.
var lineNr;

//Init method with 4 arguments
function initXmlElement(inFullName, inNamespace, inSystemID, inLineNr){
}
//Init method with 3 arguments
function initXmlElement(inFullName, inNamespace, inSystemID){
}
//The number of arguments given. Use to determine the initMethod to call
var argsNumber = XMLElement.arguments.length;
switch(argsNumber){
case (4):
initXmlElement(inFullName, inNamespace, inSystemID, inLineNr);
break;
}
alert("toto");
}

var xmlElement = new XMLElement(this, "sites.xml", "", "");

Leave a Comment more...

Problem in implementation

by admin on jan.18, 2010, under Non classé

Last week I talk took Dabid Humphrey.

I wanted to ask him if we could just implement some public members in the javascript class.
I asked this after having taking a look at the XmlElement.java.

Me and RaphKun saw that the implemented class did not match with the interface define in documentation.

So after discussing with David we agreed that the javascript implementation should follow the java implementation.

The problem is that now we have to reimplement what we did.
But this time we know how to implement a javascript object class. Wich was the ardest part for me ^^.

Leave a Comment more...

Version 0.1

by admin on jan.04, 2010, under Non classé

There is the 0.1 version.


//XMLElement constructor
function XMLElement(parent, file){
// The parent of the XMLELement
this.parent = parent;
// The file of the XMLELement
this.file = file;
// The XML document
this.xmlDoc = null;
// The ChildNodes of The XML Elements
this.childNodes = null;
// The name of the XMLELement
this.name = null;
// The content of the XMLElement
this.content = null;
// The file has to be loaded for the first XMLElement only
if(file != null){
try{
this.xmlDoc = document.implementation.createDocument("","",null);
this.xmlDoc.async = false;
this.xmlDoc.load(file);
}
catch(e){
alert(e.message);
return;
}
}
//This function has for goal to avoïd the text element in child Nodes. Only Node Element are managed
function getFilteredChildNodes(childNodes){
var result = new Array();
for(var i = 0; i < childNodes.length; i ++){
if(childNodes[i].nodeType == 1 ){
result[i] = childNodes[i];
}
}
return result;
};
//This function creates is used by methods getChild(), and getChildren()
function createXMLElement(childNode){
var result = new XMLElement(this, this.file);
result.xmlDoc = this.xmlDoc;
// On creation ChildNodes are always filtered
result.childNodes = getFilteredChildNodes(childNode.childNodes);
result.name = childNode.nodeName;
return result;
};
//This function has for goal to check that the given index is correct for a childNodeArray
function checkIndexValidity(childNodes, index){
if(index >= childNodes.length || index < 0){
throw "The given index is not valid and is not between 0 and childNodes.length";
}
};
//Check if the childNodes attribute is null, if it is null it means that the current element is the root element.
// So the array must be filtered.
function getFilteredChildNodes(childNodes, xmlDoc){
if(childNodes == null){
return getFilteredChildNodes(xmlDoc.childNodes);
}else{
return childNodes;
}
};
//This method must return the number of children of the element
this.getChildCount = function getChildCount(){
return getFilteredChildNodes(this.childNodes, this.xmlDoc).length;
};
//This method must return the child at the given index.
this.getChild = function getChild(index){
var child = getFilteredChildNodes(this.childNodes, this.xmlDoc)[index];
var result = createXMLElement(child);
return result;
};
// Returns all of the children as an XMLElement array
this.getChildren = function getChildren(){
var result = new Array();
var childNodesArray = getFilteredChildNodes(this.childNodes, this.xmlDoc);
for(var i = 0; i < childNodesArray.length; i ++){
var child = createXMLElement(childNodesArray[i]);
result.push(child);
}
return result;
}
// Returns the content of the element
this.getContent = function getContent(){

}
};

function setup(){
var xmlElement = new XMLElement(this, "sites.xml");
var childCount = xmlElement.getChildCount();
var childName = xmlElement.getChild(0).getChild(1).name;
var children = xmlElement.getChild(0).getChildren();
var stop;
};
setup();

///////////////////*************************************************************************
function Personne(){
this.call = function call(){
alert("yo");
};
};
var personne = new Personne();
personne.call();
///////////////////*************************************************************************
Leave a Comment more...

by admin on jan.03, 2010, under Non classé

There is the new version of the xmlElement. This time oriented object javascript has been really used.

There are actually 3 methods that miss :


//XMLElement constructor
function XMLElement(parent, file){
// The parent of the XMLELement
this.parent = parent;
// The file of the XMLELement
this.file = file;
// The XML document
this.xmlDoc = null;
// The ChildNodes of The XML Elements
this.childNodes = null;
// The name of the XMLELement
this.name = null;
// The file has to be loaded for the first XMLElement only
if(file != null){
try{
this.xmlDoc = document.implementation.createDocument("","",null);
this.xmlDoc.async = false;
this.xmlDoc.load(file);
}
catch(e){
alert(e.message);
return;
}
}
//This function has for goal to avoïd the text element in child Nodes. Only Node Element are managed
function getFilteredChildNodes(childNodes){
var result = new Array();
for(var i = 0; i < childNodes.length; i ++){
if(childNodes[i].nodeType == 1 ){
result[i] = childNodes[i];
}
if(childNodes[i].nodeType == 3 ){
this.content = childNodes[i];
}
}
return result;
};
//This function has for goal to check that the given index is correct for a childNodeArray
function checkIndexValidity(childNodes, index){
if(index >= childNodes.length || index < 0){
throw "The given index is not valid and is not between 0 and childNodes.length";
}
};
//Check if the childNodes attribute is null, if it is null it means that the current element is the root element.
// So the array must be filtered.
function getFilteredChildNodes(childNodes, xmlDoc){
if(childNodes == null){
return getFilteredChildNodes(xmlDoc.childNodes);
}else{
return childNodes;
}
};
//This method must return the number of children of the element
this.getChildCount = function getChildCount(){
return getFilteredChildNodes(this.childNodes, this.xmlDoc).length;
};
//This method must return the child at the given index.
this.getChild = function getChild(index){
var result = new XMLElement(this, file);
var kidNode = getFilteredChildNodes(this.childNodes, this.xmlDoc)[index];
result.xmlDoc = this.xmlDoc;
// On creation ChildNodes are always filtered
result.childNodes = getFilteredChildNodes(kidNode.childNodes);
result.name = kidNode.nodeName;
return result;
};
// Returns all of the children as an XMLElement array
this.getChildren = function getChildren(){
var result = new Array();
var childNodesArray = getFilteredChildNodes(this.childNodes, this.xmlDoc);
for(var i = 0; i < childNodesArray.length; i ++){
var child = new XMLElement(this, file);
child.xmlDoc = this.xmlDoc;
// On creation ChildNodes are always filtered
child.childNodes = getFilteredChildNodes(childNodesArray[i].childNodes);
child.name = childNodesArray[i].nodeName;
result.push(child);
}
return result;
}
// Returns the content of the element
this.getContent = function getContent(){

}
};
this.setup = function setup(){
var xmlElement = new XMLElement(this, "sites.xml");
var childCount = xmlElement.getChildCount();
var childName = xmlElement.getChild(0).getChild(1).name;
var children = xmlElement.getChild(0).getChildren();
var stop;
}
setup();

///////////////////*************************************************************************
function Personne(){
this.call = function call(){
alert("yo");
};
};

var personne = new Personne();
personne.call();
///////////////////*************************************************************************
Leave a Comment more...

first implementation

by admin on déc.19, 2009, under Non classé

Here is a first implementation of the XMLElement. This one is just a first test of instance creation.

It will evolv in the new few days.


//XMLElement constructor
this.XMLElement = function XMLElement(parent, file){
// Don't know if Internet Explorer has to be managed.
// Don't know yet how the parent element will be usefull because methods of the object are not calling it
this.parent = parent;
try{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e){
alert(e.message);
return;
}
xmlDoc.async=false;
xmlDoc.load(file);
// All this methods declaration should be filled next.
//This method must return the number of children of the interrogated node.
this.getChildCount = function getChildCount(){
return this.childNodes.length;
};
//This method must return the child at the given index.
this.getChild = function getChild(index){
return this.childNodes[index];
};
this.getChild = function getChild(path){};
//This method must return all children as an XMLElement Array.
this.getChildren = function getChildren(){};
this.getChildren = function getChildren(path){};
//Returns the content of an element. If there is no such content, null is returned.
this.getContent = function getContent(){};
//Returns an integer attribute of the element.
//If the default parameter is used and the attribute doesn't exist, the default value is returned.
//When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned.
//Don't understand that one ...
this.getIntAttribute = function getIntAttribute(name){};
this.getIntAttribute = function getIntAttribute(name, defaultValue){};
//     Returns a float attribute of the element.
//If the default parameter is used and the attribute doesn't exist, the default value is returned.
//When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned.
this.getFloatAttribute = function getFloatAttribute(name){};
this.getFloatAttribute = function getFloatAttribute(name, defaultValue){};
//Returns a String attribute of the element.
//If the default parameter is used and the attribute doesn't exist, the default value is returned.
//When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned.
this.getStringAttribute = function getStringAttribute(name){};
this.getStringAttribute = function getStringAttribute(name, defaultValue){};
//Returns the name of the element
this.getName = function getName(){};
return xmlDoc;
};
Leave a Comment more...

Premier contact

by admin on nov.30, 2009, under Non classé

Premier contact avec David Humphrey.

David, nous a demandé à Raphael et à moi de nous consacrer dans un premier à l’étude concernant la librairie

processing.js. Ce que l’on peut trouver la(documentation), la(documentation XML element), et la(download de la librairie).

Un extrait du mail :

What you would need to do is add XMLElement, and all its member functions, to the Processing.js code, which would involve wrapping DOM/JS XML calls so they match the syntax that Processing uses.

I think this is a good first bug, as it will allow you to do all of the following:

1) Get on irc.mozilla.org and into the #processing.js channel to meet people, find a place to ask questions

2) Download the Processing.js code locally using git, see http://annasob.wordpress.com/2009/10/05/setting-up-gitgithub-on-windows

3) Look at how the Processing.js code works.

4) Learn more about JavaScript, the DOM, XML

5) Get accounts on Lighthouse (the Processing.js bugtracker) – http://processing-js.lighthouseapp.com/projects/41284-processingjs/overview

6) Create a ticket for the XMLElement enhancement

7) Learn about making a branch of the code, pushing your changes there, getting review, etc.

Leave a Comment more...

Et la lumière fut… ou pas.

by admin on nov.29, 2009, under Non classé

Premier post de ce blog consacré au développement sur processing.

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

    Archives

    All entries, chronologically...