001: package org.ontoware.rdf2go.impl.jena24;
002:
003: import java.io.StringWriter;
004: import java.util.Arrays;
005: import java.util.Iterator;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.ontoware.rdf2go.Reasoning;
010: import org.ontoware.rdf2go.exception.ModelRuntimeException;
011: import org.ontoware.rdf2go.model.Model;
012: import org.ontoware.rdf2go.model.ModelSet;
013: import org.ontoware.rdf2go.model.Statement;
014:
015: public class DumpUtils {
016:
017: private static Log log = LogFactory.getLog(DumpUtils.class);
018:
019: public static void addCommonPrefixesToJenaModel(
020: com.hp.hpl.jena.rdf.model.Model jenaModel) {
021: jenaModel.setNsPrefix("rdf",
022: "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
023: jenaModel.setNsPrefix("rdfs",
024: "http://www.w3.org/2000/01/rdf-schema#");
025: jenaModel.setNsPrefix("owl", "http://www.w3.org/2002/07/owl#");
026: jenaModel.setNsPrefix("foaf", "http://xmlns.com/foaf/0.1/");
027: jenaModel.setNsPrefix("rnd", "rnd://rnd/");
028:
029: // SMW
030: jenaModel.setNsPrefix("smw",
031: "http://smw.ontoware.org/2005/smw#");
032: jenaModel.setNsPrefix("crawl",
033: "http://www.xam.de/2006/07/crawl#");
034:
035: }
036:
037: public static String toN3(Model m) {
038: try {
039: ModelImplJena24 jModel = new ModelImplJena24(Reasoning.none);
040: jModel.addAll(m.iterator());
041: StringWriter sw = new StringWriter();
042: addCommonPrefixesToJenaModel(jModel.getInternalJenaModel());
043: jModel.getInternalJenaModel().write(sw, "N3");
044: return sw.toString();
045: } catch (Exception e) {
046: throw new RuntimeException(e);
047: }
048: }
049:
050: /**
051: * dumps the model at the logger of the implementation.
052: *
053: * --usually only used for debugging--
054: *
055: * @param options
056: * null for default. format: "n3"|"rdfxml"|"ntriple", output:
057: * "sysout"|"log"
058: * @throws Exception
059: * Jena knows: "RDF/XML","RDF/XML-ABBREV", "N-TRIPLE" and "N3".
060: */
061: public static void dump(Model m, String[] options)
062: throws ModelRuntimeException {
063: boolean n3 = false;
064: boolean rdf = false;
065: boolean ntriples = false;
066: boolean sysout = true;
067: boolean _log = false;
068: if (options != null) {
069: n3 = Arrays.asList(options).contains("n3");
070: rdf = Arrays.asList(options).contains("rdfxml");
071: ntriples = Arrays.asList(options).contains("nt");
072: sysout = Arrays.asList(options).contains("sysout");
073: _log = Arrays.asList(options).contains("log");
074: }
075: if (!(sysout | _log))
076: sysout = true;
077:
078: if (n3 | rdf | ntriples) {
079: ModelImplJena24 jModel = new ModelImplJena24(Reasoning.none);
080:
081: // makes the N3 model look nicer
082: // TODO guess all namespaces automatically
083: addCommonPrefixesToJenaModel(jModel.getInternalJenaModel());
084:
085: jModel.addAll(m.iterator());
086: StringWriter sw = new StringWriter();
087: if (n3) {
088: jModel.getInternalJenaModel().write(sw, "N3-PP");
089: if (sysout)
090: System.out.println(sw.getBuffer());
091: if (_log)
092: log.debug("dump: " + sw.getBuffer());
093: }
094: if (rdf) {
095: jModel.getInternalJenaModel().write(sw, "RDF/XML");
096: if (sysout)
097: System.out.println(sw.getBuffer());
098: if (_log)
099: log.debug("dump: " + sw.getBuffer());
100: }
101: if (ntriples) {
102: jModel.getInternalJenaModel().write(sw, "N-TRIPLE");
103: if (sysout)
104: System.out.println(sw.getBuffer());
105: if (_log)
106: log.debug("dump: " + sw.getBuffer());
107: }
108: } else {
109: Iterator<Statement> sit = m.iterator();
110: while (sit.hasNext()) {
111: sit.next().dump(options);
112: }
113: }
114:
115: } // //////////////////////////////
116:
117: public static void dump(ModelSet modelSet, String[] options)
118: throws ModelRuntimeException {
119: Iterator<? extends Model> it = modelSet.getModels();
120: assert it != null;
121: while (it.hasNext()) {
122: Model m = it.next();
123: DumpUtils.dump(m, options);
124: }
125: }
126:
127: }
|