Skip to content

Commit

Permalink
Add support for removing elements from the resource file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Nov 3, 2016
1 parent f627ebf commit cb9a702
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 23 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ or if you want to write it to a string:
poet.indent(true);
String resourcesXml = poet.build();
```
You can even start with and modify an existing resource file:
```java
File file = new File("some/path/to/file");
ResourcesPoet poet = ResourcesPoet.create(file)
.remove(Type.STRING, "app_name")
.addString("app_name", "Even Better App Name");
```

## Supported Types
Most [resource types](https://developer.android.com/guide/topics/resources/available-resources.html) are supported. All look similar in usage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
Expand All @@ -32,7 +29,7 @@ public class ResourcesPoet {
/**
* Create a new builder
*
* @return a resources builder
* @return poet
*/
public static ResourcesPoet create() {
init();
Expand All @@ -46,13 +43,26 @@ public static ResourcesPoet create() {
* Creates a builder on top of the current resources XML file
*
* @param file the resources file you want to add to
* @return the builder
* @throws SAXException
* @return poet
*/
public static ResourcesPoet create(File file) {
try {
return create(new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new IllegalStateException("Unable to parse the resource file you passed. Make sure it is properly formatted", e);
}
}

/**
* Creates a builder on top of the current resources XML file
*
* @param inputStream the input stream of the resources file you want to add to
* @return poet
*/
public static ResourcesPoet create(InputStream inputStream) {
init();
try {
Document document = sDocumentBuilder.parse(file);
Document document = sDocumentBuilder.parse(inputStream);
Element resources;
NodeList list = document.getElementsByTagName(ELEMENT_RESOURCES);
if (list == null || list.getLength() == 0) {
Expand Down Expand Up @@ -98,20 +108,20 @@ private ResourcesPoet() {
/**
* Add an attr to the config
*
* @param attr the defined attribute
* @param attr the defined attribute
* @return poet
*/
public ResourcesPoet addAttr(Attr attr) {
//<attr name="gravityX" format="float"/>
Element element = document.createElement("attr");
Element element = document.createElement(Type.ATTR.toString());
element.setAttribute("name", attr.name);
if (attr.formats != null && !attr.formats.isEmpty()) {
String formatString = "";
for (Attr.Format format : attr.formats) {
formatString = formatString + format.toString() + "|";
}
//remove last |
formatString = formatString.substring(0, formatString.length()-1);
formatString = formatString.substring(0, formatString.length() - 1);
element.setAttribute("format", formatString);
}
resourceElement.appendChild(element);
Expand Down Expand Up @@ -139,7 +149,7 @@ public ResourcesPoet addBool(String name, boolean value) {
*/
public ResourcesPoet addBool(String name, String value) {
//<bool name="is_production">false</bool>
Element element = document.createElement("bool");
Element element = document.createElement(Type.BOOL.toString());
element.setAttribute("name", name);
element.appendChild(document.createTextNode(value));
resourceElement.appendChild(element);
Expand All @@ -155,7 +165,7 @@ public ResourcesPoet addBool(String name, String value) {
*/
public ResourcesPoet addColor(String name, String value) {
//<color name="color_primary">#7770CB</color>
Element element = document.createElement("color");
Element element = document.createElement(Type.COLOR.toString());
element.setAttribute("name", name);
element.appendChild(document.createTextNode(value));
resourceElement.appendChild(element);
Expand Down Expand Up @@ -183,7 +193,7 @@ public ResourcesPoet addComment(String comment) {
*/
public ResourcesPoet addDrawable(String name, String value) {
//<drawable name="logo">@drawable/logo</drawable>
Element bool = document.createElement("drawable");
Element bool = document.createElement(Type.DRAWABLE.toString());
bool.setAttribute("name", name);
bool.appendChild(document.createTextNode(value));
resourceElement.appendChild(bool);
Expand All @@ -199,7 +209,7 @@ public ResourcesPoet addDrawable(String name, String value) {
*/
public ResourcesPoet addDimension(String name, String value) {
//<drawable name="logo">@drawable/logo</drawable>
Element bool = document.createElement("dimen");
Element bool = document.createElement(Type.DIMENSION.toString());
bool.setAttribute("name", name);
bool.appendChild(document.createTextNode(value));
resourceElement.appendChild(bool);
Expand All @@ -216,7 +226,7 @@ public ResourcesPoet addId(String id) {
// <item
// type="id"
// name="id_name" />
Element bool = document.createElement("item");
Element bool = document.createElement(Type.ID.toString());
bool.setAttribute("name", id);
bool.setAttribute("type", "id");
resourceElement.appendChild(bool);
Expand Down Expand Up @@ -244,7 +254,7 @@ public ResourcesPoet addInteger(String name, Integer value) {
*/
public ResourcesPoet addInteger(String name, String value) {
//<drawable name="logo">@drawable/logo</drawable>
Element bool = document.createElement("integer");
Element bool = document.createElement(Type.INTEGER.toString());
bool.setAttribute("name", name);
bool.appendChild(document.createTextNode(String.valueOf(value)));
resourceElement.appendChild(bool);
Expand Down Expand Up @@ -279,7 +289,7 @@ public ResourcesPoet addIntegerArrayStrings(String name, @NotNull List<String> v
// <item>0</item>
// <item>1</item>
// </integer-array>
Element element = document.createElement("integer-array");
Element element = document.createElement(Type.INTEGER_ARRAY.toString());
element.setAttribute("name", name);
for (String value : values) {
//Does this mess up the ordering?
Expand All @@ -304,7 +314,7 @@ public ResourcesPoet addPlurals(String name, @NotNull List<Plural> plurals) {
// <item quantity="few">Znaleziono %d piosenki.</item>
// <item quantity="other">Znaleziono %d piosenek.</item>
// </plurals>
Element element = document.createElement("plurals");
Element element = document.createElement(Type.PLURALS.toString());
element.setAttribute("name", name);
for (Plural plural : plurals) {
//Does this mess up the ordering?
Expand All @@ -327,7 +337,7 @@ public ResourcesPoet addPlurals(String name, @NotNull List<Plural> plurals) {
*/
public ResourcesPoet addString(String name, String value) {
//<string name="app_name">Cool</string>
Element element = document.createElement("string");
Element element = document.createElement(Type.STRING.toString());
element.setAttribute("name", name);
element.appendChild(document.createTextNode(value));
resourceElement.appendChild(element);
Expand All @@ -346,7 +356,7 @@ public ResourcesPoet addStringArray(String name, @NotNull List<String> values) {
// <item>Country</item>
// <item>United States</item>
// </string-array>
Element element = document.createElement("string-array");
Element element = document.createElement(Type.STRING_ARRAY.toString());
element.setAttribute("name", name);
for (String value : values) {
//Does this mess up the ordering?
Expand Down Expand Up @@ -378,7 +388,7 @@ public ResourcesPoet addStyle(String name, @Nullable String parentRef) {
*/
public ResourcesPoet addStyle(String name, @Nullable String parentRef, @Nullable List<StyleItem> styleItems) {
//<style name="AppTheme.Dark" parent="Base.AppTheme.Dark"/>
Element element = document.createElement("style");
Element element = document.createElement(Type.STYLE.toString());
element.setAttribute("name", name);
if (parentRef != null) {
element.setAttribute("parent", parentRef);
Expand Down Expand Up @@ -407,7 +417,7 @@ public ResourcesPoet addTypedArray(String name, @NotNull List<String> values) {
// <item>Country</item>
// <item>United States</item>
// </array>
Element element = document.createElement("array");
Element element = document.createElement(Type.TYPED_ARRAY.toString());
element.setAttribute("name", name);
for (String value : values) {
Element valueElement = document.createElement("item");
Expand All @@ -418,8 +428,28 @@ public ResourcesPoet addTypedArray(String name, @NotNull List<String> values) {
return this;
}

/**
* Remove the resource which matches the name and type
* @param type the type of the resource you wish to remove
* @param name the name of the element to remove
* @return poet
*/
public ResourcesPoet remove(@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
resourceElement.removeChild(nodeList.item(i));
}
}
return this;
}

/**
* Specify if you want the output to be indented or not
*
* @param indent true if you want indentation. false if not. Default is false
* @return poet
*/
Expand All @@ -430,6 +460,7 @@ public ResourcesPoet indent(boolean indent) {

/**
* Build the XML to a string
*
* @return the xml as a string
*/
public String build() {
Expand All @@ -442,6 +473,7 @@ public String build() {
/**
* Build the XML to a file. You should call {@link File#createNewFile()} or validate that the file exists
* before calling
*
* @param file the file to output the XML to
*/
public void build(File file) {
Expand All @@ -451,6 +483,7 @@ public void build(File file) {

/**
* Build the XML to the {@link OutputStream}
*
* @param outputStream the output stream to output the XML to
*/
public void build(OutputStream outputStream) {
Expand All @@ -460,6 +493,7 @@ public void build(OutputStream outputStream) {

/**
* Build the XML to the {@link Writer}
*
* @param writer the writer to output the XML to
*/
public void build(Writer writer) {
Expand Down
89 changes: 89 additions & 0 deletions resourcespoet/src/main/java/com/commit451/resourcespoet/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.commit451.resourcespoet;

/**
* The various resource types
*
* @see <a href="https://developer.android.com/guide/topics/resources/available-resources.html">https://developer.android.com/guide/topics/resources/available-resources.html</a>
*/
public enum Type {

ATTR {
@Override
public String toString() {
return "attr";
}
},
BOOL {
@Override
public String toString() {
return "bool";
}
},
COLOR {
@Override
public String toString() {
return "color";
}
},
DRAWABLE {
@Override
public String toString() {
return "drawable";
}
},
DIMENSION {
@Override
public String toString() {
return "dimen";
}
},
ID {
@Override
public String toString() {
return "item";
}
},
INTEGER {
@Override
public String toString() {
return "integer";
}
},
INTEGER_ARRAY {
@Override
public String toString() {
return "integer-array";
}
},
PLURALS {
@Override
public String toString() {
return "plurals";
}
},
STRING {
@Override
public String toString() {
return "string";
}
},
STRING_ARRAY {
@Override
public String toString() {
return "string-array";
}
},
STYLE {
@Override
public String toString() {
return "style";
}
},
TYPED_ARRAY {
@Override
public String toString() {
return "array";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.commit451.resourcespoet;

import org.junit.Test;

import java.io.File;

/**
* Tests the resources creation
*/
public class RemoveTest {

@Test
public void removeTest() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("remove_before.xml").getFile());
ResourcesPoet poet = ResourcesPoet.create(file)
.remove(Type.STRING, "red");

TestUtil.assertEquals("remove_after.xml", poet);
}
}
5 changes: 5 additions & 0 deletions resourcespoet/src/test/resources/remove_after.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<color name="red">#FF0000</color>

</resources>
5 changes: 5 additions & 0 deletions resourcespoet/src/test/resources/remove_before.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<color name="red">#FF0000</color>
<string name="red">red</string>
</resources>

0 comments on commit cb9a702

Please sign in to comment.