| 1 19 package org.openharmonise.him.metadata.range.swing.resourcehandling; 20 21 import java.awt.*; 22 import java.io.*; 23 import java.net.*; 24 import java.util.*; 25 import java.util.List ; 26 27 import org.openharmonise.commons.net.*; 28 import org.openharmonise.him.metadata.range.swing.*; 29 import org.openharmonise.vfs.*; 30 import org.openharmonise.vfs.metadata.*; 31 import org.openharmonise.vfs.metadata.range.*; 32 import org.openharmonise.vfs.metadata.value.*; 33 import org.openharmonise.vfs.servers.*; 34 35 36 42 public class Reason { 43 44 private ResourceRangeDisplay m_display = null; 45 46 private Property m_prop = null; 47 private PropertyInstance m_propInst = null; 48 private int m_nTotal = 0; 49 50 private int m_nMinOccurs = 0; 51 private int m_nMaxOccurs = -1; 52 53 private ArrayList m_aContentTypes = new ArrayList(); 54 55 private ArrayList m_aHREFs = new ArrayList(); 56 57 private WarningsLabel m_warning = null; 58 59 public Reason(PropertyInstance propInst, ResourceRangeDisplay display) { 60 this.m_display = display; 61 this.m_propInst = propInst; 62 this.m_prop = propInst.getDefinition(); 63 List domains = propInst.getDefinition().getDomains(); 64 Iterator itor = domains.iterator(); 65 66 67 68 while(itor.hasNext()) { 69 Domain domain = (Domain)itor.next(); 70 boolean bApplicableDomain = false; 71 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 72 Iterator itor2 = domain.getPaths().iterator(); 73 while (itor2.hasNext()) { 74 String sDomainPath = (String ) itor2.next(); 75 if(sFilePath.startsWith(sDomainPath)) { 76 bApplicableDomain=true; 77 } 78 } 79 80 if(bApplicableDomain) { 81 if (this.m_nMinOccurs < domain.getMinOccurs()) { 82 this.m_nMinOccurs = domain.getMinOccurs(); 83 } 84 if (this.m_nMaxOccurs==-1 || this.m_nMaxOccurs > domain.getMaxOccurs()) { 85 this.m_nMaxOccurs = domain.getMaxOccurs(); 86 } 87 } 88 } 89 90 91 if(this.m_prop.getRange() instanceof ResourceRange) { 92 List aContentTypes = ((ResourceRange)this.m_prop.getRange()).getContentTypes(); 93 if(aContentTypes.size()>0) { 94 this.m_aContentTypes = (ArrayList) aContentTypes; 95 } 96 m_aHREFs = (ArrayList) ((ResourceRange)this.m_prop.getRange()).getHREFs(); 97 } else if(this.m_prop.getRange() instanceof CollectionRange) { 98 this.m_aContentTypes.add("COLLECTION"); 99 m_aHREFs = (ArrayList) ((CollectionRange)this.m_prop.getRange()).getHREFs(); 100 } 101 } 102 103 public PropertyInstance getPropertyInstance() { 104 return this.m_propInst; 105 } 106 107 public ArrayList getHREFs() { 108 return this.m_aHREFs; 109 } 110 111 public String getDisplayName() { 112 return this.m_prop.getDisplayName(); 113 } 114 115 public void addToTotal() { 116 this.m_nTotal++; 117 } 118 119 public void removeFromTotal() { 120 this.m_nTotal--; 121 } 122 123 public Property getProperty() { 124 return this.m_prop; 125 } 126 127 public int getMinOccurs() { 128 return this.m_nMinOccurs; 129 } 130 131 public int getMaxOccurs() { 132 return this.m_nMaxOccurs; 133 } 134 135 public ArrayList getContentTypes() { 136 return this.m_aContentTypes; 137 } 138 139 public WarningsLabel getWarningLabel() { 140 return this.m_warning; 141 } 142 143 public void setWarningLabel(WarningsLabel warning) { 144 this.m_warning = warning; 145 } 146 147 protected boolean isValidReasonForResource(AbstractVirtualFileSystem vfs, String sPath) { 148 boolean bValid = true; 149 150 VirtualFile vfResource = vfs.getVirtualFile(sPath).getResource(); 151 if(vfResource.isDirectory() && !this.getContentTypes().contains( "COLLECTION" ) ) { 152 bValid=false; 153 } else if(this.getContentTypes().contains("COLLECTION") && this.getContentTypes().size()==1 && !vfResource.isDirectory()) { 154 bValid=false; 155 } 156 157 boolean bFound = false; 158 159 VirtualFile vfLogicalPropFile = this.m_propInst.getVirtualFile(); 160 if(vfLogicalPropFile.getState()!=VirtualFile.STATE_LIVE && ((VersionedVirtualFile)vfLogicalPropFile).getLiveVersionPath()!=null) { 161 vfLogicalPropFile = vfs.getVirtualFile( ((VersionedVirtualFile)vfLogicalPropFile).getLiveVersionPath() ).getResource(); 162 } 163 164 Iterator itor = this.getHREFs().iterator(); 165 while (itor.hasNext()) { 166 String sHREF = (String ) itor.next(); 167 if((sHREF.trim().equals(".") && vfResource.getFilePath().equals(vfLogicalPropFile.getFilePath())) || vfResource.getFullPath().startsWith(sHREF)) { 168 bFound=true; 169 break; 170 } 171 } 172 173 if(!bFound) { 174 bValid=false; 175 } 176 return bValid; 177 } 178 179 public WarningsLabel buildWarning() { 180 String sWarning = ""; 181 String sTopType = "resource"; 182 if(this.getContentTypes().size()>0) { 183 if(this.getContentTypes().contains("COLLECTION")) { 184 sTopType = "{collection}"; 185 } 186 } 187 188 if(this.getMinOccurs()==0 && this.getMaxOccurs()==-1) { 189 sWarning = "Choose " + sTopType + "s"; 190 } else if(this.getMinOccurs()>0 && this.getMaxOccurs()==-1) { 191 sWarning = "Choose more than {" + this.getMinOccurs() + "} {" + this.getDisplayName() + "}"; 192 } else if(this.getMinOccurs()==0 && this.getMaxOccurs()>0) { 193 sWarning = "Choose less than {" + this.getMaxOccurs() + "} {" + this.getDisplayName() + "}"; 194 } else if(this.getMinOccurs()>0 && this.getMaxOccurs()==this.getMinOccurs()) { 195 sWarning = "Choose {" + this.getMinOccurs() + "} {" + this.getDisplayName() + "}"; 196 } else if(this.getMinOccurs()>0 && this.getMaxOccurs()>this.getMinOccurs()) { 197 sWarning = "Choose between {" + this.getMinOccurs() + "} and {" + this.getMaxOccurs() + "} {" + this.getDisplayName() + "}"; 198 } 199 200 if(this.getContentTypes().size()>0) { 201 if(this.getContentTypes().contains("COLLECTION")) { 202 } else if(this.getContentTypes().size()==1) { 204 sWarning = sWarning + " of type {" + MimeTypeMapping.getDescriptionForMimeType( (String )this.getContentTypes().get(0) ) + "}"; 205 } else { 206 sWarning = sWarning + " of type either"; 207 Iterator itor = this.getContentTypes().iterator(); 208 while(itor.hasNext()) { 209 String sType = (String )itor.next(); 210 sWarning = sWarning + " {" + MimeTypeMapping.getDescriptionForMimeType( sType ) + "}"; 211 if(itor.hasNext()) { 212 sWarning = sWarning + " or "; 213 } 214 } 215 } 216 } 217 218 if(this.getHREFs().size()>0) { 219 if(this.getHREFs().size()==1) { 220 String sHREF = (String )this.getHREFs().get(0); 221 try { 222 sHREF = URLEncoder.encode(sHREF,"UTF-8"); 223 } catch (UnsupportedEncodingException e) { 224 e.printStackTrace(); 225 } 226 URIWrapper uri = new URIWrapper(sHREF); 227 String sPath = (String ) uri.getPathSegments().get(uri.getPathSegments().size()-1); 228 sWarning = sWarning + " from collection '{" + sPath + "}'"; 229 } else { 230 sWarning = sWarning + " from collections"; 231 Iterator itor = this.getHREFs().iterator(); 232 while(itor.hasNext()) { 233 String sHREF = (String )itor.next(); 234 try { 235 sHREF = URLEncoder.encode(sHREF,"UTF-8"); 236 } catch (UnsupportedEncodingException e) { 237 e.printStackTrace(); 238 } 239 URIWrapper uri = new URIWrapper(sHREF); 240 String sPath = (String ) uri.getPathSegments().get(uri.getPathSegments().size()-1); 241 sWarning = sWarning + " '{" + sPath + "}'"; 242 if(itor.hasNext()) { 243 sWarning = sWarning + ", "; 244 } 245 } 246 } 247 } 248 sWarning = sWarning + "."; 249 250 WarningsLabel warning = new WarningsLabel(sWarning); 251 252 253 String fontName = "Dialog"; 254 int fontSize = 11; 255 Font font = new Font(fontName, Font.PLAIN, fontSize); 256 warning.setFont(font); 257 this.setWarningLabel(warning); 258 return warning; 259 } 260 261 public boolean isValid() { 262 return this.checkWarning(); 263 } 264 265 protected void addValue(String sPath) { 266 ResourceValue value = (ResourceValue) this.m_propInst.getNewValueInstance(); 267 value.setValue(sPath); 268 this.m_propInst.addValue(value); 269 this.m_display.validateTab(); 270 } 271 272 protected void removeValue(String sPath) { 273 List values = this.m_propInst.getValues(); 274 ArrayList aRemoveValues = new ArrayList(); 275 Iterator itor = values.iterator(); 276 while (itor.hasNext()) { 277 ResourceValue value = (ResourceValue) itor.next(); 278 if(value.getValue().equals(sPath)) { 279 aRemoveValues.add(value); 280 } 281 } 282 values.removeAll(aRemoveValues); 283 this.m_propInst.setValues(values); 284 this.m_display.validateTab(); 285 } 286 287 private boolean checkWarning() { 288 boolean bValid = true; 289 if(this.m_aHREFs.contains(".")) { 290 return bValid; 291 } 292 293 this.m_warning.setAllHighlights(false); 294 int nValueCount = this.m_propInst.getValues().size(); 295 296 if(nValueCount<this.m_nMinOccurs) { 297 this.m_warning.setHighlight(String.valueOf(this.m_nMinOccurs), true); 298 bValid = false; 299 } 300 if(this.m_nMaxOccurs!=-1 && nValueCount>this.m_nMaxOccurs) { 301 this.m_warning.setHighlight(String.valueOf(this.m_nMaxOccurs), true); 302 bValid = false; 303 } 304 305 Iterator itor = this.m_propInst.getValues().iterator(); 306 while (itor.hasNext()) { 307 ResourceValue value = (ResourceValue) itor.next(); 308 String sPath = value.getValue(); 309 310 VirtualFile vfFile = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFile(sPath).getResource(); 311 if(vfFile!=null) { 312 if(this.getContentTypes().size()>0) { 313 if(this.getContentTypes().contains("COLLECTION")) { 314 if(!vfFile.isDirectory()) { 315 bValid = false; 316 this.m_warning.setHighlight("collection", true); 317 } 318 } else { 319 String sContentType = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFileSystemView().getContentType(vfFile); 320 if(vfFile.isDirectory() || !this.getContentTypes().contains(sContentType.trim())) { 321 bValid = false; 322 Iterator ctItor = this.getContentTypes().iterator(); 323 while(ctItor.hasNext()) { 324 String sMimeType = (String )ctItor.next(); 325 this.m_warning.setHighlight(MimeTypeMapping.getDescriptionForMimeType(sMimeType), true); 326 } 327 } 328 } 329 } else if(this.getContentTypes().size()<1 && vfFile.isDirectory()) { 330 bValid = false; 331 } 332 333 if(this.getHREFs().size()>0) { 334 boolean bPassed = false; 335 Iterator HREFitor = this.getHREFs().iterator(); 336 while(HREFitor.hasNext()) { 337 String sHREF = (String )HREFitor.next(); 338 try { 339 sHREF = URLEncoder.encode(sHREF,"UTF-8"); 340 } catch (UnsupportedEncodingException e) { 341 e.printStackTrace(); 343 } 344 URIWrapper uri = new URIWrapper(sHREF); 345 String cleanHREF = uri.getPath().replaceFirst(vfFile.getVFS().getInitialPath(), ""); 346 try { 347 cleanHREF = URLDecoder.decode(cleanHREF,"UTF-8"); 348 } catch (UnsupportedEncodingException e1) { 349 e1.printStackTrace(); 350 } 351 if(vfFile.getFullPath().startsWith(cleanHREF)) { 352 bPassed=true; 353 } 354 } 355 if(!bPassed) { 356 bValid = false; 357 Iterator colItor = this.getHREFs().iterator(); 358 while(colItor.hasNext()) { 359 String sHREF = (String )colItor.next(); 360 try { 361 sHREF = URLEncoder.encode(sHREF,"UTF-8"); 362 } catch (UnsupportedEncodingException e) { 363 e.printStackTrace(); 364 } 365 URIWrapper uri = new URIWrapper(sHREF); 366 this.m_warning.setHighlight((String ) uri.getPathSegments().get(uri.getPathSegments().size()-1), true); 367 } 368 } 369 } 370 } 371 372 } 373 374 return bValid; 375 } 376 377 } 378 | Popular Tags |