KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > ComparableResourceImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/ComparableResourceImpl.java,v 1.9.2.1 2004/09/01 15:40:12 pnever Exp $
3  * $Revision: 1.9.2.1 $
4  * $Date: 2004/09/01 15:40:12 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24
25 package org.apache.slide.search.basic;
26 import java.util.Date;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Set;
31 import org.apache.slide.common.Domain;
32 import org.apache.slide.common.PropertyName;
33 import org.apache.slide.common.SlideException;
34 import org.apache.slide.common.SlideToken;
35 import org.apache.slide.common.UriPath;
36 import org.apache.slide.content.Content;
37 import org.apache.slide.content.NodeProperty;
38 import org.apache.slide.content.NodeProperty.NamespaceCache;
39 import org.apache.slide.content.NodeRevisionContent;
40 import org.apache.slide.content.NodeRevisionDescriptor;
41 import org.apache.slide.content.NodeRevisionDescriptors;
42 import org.apache.slide.search.CompareHint;
43 import org.apache.slide.search.PropertyProvider;
44 import org.apache.slide.search.QueryScope;
45 import org.apache.slide.search.SearchToken;
46 import org.apache.slide.search.SlideUri;
47 import org.apache.slide.search.basic.Literals;
48 import org.apache.slide.security.AccessDeniedException;
49 import org.apache.slide.structure.ObjectNode;
50
51 /**
52  * Represents one Resource. Implements operators like greaterThan,
53  * equals, ...
54  *
55  * @version $Revision: 1.9.2.1 $
56  */

