- Timestamp:
- 2007-11-07T11:10:02+13:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/trunk/src/org/greenstone/gatherer/cdm/ArgumentContainer.java
r12826 r14783 43 43 */ 44 44 abstract public class ArgumentContainer 45 extends ArrayList 46 implements Comparable, DOMProxyListEntry, Serializable { 47 48 // The DOM Element this container is modelled on 49 protected Element element; 50 protected boolean is_abstract = false; 51 protected ArgumentContainer super_container; 52 protected String description; 53 protected String name; 54 55 /** Constructor used in DOMProxyListModel initializations 56 */ 57 public ArgumentContainer() { 58 } 59 60 public ArgumentContainer(Element element, ArgumentContainer base_container) { 61 62 super(); 63 this.element = element; 64 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 65 66 // Parse in any argument options for this container, keeping a list of the ones found 67 HashMap known_arguments = new HashMap(); 68 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT); 69 int option_elements_length = option_elements.getLength(); 70 for(int i = 0; i < option_elements_length; i++) { 71 Element option_element = (Element) option_elements.item(i); 72 Argument argument = new Argument(option_element); 73 ///ystem.err.println("Rebuilding existing argument: " + argument.getName()); 74 known_arguments.put(argument.getName(), argument); 75 } 76 // If a base classifier was given 77 if(base_container != null) { 78 // Copy the details, and add a reference to whatever the base container's super container is 79 description = base_container.getDescription(); 80 // Now search through the 'dummy' arguments belonging to the base container. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this container, copy them and add them, but without a value. 81 ArrayList all_arguments = base_container.getArguments(); 82 int argument_count = all_arguments.size(); 83 for(int j = 0; j < argument_count; j++) { 84 Argument base_argument = (Argument) all_arguments.get(j); 85 String base_argument_name = base_argument.getName(); 86 ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name); 87 Argument existing_argument = (Argument) known_arguments.get(base_argument_name); 88 // Found an existing argument. Complete its details 89 if(existing_argument != null) { 90 ///ystem.err.println("Found existing argument. Filling out details."); 91 known_arguments.remove(base_argument_name); 92 existing_argument.setDefaultValue(base_argument.getDefaultValue()); 93 existing_argument.setDescription(base_argument.getDescription()); 94 existing_argument.setOptions(base_argument.getOptions()); 95 existing_argument.setRequired(base_argument.isRequired()); 96 existing_argument.setType(base_argument.getType()); 97 existing_argument.setMinimum(base_argument.getMinimum()); 98 existing_argument.setMaximum(base_argument.getMaximum()); 99 existing_argument.setOwner(base_argument.getOwner()); 100 existing_argument.setHiddenGLI(base_argument.isHiddenGLI()); 101 add(existing_argument); 102 } 103 // No existing argument. Copy base_argument and add it, but set its assigned flag to false 104 else { 105 ///atherer.println("No such argument. Adding new, unassigned, argument."); 106 // The trick thing is that we have to create a new element in the DOM as well. 107 Argument new_argument = base_argument.copy(); 108 Element argument_element = CollectionConfiguration.createElement(StaticStrings.OPTION_ELEMENT); 109 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name); 110 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR); 111 new_argument.setElement(argument_element); 112 // All done. Add it. 113 element.appendChild(argument_element); 114 add(new_argument); 115 } 116 } 117 } 118 119 // The first time we do this, the args come from the config file and 120 // may contain invalid args. Here we remove them from the DOM. 121 if (!known_arguments.isEmpty()) { 122 Set entry_set = known_arguments.entrySet(); 123 Iterator entry_iterator = entry_set.iterator(); 124 while (entry_iterator.hasNext()) { 125 Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue(); 126 Element e = arg.getElement(); 127 element.removeChild(e); 128 } 129 } 130 } 131 132 133 134 135 /** Constructor used for the plugins that are in the DOMProxyList */ 136 /** Method to add an argument to this container. Only adds the argument if it isn't already present. 137 * @param argument The <strong>Argument</strong> to add. 138 */ 139 public void addArgument(Argument argument) { 140 if(element == null && !contains(argument)) { 141 add(argument); 142 argument.setOwner(name); 143 } 144 } 145 146 /** Method to compare two containers for ordering. 147 * @param object The container we are comparing to, as an <strong>Object</strong>. 148 * @return An <i>int</i> specifying the container order, using values as set out in String. 149 * @see java.lang.String#compareTo 150 */ 151 public int compareTo(Object object) { 152 if(object == null ) { 153 return -1; 154 } 155 return toString().compareTo(object.toString()); 156 } 157 158 /** Method to determine if two containers are equal. 159 * @param object The container to test against, as an <strong>Object</strong>. 160 * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise. 161 */ 162 163 public boolean equals(Object object) { 164 return (compareTo(object) == 0); 165 } 166 167 abstract public DOMProxyListEntry create(Element element); 168 /** Method to retrieve an argument by its name. 169 * @param name The name of the argument as a <strong>String</strong>. 170 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument. 171 */ 172 public Argument getArgument(String name) { 173 // The name given may still include the '-' 174 if(name.startsWith("-")) { 175 name = name.substring(1); 176 } 177 //ArrayList arguments = getArguments(true, true); 178 ArrayList arguments = getArguments(); 179 for(int i = 0; i < arguments.size(); i++) { 180 Argument argument = (Argument)arguments.get(i); 181 if(argument.getName().equals(name)) { 182 return argument; 183 } 184 } 185 return null; 186 } 187 188 /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them. 189 * @return the arguments within a ArrayList 190 */ 191 public ArrayList getArguments() { 192 ArrayList arguments = new ArrayList(); 193 arguments.addAll(this); 194 if(super_container != null) { 195 ArrayList remainder = super_container.getArguments(); 196 remainder.removeAll(arguments); 197 arguments.addAll(remainder); 198 } 199 return arguments; 200 201 } 202 203 public String getDescription() { 204 return description; 205 } 206 207 public Element getElement() { 208 return element; 209 } 210 /** Method to retrieve the name associated with this argument container. 211 * @return the name as a String 212 */ 213 public String getName() { 214 if(name == null && element != null) { 215 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 216 } 217 return name; 218 } 219 220 public boolean isAbstract() { 221 return is_abstract; 222 } 223 224 public boolean isAssigned() { 225 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR)); 226 } 227 228 public void setAssigned(boolean assigned) { 229 if(element != null) { 230 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 231 } 232 } 233 234 public void setDescription(String description) { 235 this.description = description; 236 } 237 238 public void setElement(Element element) { 239 this.element = element; 240 } 241 242 public void setIsAbstract(boolean is_abstract) { 243 this.is_abstract = is_abstract; 244 } 245 246 public void setName(String name) { 247 this.name = name; 248 } 249 250 /** Method to set the value of the super_container. 251 * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance. 252 */ 253 public void setSuper(ArgumentContainer super_container) { 254 this.super_container = super_container; 255 } 256 257 public String toString() 258 { 259 if (element == null) { 260 return name; 261 } 262 263 if (name == null) { 264 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 265 } 266 267 StringBuffer text = new StringBuffer(" "); 268 if (!isAssigned()) { 269 text.append("#"); 270 } 271 text.append(name); 272 273 ArrayList arguments = getArguments(); 274 for (int i = 0; i < arguments.size(); i++) { 275 Argument argument = (Argument) arguments.get(i); 276 if (argument.isAssigned()) { 277 text.append(" "); 278 text.append(argument.toString()); 279 } 280 } 281 282 return text.toString(); 283 } 284 285 public ArrayList getArguments(boolean include_normal, boolean include_custom){ 286 return this; 287 } 45 extends ArrayList 46 implements Comparable, DOMProxyListEntry, Serializable { 47 48 // The DOM Element this container is modelled on 49 protected Element element; 50 protected boolean is_abstract = false; 51 protected ArgumentContainer super_container; 52 protected String description; 53 protected String name; 54 55 /** Constructor used in DOMProxyListModel initializations 56 */ 57 public ArgumentContainer() { 58 } 59 60 public ArgumentContainer(Element element, ArgumentContainer base_container) { 61 62 super(); 63 this.element = element; 64 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 65 66 // Parse in any argument options for this container, keeping a list of the ones found 67 HashMap known_arguments = new HashMap(); 68 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT); 69 int option_elements_length = option_elements.getLength(); 70 for(int i = 0; i < option_elements_length; i++) { 71 Element option_element = (Element) option_elements.item(i); 72 Argument argument = new Argument(option_element); 73 ///ystem.err.println("Rebuilding existing argument: " + argument.getName()); 74 known_arguments.put(argument.getName(), argument); 75 } 76 // If a base classifier was given 77 if(base_container != null) { 78 // Copy the details, and add a reference to whatever the base container's super container is 79 description = base_container.getDescription(); 80 // Now search through the 'dummy' arguments belonging to the base container. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this container, copy them and add them, but without a value. 81 ArrayList all_arguments = base_container.getArguments(); 82 int argument_count = all_arguments.size(); 83 for(int j = 0; j < argument_count; j++) { 84 Argument base_argument = (Argument) all_arguments.get(j); 85 String base_argument_name = base_argument.getName(); 86 ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name); 87 Argument existing_argument = (Argument) known_arguments.get(base_argument_name); 88 // Found an existing argument. Complete its details 89 if(existing_argument != null) { 90 ///ystem.err.println("Found existing argument. Filling out details."); 91 known_arguments.remove(base_argument_name); 92 existing_argument.setDispalyName(base_argument.getDisplayName()); 93 existing_argument.setDefaultValue(base_argument.getDefaultValue()); 94 existing_argument.setDescription(base_argument.getDescription()); 95 existing_argument.setOptions(base_argument.getOptions()); 96 existing_argument.setRequired(base_argument.isRequired()); 97 existing_argument.setType(base_argument.getType()); 98 existing_argument.setMinimum(base_argument.getMinimum()); 99 existing_argument.setMaximum(base_argument.getMaximum()); 100 existing_argument.setOwner(base_argument.getOwner()); 101 existing_argument.setHiddenGLI(base_argument.isHiddenGLI()); 102 add(existing_argument); 103 } 104 // No existing argument. Copy base_argument and add it, but set its assigned flag to false 105 else { 106 ///atherer.println("No such argument. Adding new, unassigned, argument."); 107 // The trick thing is that we have to create a new element in the DOM as well. 108 Argument new_argument = base_argument.copy(); 109 Element argument_element = CollectionConfiguration.createElement(StaticStrings.OPTION_ELEMENT); 110 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name); 111 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR); 112 new_argument.setElement(argument_element); 113 // All done. Add it. 114 element.appendChild(argument_element); 115 add(new_argument); 116 } 117 } 118 } 119 120 // The first time we do this, the args come from the config file and 121 // may contain invalid args. Here we remove them from the DOM. 122 if (!known_arguments.isEmpty()) { 123 Set entry_set = known_arguments.entrySet(); 124 Iterator entry_iterator = entry_set.iterator(); 125 while (entry_iterator.hasNext()) { 126 Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue(); 127 Element e = arg.getElement(); 128 element.removeChild(e); 129 } 130 } 131 } 132 133 134 135 136 /** Constructor used for the plugins that are in the DOMProxyList */ 137 /** Method to add an argument to this container. Only adds the argument if it isn't already present. 138 * @param argument The <strong>Argument</strong> to add. 139 */ 140 public void addArgument(Argument argument) { 141 if(element == null && !contains(argument)) { 142 add(argument); 143 argument.setOwner(name); 144 } 145 } 146 147 /** Method to compare two containers for ordering. 148 * @param object The container we are comparing to, as an <strong>Object</strong>. 149 * @return An <i>int</i> specifying the container order, using values as set out in String. 150 * @see java.lang.String#compareTo 151 */ 152 public int compareTo(Object object) { 153 if(object == null ) { 154 return -1; 155 } 156 return toString().compareTo(object.toString()); 157 } 158 159 /** Method to determine if two containers are equal. 160 * @param object The container to test against, as an <strong>Object</strong>. 161 * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise. 162 */ 163 164 public boolean equals(Object object) { 165 return (compareTo(object) == 0); 166 } 167 168 abstract public DOMProxyListEntry create(Element element); 169 /** Method to retrieve an argument by its name. 170 * @param name The name of the argument as a <strong>String</strong>. 171 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument. 172 */ 173 public Argument getArgument(String name) { 174 // The name given may still include the '-' 175 if(name.startsWith("-")) { 176 name = name.substring(1); 177 } 178 //ArrayList arguments = getArguments(true, true); 179 ArrayList arguments = getArguments(); 180 for(int i = 0; i < arguments.size(); i++) { 181 Argument argument = (Argument)arguments.get(i); 182 if(argument.getName().equals(name)) { 183 return argument; 184 } 185 } 186 return null; 187 } 188 189 /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them. 190 * @return the arguments within a ArrayList 191 */ 192 public ArrayList getArguments() { 193 ArrayList arguments = new ArrayList(); 194 arguments.addAll(this); 195 if(super_container != null) { 196 ArrayList remainder = super_container.getArguments(); 197 remainder.removeAll(arguments); 198 arguments.addAll(remainder); 199 } 200 return arguments; 201 202 } 203 204 public String getDescription() { 205 return description; 206 } 207 208 public Element getElement() { 209 return element; 210 } 211 /** Method to retrieve the name associated with this argument container. 212 * @return the name as a String 213 */ 214 public String getName() { 215 if(name == null && element != null) { 216 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 217 } 218 return name; 219 } 220 221 public boolean isAbstract() { 222 return is_abstract; 223 } 224 225 public boolean isAssigned() { 226 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR)); 227 } 228 229 public void setAssigned(boolean assigned) { 230 if(element != null) { 231 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 232 } 233 } 234 235 public void setDescription(String description) { 236 this.description = description; 237 } 238 239 public void setElement(Element element) { 240 this.element = element; 241 } 242 243 public void setIsAbstract(boolean is_abstract) { 244 this.is_abstract = is_abstract; 245 } 246 247 public void setName(String name) { 248 this.name = name; 249 } 250 251 /** Method to set the value of the super_container. 252 * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance. 253 */ 254 public void setSuper(ArgumentContainer super_container) { 255 this.super_container = super_container; 256 } 257 258 public String toString() 259 { 260 if (element == null) { 261 return name; 262 } 263 264 if (name == null) { 265 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE); 266 } 267 268 StringBuffer text = new StringBuffer(" "); 269 if (!isAssigned()) { 270 text.append("#"); 271 } 272 text.append(name); 273 274 ArrayList arguments = getArguments(); 275 for (int i = 0; i < arguments.size(); i++) { 276 Argument argument = (Argument) arguments.get(i); 277 if (argument.isAssigned()) { 278 text.append(" "); 279 text.append(argument.toString()); 280 } 281 } 282 283 return text.toString(); 284 } 285 286 public ArrayList getArguments(boolean include_normal, boolean include_custom){ 287 return this; 288 } 288 289 }
Note:
See TracChangeset
for help on using the changeset viewer.