📄 unparser.java
字号:
printRdfAt("about");
print("=");
wURIreference(r);
return true;
}
private void wURIreference(String s) {
print(quote(prettyWriter.relativize(s)));
}
private void wURIreference(Resource r) {
wURIreference(r.getURI());
}
/*
* [6.16] idRefAttr ::= idAttr | resourceAttr
*/
private void wIdRefAttrOpt(Statement s, Resource r) {
wIdAttrReified(s);
if (!isGenuineAnon(r)) {
wResourceNodeIDAttr(r);
}
}
/*
* [6.6] idAttr ::= ' ID="' IDsymbol '"'
*/
private void wIdAttrReified(Statement s) {
if (wantReification(s)) {
/*
* if ( prettyWriter.sReification ) System.err.println("???"); else
* System.err.println("!!!");
*/
Statement reify[] = reification(s);
Resource res = (Resource) statement2res.get(s);
idDone.add(res);
int i;
for (i = 0; i < reify.length; i++)
done(reify[i]);
print(" ");
printRdfAt("ID");
print("=");
print(quote(getLocalName(res)));
haveReified.add(res);
}
}
/*
* [6.18] resourceAttr ::= ' resource="' URI-reference '"'
*/
private boolean wResourceNodeIDAttr(Resource r) {
return wNodeIDAttr(r) || wResourceAttr(r);
}
/*
* nodeIDAttr ::= ' rdf:nodeID="' URI-reference '"'
*/
private boolean wNodeIDAttr(Resource r) {
if (!r.isAnon())
return false;
print(" ");
printRdfAt("nodeID");
print("=");
print(q(prettyWriter.anonId(r)));
return true;
}
/*
* [6.18] resourceAttr ::= ' resource="' URI-reference '"'
*/
private boolean wResourceAttr(Resource r) {
if (r.isAnon())
return false;
print(" ");
printRdfAt("resource");
print("=");
wURIreference(r);
return true;
}
int codeCoverage[] = new int[8];
/*
* [6.19] Qname ::= [ NSprefix ':' ] name
*
* private void wQnameStart(String ns, String local) {
* print(prettyWriter.startElementTag(ns, local)); }
*
* private void wQnameEnd(String ns, String local) {
* print(prettyWriter.endElementTag(ns, local)); }
*/
private void wQNameAttr(Property p) {
print(prettyWriter.attributeTag(p.getURI()));
}
private void printRdfAt(String s) {
print(prettyWriter.rdfAt(s));
}
/*
* [6.10] propAttr ::= typeAttr | propName '="' string '"' (with embedded
* quotes escaped) [6.11] typeAttr ::= ' type="' URI-reference '"'
*/
private void wPropAttr(Property p, RDFNode n) {
tab();
if (p.equals(RDF.type))
wTypeAttr((Resource) n);
else
wPropAttrString(p, (Literal) n);
}
private void wTypeAttr(Resource r) {
print(" ");
printRdfAt("type");
print("=");
wURIreference(r);
// print(quote(r.getURI()));
}
private void wPropAttrString(Property p, Literal l) {
print(" ");
wQNameAttr(p);
print("=" + quote(l.getString()));
}
/*
* [daml.2] parseDamlCollection ::= ' parseType="daml:collection"'
*/
private void wParseDamlCollection() {
print(" ");
printRdfAt("parseType");
print("=" + q("daml:collection"));
}
/*
* [List.2] parseCollection ::= ' parseType="Collection"'
*/
private void wParseCollection() {
print(" ");
printRdfAt("parseType");
print("=" + q("Collection"));
}
/*
* [6.32] parseLiteral ::= ' parseType="Literal"'
*/
private void wParseLiteral() {
print(" ");
printRdfAt("parseType");
print("=" + q("Literal"));
}
private void wDatatype(String dtURI) {
print(" ");
printRdfAt("datatype");
print("=");
maybeNewline();
wURIreference(dtURI);
}
/*
* [6.33] parseResource ::= ' parseType="Resource"'
*/
private void wParseResource() {
print(" ");
printRdfAt("parseType");
print("=" + q("Resource"));
}
private void printNameSpaceDefn() {
print(prettyWriter.xmlnsDecl());
}
/***************************************************************************
* Utility routines ...
*
**************************************************************************/
/***************************************************************************
* Output and indentation.
**************************************************************************/
private int indentLevel = 0;
private int currentColumn = 0;
static private String filler(int lgth) {
char rslt[] = new char[lgth];
Arrays.fill(rslt, ' ');
return new String(rslt);
}
private void tab() {
int desiredColumn = prettyWriter.tabSize * indentLevel;
if (desiredColumn > prettyWriter.width) {
desiredColumn = 4 + (desiredColumn - 4) % prettyWriter.width;
}
if ((desiredColumn == 0 && currentColumn == 0)
|| desiredColumn > currentColumn) {
String spaces = filler(desiredColumn - currentColumn);
out.print(spaces);
} else {
out.println();
out.print(filler(desiredColumn));
}
currentColumn = desiredColumn;
}
private void maybeNewline() {
if (currentColumn > prettyWriter.width) {
tab();
}
}
/**
* Quote str with either ' or " quotes to be in attribute position in XML.
* The real rules are found at http://www.w3.org/TR/REC-xml#AVNormalize
*/
private String quote( String str ) {
return prettyWriter.substitutedAttribute(str);
}
private String q(String str) {
return prettyWriter.attributeQuoted(str);
}
/**
* Indentation screws up if there is a tab character in s. We do not check
* this.
*/
private void print(String s) {
out.print(s);
int ix = s.lastIndexOf('\n');
if (ix == -1)
currentColumn += s.length();
else
currentColumn = s.length() - ix - 1;
}
private void indentPlus() {
indentLevel++;
}
private void indentMinus() {
indentLevel--;
}
/*
* Unexpected error.
*/
private void error(String msg) {
JenaException e = new BrokenException("Internal error in Unparser: "
+ msg);
this.prettyWriter.fatalError(e);
throw e; // Just in case.
}
/**
* Name space stuff.
*/
private void addTypeNameSpaces() {
NodeIterator nn = model.listObjectsOfProperty(RDF.type);
try {
while (nn.hasNext()) {
RDFNode obj = nn.nextNode();
int split = isOKType(obj);
if (split != -1)
prettyWriter.addNameSpace(((Resource) obj).getURI()
.substring(0, split));
}
} finally {
nn.close();
}
}
private String getNameSpace(Resource r) {
if (r.isAnon()) {
logger.error("Internal error - Unparser.getNameSpace; giving up");
throw new BrokenException("Internal error: getNameSpace(bNode)");
}
String uri = r.getURI();
int split = Util.splitNamespace(uri);
return uri.substring(0, split);
}
/**
* Local and/or anonymous resources.
*/
private boolean isGenuineAnon(Resource r) {
if (!r.isAnon())
return false;
Integer v = (Integer) objectTable.get(r);
return v == null
|| ((!prettyWriter.sResourcePropertyElt) && v.intValue() <= 1 && (!haveReified
.contains(r)));
}
private boolean isLocalReference(Resource r) {
return (!r.isAnon()) && getNameSpace(r).equals(localName + "#")
&& XMLChar.isValidNCName(getLocalName(r));
}
/*
* Utility for turning an integer into an alphabetic string.
*
* private static String getSuffix(int suffixId) { if (suffixId == 0) return
* ""; else { suffixId--; int more = (suffixId / 26);
*
* return getSuffix(more) + new Character((char) ('a' + suffixId % 26)); } }
*/
private String getLocalName(Resource r) {
if (r.isAnon()) {
logger.error("Internal error - giving up - Unparser.getLocalName");
throw new BrokenException("Internal error: getLocalName(bNode)");
}
String uri = r.getURI();
int split = Util.splitNamespace(uri);
return uri.substring(split);
}
/**
* objectTable initialization.
*/
private void increaseObjectCount(Resource r) {
if (!r.isAnon())
return;
Integer cnt = (Integer) objectTable.get(r);
if (cnt == null) {
cnt = one;
} else {
cnt = new Integer(cnt.intValue() + 1);
}
objectTable.put(r, cnt);
}
/***************************************************************************
* Reification support.
**************************************************************************/
/*
* Is the use of ID in rule [6.12] to create a reification helpful or not?
*/
private boolean wantReification(Statement s) {
return wantReification(s, (Resource) statement2res.get(s));
}
private boolean wantReification(Resource res) {
return wantReification((Statement) res2statement.get(res), res);
}
private boolean wantReification(Statement s, Resource ref) {
if (s == null || ref == null || ref.isAnon()
|| prettyWriter.sReification)
return false;
if (!(isLocalReference(ref)))
return false;
Statement reify[] = reification(s);
int i;
for (i = 0; i < reify.length; i++)
if (doneSet.contains(reify[i]) || (!model.contains(reify[i])))
return false; // Some of reification already done.
return true; // Reification rule helps.
}
private Statement[] reification(Statement s) {
Model m = s.getModel();
Resource r = (Resource) statement2res.get(s);
return new Statement[] { m.createStatement(r, RDF.type, RDF.Statement),
m.createStatement(r, RDF.subject, s.getSubject()),
m.createStatement(r, RDF.predicate, s.getPredicate()),
m.createStatement(r, RDF.object, s.getObject()) };
}
private boolean hasProperties(Resource r) {
ExtendedIterator ss = listProperties(r);
if (avoidExplicitReification && // ( r instanceof Statement ) &&
(!r.isAnon()) && isLocalReference(r)
&& res2statement.containsKey(r)) {
ss = new MapFilterIterator(new MapFilter() {
public Object accept(Object o) {
Statement s = (Statement) o;
Property p = s.getPredicate();
return ((!p.getNameSpace().equals(rdfns)) || !((RDF.type
.equals(p) && s.getObject().equals(RDF.Statement))
|| RDF.object.equals(p) || RDF.predicate.equals(p) || RDF.subject
.equals(p))) ? o : null;
}
}, ss);
}
try {
return ss.hasNext();
} finally {
ss.close();
}
}
private ExtendedIterator listProperties(Resource r) {
return new MapFilterIterator(new MapFilter() {
public Object accept(Object o) {
return doneSet.contains(o) ? null : o;
}
}, r.listProperties());
}
// Good type statement, or simple string valued statement with no langID
// See http://www.w3.org/TR/REC-xml#AVNormalize
private boolean canBeAttribute(Statement s, Set seen) {
Property p = s.getPredicate();
// Check seen first.
if (prettyWriter.sPropertyAttr || seen.contains(p)) // We can't use the
// same attribute
// twice in one rule.
return false;
seen.add(p);
if (p.equals(RDF.type)) {
// If we have a model in which a type is given
// as a string, then we avoid the attribute rule 6.10 which is
// ambiguous with 6.11.
RDFNode n = s.getObject();
// return (n instanceof Resource) && !((Resource) n).isAnon();
return n.isURIResource();
}
if (s.getObject() instanceof Literal) {
Literal l = s.getLiteral();
if (l.getDatatypeURI() != null)
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -