1 package sourceforge.org.qmc2.options.editor.model;
2 
3 import java.util.HashMap;
4 import java.util.Map;
5 
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.w3c.dom.Node;
9 
10 public class Option extends DescriptableItem {
11 
12 	public enum OptionType {
13 		BOOL, COMBO, DIRECTORY, FILE, FLOAT, FLOAT2, FLOAT3, INT, STRING, UNKNOWN
14 	};
15 
16 	private String type;
17 
18 	private String defaultValue;
19 
20 	public final static String TAG_OPTION = "option";
21 
22 	private final static String ATTRIBUTE_TYPE = "type";
23 
24 	private final static String ATTRIBUTE_DEFAULT = "default";
25 
26 	private Map<String, String> unmappedAttributes;
27 
Option(String name, String type, String defaultValue)28 	public Option(String name, String type, String defaultValue) {
29 		super(name);
30 		this.type = type;
31 		this.defaultValue = defaultValue;
32 	}
33 
getType()34 	public String getType() {
35 		return type;
36 	}
37 
getDefaultValue()38 	public String getDefaultValue() {
39 		return defaultValue;
40 	}
41 
parseOption(Node optionNode)42 	public static Option parseOption(Node optionNode) {
43 
44 		String name = null;
45 		String type = null;
46 		String defaultValue = null;
47 		Map<String, String> unmappedAttrs = new HashMap<String, String>();
48 		for (int i = 0; i < optionNode.getAttributes().getLength(); i++) {
49 			Node item = optionNode.getAttributes().item(i);
50 			if (ATTRIBUTE_NAME.equals(item.getNodeName())) {
51 				name = item.getNodeValue();
52 			}
53 			else if(ATTRIBUTE_TYPE.equals(item.getNodeName())){
54 				type = item.getNodeValue();
55 			}
56 			else if(ATTRIBUTE_DEFAULT.equals(item.getNodeName())) {
57 				defaultValue = item.getNodeValue();
58 			}
59 			else {
60 				unmappedAttrs.put(item.getNodeName(), item.getNodeValue());
61 			}
62 		}
63 
64 
65 
66 		OptionType optionType = null;
67 		try {
68 			optionType = OptionType.valueOf(type.toUpperCase());
69 		} catch (Exception e) {
70 			optionType = OptionType.UNKNOWN;
71 		}
72 
73 		Option option = null;
74 
75 		switch (optionType) {
76 		case COMBO:
77 			option = new ComboOption(name, optionType.name().toLowerCase(),
78 					defaultValue);
79 			break;
80 
81 		case UNKNOWN:
82 			option = new Option(name, type, defaultValue);
83 
84 		default:
85 			option = new Option(name, optionType.name().toLowerCase(),
86 					defaultValue);
87 			break;
88 		}
89 
90 		option.parseDescriptions(optionNode);
91 		option.parseData(optionNode);
92 		option.setUnmappedAttributes(unmappedAttrs);
93 
94 		return option;
95 	}
96 
parseData(Node optionNode)97 	protected void parseData(Node optionNode) {
98 
99 	}
100 
101 
setUnmappedAttributes(Map<String, String> unmappedAttributes)102 	public void setUnmappedAttributes(Map<String, String> unmappedAttributes) {
103 		this.unmappedAttributes = unmappedAttributes;
104 	}
105 
106 	@Override
getTagName()107 	public String getTagName() {
108 		return TAG_OPTION;
109 	}
110 
111 	@Override
toXML(Document document)112 	public Element toXML(Document document) {
113 		Element option = super.toXML(document);
114 		option.setAttribute(ATTRIBUTE_TYPE, type);
115 		option.setAttribute(ATTRIBUTE_DEFAULT, defaultValue);
116 		for (String attr: unmappedAttributes.keySet()) {
117 			option.setAttribute(attr, unmappedAttributes.get(attr));
118 		}
119 
120 
121 		return option;
122 
123 	}
124 
setDefaultValue(String defaultValue)125 	public void setDefaultValue(String defaultValue) {
126 		this.defaultValue = defaultValue;
127 
128 	}
129 
setType(String newType)130 	public void setType(String newType) {
131 		this.type = newType;
132 
133 	}
134 
135 }
136