001: /*
002: (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: NodeCreateUtils.java,v 1.2 2008/01/02 12:05:32 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
010: import com.hp.hpl.jena.graph.Node;
011: import com.hp.hpl.jena.graph.impl.LiteralLabel;
012: import com.hp.hpl.jena.rdf.model.AnonId;
013: import com.hp.hpl.jena.shared.*;
014:
015: /**
016: Creating nodes from string specifications.
017: */
018: public class NodeCreateUtils {
019: /**
020: Returns a Node described by the string, primarily for testing purposes.
021: The string represents a URI, a numeric literal, a string literal, a bnode label,
022: or a variable.
023: <ul>
024: <li> 'some text' :: a string literal with that text
025: <li> 'some text'someLanguage:: a string literal with that text and language
026: <li> 'some text'someURI:: a typed literal with that text and datatype
027: <li> digits :: a literal [OF WHAT TYPE] with that [numeric] value
028: <li> _XXX :: a bnode with an AnonId built from _XXX
029: <li> ?VVV :: a variable with name VVV
030: <li> &PPP :: to be done
031: <li> name:stuff :: the URI; name may be expanded using the Extended map
032: </ul>
033: @param x the string describing the node
034: @return a node of the appropriate type with the appropriate label
035: */
036: public static Node create(String x) {
037: return create(PrefixMapping.Extended, x);
038: }
039:
040: /**
041: Returns a Node described by the string, primarily for testing purposes.
042: The string represents a URI, a numeric literal, a string literal, a bnode label,
043: or a variable.
044: <ul>
045: <li> 'some text' :: a string literal with that text
046: <li> 'some text'someLanguage:: a string literal with that text and language
047: <li> 'some text'someURI:: a typed literal with that text and datatype
048: <li> digits :: a literal [OF WHAT TYPE] with that [numeric] value
049: <li> _XXX :: a bnode with an AnonId built from _XXX
050: <li> ?VVV :: a variable with name VVV
051: <li> &PPP :: to be done
052: <li> name:stuff :: the URI; name may be expanded using the Extended map
053: </ul>
054:
055: @param pm the PrefixMapping for translating pre:X strings
056: @param x the string encoding the node to create
057: @return a node with the appropriate type and label
058: */
059: public static Node create(PrefixMapping pm, String x) {
060: if (x.equals(""))
061: throw new JenaException(
062: "Node.create does not accept an empty string as argument");
063: char first = x.charAt(0);
064: if (first == '\'' || first == '\"')
065: return Node.createLiteral(newString(pm, first, x));
066: if (Character.isDigit(first))
067: return Node.createLiteral(x, "", XSDDatatype.XSDinteger);
068: if (first == '_')
069: return Node.createAnon(new AnonId(x));
070: if (x.equals("??"))
071: return Node.ANY;
072: if (first == '?')
073: return Node.createVariable(x.substring(1));
074: if (first == '&')
075: return Node.createURI("q:" + x.substring(1));
076: int colon = x.indexOf(':');
077: String d = pm.getNsPrefixURI("");
078: return colon < 0 ? Node.createURI((d == null ? "eh:/" : d) + x)
079: : Node.createURI(pm.expandPrefix(x));
080: }
081:
082: public static String unEscape(String spelling) {
083: if (spelling.indexOf('\\') < 0)
084: return spelling;
085: StringBuffer result = new StringBuffer(spelling.length());
086: int start = 0;
087: while (true) {
088: int b = spelling.indexOf('\\', start);
089: if (b < 0)
090: break;
091: result.append(spelling.substring(start, b));
092: result.append(unEscape(spelling.charAt(b + 1)));
093: start = b + 2;
094: }
095: result.append(spelling.substring(start));
096: return result.toString();
097: }
098:
099: public static char unEscape(char ch) {
100: switch (ch) {
101: case '\\':
102: case '\"':
103: case '\'':
104: return ch;
105: case 'n':
106: return '\n';
107: case 's':
108: return ' ';
109: case 't':
110: return '\t';
111: default:
112: return 'Z';
113: }
114: }
115:
116: public static LiteralLabel literal(PrefixMapping pm,
117: String spelling, String langOrType) {
118: String content = unEscape(spelling);
119: int colon = langOrType.indexOf(':');
120: return colon < 0 ? new LiteralLabel(content, langOrType, false)
121: : LiteralLabel.createLiteralLabel(content, "", Node
122: .getType(pm.expandPrefix(langOrType)));
123: }
124:
125: public static LiteralLabel newString(PrefixMapping pm, char quote,
126: String nodeString) {
127: int close = nodeString.lastIndexOf(quote);
128: return literal(pm, nodeString.substring(1, close), nodeString
129: .substring(close + 1));
130: }
131: }
132:
133: /*
134: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
135: * All rights reserved.
136: *
137: * Redistribution and use in source and binary forms, with or without
138: * modification, are permitted provided that the following conditions
139: * are met:
140: * 1. Redistributions of source code must retain the above copyright
141: * notice, this list of conditions and the following disclaimer.
142: * 2. Redistributions in binary form must reproduce the above copyright
143: * notice, this list of conditions and the following disclaimer in the
144: * documentation and/or other materials provided with the distribution.
145: * 3. The name of the author may not be used to endorse or promote products
146: * derived from this software without specific prior written permission.
147: *
148: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
149: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
151: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
152: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
153: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
154: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
155: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
156: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
157: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
158: */
|