1 package sourceforge.org.qmc2.options.editor.model; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 10 import org.w3c.dom.Document; 11 import org.w3c.dom.Element; 12 import org.w3c.dom.Node; 13 import org.w3c.dom.NodeList; 14 15 public class Section extends DescriptableItem { 16 17 private final List<Option> options = new ArrayList<Option>(); 18 19 public final static String TAG_SECTION = "section"; 20 21 private Map<String, String> unmappedAttributes = null; 22 Section(String name)23 public Section(String name) { 24 super(name); 25 } 26 addOption(Option o, int index)27 public void addOption(Option o, int index) { 28 options.add(index, o); 29 for (int i = index + 1; i < options.size(); i++) { 30 options.get(i).setIndex(i); 31 } 32 } 33 addOption(Option o)34 public void addOption(Option o) { 35 o.setIndex(options.size()); 36 options.add(o); 37 } 38 getOptions()39 public List<Option> getOptions() { 40 return options; 41 } 42 parseSectionName(Node sectionNode)43 public static String parseSectionName(Node sectionNode) { 44 String name = null; 45 for (int i = 0; i < sectionNode.getAttributes().getLength(); i++) { 46 Node item = sectionNode.getAttributes().item(i); 47 if (ATTRIBUTE_NAME.equals(item.getNodeName())) { 48 name = item.getNodeValue(); 49 } 50 } 51 return name; 52 } 53 parseSection(Node sectionNode)54 public static Section parseSection(Node sectionNode) { 55 56 String name = null; 57 Map<String, String> unmappedAttributes = new HashMap<String, String>(); 58 59 for (int i = 0; i < sectionNode.getAttributes().getLength(); i++) { 60 Node item = sectionNode.getAttributes().item(i); 61 if (ATTRIBUTE_NAME.equals(item.getNodeName())) { 62 name = item.getNodeValue(); 63 } 64 else { 65 unmappedAttributes.put(item.getNodeName(), item.getNodeValue()); 66 } 67 } 68 69 Section section = new Section(name); 70 71 section.parseDescriptions(sectionNode); 72 section.setUnmappedAttributes(unmappedAttributes); 73 74 NodeList options = sectionNode.getChildNodes(); 75 76 for (int i = 0; i < options.getLength(); i++) { 77 Node option = options.item(i); 78 if (Option.TAG_OPTION.equals(option.getNodeName())) { 79 Option o = Option.parseOption(option); 80 o.setParent(section); 81 section.addOption(o); 82 } 83 } 84 85 return section; 86 } 87 setUnmappedAttributes(Map<String, String> unmappedAttributes)88 public void setUnmappedAttributes(Map<String, String> unmappedAttributes) { 89 this.unmappedAttributes = unmappedAttributes; 90 } 91 92 @Override toXML(Document document)93 public Element toXML(Document document) { 94 Element section = super.toXML(document); 95 for (String attr: unmappedAttributes.keySet()) { 96 section.setAttribute(attr, unmappedAttributes.get(attr)); 97 } 98 for (Option o : options) { 99 section.appendChild(o.toXML(document)); 100 } 101 return section; 102 103 } 104 105 @Override getTagName()106 public String getTagName() { 107 return TAG_SECTION; 108 } 109 110 @Override getLanguages()111 public Set<String> getLanguages() { 112 Set<String> languages = new HashSet<String>(); 113 languages.addAll(super.getLanguages()); 114 for (Option o : options) { 115 languages.addAll(o.getLanguages()); 116 } 117 return super.getLanguages(); 118 } 119 removeOption(String name)120 public void removeOption(String name) { 121 List<Option> optionsCopy = new ArrayList<Option>(options); 122 for (Option o : optionsCopy) { 123 if (o.getName().equals(name)) { 124 options.remove(o); 125 } 126 } 127 for (int i = 0; i < options.size(); i++) { 128 options.get(i).setIndex(i); 129 } 130 131 } 132 133 } 134