001: /*
002: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * PropertyImpl.java
028: *
029: * Created on 03 August 2000, 13:47
030: */
031:
032: package com.hp.hpl.jena.rdf.model.impl;
033:
034: import com.hp.hpl.jena.rdf.model.*;
035: import com.hp.hpl.jena.vocabulary.RDF;
036: import com.hp.hpl.jena.graph.*;
037: import com.hp.hpl.jena.enhanced.*;
038: import com.hp.hpl.jena.shared.*;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /** An implementation of Property.
044: *
045: * @author bwm
046: * @version Release='$Name: $' Revision='$Revision: 1.17 $' Date='$Date: 2008/01/02 12:05:11 $'
047: */
048:
049: public class PropertyImpl extends ResourceImpl implements Property {
050:
051: final static public Implementation factory = new Implementation() {
052: public boolean canWrap(Node n, EnhGraph eg) {
053: return n.isURI();
054: }
055:
056: public EnhNode wrap(Node n, EnhGraph eg) {
057: return new PropertyImpl(n, eg);
058: }
059: };
060:
061: protected static Log logger = LogFactory.getLog(PropertyImpl.class);
062:
063: protected int ordinal = -1;
064:
065: /** Creates new PropertyImpl */
066: public PropertyImpl(String uri) {
067: super (uri);
068: checkLocalName();
069: checkOrdinal();
070: }
071:
072: public RDFNode inModel(Model m) {
073: return getModel() == m ? this : m.createProperty(getURI());
074: }
075:
076: private void checkLocalName() {
077: String localName = getLocalName();
078: if (localName == null || localName.equals(""))
079: throw new InvalidPropertyURIException(getURI());
080: }
081:
082: public PropertyImpl(String nameSpace, String localName) {
083: super (nameSpace, localName);
084: checkLocalName();
085: checkOrdinal();
086: }
087:
088: public PropertyImpl(String uri, ModelCom m) {
089: super (uri, m);
090: checkOrdinal();
091: }
092:
093: public PropertyImpl(String nameSpace, String localName, ModelCom m) {
094: super (nameSpace, localName, m);
095: checkOrdinal();
096: }
097:
098: public PropertyImpl(Node n, EnhGraph m) {
099: super (n, m);
100: checkOrdinal();
101: }
102:
103: public PropertyImpl(String nameSpace, String localName,
104: int ordinal, ModelCom m) {
105: super (nameSpace, localName, m);
106: checkLocalName();
107: this .ordinal = ordinal;
108: }
109:
110: public boolean isProperty() {
111: return true;
112: }
113:
114: public int getOrdinal() {
115: if (ordinal < 0)
116: ordinal = computeOrdinal();
117: return ordinal;
118: }
119:
120: private int computeOrdinal() {
121: String localName = getLocalName();
122: if (getNameSpace().equals(RDF.getURI())
123: && localName.matches("_[0-9]+"))
124: return parseInt(localName.substring(1));
125: return 0;
126: }
127:
128: private int parseInt(String digits) {
129: try {
130: return Integer.parseInt(digits);
131: } catch (NumberFormatException e) {
132: throw new JenaException("checkOrdinal fails on " + digits,
133: e);
134: }
135: }
136:
137: // Remove shortly.
138:
139: protected void checkOrdinal() {
140: // char c;
141: // String nameSpace = getNameSpace();
142: // String localName = getLocalName();
143: // // check for an rdf:_xxx property
144: // if (localName.length() > 0)
145: // {
146: // if (localName.charAt(0) == '_' && nameSpace.equals(RDF.getURI())
147: // && nameSpace.equals(RDF.getURI())
148: // && localName.length() > 1
149: // )
150: // {
151: // for (int i=1; i<localName.length(); i++) {
152: // c = localName.charAt(i);
153: // if (c < '0' || c > '9') return;
154: // }
155: // try {
156: // ordinal = Integer.parseInt(localName.substring(1));
157: // } catch (NumberFormatException e) {
158: // logger.error( "checkOrdinal fails on " + localName, e );
159: // }
160: // }
161: // }
162: }
163:
164: }
|