001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email ian.dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 04-Dec-2003
009: * Filename $RCSfile: DIGValueToNodeMapper.java,v $
010: * Revision $Revision: 1.7 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:11 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * [See end of file]
018: *****************************************************************************/package com.hp.hpl.jena.reasoner.dig;
019:
020: // Imports
021: ///////////////
022: import org.w3c.dom.Element;
023:
024: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
025: import com.hp.hpl.jena.graph.Node;
026: import com.hp.hpl.jena.rdf.model.AnonId;
027: import com.hp.hpl.jena.util.iterator.Map1;
028:
029: /**
030: * <p>
031: * Mapper to map DIG identifier names and concrete value elements to Jena graph nodes.
032: * </p>
033: *
034: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
035: * @version CVS $Id: DIGValueToNodeMapper.java,v 1.7 2008/01/02 12:07:11 andy_seaborne Exp $
036: */
037: public class DIGValueToNodeMapper implements Map1 {
038: // Constants
039: //////////////////////////////////
040:
041: // Static variables
042: //////////////////////////////////
043:
044: // Instance variables
045: //////////////////////////////////
046:
047: // Constructors
048: //////////////////////////////////
049:
050: // External signature methods
051: //////////////////////////////////
052:
053: /**
054: * <p>Return the node corresponding to the given element; either a literal
055: * node for ival and sval values, or a URI node for named elements.</p>
056: * @param o An object, expected to be an XML element
057: * @return A node corresponding to the given value
058: */
059: public Object map1(Object o) {
060: return mapToNode(o);
061: }
062:
063: /**
064: * <p>Return the node corresponding to the given element; either a literal
065: * node for ival and sval values, or a URI node for named elements.</p>
066: * @param o An object, expected to be an XML element
067: * @return A node corresponding to the given value
068: */
069: public Node mapToNode(Object o) {
070: if (o instanceof Element) {
071: // we know that this mapper is applied to lists of Elements
072: Element elem = (Element) o;
073:
074: if (elem.getNodeName().equals(DIGProfile.IVAL)) {
075: // this is an integer element
076: return Node.createLiteral(elem.getNodeValue(), null,
077: XSDDatatype.XSDint);
078: } else if (elem.getNodeName().equals(DIGProfile.SVAL)) {
079: // this is an integer element
080: return Node.createLiteral(elem.getNodeValue(), null,
081: XSDDatatype.XSDstring);
082: } else if (elem.hasAttribute(DIGProfile.NAME)) {
083: return convertNameToNode(elem
084: .getAttribute(DIGProfile.NAME));
085: }
086: } else if (o instanceof String) {
087: return convertNameToNode((String) o);
088: }
089:
090: throw new IllegalArgumentException("Cannot map value " + o
091: + " to an RDF node because it is not a recognised type");
092: }
093:
094: // Internal implementation methods
095: //////////////////////////////////
096:
097: /** Answer the node with the given name. It may be the node ID of a bNode */
098: private Node convertNameToNode(String name) {
099: if (name.startsWith(DIGAdapter.ANON_MARKER)) {
100: String anonID = name.substring(DIGAdapter.ANON_MARKER
101: .length());
102: return Node.createAnon(new AnonId(anonID));
103: } else {
104: return Node.createURI(name);
105: }
106: }
107:
108: //==============================================================================
109: // Inner class definitions
110: //==============================================================================
111:
112: }
113:
114: /*
115: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
116: * All rights reserved.
117: *
118: * Redistribution and use in source and binary forms, with or without
119: * modification, are permitted provided that the following conditions
120: * are met:
121: * 1. Redistributions of source code must retain the above copyright
122: * notice, this list of conditions and the following disclaimer.
123: * 2. Redistributions in binary form must reproduce the above copyright
124: * notice, this list of conditions and the following disclaimer in the
125: * documentation and/or other materials provided with the distribution.
126: * 3. The name of the author may not be used to endorse or promote products
127: * derived from this software without specific prior written permission.
128: *
129: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
130: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
131: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
132: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
133: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
134: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
135: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
136: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
137: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
138: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
139: */
|