View Javadoc

1   /* 
2    * Copyright 2004 University of Hannover
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package olr.viewer;
16  
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import olr.SessionData;
22  import olr.rdf.Attribute;
23  import olr.rdf.Definitions;
24  import olr.rdf.SortedAttributeList;
25  
26  /***
27   * @version $Id: SequenceAttributes.java,v 1.12 2004/08/03 08:33:53 roku Exp $
28   */
29  public class SequenceAttributes {
30      List sortedAttributes = new SortedSequenceAttributeList();
31  
32      public SequenceAttributes(List oriAttributes) {
33          List attributes = new ArrayList(oriAttributes);
34          List seqAttributes = new ArrayList();
35  
36          // find Sequences and move them to a temporary list
37          for (int i = attributes.size() - 1; i >= 0; i--) {
38              Attribute attribute = (Attribute) attributes.get(i);
39              if (SessionData.getCurrentSession().getStatementPool().isInstanceOf(
40                      attribute.getObject(), Definitions.RDF_SEQ)) {
41                  seqAttributes.add(attribute);
42                  attributes.remove(attribute);
43              }
44          }
45  
46          // now use the sequences to give the remaining attributes a position
47          for (Iterator it = seqAttributes.iterator(); it.hasNext();) {
48              List listItems = SessionData.getCurrentSession().getStatementPool().getAttributesAbout(
49                      ((Attribute) it.next()).getObject());
50              for (Iterator it2 = listItems.iterator(); it2.hasNext();) {
51                  Attribute listItem = (Attribute) it2.next();
52  
53                  try {
54                      int pos = Integer.parseInt(listItem.getPredicate().substring(
55                              listItem.getPredicate().lastIndexOf('_') + 1));
56  
57                      for (Iterator it3 = getAttributesWithObject(attributes, listItem.getObject())
58                              .iterator(); it3.hasNext();) {
59                          Attribute objAttribute = (Attribute) it3.next();
60                          attributes.remove(objAttribute);
61                          sortedAttributes.add(new SequenceAttribute(objAttribute, pos));
62                      }
63                  } catch (Exception e) {
64                  }
65              }
66          }
67  
68          // now move the remaining attributes (without a position) to the sorted list
69          sortedAttributes.addAll(attributes);
70      }
71  
72      public List getAttributes() {
73          return sortedAttributes;
74      }
75  
76      /***
77       * returns all Attributes of the given list that have the given object
78       */
79      private List getAttributesWithObject(List attributes, String object) {
80          final List subSet = new ArrayList();
81          for (Iterator it = attributes.iterator(); it.hasNext();) {
82              final Attribute attribute = (Attribute) it.next();
83              if (object.equals(attribute.getObject()))
84                  subSet.add(attribute);
85          }
86          return subSet;
87      }
88  
89      /************************************************************************************************
90       * * Internal classes * *
91       **********************************************************************************************/
92  
93      private class SequenceAttribute extends Attribute {
94          private int pos = -1;
95  
96          public SequenceAttribute(Attribute attribute, int pos) {
97              super(attribute.getPredicate(), attribute.getObject(), attribute.getID());
98              this.pos = pos;
99          }
100 
101         private int getPos() {
102             return pos;
103         }
104     }
105 
106     private class SortedSequenceAttributeList extends SortedAttributeList {
107         protected int compare(Object o1, Object o2) {
108             if (o1 instanceof SequenceAttribute) {
109                 if (o2 instanceof SequenceAttribute) {
110                     if (((SequenceAttribute) o1).getPos() < ((SequenceAttribute) o2).getPos())
111                         return -1;
112                     else if (((SequenceAttribute) o1).getPos() > ((SequenceAttribute) o2).getPos())
113                         return 1;
114                     else
115                         return super.compare(o1, o2);
116                 } else
117                     return -1;
118             } else {
119                 if (o2 instanceof SequenceAttribute)
120                     return 1;
121                 else
122                     return super.compare(o1, o2);
123             }
124         }
125     }
126 }