001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing.resourcehandling;
020:
021: import java.awt.*;
022: import java.io.*;
023: import java.net.*;
024: import java.util.*;
025: import java.util.List;
026:
027: import org.openharmonise.commons.net.*;
028: import org.openharmonise.him.metadata.range.swing.*;
029: import org.openharmonise.vfs.*;
030: import org.openharmonise.vfs.metadata.*;
031: import org.openharmonise.vfs.metadata.range.*;
032: import org.openharmonise.vfs.metadata.value.*;
033: import org.openharmonise.vfs.servers.*;
034:
035: /**
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class Reason {
042:
043: private ResourceRangeDisplay m_display = null;
044:
045: private Property m_prop = null;
046: private PropertyInstance m_propInst = null;
047: private int m_nTotal = 0;
048:
049: private int m_nMinOccurs = 0;
050: private int m_nMaxOccurs = -1;
051:
052: private ArrayList m_aContentTypes = new ArrayList();
053:
054: private ArrayList m_aHREFs = new ArrayList();
055:
056: private WarningsLabel m_warning = null;
057:
058: public Reason(PropertyInstance propInst,
059: ResourceRangeDisplay display) {
060: this .m_display = display;
061: this .m_propInst = propInst;
062: this .m_prop = propInst.getDefinition();
063: List domains = propInst.getDefinition().getDomains();
064: Iterator itor = domains.iterator();
065:
066: while (itor.hasNext()) {
067: Domain domain = (Domain) itor.next();
068: boolean bApplicableDomain = false;
069: String sFilePath = ((VersionedVirtualFile) this
070: .getPropertyInstance().getVirtualFile())
071: .getLogicalPath();
072: Iterator itor2 = domain.getPaths().iterator();
073: while (itor2.hasNext()) {
074: String sDomainPath = (String) itor2.next();
075: if (sFilePath.startsWith(sDomainPath)) {
076: bApplicableDomain = true;
077: }
078: }
079:
080: if (bApplicableDomain) {
081: if (this .m_nMinOccurs < domain.getMinOccurs()) {
082: this .m_nMinOccurs = domain.getMinOccurs();
083: }
084: if (this .m_nMaxOccurs == -1
085: || this .m_nMaxOccurs > domain.getMaxOccurs()) {
086: this .m_nMaxOccurs = domain.getMaxOccurs();
087: }
088: }
089: }
090:
091: if (this .m_prop.getRange() instanceof ResourceRange) {
092: List aContentTypes = ((ResourceRange) this .m_prop
093: .getRange()).getContentTypes();
094: if (aContentTypes.size() > 0) {
095: this .m_aContentTypes = (ArrayList) aContentTypes;
096: }
097: m_aHREFs = (ArrayList) ((ResourceRange) this .m_prop
098: .getRange()).getHREFs();
099: } else if (this .m_prop.getRange() instanceof CollectionRange) {
100: this .m_aContentTypes.add("COLLECTION");
101: m_aHREFs = (ArrayList) ((CollectionRange) this .m_prop
102: .getRange()).getHREFs();
103: }
104: }
105:
106: public PropertyInstance getPropertyInstance() {
107: return this .m_propInst;
108: }
109:
110: public ArrayList getHREFs() {
111: return this .m_aHREFs;
112: }
113:
114: public String getDisplayName() {
115: return this .m_prop.getDisplayName();
116: }
117:
118: public void addToTotal() {
119: this .m_nTotal++;
120: }
121:
122: public void removeFromTotal() {
123: this .m_nTotal--;
124: }
125:
126: public Property getProperty() {
127: return this .m_prop;
128: }
129:
130: public int getMinOccurs() {
131: return this .m_nMinOccurs;
132: }
133:
134: public int getMaxOccurs() {
135: return this .m_nMaxOccurs;
136: }
137:
138: public ArrayList getContentTypes() {
139: return this .m_aContentTypes;
140: }
141:
142: public WarningsLabel getWarningLabel() {
143: return this .m_warning;
144: }
145:
146: public void setWarningLabel(WarningsLabel warning) {
147: this .m_warning = warning;
148: }
149:
150: protected boolean isValidReasonForResource(
151: AbstractVirtualFileSystem vfs, String sPath) {
152: boolean bValid = true;
153:
154: VirtualFile vfResource = vfs.getVirtualFile(sPath)
155: .getResource();
156: if (vfResource.isDirectory()
157: && !this .getContentTypes().contains("COLLECTION")) {
158: bValid = false;
159: } else if (this .getContentTypes().contains("COLLECTION")
160: && this .getContentTypes().size() == 1
161: && !vfResource.isDirectory()) {
162: bValid = false;
163: }
164:
165: boolean bFound = false;
166:
167: VirtualFile vfLogicalPropFile = this .m_propInst
168: .getVirtualFile();
169: if (vfLogicalPropFile.getState() != VirtualFile.STATE_LIVE
170: && ((VersionedVirtualFile) vfLogicalPropFile)
171: .getLiveVersionPath() != null) {
172: vfLogicalPropFile = vfs.getVirtualFile(
173: ((VersionedVirtualFile) vfLogicalPropFile)
174: .getLiveVersionPath()).getResource();
175: }
176:
177: Iterator itor = this .getHREFs().iterator();
178: while (itor.hasNext()) {
179: String sHREF = (String) itor.next();
180: if ((sHREF.trim().equals(".") && vfResource.getFilePath()
181: .equals(vfLogicalPropFile.getFilePath()))
182: || vfResource.getFullPath().startsWith(sHREF)) {
183: bFound = true;
184: break;
185: }
186: }
187:
188: if (!bFound) {
189: bValid = false;
190: }
191: return bValid;
192: }
193:
194: public WarningsLabel buildWarning() {
195: String sWarning = "";
196: String sTopType = "resource";
197: if (this .getContentTypes().size() > 0) {
198: if (this .getContentTypes().contains("COLLECTION")) {
199: sTopType = "{collection}";
200: }
201: }
202:
203: if (this .getMinOccurs() == 0 && this .getMaxOccurs() == -1) {
204: sWarning = "Choose " + sTopType + "s";
205: } else if (this .getMinOccurs() > 0 && this .getMaxOccurs() == -1) {
206: sWarning = "Choose more than {" + this .getMinOccurs()
207: + "} {" + this .getDisplayName() + "}";
208: } else if (this .getMinOccurs() == 0 && this .getMaxOccurs() > 0) {
209: sWarning = "Choose less than {" + this .getMaxOccurs()
210: + "} {" + this .getDisplayName() + "}";
211: } else if (this .getMinOccurs() > 0
212: && this .getMaxOccurs() == this .getMinOccurs()) {
213: sWarning = "Choose {" + this .getMinOccurs() + "} {"
214: + this .getDisplayName() + "}";
215: } else if (this .getMinOccurs() > 0
216: && this .getMaxOccurs() > this .getMinOccurs()) {
217: sWarning = "Choose between {" + this .getMinOccurs()
218: + "} and {" + this .getMaxOccurs() + "} {"
219: + this .getDisplayName() + "}";
220: }
221:
222: if (this .getContentTypes().size() > 0) {
223: if (this .getContentTypes().contains("COLLECTION")) {
224: //NO-OP
225: } else if (this .getContentTypes().size() == 1) {
226: sWarning = sWarning
227: + " of type {"
228: + MimeTypeMapping
229: .getDescriptionForMimeType((String) this
230: .getContentTypes().get(0))
231: + "}";
232: } else {
233: sWarning = sWarning + " of type either";
234: Iterator itor = this .getContentTypes().iterator();
235: while (itor.hasNext()) {
236: String sType = (String) itor.next();
237: sWarning = sWarning
238: + " {"
239: + MimeTypeMapping
240: .getDescriptionForMimeType(sType)
241: + "}";
242: if (itor.hasNext()) {
243: sWarning = sWarning + " or ";
244: }
245: }
246: }
247: }
248:
249: if (this .getHREFs().size() > 0) {
250: if (this .getHREFs().size() == 1) {
251: String sHREF = (String) this .getHREFs().get(0);
252: try {
253: sHREF = URLEncoder.encode(sHREF, "UTF-8");
254: } catch (UnsupportedEncodingException e) {
255: e.printStackTrace();
256: }
257: URIWrapper uri = new URIWrapper(sHREF);
258: String sPath = (String) uri.getPathSegments().get(
259: uri.getPathSegments().size() - 1);
260: sWarning = sWarning + " from collection '{" + sPath
261: + "}'";
262: } else {
263: sWarning = sWarning + " from collections";
264: Iterator itor = this .getHREFs().iterator();
265: while (itor.hasNext()) {
266: String sHREF = (String) itor.next();
267: try {
268: sHREF = URLEncoder.encode(sHREF, "UTF-8");
269: } catch (UnsupportedEncodingException e) {
270: e.printStackTrace();
271: }
272: URIWrapper uri = new URIWrapper(sHREF);
273: String sPath = (String) uri.getPathSegments().get(
274: uri.getPathSegments().size() - 1);
275: sWarning = sWarning + " '{" + sPath + "}'";
276: if (itor.hasNext()) {
277: sWarning = sWarning + ", ";
278: }
279: }
280: }
281: }
282: sWarning = sWarning + ".";
283:
284: WarningsLabel warning = new WarningsLabel(sWarning);
285:
286: String fontName = "Dialog";
287: int fontSize = 11;
288: Font font = new Font(fontName, Font.PLAIN, fontSize);
289: warning.setFont(font);
290: this .setWarningLabel(warning);
291: return warning;
292: }
293:
294: public boolean isValid() {
295: return this .checkWarning();
296: }
297:
298: protected void addValue(String sPath) {
299: ResourceValue value = (ResourceValue) this .m_propInst
300: .getNewValueInstance();
301: value.setValue(sPath);
302: this .m_propInst.addValue(value);
303: this .m_display.validateTab();
304: }
305:
306: protected void removeValue(String sPath) {
307: List values = this .m_propInst.getValues();
308: ArrayList aRemoveValues = new ArrayList();
309: Iterator itor = values.iterator();
310: while (itor.hasNext()) {
311: ResourceValue value = (ResourceValue) itor.next();
312: if (value.getValue().equals(sPath)) {
313: aRemoveValues.add(value);
314: }
315: }
316: values.removeAll(aRemoveValues);
317: this .m_propInst.setValues(values);
318: this .m_display.validateTab();
319: }
320:
321: private boolean checkWarning() {
322: boolean bValid = true;
323: if (this .m_aHREFs.contains(".")) {
324: return bValid;
325: }
326:
327: this .m_warning.setAllHighlights(false);
328: int nValueCount = this .m_propInst.getValues().size();
329:
330: if (nValueCount < this .m_nMinOccurs) {
331: this .m_warning.setHighlight(String
332: .valueOf(this .m_nMinOccurs), true);
333: bValid = false;
334: }
335: if (this .m_nMaxOccurs != -1 && nValueCount > this .m_nMaxOccurs) {
336: this .m_warning.setHighlight(String
337: .valueOf(this .m_nMaxOccurs), true);
338: bValid = false;
339: }
340:
341: Iterator itor = this .m_propInst.getValues().iterator();
342: while (itor.hasNext()) {
343: ResourceValue value = (ResourceValue) itor.next();
344: String sPath = value.getValue();
345:
346: VirtualFile vfFile = ServerList.getInstance()
347: .getHarmoniseServer().getVFS()
348: .getVirtualFile(sPath).getResource();
349: if (vfFile != null) {
350: if (this .getContentTypes().size() > 0) {
351: if (this .getContentTypes().contains("COLLECTION")) {
352: if (!vfFile.isDirectory()) {
353: bValid = false;
354: this .m_warning.setHighlight("collection",
355: true);
356: }
357: } else {
358: String sContentType = ServerList.getInstance()
359: .getHarmoniseServer().getVFS()
360: .getVirtualFileSystemView()
361: .getContentType(vfFile);
362: if (vfFile.isDirectory()
363: || !this .getContentTypes().contains(
364: sContentType.trim())) {
365: bValid = false;
366: Iterator ctItor = this .getContentTypes()
367: .iterator();
368: while (ctItor.hasNext()) {
369: String sMimeType = (String) ctItor
370: .next();
371: this .m_warning
372: .setHighlight(
373: MimeTypeMapping
374: .getDescriptionForMimeType(sMimeType),
375: true);
376: }
377: }
378: }
379: } else if (this .getContentTypes().size() < 1
380: && vfFile.isDirectory()) {
381: bValid = false;
382: }
383:
384: if (this .getHREFs().size() > 0) {
385: boolean bPassed = false;
386: Iterator HREFitor = this .getHREFs().iterator();
387: while (HREFitor.hasNext()) {
388: String sHREF = (String) HREFitor.next();
389: try {
390: sHREF = URLEncoder.encode(sHREF, "UTF-8");
391: } catch (UnsupportedEncodingException e) {
392: // TODO Auto-generated catch block
393: e.printStackTrace();
394: }
395: URIWrapper uri = new URIWrapper(sHREF);
396: String cleanHREF = uri.getPath().replaceFirst(
397: vfFile.getVFS().getInitialPath(), "");
398: try {
399: cleanHREF = URLDecoder.decode(cleanHREF,
400: "UTF-8");
401: } catch (UnsupportedEncodingException e1) {
402: e1.printStackTrace();
403: }
404: if (vfFile.getFullPath().startsWith(cleanHREF)) {
405: bPassed = true;
406: }
407: }
408: if (!bPassed) {
409: bValid = false;
410: Iterator colItor = this .getHREFs().iterator();
411: while (colItor.hasNext()) {
412: String sHREF = (String) colItor.next();
413: try {
414: sHREF = URLEncoder.encode(sHREF,
415: "UTF-8");
416: } catch (UnsupportedEncodingException e) {
417: e.printStackTrace();
418: }
419: URIWrapper uri = new URIWrapper(sHREF);
420: this .m_warning.setHighlight((String) uri
421: .getPathSegments().get(
422: uri.getPathSegments()
423: .size() - 1), true);
424: }
425: }
426: }
427: }
428:
429: }
430:
431: return bValid;
432: }
433:
434: }
|