Xml.java - method to get all attributes of element

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Xml.java - method to get all attributes of element

Postby andersRson » Sun Aug 14, 2011 1:46 pm

Hi!

I made a tiny change to Xml.java that allows you to get the number of attributes of an element and also a copy of the map containing the attributes. I couldn't find any word of in what format patches should be submitted so I'll just paste the diff here. Lemme know if it's not acceptable.

This contribution is obviously under Apache 2 license, just like the file it patches. Just for the sake of correctness :)

Oh, and the change is actually to Xml.rl and not Xml.java. I assumed that was the way it was supposed to be.

Code: Select all
-------------------- gdx/src/com/badlogic/gdx/utils/Xml.rl --------------------
diff --git a/gdx/src/com/badlogic/gdx/utils/Xml.rl b/gdx/src/com/badlogic/gdx/utils/Xml.rl
index 4ea47a9..ab33bf4 100644
--- a/gdx/src/com/badlogic/gdx/utils/Xml.rl
+++ b/gdx/src/com/badlogic/gdx/utils/Xml.rl
@@ -244,6 +244,25 @@
          return value;
       }
 
+      /**
+       * Gets the attributes set on this element.
+       * Changes on the returned map does not change the state of the Element.
+       * If there are no attributes an empty map is returned.
+       * @return Map of attributes
+       */
+      public ObjectMap<String, String> getAttributes() {
+         if(attributes == null) return new ObjectMap<String, String>();
+         
+         ObjectMap<String, String> attrs = new ObjectMap<String, String>(attributes.size);
+         attrs.putAll(attributes);
+         return attrs;
+      }
+      
+      public int getAttributesCount() {
+         if(attributes == null) return 0;
+         return attributes.size;
+      }
+      
       public void setAttribute (String name, String value) {
          if (attributes == null) attributes = new ObjectMap(8);
          attributes.put(name, value);
andersRson
 
Posts: 5
Joined: Sun Aug 14, 2011 11:12 am

Re: Xml.java - method to get all attributes of element

Postby mzechner » Sun Aug 14, 2011 11:25 pm

I'm not sure the second method is needed, since you can just return the ObjectMap and query its size. The first method allocates a little to much for my taste. I'll pass i by Nate. Thanks a bunch!
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Xml.java - method to get all attributes of element

Postby andersRson » Mon Aug 15, 2011 6:42 pm

mzechner wrote:The first method allocates a little to much for my taste.

I don't disagree, but I poked around and it's the way it's already done in the method getChildrenByName, I figured I'd just keep the style consistent.

Code: Select all
public Array<Element> getChildrenByName(String name) {
         Array<Element> children = new Array<Element>();
         if(this.children == null) return children;
         for(int i = 0; i < this.children.size; i++) {
            Element child = this.children.get(i);
            if(child.name.equals(name)) children.add(child);
         }
         return children;
      }


Anyway, I'm just starting out with libgdx and I'm really liking it. Hopefully I'll be able to contribute some more, and maaaaybe even get a game out there :)
Great stuff!
andersRson
 
Posts: 5
Joined: Sun Aug 14, 2011 11:12 am

Re: Xml.java - method to get all attributes of element

Postby NateS » Tue Aug 23, 2011 6:47 am

getAttributes method added (no allocation).
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest