1 package org.sapia.magnet.domain; 2 3 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.sapia.magnet.render.AbstractRenderable; 13 import org.sapia.magnet.render.MagnetContext; 14 import org.sapia.magnet.render.RenderingException; 15 16 17 28 public class Path extends AbstractRenderable { 29 30 34 35 public static final String PROTOCOL_FILE = "file"; 36 37 38 public static final String PROTOCOL_HTTP = "http"; 39 40 41 public static final String DEFAULT_PROTOCOL = "file"; 42 43 44 public static final String DEFAULT_HOST = "localhost"; 45 46 47 public static final String SORTING_ASCENDING = "ascending"; 48 49 50 public static final String SORTING_DESCENDING = "descending"; 51 52 56 57 private String _theDirectory; 58 59 60 private String _theHost; 61 62 63 private String _theProtocol; 64 65 66 private String _theSorting; 67 68 69 private List _theIncludePatterns; 70 71 72 private List _theExcludePatterns; 73 74 75 private Collection _theSelectedResources; 76 77 81 84 public Path() { 85 _theProtocol = DEFAULT_PROTOCOL; 86 _theHost = DEFAULT_HOST; 87 _theIncludePatterns = new ArrayList (); 88 _theExcludePatterns = new ArrayList (); 89 } 90 91 95 100 public String getProtocol() { 101 return _theProtocol; 102 } 103 104 109 public String getHost() { 110 return _theHost; 111 } 112 113 118 public String getDirectory() { 119 return _theDirectory; 120 } 121 122 127 public String getSorting() { 128 return _theSorting; 129 } 130 131 136 public Collection getIncludes() { 137 return _theIncludePatterns; 138 } 139 140 145 public Collection getExcludes() { 146 return _theExcludePatterns; 147 } 148 149 154 public Collection getSelectedResources() { 155 return _theSelectedResources; 156 } 157 158 162 167 public void setProtocol(String aProtocol) { 168 _theProtocol = aProtocol; 169 } 170 171 176 public void setHost(String aHost) { 177 _theHost = aHost; 178 } 179 180 185 public void setDirectory(String aDirectory) { 186 _theDirectory = aDirectory; 187 } 188 189 194 public void setSorting(String aSorting) { 195 _theSorting = aSorting; 196 } 197 198 204 public void addInclude(Include anInclude) { 205 if (anInclude == null) { 206 throw new IllegalArgumentException ("The include pattern passed in is null"); 207 } 208 209 _theIncludePatterns.add(anInclude); 210 } 211 212 218 public void removeInclude(Include anInclude) { 219 if (anInclude == null) { 220 throw new IllegalArgumentException ("The include pattern passed in is null"); 221 } 222 223 _theIncludePatterns.remove(anInclude); 224 } 225 226 229 public void clearIncludes() { 230 _theIncludePatterns.clear(); 231 } 232 233 239 public void addExclude(Exclude anExclude) { 240 if (anExclude == null) { 241 throw new IllegalArgumentException ("The exclude pattern passed in is null"); 242 } 243 244 _theExcludePatterns.add(anExclude); 245 } 246 247 253 public void removeExclude(Exclude anExclude) { 254 if (anExclude == null) { 255 throw new IllegalArgumentException ("The exclude pattern passed in is null"); 256 } 257 258 _theExcludePatterns.remove(anExclude); 259 } 260 261 264 public void clearExcludes() { 265 _theExcludePatterns.clear(); 266 } 267 268 272 278 public void render(MagnetContext aContext) throws RenderingException { 279 try { 281 _theProtocol = resolveValue(aContext, _theProtocol); 282 _theHost = resolveValue(aContext, _theHost); 283 _theDirectory = resolveValue(aContext, _theDirectory); 284 } catch (RenderingException re) { 285 StringBuffer aBuffer = new StringBuffer (); 286 aBuffer.append("Unable to resolve an attribute of the path "). 287 append(" protocol=").append(_theProtocol). 288 append(" host=").append(_theHost). 289 append(" directory=").append(_theDirectory); 290 291 throw new RenderingException(aBuffer.toString(), re); 292 } 293 294 try { 295 for (Iterator it = _theIncludePatterns.iterator(); it.hasNext(); ) { 297 Include anInclude = (Include) it.next(); 298 anInclude.render(aContext); 299 } 300 301 for (Iterator it = _theExcludePatterns.iterator(); it.hasNext(); ) { 303 Exclude anExclude = (Exclude) it.next(); 304 anExclude.render(aContext); 305 } 306 307 ProtocolHandlerIF aHandler = HandlerFactory.getInstance().createProtocolHandler(_theProtocol); 309 _theSelectedResources = aHandler.resolveResources(this, _theSorting); 310 311 } catch (RenderingException re) { 312 StringBuffer aBuffer = new StringBuffer (); 313 aBuffer.append("Unable to render the patterns of the path "). 314 append(" protocol=").append(_theProtocol). 315 append(" host=").append(_theHost). 316 append(" directory=").append(_theDirectory); 317 318 throw new RenderingException(aBuffer.toString(), re); 319 320 } catch (ObjectCreationException oce) { 321 StringBuffer aBuffer = new StringBuffer ("Unable to create the protocol handler for the path: "); 322 aBuffer.append("protocol=").append(_theProtocol). 323 append(" host=").append(_theHost). 324 append(" directory=").append(_theDirectory); 325 throw new RenderingException(aBuffer.toString(), oce); 326 } 327 } 328 329 334 public String toString() { 335 StringBuffer aBuffer = new StringBuffer (super.toString()); 336 aBuffer.append("[protocol=").append(_theProtocol). 337 append(" host=").append(_theHost). 338 append(" directory=").append(_theDirectory). 339 append(" includes=").append(_theIncludePatterns). 340 append(" exludes=").append(_theExcludePatterns). 341 append(" resources=").append(_theSelectedResources). 342 append("]"); 343 344 return aBuffer.toString(); 345 } 346 } 347 | Popular Tags |