Source Code Cross Referenced for TypeConversion.java in  » Search-Engine » semweb4j » org » ontoware » rdf2go » impl » jena24 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » semweb4j » org.ontoware.rdf2go.impl.jena24 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.ontoware.rdf2go.impl.jena24;
002:
003:        import org.apache.commons.logging.Log;
004:        import org.apache.commons.logging.LogFactory;
005:        import org.ontoware.rdf2go.exception.ModelRuntimeException;
006:        import org.ontoware.rdf2go.model.node.BlankNode;
007:        import org.ontoware.rdf2go.model.node.DatatypeLiteral;
008:        import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
009:        import org.ontoware.rdf2go.model.node.PlainLiteral;
010:        import org.ontoware.rdf2go.model.node.URI;
011:        import org.ontoware.rdf2go.model.node.Variable;
012:        import org.ontoware.rdf2go.model.node.impl.AbstractBlankNodeImpl;
013:        import org.ontoware.rdf2go.model.node.impl.DatatypeLiteralImpl;
014:        import org.ontoware.rdf2go.model.node.impl.LanguageTagLiteralImpl;
015:        import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl;
016:        import org.ontoware.rdf2go.model.node.impl.URIImpl;
017:
018:        import com.hp.hpl.jena.graph.Node;
019:        import com.hp.hpl.jena.graph.impl.LiteralLabel;
020:        import com.hp.hpl.jena.rdf.model.AnonId;
021:        import com.hp.hpl.jena.rdf.model.Literal;
022:        import com.hp.hpl.jena.shared.impl.JenaParameters;
023:
024:        /**
025:         * converter between java objects and jena nodes (both ways)
026:         * 
027:         * @author xam
028:         * 
029:         */
030:        public class TypeConversion {
031:
032:            /**
033:             * the logger
034:             */
035:            static Log log = LogFactory.getLog(TypeConversion.class);
036:
037:            /**
038:             * The new Implementation: model is needed because Jena can only create data
039:             * typed literals with unknown data types if it creates them directly in the
040:             * context of a model instance.
041:             */
042:            public static Node toJenaNode(Object o,
043:                    com.hp.hpl.jena.rdf.model.Model model)
044:                    throws RuntimeException {
045:                assert o != null;
046:                if (o instanceof  URI) {
047:                    log.debug("instanceof URI");
048:                    return Node.createURI(((URI) o).toString());
049:                }
050:
051:                if (o instanceof  String) {
052:                    // plain literal
053:                    log.debug("instanceof String");
054:                    return Node.createLiteral((String) o, null, false);
055:                }
056:
057:                if (o instanceof  PlainLiteral) {
058:                    // plain literal
059:                    log.debug("instanceof String");
060:                    return Node.createLiteral(((PlainLiteral) o).getValue(),
061:                            null, false);
062:                }
063:
064:                if (o instanceof  DatatypeLiteral) {
065:                    // datatyped
066:                    log.debug("instanceof DatatypeLiteral");
067:
068:                    boolean originalFlag = JenaParameters.enableSilentAcceptanceOfUnknownDatatypes;
069:                    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = true;
070:
071:                    Literal l = model.createTypedLiteral(((DatatypeLiteral) o)
072:                            .getValue(), ((DatatypeLiteral) o).getDatatype()
073:                            .toString());
074:
075:                    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
076:
077:                    return l.asNode();
078:
079:                }
080:
081:                if (o instanceof  LanguageTagLiteral) {
082:                    // langtag
083:                    log.debug("instanceof LanguageTagLiteral");
084:                    return Node.createLiteral(((LanguageTagLiteral) o)
085:                            .getValue(), ((LanguageTagLiteral) o)
086:                            .getLanguageTag(), null);
087:                }
088:
089:                if (o instanceof  BlankNode) {
090:                    // blank node
091:                    log.debug("instanceof BlankNode");
092:                    assert o instanceof  AbstractBlankNodeImpl : "expected a BlankNodeImpl and found a "
093:                            + o.getClass();
094:                    AnonId anonId = new AnonId(((AbstractBlankNodeImpl) o)
095:                            .getUnderlyingBlankNode().toString());
096:                    Node jenaNode = Node.createAnon(anonId);
097:                    return jenaNode;
098:                }
099:
100:                if (o instanceof  Variable) {
101:                    // variable
102:                    log.debug("instanceof Variable");
103:                    return Node.ANY;
104:                }
105:
106:                throw new RuntimeException("no transformation from "
107:                        + o.getClass() + " to jena has been implemented");
108:            }
109:
110:            /**
111:             * transforms the given object into a Jena-node
112:             * 
113:             * @param o
114:             *            The object to transform
115:             * @return The jena node
116:             * @throws RuntimeException
117:             *             if the object can't be transformed in a jena node
118:             */
119:            public static Node toJenaNode(Object o) throws RuntimeException {
120:                assert o != null;
121:                if (o instanceof  URI) {
122:                    log.debug("instanceof URI");
123:                    return Node.createURI(((URI) o).toString());
124:                }
125:
126:                if (o instanceof  String) {
127:                    // plain literal
128:                    log.debug("instanceof String");
129:                    return Node.createLiteral((String) o, null, false);
130:                }
131:
132:                if (o instanceof  PlainLiteral) {
133:                    // plain literal
134:                    log.debug("instanceof String");
135:                    return Node.createLiteral(((PlainLiteral) o).getValue(),
136:                            null, false);
137:                }
138:
139:                if (o instanceof  DatatypeLiteral) {
140:                    // datatyped
141:                    log.debug("instanceof DatatypeLiteral");
142:                    // there is an issue with jena datatypes
143:                    // the language (middle part parameter) has to be null
144:                    // the RDF specification says it must not be set!
145:                    // (this is left in JENA because of backward compatibility)
146:                    return Node.createLiteral(((DatatypeLiteral) o).getValue(),
147:                            null, new GeneralDataType(((DatatypeLiteral) o)
148:                                    .getDatatype()
149:                                    + ""));
150:                }
151:
152:                if (o instanceof  LanguageTagLiteral) {
153:                    // langtag
154:                    log.debug("instanceof LanguageTagLiteral");
155:                    return Node.createLiteral(((LanguageTagLiteral) o)
156:                            .getValue(), ((LanguageTagLiteral) o)
157:                            .getLanguageTag(), null);
158:                }
159:
160:                if (o instanceof  BlankNode) {
161:                    // blank node
162:                    log.debug("instanceof BlankNode");
163:                    assert o instanceof  AbstractBlankNodeImpl : "expected a BlankNodeImpl and found a "
164:                            + o.getClass();
165:                    AnonId anonId = new AnonId(((AbstractBlankNodeImpl) o)
166:                            .getUnderlyingBlankNode().toString());
167:                    Node jenaNode = Node.createAnon(anonId);
168:                    return jenaNode;
169:                }
170:
171:                if (o instanceof  Variable) {
172:                    // variable
173:                    log.debug("instanceof Variable");
174:                    return Node.ANY;
175:                }
176:
177:                throw new RuntimeException("no transformation from "
178:                        + o.getClass() + " to jena has been implemented");
179:            }
180:
181:            /**
182:             * Transforms a Jena node into a java object. Possible node types: uri,
183:             * variable, literal (datatype, languageTag), blank node
184:             * 
185:             * @param n
186:             *            The node to transform
187:             * @return A specific java object
188:             * @throws RuntimeException
189:             * @throws ModelRuntimeException
190:             */
191:            public static org.ontoware.rdf2go.model.node.Node toRDF2Go(Node n)
192:                    throws ModelRuntimeException {
193:                assert n != null;
194:                if (n.isURI())
195:                    return new URIImpl(n.getURI());
196:
197:                if (n.isVariable())
198:                    throw new RuntimeException(
199:                            "Cannot convert a Jena variable to an RDF2Go node");
200:
201:                if (n.isLiteral()) {
202:                    LiteralLabel lit = n.getLiteral();
203:                    // datatype
204:                    if (lit.getDatatypeURI() != null) {
205:                        return new DatatypeLiteralImpl(lit.getLexicalForm(),
206:                                new URIImpl(lit.getDatatypeURI()));
207:                    }
208:
209:                    // language tagged
210:                    if (lit.language() != null && !lit.language().equals(""))
211:                        return new LanguageTagLiteralImpl(lit.getLexicalForm(),
212:                                lit.language());
213:
214:                    // plain
215:                    return new PlainLiteralImpl(lit.getLexicalForm());
216:                }
217:
218:                if (n.isBlank())
219:                    return new JenaBlankNode(n);
220:
221:                // none of the above - don't know how to transform that
222:                throw new RuntimeException("no transformation defined from "
223:                        + n + " to java");
224:            }
225:
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.