Created
February 12, 2018 14:28
-
-
Save farrukhnajmi/ae07e9b1fe1777733c7c523c6fa2679b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapp.localization; | |
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
import org.xmlpull.v1.XmlPullParserFactory; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
public class AndroidStringManagerMCVE { | |
/** | |
* Loads the android string resources from strings.xml file. | |
* Process custom attributes in <string/> elements. | |
*/ | |
public void loadAndroidStringResources() throws XmlPullParserException, IOException { | |
try (FileInputStream in = new FileInputStream("app/src/main/res/values/strings.xml")) { | |
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); | |
factory.setNamespaceAware(true); | |
XmlPullParser parser = factory.newPullParser(); | |
parser.setInput(in, null); | |
AndroidStringResource resource = null; | |
int eventType = parser.getEventType(); | |
while (eventType != parser.END_DOCUMENT) { | |
eventType = parser.next(); | |
if (eventType == parser.START_TAG) { | |
if (parser.getName().equals("string")) { | |
resource = new AndroidStringResource(); | |
resource.name = parser.getAttributeValue(null, "name"); | |
resource.context = parser.getAttributeValue(null, "context"); | |
String maxLengthStr = parser.getAttributeValue(null, "maxLength"); | |
resource.maxLength = (maxLengthStr == null) ? -1 : Integer.parseInt(maxLengthStr); | |
} | |
} else if (eventType == parser.TEXT) { | |
if (resource != null) { | |
resource.value = parser.getText(); | |
} | |
} else if (eventType == parser.END_TAG) { | |
if (parser.getName().equals("string")) { | |
resource = null; | |
} | |
} | |
} | |
} | |
} | |
public static void main(String[] args) throws IOException, XmlPullParserException { | |
AndroidStringManagerMCVE mcve = new AndroidStringManagerMCVE(); | |
mcve.loadAndroidStringResources(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment