GithubHelp home page GithubHelp logo

boiledwater's Introduction

一个以XML为载体的通用属性配置模块,可以通过唯一标记值key获取配置,目前支持2层嵌套配置,配置支持:

单值

包括java基本数据类型对象:@Boolean,@Byte,@Short,@Integer,@Long,@Float,@Double,@Character和@String字符串类,及定义在包名路径下attributepath目录下继承com.deepindex.attributeset.AttributeParser.Attribute类的自定义子类,自定义子类可以嵌套使用上述基本类型和String类型,及自定义类型。

列表

以单值、列表、键值对为value的列表。

键值对

以String为key,以单值、列表、键值对为value的键值对。

例子:

XML配置

<attribute key="key0" type="boolean" value="true" />

code(fileName为XML配置文件名)

AttributeCollection attributeCollection = new AttributeCollection();
AttributeParser parser = new AttributeParser(context, attributeCollection);
try (InputStream inputStream = new ByteArrayInputStream(
        fileName.getBytes(StandardCharsets.UTF_8))) {
    parser.parse(inputStream);
} catch (IOException | XmlPullParserException e) {
    Log.e("AttributeCollection", "parse error", e);
}

//true
boolean value = attributeCollection.getBoolean("key0");

XML配置

<attribute_list key="key1" type="String" >
  <sub value="hello" />
  <sub value="world" />
</attribute_list>

code(fileName为XML配置文件名)

AttributeCollection attributeCollection = new AttributeCollection();
AttributeParser parser = new AttributeParser(context, attributeCollection);
try (InputStream inputStream = new ByteArrayInputStream(
        fileName.getBytes(StandardCharsets.UTF_8))) {
    parser.parse(inputStream);
} catch (IOException | XmlPullParserException e) {
    Log.e("AttributeCollection", "parse error", e);
}

ArrayList<String> list = attributeCollection.getStringArrayList("key1");
//hello
String value = list.get(0);
//world
value = list.get(1);

XML配置

<attribute_map key="key2" type="int" >
    <sub key="subkey1" value="11" />
    <sub key="subkey2" value="22" />
</attribute_map>

code(fileName为XML配置文件名)

AttributeCollection attributeCollection = new AttributeCollection();
AttributeParser parser = new AttributeParser(context, attributeCollection);
try (InputStream inputStream = new ByteArrayInputStream(
        fileName.getBytes(StandardCharsets.UTF_8))) {
    parser.parse(inputStream);
} catch (IOException | XmlPullParserException e) {
    Log.e("AttributeCollection", "parse error", e);
}

ArrayMap<String, Integer> map = attributeCollection.getIntegerArrayMap("key2");
//11
int value = map.get("subkey1");
//22
value = map.get("subkey2");

XML配置

<attribute_map key="key4" type="PersonAttribute" >
    <sub key="subkey1" name="li" age="20" high="180" weight="70" />
    <sub key="subkey2" name="wang" age="21" high="185" weight="80" />
</attribute_map>

code(fileName为XML配置文件名)

public class PersonAttribute extends Attribute {
	private String mName;
	private int mAge;
	private int mHigh;
	private int mWeight;

	public PersonAttribute() {
	}

	public PersonAttribute(String name, int age, int high, int weight) {
		mName = name;
		mAge = age;
		mHigh = high;
		mWeight = weight;
	}

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		mName = name;
	}

	public int getAge() {
		return mAge;
	}

	public void setAge(int age) {
		mAge = age;
	}

	public int getHigh() {
		return mHigh;
	}

	public void setHigh(int high) {
		mHigh = high;
	}

	public int getWeight() {
		return mWeight;
	}

	public void setWeight(int weight) {
		mWeight = weight;
	}
}


AttributeCollection attributeCollection = new AttributeCollection();
AttributeParser parser = new AttributeParser(context, attributeCollection);
try (InputStream inputStream = new ByteArrayInputStream(
        fileName.getBytes(StandardCharsets.UTF_8))) {
    parser.parse(inputStream);
} catch (IOException | XmlPullParserException e) {
    Log.e("AttributeCollection", "parse error", e);
}

ArrayMap<String, PersonAttribute> map = attributeCollection.getAttributeArrayMap("key4");
PersonAttribute value = map.get("subkey1");
<sub key="subkey1" name="li" age="20" high="180" weight="70" />
<sub key="subkey2" name="wang" age="21" high="185" weight="80" />
//li
String name = value.getName();
//20
int age = value.getAge();
//180
int high = value.getHigh();
//70
int weight = value.getWeight();

更多例子可以参考assets文件夹下attributetest.xml

boiledwater's People

Contributors

lishengxu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.