View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.viewer;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import olr.rdf.Attribute;
25  import olr.rdf.Definitions;
26  import olr.rdf.Model;
27  import olr.rdf.Resource;
28  import olr.rdf.util.SortedStringList;
29  
30  /***
31   * @version $Id: SchemaViewer.java,v 1.12 2004/08/03 12:27:14 roku Exp $
32   */
33  public final class SchemaViewer extends ModelViewer {
34      /***
35       * Initializes an instance of this class.
36       * 
37       * @param model
38       * @param title
39       */
40      public SchemaViewer(Model model, String title) {
41          super(model, null, title);
42      }
43  
44      protected void initializeModelRows() {
45          super.modelRows = new ArrayList();
46          if (!super.expanded)
47              return;
48          Collection resources = new SortedStringList(super.model.getSubjects());
49          Iterator it = resources.iterator();
50          do {
51              if (!it.hasNext())
52                  break;
53              Resource property = getModel().getStatementPool().getResource(it.next().toString());
54              if (getModel().getStatementPool().isProperty(property)
55                      && property.getNS().equals(getModel().getNamespace()))
56                  super.modelRows.add(new ModelViewerRow(this, property.toString(), 0));
57          } while (true);
58      }
59  
60      public boolean checkSub(int rowNr) {
61          ModelViewerRow row = (ModelViewerRow) super.modelRows.get(rowNr);
62          row.setSub(false);
63          Resource resElement = getModel().getStatementPool().getResource(row.getResource());
64          if (resElement != null && resElement.getNS().equals(getModel().getNamespace())) {
65  
66              List attributes = resElement.getAttributes();
67              Iterator it = attributes.iterator();
68              do {
69                  if (!it.hasNext())
70                      break;
71                  Attribute attribute = (Attribute) it.next();
72                  String predicate = attribute.getPredicate();
73                  if (!Definitions.isType(predicate)) {
74                      row.setSub(true);
75                  }
76              } while (true);
77          }
78          return row.isExpandable();
79      }
80  
81      protected void addSubitems(int rowNr) {
82          ModelViewerRow row = (ModelViewerRow) super.modelRows.get(rowNr);
83          Resource resElement = getModel().getStatementPool().getResource(row.getResource());
84          if (resElement != null && resElement.getNS().equals(getModel().getNamespace())) {
85              int level = row.getIndent();
86              int n = 0;
87              String lastPredicate = "";
88              List attributes = resElement.getAttributes();
89              Iterator it = attributes.iterator();
90              do {
91                  if (!it.hasNext())
92                      break;
93                  Attribute attribute = (Attribute) it.next();
94                  String predicate = attribute.getPredicate();
95                  if (!Definitions.isType(predicate)) {
96                      if (!lastPredicate.equals(predicate))
97                          super.modelRows.add(rowNr + 1 + n++, new ModelViewerRow(this, predicate,
98                                  level + 1, true));
99                      super.modelRows.add(rowNr + 1 + n++, new ModelViewerRow(this, attribute
100                             .getObject(), level + 2));
101                     lastPredicate = predicate;
102                 }
103             } while (true);
104         }
105     }
106 }