Skip to content

Commit

Permalink
Add ability to get the value of a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Nov 18, 2016
1 parent 7253885 commit 19535ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,26 @@ public ResourcesPoet remove(@NotNull Type type, @NotNull String name) {
return this;
}

/**
* Get the value of the current resource of this type and name
* @param type the type
* @param name the name
* @return the value or null if it does not exist
*/
@Nullable
public String value(@NotNull Type type, @NotNull String name) {
NodeList nodeList = resourceElement.getElementsByTagName(type.toString());
for (int i=0; i<nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element && name.equals(((Element)node).getAttribute("name"))) {
//For some reason, this will remove the element and leave a line break in its place
//Somewhat unfortunate but I do not think there is much we could do about it
return nodeList.item(i).getTextContent();
}
}
return null;
}

/**
* Specify if you want the output to be indented or not
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.commit451.resourcespoet;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;

/**
* Tests reading a value
*/
public class ValueTest {

@Test
public void valueTest() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("bool.xml").getFile());
ResourcesPoet poet = ResourcesPoet.create(file);
String value = poet.value(Type.BOOL, "is_cool");
Assert.assertEquals("true", value);
}
}

0 comments on commit 19535ab

Please sign in to comment.