View Javadoc

1   /*
2    *  $Id: Model.java,v 1.4 2004/07/25 21:44:43 roku Exp $ 
3    *
4    *  Copyright 2004 University of Hannover
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package olr.rdf;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  
27  import org.apache.log4j.Logger;
28  import org.w3c.rdf.model.Statement;
29  import org.w3c.rdf.util.RDFFactory;
30  import org.w3c.rdf.util.RDFFactoryImpl;
31  import org.w3c.rdf.util.RDFUtil;
32  
33  
34  public final class Model 
35  {
36      private String namespaceURI;
37      private Set statements;
38      private Set subjects;
39      private StatementPool statementPool;
40      
41      public Model(String namespaceURI, StatementPool statementPool) 
42          throws Exception {
43          
44          this.statements = new HashSet();
45          subjects = new HashSet();
46          this.statementPool = statementPool;
47          try
48          {
49              Logger.getLogger(getClass()).debug(String.valueOf(String.valueOf((new StringBuffer("Parsing model <")).append(namespaceURI).append("> ... "))));
50              RDFFactory f = new RDFFactoryImpl();
51              org.w3c.rdf.model.Model m = f.createModel();
52              RDFUtil.parse(namespaceURI, f.createParser(), m);
53              this.namespaceURI = namespaceURI;
54              if(statementPool.isParsedNamespaceURI(this.namespaceURI))
55                  throw new Exception("Model URI is in the system already!");
56              Statement statements[] = RDFUtil.getStatementArray(m);
57              for(int i = 0; i < statements.length; i++)
58                  addStatement(statements[i].subject().getURI(), statements[i].predicate().getURI(), statements[i].object().getLabel());
59  
60              if(this.namespaceURI.equals(Definitions.RDF_NS))
61              {
62                  for(int i = 1; i <= 20; i++)
63                  {
64                      String listItem = String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(Definitions.RDF_NS)))).append("_").append(i)));
65                      addStatement(listItem, Definitions.RDF_TYPE, Definitions.RDF_PROPERTY);
66                      addStatement(listItem, Definitions.RDF_DOMAIN, Definitions.RDF_SEQ);
67                  }
68  
69              }
70              Logger.getLogger(getClass()).debug("Parsing OK!");
71          }
72          catch(Exception e)
73          {
74              Logger.getLogger(getClass()).error("Parsing error: " + e);
75              throw e;
76          }
77          statementPool.addParsedNamespaceURI(this.namespaceURI);
78      }
79  
80      public void delete()
81      {
82          for(Iterator it = statements.iterator(); it.hasNext(); statementPool.removeStatement((olr.rdf.Statement)it.next()));
83          statementPool.removeParsedNamespaceURI(getNamespace());
84      }
85  
86      public StatementPool getStatementPool()
87      {
88          return statementPool;
89      }
90  
91      public String getNamespace()
92      {
93          return namespaceURI;
94      }
95  
96      public void addStatements(olr.rdf.Statement statement[])
97      {
98          for(int i = 0; i < statement.length; i++)
99              addStatement(statement[i]);
100 
101     }
102 
103     public void addStatement(olr.rdf.Statement statement)
104     {
105         statementPool.addStatement(statement);
106         statements.add(statement);
107         subjects.add(statement.getSubject());
108     }
109 
110     public void addStatement(String subject, String predicate, String object)
111     {
112         addStatement(new olr.rdf.Statement(subject, predicate, object));
113     }
114 
115     public Collection getSubjects()
116     {
117         return subjects;
118     }
119 }