1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
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 }