juergen     2004/06/02 06:44:41

  Modified:    
proposals/tamino/src/store/org/apache/slide/store/tamino/datastore/search
                        Tag: TWS421_BRANCH XResourceImpl.java
  Log:
  First fix for a proper sorting.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.4.1   +150 -4    
jakarta-slide/proposals/tamino/src/store/org/apache/slide/store/tamino/datastore/search/XResourceImpl.java
  
  Index: XResourceImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/proposals/tamino/src/store/org/apache/slide/store/tamino/datastore/search/XResourceImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.1.4.1
  diff -u -r1.1 -r1.1.4.1
  --- XResourceImpl.java        25 Mar 2004 16:18:02 -0000      1.1
  +++ XResourceImpl.java        2 Jun 2004 13:44:40 -0000       1.1.4.1
  @@ -24,16 +24,27 @@
   
   import org.apache.slide.store.tamino.common.XGlobals;
   import org.apache.slide.store.tamino.datastore.XContentId;
  +
  +import java.io.StringReader;
  +import java.io.StringWriter;
   import java.util.Iterator;
  +import java.util.List;
  +
   import org.apache.slide.common.SlideException;
   import org.apache.slide.content.NodeProperty;
   import org.apache.slide.search.SearchException;
   import org.apache.slide.search.basic.ComparableResourceImpl;
  +import org.apache.slide.search.basic.ComparableResource;
  +import org.apache.slide.search.CompareHint;
   import org.apache.slide.search.basic.IBasicQuery;
   import org.apache.slide.structure.ObjectNode;
   import org.apache.slide.util.XMLValue;
  +import org.jdom.Document;
   import org.jdom.Element;
   import org.jdom.Namespace;
  +import org.jdom.input.SAXBuilder;
  +import org.jdom.output.XMLOutputter;
  +import org.jdom.xpath.XPath;
   
   /**
    * Represents one result set object. This implementation deals with the xdav
  @@ -180,6 +191,141 @@
               xmlValue.add (result);
           }
       }
  +    
  +    
  +    /**
  +     * compares two RequestedResources according to OrderByHint. NULL values are
  +     * always considered as lessThan. (see [DASL] 5.6). Called in orderBy context
  +     * May only return 0 if the URIs are equal.
  +     * If both properties are XML valued and this value was created by an xpath
  +     * expression, do the specialised handling, else call the super method.
  +     *
  +     * @param    otherResource       a  RequestedResource
  +     * @param    hint                an OrderByHint
  +     *
  +     * @return   an int
  +     *
  +     */
  +    public int compareTo (ComparableResource otherResource, CompareHint hint) {
  +        int result = 0;
  +
  +        if (getInternalHref().equals (otherResource.getInternalHref()))
  +            return 0;
  +        
  +        Comparable otherValue = (Comparable)otherResource.getThisValue 
(hint.getPropName(), hint.getPropNamespace());
  +        Comparable thisValue  = (Comparable) getThisValue (hint.getPropName(), 
hint.getPropNamespace());
  +        
  +        if ((hint instanceof XCompareHint) && (((XCompareHint)hint).getSortBy() != 
null) &&
  +                     (otherValue instanceof XMLValue) && (thisValue instanceof 
XMLValue)) {
  +            if (thisValue != null && otherValue != null) {
  +                result = compareXpathDrivenValues((XMLValue)thisValue, 
(XMLValue)otherValue, (XCompareHint)hint);
  +            }
  +            else if (thisValue == null)
  +                result = -1;
  +                
  +            else if (otherValue == null)
  +                result = 1;
  +        }
  +        else {
  +             return super.compareTo(otherResource, hint);
  +     
  +        }
  +
  +        if (hint.isAscending() == false)
  +            result = result * -1;
  +        
  +        return result;
  +    }
  +    
  +    public int compareXpathDrivenValues(XMLValue thisResource, XMLValue 
otherResource, XCompareHint hint) {
  +        int result = 0;
  +        
  +        try {
  +             
  +             List thisList  = 
XPath.newInstance("string(//"+hint.getSortBy()+")").selectNodes(getElementAsElement((Element)(thisResource.getList().get(1))));
  +             List otherList = 
XPath.newInstance("string(//"+hint.getSortBy()+")").selectNodes(getElementAsElement((Element)(otherResource.getList().get(1))));
  +     
  +//           System.out.println( );
  +//           System.out.println("Element 1\n" + 
getElementAsString((Element)(thisResource.getList().get(1))));
  +//           System.out.println("Element 2\n" + 
getElementAsString((Element)(otherResource.getList().get(1))));
  +//           System.out.println("Comparing " );
  +//           System.out.println("String1 = " + otherList.get(0) );
  +//           System.out.println("String2 = " + thisList.get(0) );
  +             
  +             int thisSize  = thisList.size();
  +             int otherSize = otherList.size();
  +     
  +             // compare based on the size of the lists
  +             if (thisSize != otherSize) {
  +                     return new Integer(thisSize).compareTo(new Integer(otherSize));
  +             }
  +             
  +             
  +             for( int i=0; i < thisSize; i++ ) {
  +                     String thisString = (String)thisList.get(i);
  +                     String thatString = (String)otherList.get(i);
  +     
  +                             if (!thisString.equals(thatString)) {
  +                                     return thisString.compareTo(thatString);
  +                             }
  +             }
  +             
  +        }
  +        catch (Exception e) {
  +             e.printStackTrace();
  +        }
  +    
  +    return result;
  +
  +    }
  +    
  +    
  +    
  +    
  +
  +
  +     /**
  +      * @throws IOException
  +      */
  +     private String getElementAsString(Element e)  {
  +             StringWriter writer = new StringWriter(); 
  +
  +             XMLOutputter o2 = new XMLOutputter("  ", true, "utf-8");
  +             o2.setTrimAllWhite(true);
  +             try {
  +                 o2.output( e, writer );
  +             }
  +             catch (Exception exception) {
  +                     exception.printStackTrace();
  +             }
  +             
  +             return writer.toString();
  +
  +             }
  +     
  +     private Element getElementAsElement(Element e)  {
  +             return getStringAsElement(getElementAsString(e));
  +             }
  +
  +private Element getStringAsElement(String s)  {
  +     Element e = null;
  +     try {
  +             SAXBuilder b = new SAXBuilder();
  +         Document d = b.build( new StringReader(s) );
  +         e = d.getRootElement();
  +             
  +     } catch (Exception ex) {
  +             ex.printStackTrace();
  +     }
  +
  +     
  +     return e;
  +
  +     }
  +
  +
  +
  +
   }
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to