001: package org.mandarax.rdf;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.net.URL;
022: import java.util.*;
023: import org.mandarax.kernel.*;
024: import org.mandarax.util.ClauseIterator;
025:
026: import com.hp.hpl.jena.rdf.model.Property;
027: import com.hp.hpl.jena.rdf.model.Statement;
028:
029: /**
030: * RDF related utilities.
031: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A> <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
032: * @version 1.1 <01 August 2004>
033: * @since 0.2
034: */
035:
036: public class RDFUtils implements RDFLogger, RDFConstants {
037:
038: public static Predicate[] findPredicates(URL url, String language,
039: int maxFetchSize) throws ClauseSetException {
040: Set keys = new HashSet();
041: int counter = 0;
042: ClauseIterator clauses = new RDFClauseIterator(url, language,
043: null);
044: while (clauses.hasMoreClauses()
045: && (maxFetchSize == -1 || counter < maxFetchSize)) {
046: Object nextKey = (Predicate) clauses.nextClause().getKey();
047: if (!keys.contains(nextKey)) {
048: keys.add(nextKey);
049: }
050: counter = counter + 1;
051: }
052: Predicate[] p = new Predicate[keys.size()];
053: keys.toArray(p);
054:
055: if (LOG_RDF.isDebugEnabled()) {
056: LOG_RDF
057: .debug("" + p.length + " predicates found in "
058: + url);
059: }
060:
061: return p;
062: }
063:
064: /**
065: * Collect the predicates from a RDF source. I.e., scan the RDF source for predicates, and store this predicates.
066: * This is important to support the getKey method which is used by container objects in order
067: * to index clause sets.
068: * @param url a URL
069: * @throws ClauseSetException
070: */
071: public static Predicate[] findPredicates(URL url)
072: throws ClauseSetException {
073: return findPredicates(url, RDFConstants.RDFXML, -1);
074: }
075:
076: /**
077: * Indicates whether the property is a contains property
078: * associating a container or a collection (RDF Linked List) and one of its elements.
079: * @param property a property
080: * @return a boolean
081: */
082: public static boolean isContainsProperty(Property property) {
083: String uri = property.getURI();
084: // Property is RDF Container Property?
085: if (uri.startsWith(RDF_NS)
086: && uri.charAt(RDF_NS.length()) == '_') {
087: try {
088: Integer.parseInt(uri.substring(RDF_NS.length() + 1));
089: return true;
090: } catch (Exception x) {
091: LOG_RDF
092: .warn("Cannot parse numeric part in contains predicate");
093: }
094: } else
095: // Property is RDF Collection Property ?
096: if (uri.startsWith(RDF_NS)
097: && property.getLocalName().equals("first")) {
098: return true;
099: }
100:
101: return false;
102: }
103:
104: /**
105: * Indicates whether the statement desribes a RDF container
106: * associating a container and one of its elements.
107: * @param Statement a statement
108: * @return a boolean
109: */
110: public static boolean isContainer(Statement stmnt) {
111: Property property = stmnt.getPredicate();
112: String uri = property.getURI();
113: // Property is RDF Container Property? --> Statement describes container
114: if (uri.startsWith(RDF_NS)
115: && uri.charAt(RDF_NS.length()) == '_') {
116: try {
117: Integer.parseInt(uri.substring(RDF_NS.length() + 1));
118: return true;
119: } catch (Exception x) {
120: LOG_RDF
121: .warn("Cannot parse numeric part in contains predicate");
122: }
123: }
124:
125: return false;
126: }
127:
128: /**
129: * Indicates whether the statement desribes a RDF collection
130: * associating a collection and one of its elements.
131: * @param Statement a statement
132: * @return a boolean
133: */
134: public static boolean isCollection(Statement stmnt) {
135: Property property = stmnt.getPredicate();
136: String uri = property.getURI();
137: // Property is RDF Collection Property ?
138: if (uri.startsWith(RDF_NS)
139: && property.getLocalName().equals("first")) {
140: return true;
141: }
142:
143: return false;
144: }
145: }
|