1 25 package org.archive.crawler.settings; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.management.AttributeNotFoundException ; 31 import javax.management.InvalidAttributeValueException ; 32 33 import org.archive.crawler.settings.Constraint.FailedCheck; 34 35 55 public class MapType extends ComplexType { 56 57 private static final long serialVersionUID = -3694800285930202700L; 58 59 60 private final Type definition; 61 62 67 public MapType(String name, String description) { 68 this(name, description, Object .class); 69 } 70 71 77 public MapType(String name, String description, Class type) { 78 super(name, description); 79 this.definition = new SimpleType("dummy", "dummy", null); 80 this.definition.setLegalValueType(type); 81 } 82 83 90 public Type addElement(CrawlerSettings settings, Type element) 91 throws InvalidAttributeValueException { 92 settings = settings == null ? globalSettings() : settings; 93 94 if (settings != globalSettings()) { 95 try { 96 getAttribute(settings, element.getName()); 97 throw new IllegalArgumentException ( 99 "Duplicate element: " + element.getName()); 100 101 } catch (AttributeNotFoundException e) { 102 } 104 } 105 106 if (!(element instanceof MapType)) { 107 return super.addElement(settings, element); 108 } else { 109 throw new IllegalArgumentException ("Nested maps are not allowed."); 110 } 111 } 112 113 121 public Object removeElement(CrawlerSettings settings, String name) 122 throws AttributeNotFoundException { 123 settings = settings == null ? globalSettings() : settings; 124 return settings.getData(this).removeElement(name); 125 } 126 127 136 public boolean moveElementUp(CrawlerSettings settings, String name) 137 throws AttributeNotFoundException { 138 settings = settings == null ? globalSettings() : settings; 139 return settings.getData(this).moveElementUp(name); 140 } 141 142 151 public boolean moveElementDown(CrawlerSettings settings, String name) 152 throws AttributeNotFoundException { 153 settings = settings == null ? globalSettings() : settings; 154 return settings.getData(this).moveElementDown(name); 155 } 156 157 163 public boolean isEmpty(Object context) { 164 Context ctxt = getSettingsFromObject(context); 165 166 DataContainer data = getDataContainerRecursive(ctxt); 167 while (data != null) { 168 if (data.hasAttributes()) { 169 return false; 170 } 171 ctxt.settings = data.getSettings().getParent(); 172 data = getDataContainerRecursive(ctxt); 173 } 174 return true; 175 } 176 177 183 public int size(Object context) { 184 Context ctxt = getSettingsFromObject(context); 185 186 int size = 0; 187 DataContainer data = getDataContainerRecursive(ctxt); 188 while (data != null) { 189 size += data.size(); 190 ctxt.settings = data.getSettings().getParent(); 191 data = getDataContainerRecursive(ctxt); 192 } 193 return size; 194 } 195 196 203 Type getDefinition(String attributeName) { 204 return definition; 205 } 206 207 212 public Class getContentType() { 213 return this.definition.getLegalValueType(); 214 } 215 216 FailedCheck checkValue(CrawlerSettings settings, String attributeName, 217 Type definition, Object value) { 218 FailedCheck res = super.checkValue(settings, attributeName, definition, 219 value); 220 221 definition = super.getDefinition(attributeName); 222 223 List constraints = definition != null ? definition.getConstraints() 225 : null; 226 if (constraints != null) { 227 FailedCheck ac = null; 228 for (Iterator it = constraints.iterator(); it.hasNext() 229 && ac == null;) { 230 ac = ((Constraint) it.next()).check(settings, this, definition, 231 value); 232 if (res == null 233 || ac.getLevel().intValue() >= res.getLevel() 234 .intValue()) { 235 res = ac; 236 } 237 } 238 } 239 240 return res; 241 } 242 243 246 public void addConstraint(Constraint constraint) { 247 definition.addConstraint(constraint); 248 } 249 250 253 public List getConstraints() { 254 return definition.getConstraints(); 255 } 256 } 257 | Popular Tags |