io.konig.core.io.DebugGraphReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konig-core Show documentation
Show all versions of konig-core Show documentation
A library for core classes (Graph, Vertex, Edge, etc.)
package io.konig.core.io;
/*
* #%L
* konig-core
* %%
* Copyright (C) 2015 Gregory McFall
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static io.konig.core.io.GraphConstants.BNODE;
import static io.konig.core.io.GraphConstants.IRI;
import static io.konig.core.io.GraphConstants.LABEL;
import static io.konig.core.io.GraphConstants.LANG;
import static io.konig.core.io.GraphConstants.LITERAL_IRI;
import static io.konig.core.io.GraphConstants.LITERAL_QNAME;
import static io.konig.core.io.GraphConstants.LITERAL_TERM;
import static io.konig.core.io.GraphConstants.PLAIN;
import static io.konig.core.io.GraphConstants.QNAME;
import static io.konig.core.io.GraphConstants.RESOURCE;
import static io.konig.core.io.GraphConstants.GRAPH;
import static io.konig.core.io.GraphConstants.TERM;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.List;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.konig.core.Context;
import io.konig.core.ContextManager;
import io.konig.core.Term;
public class DebugGraphReader {
private static final char newline = '\n';
private ByteBuffer data;
private Context context;
private StringBuilder builder;
public String toDebugString(byte[] array, ContextManager manager) {
data = ByteBuffer.wrap(array);
builder = new StringBuilder();
short version = data.getShort();
builder.append("0 VERSION ").append(version).append(newline);
mark();
String contextURI = readString();
append("CONTEXT ").append(contextURI).append(newline);
context = manager.getContextByURI(contextURI);
if (context == null) {
throw new KonigReadException("Context not found: " + contextURI);
}
context.compile();
appendVertices();
return builder.toString();
}
private void appendVertices() {
List termList = context.asList();
while (data.hasRemaining()) {
mark();
byte token = data.get();
switch (token) {
case IRI:
String iriValue = readString();
append(" IRI ").append(iriValue);
break;
case TERM:
short termIndex = data.getShort();
append(" TERM[").append(termIndex).append("] ");
if (termIndex >= termList.size()) {
throw new KonigReadException("Term index out of range: " + termIndex);
}
Term term = termList.get(termIndex);
String termValue = context.expandIRI(term.getKey());
append(termValue);
break;
case QNAME:
short qnameIndex = data.getShort();
append(" QNAME[").append(qnameIndex).append("] ");
if (qnameIndex >= termList.size()) {
throw new KonigReadException("QNAME index out of range: " + qnameIndex);
}
Term prefixTerm = termList.get(qnameIndex);
URI prefixURI = prefixTerm.getExpandedId();
String prefixValue = prefixURI == null ? "null" : prefixURI.stringValue();
String localName = readString();
append(prefixValue).append(' ').append(localName);
break;
case BNODE:
short bnodeId = data.getShort();
append(" BNode[").append(bnodeId).append("]");
break;
default:
invalidToken("Invalid token for subject: ", token);
}
short predicateCount = data.getShort();
append("; PREDICATE-COUNT: ").append(predicateCount).append(newline);
for (int i=0; i termList = context.asList();
mark();
append(" ");
byte token = data.get();
switch (token) {
case IRI :
String iriValue = readString();
append("IRI ").append(iriValue);
break;
case TERM:
short termIndex = data.getShort();
if (termIndex >= termList.size()) {
throw new KonigReadException("Term index out of range: " + termIndex);
}
Term term = termList.get(termIndex);
String termValue = context.expandIRI(term.getKey());
append("TERM[").append(termIndex).append("] ");
append(termValue);
break;
case QNAME:
short qnameIndex = data.getShort();
append("QNAME[").append(qnameIndex).append("] ");
if (qnameIndex >= termList.size()) {
throw new KonigReadException("QNAME index out of range: " + qnameIndex);
}
Term prefixTerm = termList.get(qnameIndex);
URI prefixURI = prefixTerm.getExpandedId();
String prefixValue = prefixURI == null ? "null" : prefixURI.stringValue();
String localName = readString();
append(prefixValue).append(' ').append(localName);
break;
default:
invalidToken("Invalid predicate token: ", token);
}
short objectCount = data.getShort();
append("; OBJECT-COUNT: ").append(objectCount).append(newline);
for (int i=0; i termList = context.asList();
mark();
append("