57 public class ComparableResourceImpl implements ComparableResource {
58     
59     // TODO: properties with namespace...
60

61     
62     /** The ObjectNode of this resource */
63     private ObjectNode objectNode;
64     
65     /** the latest revision of this resource */
66     protected NodeRevisionDescriptor revisionDescriptor;
67     
68     private Content contentHelper;
69     private NodeRevisionDescriptors revisionDescriptors;
70     private SlideToken slideToken;
71     private SearchToken searchToken;
72     
73     /**
74      * The PropertyProvider to use (may be <code>null</code>).
75      */

76     protected PropertyProvider propertyProvider = null;
77     
78     /**
79      * The scope.
80      */

81     protected QueryScope scope = null;
82     
83     
84     /** coantains allowed on */
85     static final private Set containsForbiddenOn = new HashSet();
86     static {
87         containsForbiddenOn.add ("image/gif");
88         containsForbiddenOn.add ("image/jpeg");
89         containsForbiddenOn.add ("image/pjpeg");
90         containsForbiddenOn.add ("image/jpg");
91         containsForbiddenOn.add ("audio/mpg");
92         containsForbiddenOn.add ("audio/mpeg");
93     };
94     
95     private static final int EQ = 1;
96     private static final int GT = 2;
97     private static final int LT = 3;
98     private static final int GTE = 4;
99     private static final int LTE = 5;
100     
101     
102     
103     /**
104      * Constructs a RequestedResource.
105      *
106      * @param objectNode the ObjectNode for this resource
107      * @param searchToken the SearchToken for this request
108      * @param scope the scope of the query.
109      * @param propertyProvider the PropertyProvider to use (may be
110      * <code>null</code>).
111      *
112      * @throws BadQueryException
113      */

114     public ComparableResourceImpl (ObjectNode objectNode, SearchToken searchToken, QueryScope scope, PropertyProvider propertyProvider)
115         throws SlideException
116     {
117         this (objectNode, searchToken.getSlideToken(), searchToken.getContentHelper(), scope, propertyProvider);
118         this.searchToken = searchToken;
119     }
120     
121     
122     // private boolean isCollection;
123

124     /**
125      * Constructs a RequestedResource.
126      *
127      * @param objectNode the ObjectNode for this resource
128      * @param slideToken the slideToken for this request
129      * @param contentHelper you won't believe, the ContentHelper
130      * @param scope the scope of the query.
131      * @param propertyProvider the PropertyProvider to use (may be
132      * <code>null</code>).
133      *
134      * @throws BadQueryException
135      */

136     public ComparableResourceImpl (ObjectNode objectNode, SlideToken slideToken, Content contentHelper, QueryScope scope, PropertyProvider propertyProvider) throws SlideException {
137         
138         this.objectNode = objectNode;
139         this.contentHelper = contentHelper;
140         this.slideToken = slideToken;
141         this.propertyProvider = propertyProvider;
142         this.scope = scope;
143         
144         try {
145             revisionDescriptors =
146                 contentHelper.retrieve (slideToken, objectNode.getUri());
147             
148             try {
149                 revisionDescriptor =
150                     contentHelper.retrieve (slideToken, revisionDescriptors);
151                 
152             }
153             catch (org.apache.slide.content.RevisionDescriptorNotFoundException e) {
154                 // The object doesn't have any revision, we create a dummy
155
// NodeRevisionDescriptor object
156
// isCollection = true;
157
revisionDescriptor = new NodeRevisionDescriptor(0);
158                 revisionDescriptor.setName(new UriPath(objectNode.getUri()).lastSegment());
159             }
160         }
161         catch (AccessDeniedException e) {
162             e.fillInStackTrace();
163             throw e;
164         }
165         
166         catch (Exception e) {
167             e.printStackTrace();
168             throw new SlideException (e.getMessage()); // TODO: clean exception handling
169
}
170     }
171     protected ComparableResourceImpl () {}
172     
173     /**
174      * Checks, if a property, represented by its name and value (as String),
175      * is greater than the matching property within this item.
176      *
177      * @param propName the name of the property to check
178      * @param literal the value as String to check again
179      *
180      * @return Literals.TRUE, Literals.FALSE or Literals.UNKNOWN
181      *
182      */

183     public int greaterThan (String propName, String propNamespace, String literal) {
184         return compare (propName, propNamespace, literal, GT);
185     }
186     
187     public int greaterThanEquals (String propName, String propNamespace, String literal) {
188         return compare (propName, propNamespace, literal, GTE);
189     }
190     
191     public int lowerThan (String propName, String propNamespace, String literal) {
192         return compare (propName, propNamespace, literal, LT);
193     }
194     
195     public int lowerThanEquals (String propName, String propNamespace, String literal) {
196         return compare (propName, propNamespace, literal, LTE);
197     }
198     
199     /**
200      * Checks, if a property, represented by its name and value (as String),
201      * is EQUAL the matching property within this item.
202      *
203      * @param propName the name of the property to check
204      * @param literal the value as String to check again
205      *
206      * @return Literals.TRUE, Literals.FALSE or Literals.UNKNOWN
207      *
208      */

209     public int equals (String propName, String propNamespace, String literal) {
210         return compare (propName, propNamespace, literal, EQ);
211     }
212     
213     /**
214      * checks, if a properties' value contains a literal
215      *
216      * @param propName a String
217      * @param literal a String
218      *
219      * @return Literal.TRUE, if propName's value contains literal
220      *
221      */

222     public int propContains (String propName, String propNamespace, String literal) {
223         Object o = getThisValue (propName, propNamespace);
224         
225         if (o instanceof String == false)
226             return Literals.UNKNOWN;
227         
228         String thisValue = (String)o;
229         if (thisValue.indexOf(literal) == -1)
230             return Literals.FALSE;
231         
232         return Literals.TRUE;
233     }
234     
235     /**
236      * href accessor
237      *
238      * @return the href of this item
239      */

240     public String getInternalHref() {
241         return objectNode.getUri();
242     }
243     
244     /**
245      * Method getExternalHref
246      *
247      * @return a String
248      * @deprecated
249      */

250     public String getExternalHref () {
251         SlideUri slideContext = searchToken.getSlideContext();
252         return slideContext.getContextPath (objectNode.getUri());
253     }
254     
255     
256     /**
257      * Method getThisValue
258      *
259      * @param propName the property name
260      *
261      * @return the value of the property within this item
262      */

263     public Object getThisValue (String propName, String propNamespace) {
264         Object result = null;
265         // special handling for properties as contenlength, contenttype, ...
266

267         // all other properties...
268
NodeProperty property = null;
269         try {
270             property = getProperty(propName, propNamespace);
271         }
272         catch (SlideException e) {
273             Domain.error("ComparableResourceImpl.getThisValue()", e);
274         }
275         result = (property == null) ? null : property.getValue();
276         
277         return result;
278     }
279     
280     /**
281      * Returns the NodeProperty with the given name and
282      * {@link org.apache.slide.content.NodeProperty.NamespaceCache#DEFAULT_URI
283      * default} namespace.
284      *
285      * @param propName the name of the property.
286      *
287      * @return the requested NodeProperty.
288      */

289     protected NodeProperty getProperty(String propName) {
290         NodeProperty property = null;
291         try {
292             property = getProperty(propName, NamespaceCache.DEFAULT_URI);
293         }
294         catch (SlideException e) {
295             Domain.error("ComparableResourceImpl.getProperty()", e);
296         }
297         return property;
298     }
299     
300     
301     /**
302      * Returns the URI of the resource.
303      *
304      * @return the URI of the resource.
305      */

306     public String getUri() {
307         return getInternalHref();
308     }
309     
310     /**
311      * Returns the property with the given <code>propertyName</code>.
312      *
313      * @param propertyName the PropertyName of the property.
314      *
315      * @return the property with the given <code>npropertyNameame</code>.
316      *
317      * @throws SlideException
318      */

319     public NodeProperty getProperty(PropertyName propertyName) throws SlideException {
320         return getProperty(propertyName.getName(), propertyName.getNamespace());
321     }
322     
323     /**
324      * Returns the property with the given <code>name</code> and
325      * <code>namespace</code>.
326      *
327      * @param name the name of the property.
328      * @param namespace the namespace URI of the property.
329      *
330      * @return the property with the given <code>name</code> and
331      * <code>namespace</code>.
332      *
333      * @throws SlideException
334      */

335     public NodeProperty getProperty(String name, String namespace) throws SlideException {
336         
337         NodeProperty property = null;
338         if ( (propertyProvider != null) && propertyProvider.isSupportedProperty(getUri(), name, namespace) ) {
339             property = propertyProvider.getProperty(getUri(), name, namespace);
340         }
341         else {
342             property = revisionDescriptor.getProperty (name, namespace);
343         }
344         return property;
345     }
346     
347     /**
348      * Returns an Iterator of PropertyName of all properties.
349      *
350      * @return an Iterator of PropertyName.
351      *
352      * @throws SlideException
353      */

354     public Iterator getAllPropertiesNames() throws SlideException {
355         
356         Set propertyNamesSet = new HashSet();
357         Enumeration enum = revisionDescriptor.enumerateProperties();
358         NodeProperty property = null;
359         if (enum != null) {
360             while (enum.hasMoreElements()) {
361                 property = (NodeProperty)enum.nextElement();
362                 propertyNamesSet.add(new PropertyName(property.getName(),
363                                                        property.getNamespace()));
364             }
365         }
366         
367         Iterator iterator = propertyProvider.getSupportedPropertiesNames(getUri());
368         while (iterator.hasNext()) {
369             propertyNamesSet.add(iterator.next());
370         }
371         
372         return propertyNamesSet.iterator();
373     }
374     
375     /**
376      * Returns all properties as an Iterator of NodeProperty objects.
377      *
378      * @return all properties as an Iterator of NodeProperty objects.
379      *
380      * @throws SlideException
381      */

382     public Iterator getAllProperties() throws SlideException {
383         
384         Set propertySet = new HashSet();
385         Enumeration enum = revisionDescriptor.enumerateProperties();
386         if (enum != null) {
387             while (enum.hasMoreElements()) {
388                 propertySet.add(enum.nextElement());
389             }
390         }
391         
392         if (propertyProvider != null) {
393             Iterator iterator = propertyProvider.getSupportedProperties(getUri());
394             while (iterator.hasNext()) {
395                 propertySet.add(iterator.next());
396             }
397         }
398         
399         return propertySet.iterator();
400     }
401     
402     // brauchen wir die???
403
/**
404      * Method isCollection
405      *
406      * @param revisionDescriptor a NodeRevisionDescriptor
407      *
408      * @return a boolean
409      *
410      */

411     public boolean isCollection (NodeRevisionDescriptor revisionDescriptor) {
412         boolean result = false;
413         
414         if (revisionDescriptor == null)
415             return true;
416         
417         NodeProperty property = revisionDescriptor.getProperty("resourcetype");
418         
419         if ((property != null)
420             && (property.getValue().equals(NodeRevisionDescriptor.COLLECTION_TYPE))) {
421             result = true;
422         }
423         
424         return result;
425     }
426     
427     
428     /**
429      * compares two RequestedResources according to OrderByHint. NULL values are
430      * always considered as lessThan. (see [DASL] 5.6). Called in orderBy context
431      * May only return 0 if the URIs are equal.
432      *
433      * @param otherResource a RequestedResource
434      * @param hint an OrderByHint
435      *
436      * @return an int
437      *
438      */

439     public int compareTo (ComparableResource otherResource, CompareHint hint) {
440         int result = 0;
441         
442         Comparable otherValue = (Comparable) otherResource.getThisValue (hint.getPropName(), hint.getPropNamespace());
443         Comparable thisValue = (Comparable) getThisValue (hint.getPropName(), hint.getPropNamespace());
444         
445         if (getInternalHref().equals (otherResource.getInternalHref()))
446             result = 0;
447             
448         else if (thisValue != null && otherValue != null) {
449             result = thisValue.compareTo(otherValue);
450 // if (result == 0)
451
// result = 1;
452
}
453         else if (thisValue == null)
454             result = -1;
455             
456         else if (otherValue == null)
457             result = 1;
458         
459         if (hint.isAscending() == false)
460             result = result * -1;
461         
462         // todo: take casesensitive into account
463

464         return result;
465     }
466     
467     /**
468      * Method isDefined
469      *
470      * @param propName a String
471      *
472      * @return true if propName is defined in this resource.
473      *
474      */

475     public boolean isDefined (String propName, String propNamespace) {
476         return getThisValue (propName, propNamespace) == null ? false : true;
477     }
478     
479     /**
480      * Checks, if the content of the resource contains a specific literal.
481      *
482      * @param literal a String
483      *
484      * @return true if literal is contained in this resources' content
485      *
486      */

487     public int contains (String literal) {
488         int result = Literals.UNKNOWN;
489         
490         NodeProperty property = getProperty("getcontenttype");
491         
492         if (property != null) {
493             String contentType = (String) property.getValue();
494             if (!containsForbiddenOn.contains (contentType)) {
495                 
496                 try {
497                     NodeRevisionContent revisionContent = contentHelper.retrieve (slideToken, revisionDescriptors, revisionDescriptor);
498                     
499                     // TODO: be aware of I18N
500

501                     String content = new String (revisionContent.getContentBytes());
502                     
503                     if (content.indexOf (literal) > -1)
504                         result = Literals.TRUE;
505                     else
506                         result = Literals.FALSE;
507                 }
508                 catch (org.apache.slide.common.SlideException e) {
509                     // error handling ???
510
}
511                 
512             }
513         }
514         
515         return result;
516     }
517     
518     
519     /**
520      * Compares the value of this resources property (propname) to the
521      * propname / literal pair. Returns an integer greater, equal or less than
522      * 0 according java.lang.Comparable
523      *
524      * @param propName a String
525      * @param literal a String
526      *
527      * @return an int
528      *
529      * @throws NumberFormatException
530      * @throws UnknownException
531      *
532      */

533     private int compareTo (String propName, String propNamespace, String literal)
534         throws NumberFormatException, UnknownException
535     {
536         int result = 0;
537         
538         Object thisValue = getThisValue (propName, propNamespace);
539         if (thisValue == null)
540             throw new UnknownException ();
541         
542         Object otherValue = null;
543         
544         otherValue = getOtherValue (thisValue, literal);
545         result = ((Comparable)thisValue).compareTo((Comparable)otherValue);
546         return result;
547     }
548     
549     /**
550      * Method getOtherValue
551      *
552      * @param thisValue an Object
553      * @param literal a String
554      *
555      * @return an Object
556      *
557      * @throws NumberFormatException
558      */

559     private Object getOtherValue (Object thisValue, String literal)
560         throws NumberFormatException
561     {
562         Object otherValue = null;
563         if (thisValue instanceof Boolean)
564             otherValue = new Boolean (literal);
565             
566         else if (thisValue instanceof Date) {
567             otherValue = new Date (); // TODO: dateTime.iso8601tz
568
// otherValue = new Date (literal); // TODO: dateTime.iso8601tz
569
}
570             
571         else if (thisValue instanceof Float)
572             otherValue = new Float (literal);
573             
574         else if (thisValue instanceof Integer)
575             otherValue = new Integer (literal);
576             
577         else if (thisValue instanceof Long)
578             otherValue = new Long (literal);
579             
580         else
581             otherValue = literal;
582         return otherValue;
583     }
584     
585     private int compare (String propName, String propNamespace, String literal, int op) {
586         int result = Literals.FALSE;
587         
588         Object thisValue = getThisValue (propName, propNamespace);
589         if (thisValue == null)
590             return Literals.UNKNOWN;
591         
592         Object otherValue = null;
593         
594         try {
595             otherValue = getOtherValue (thisValue, literal);
596             
597             switch (op)
598             {
599                 case EQ:
600                     if (compareTo (propName, propNamespace, literal) == 0)
601                         result = Literals.TRUE;
602                     break;
603                     
604                 case GT:
605                     if (compareTo (propName, propNamespace, literal) > 0)
606                         result = Literals.TRUE;
607                     break;
608                     
609                 case LT:
610                     if (compareTo (propName, propNamespace, literal) < 0)
611                         result = Literals.TRUE;
612                     break;
613                     
614                 case GTE:
615                     if (compareTo (propName, propNamespace, literal) >= 0)
616                         result = Literals.TRUE;
617                     break;
618                     
619                 case LTE:
620                     if (compareTo (propName, propNamespace, literal) <= 0)
621                         result = Literals.TRUE;
622                     break;
623                 default:
624                     break;
625             }
626         }
627         catch (NumberFormatException e) {
628             result = Literals.UNKNOWN;
629         }
630         catch (UnknownException e) {
631             result = Literals.UNKNOWN;
632         }
633         return result;
634     }
635     
636     public boolean equals (Object otherObject) {
637         if (!(otherObject instanceof ComparableResource))
638             return false;
639
640         String otherUri = null;
641         try {
642             otherUri = ((ComparableResource)otherObject).getUri();
643         } catch (SlideException e) {}
644
645         if (getUri().equals (otherUri))
646             return true;
647         else
648             return false;
649     }
650
651     public int hashCode () {
652         return getUri().hashCode();
653     }
654
655
656     /**
657      * Dummy Exception to indicate UNKNOWN state
658      */

659     private class UnknownException extends Exception {
660     }
661 }
662
663
Popular Tags