📄 unparser.java
字号:
*/
private boolean wPropertyEltResource(WType wt, Property prop, Statement s,
RDFNode r) {
if (prettyWriter.sParseTypeResourcePropertyElt)
return false;
if (r instanceof Literal)
return false;
Resource res = (Resource) r;
if (!isGenuineAnon(res))
return false;
if (getType(res) != null)
return false; // preferred typed node construction.
// print out.
done(s);
tab();
print("<");
wt.wTypeStart(prop);
indentPlus();
wIdAttrReified(s);
wParseResource();
print(">");
wPropertyEltStar(res);
indentMinus();
tab();
print("</");
wt.wTypeEnd(prop);
print(">");
return true;
}
/*
* [6.12] propertyElt ::= '<' propName idAttr? '>' value '</' propName '>'
*/
private boolean wPropertyEltValue(WType wt, Property prop, Statement s,
RDFNode r) {
return wPropertyEltValueString(wt, prop, s, r)
|| wPropertyEltValueObj(wt, prop, s, r);
}
/*
* [6.12] propertyElt ::= '<' propName idAttr? '>' value '</' propName '>'
*/
private boolean wPropertyEltValueString(WType wt, Property prop,
Statement s, RDFNode r) {
if (r instanceof Literal) {
done(s);
Literal lt = (Literal) r;
String lang = lt.getLanguage();
tab();
print("<");
wt.wTypeStart(prop);
wIdAttrReified(s);
maybeNewline();
if (lang != null && lang.length() > 0)
print(" xml:lang=" + q(lang));
maybeNewline();
print(">");
wValueString(lt);
print("</");
wt.wTypeEnd(prop);
print(">");
return true;
}
return false;
}
/*
* [6.17.2] value ::= string
*/
private void wValueString(Literal lt) {
String val = lt.getString();
print(Util.substituteEntitiesInElementContent(val));
}
/*
* [6.12] propertyElt ::= '<' propName idAttr? '>' value '</' propName '>'
* [6.17.1] value ::= obj
*/
private boolean wPropertyEltValueObj(WType wt, Property prop, Statement s,
RDFNode r) {
if (r instanceof Resource && !prettyWriter.sResourcePropertyElt) {
Resource res = (Resource) r;
done(s);
tab();
print("<");
wt.wTypeStart(prop);
wIdAttrReified(s);
print(">");
tab();
indentPlus();
wObj(res, false);
indentMinus();
tab();
print("</");
wt.wTypeEnd(prop);
print(">");
return true;
}
return false;
}
/*
* [daml.1 - 6.12 cont.] | '<' propName idAttr? parseDamlCollection '>'
* obj* '</' propName '>'
*/
private boolean wPropertyEltDamlCollection(WType wt, Property prop,
Statement s, RDFNode r) {
boolean daml = true;
Statement list[][] = getDamlList(r);
if (list == null) {
daml = false;
list = getRDFList(r);
}
if (list == null)
return false;
// print out.
done(s);
// record all done's first - they may impact the
// way we print the values.
for (int i = 0; i < list.length; i++) {
done(list[i][0]);
done(list[i][1]);
if (daml)
done(list[i][2]);
}
tab();
print("<");
wt.wTypeStart(prop);
indentPlus();
wIdAttrReified(s);
if (daml)
wParseDamlCollection();
else
wParseCollection();
print(">");
for (int i = 0; i < list.length; i++) {
wObj((Resource) list[i][0].getObject(), false);
}
indentMinus();
tab();
print("</");
wt.wTypeEnd(prop);
print(">");
return true;
}
// propAttr* with no left over statements.
private void wPropAttrAll(Resource r) {
wPropAttrSome(r);
if (hasProperties(r))
error("Bad call to wPropAttrAll");
}
// propAttr* possibly with left over statements.
private void wPropAttrSome(Resource r) {
ClosableIterator ss = listProperties(r);
try {
Set seen = new HashSet();
while (ss.hasNext()) {
Statement s = (Statement) ss.next();
if (canBeAttribute(s, seen)) {
done(s);
wPropAttr(s.getPredicate(), s.getObject());
}
}
} finally {
ss.close();
}
}
/*
* [6.2] obj ::= description | container [6.3] description ::= '<rdf:Description'
* idAboutAttr? bagIdAttr? propAttr* '/>' | '<rdf:Description' idAboutAttr?
* bagIdAttr? propAttr* '>' propertyElt* '</rdf:Description>' | typedNode
* [6.4] container ::= sequence | bag | alternative We use: [6.2a] obj ::=
* description | container | typedNode [6.3a] description ::= '<rdf:Description'
* idAboutAttr? bagIdAttr? propAttr* '/>' | '<rdf:Description' idAboutAttr?
* bagIdAttr? propAttr* '>' propertyElt* '</rdf:Description>'
*
* This method has got somewhat messy. If we are not at the topLevel we may
* choose to not expand a node but just use a typedNode ::= '<' typeName
* idAboutAttr '/>' rule. This rules also applies to Bags that we feel
* unconfortable with, such as a Bag arising from a BagId rule that we don't
* handle properly.
*
*
*/
private boolean wObj(Resource r, boolean topLevel) {
try {
doing.add(r);
Statement typeStatement = getType(r);
if (typeStatement != null) {
Resource t = typeStatement.getResource();
if (!topLevel) {
if (pleasingTypeSet.contains(t) && (!isGenuineAnon(r))) {
return wTypedNodeNoProperties(r);
}
}
return wTypedNode(r) || wDescription(r);
}
return wDescription(r);
} finally {
doing.remove(r);
}
}
abstract private class WType {
abstract void wTypeStart(Resource uri);
abstract void wTypeEnd(Resource uri);
}
static private int RDF_HASH = RDF.getURI().length();
private WType wdesc = new WType() {
void wTypeStart(Resource u) {
print(prettyWriter.rdfEl(u.getURI().substring(RDF_HASH)));
}
void wTypeEnd(Resource u) {
print(prettyWriter.rdfEl(u.getURI().substring(RDF_HASH)));
}
};
private WType wtype = new WType() {
void wTypeStart(Resource u) {
print(prettyWriter.startElementTag(u.getURI()));
}
void wTypeEnd(Resource u) {
print(prettyWriter.endElementTag(u.getURI()));
}
};
/*
* [6.3a] description ::= '<rdf:Description' idAboutAttr? bagIdAttr?
* propAttr* '/>' | '<rdf:Description' idAboutAttr? bagIdAttr? propAttr*
* '>' propertyElt* '</rdf:Description>'
*/
private boolean wDescription(Resource r) {
return wTypedNodeOrDescription(wdesc, DESCRIPTION, r);
}
/*
* [6.13] typedNode ::= '<' typeName idAboutAttr? bagIdAttr? propAttr* '/>' | '<'
* typeName idAboutAttr? bagIdAttr? propAttr* '>' propertyElt* '</'
* typeName '>'
*/
private boolean wTypedNode(Resource r) {
Statement st = getType(r);
if (st == null)
return false;
Resource type = st.getResource();
done(st);
return wTypedNodeOrDescription(wtype, type, r);
}
private boolean wTypedNodeOrDescription(WType wt, Resource ty, Resource r) {
// preparation - look for the li's.
Vector found = new Vector();
ClosableIterator ss = listProperties(r);
try {
int greatest = 0;
if (!prettyWriter.sListExpand)
while (ss.hasNext()) {
Statement s = (Statement) ss.next();
int ix = s.getPredicate().getOrdinal();
if (ix != 0) {
if (ix > greatest) {
found.setSize(ix);
greatest = ix;
}
found.set(ix - 1, s);
}
}
} finally {
ss.close();
}
int last = found.indexOf(null);
List li = last == -1 ? found : found.subList(0, last);
return wTypedNodeOrDescriptionCompact(wt, ty, r, li)
|| wTypedNodeOrDescriptionLong(wt, ty, r, li);
}
/*
* [6.13.1] typedNode ::= '<' typeName idAboutAttr? bagIdAttr? propAttr*
* '/>'
*/
private boolean wTypedNodeOrDescriptionCompact(WType wt, Resource ty,
Resource r, List li) {
// Conditions
if ((!li.isEmpty()) || !allPropsAreAttr(r))
return false;
// Write out
tab();
print("<");
wt.wTypeStart(ty);
indentPlus();
wIdAboutAttrOpt(r);
wPropAttrAll(r);
print("/>");
indentMinus();
return true;
}
/*
* [6.13.1] typedNode ::= '<' typeName idAboutAttr '/>'
*/
private boolean wTypedNodeNoProperties(Resource r) {
// Conditions
if (isGenuineAnon(r))
return false;
Statement st = getType(r);
if (st == null)
return false;
Resource type = st.getResource();
done(st);
// Write out
tab();
print("<");
wtype.wTypeStart(type);
indentPlus();
// if (hasProperties(r))
// wAboutAttr(r);
// else
wIdAboutAttrOpt(r);
print("/>");
indentMinus();
return true;
}
/*
* [6.13.2] typedNode ::= '<' typeName idAboutAttr? bagIdAttr? propAttr*
* '>' propertyElt* '</' typeName '>'
*/
private boolean wTypedNodeOrDescriptionLong(WType wt, Resource ty,
Resource r, List li) {
Iterator it = li.iterator();
while (it.hasNext()) {
done((Statement) it.next());
}
tab();
print("<");
wt.wTypeStart(ty);
indentPlus();
wIdAboutAttrOpt(r);
wPropAttrSome(r);
print(">");
wLiEltStar(li.iterator());
wPropertyEltStar(r);
indentMinus();
tab();
print("</");
wt.wTypeEnd(ty);
print(">");
return true;
}
private void wPropertyEltStar(Resource r) {
ClosableIterator ss = this.listProperties(r);
try {
while (ss.hasNext()) {
Statement s = (Statement) ss.next();
wPropertyElt(wtype, s.getPredicate(), s, s.getObject());
}
} finally {
ss.close();
}
}
private void wLiEltStar(Iterator ss) {
while (ss.hasNext()) {
Statement s = (Statement) ss.next();
wPropertyElt(wdesc, LI, s, s.getObject());
}
}
/*
* [6.5] idAboutAttr ::= idAttr | aboutAttr | aboutEachAttr we use [6.5a]
* idAboutAttr ::= idAttr | aboutAttr
*/
private Set idDone = new HashSet();
private boolean wIdAboutAttrOpt(Resource r) {
return wIdAttrOpt(r) || wNodeIDAttr(r) || wAboutAttr(r);
}
/**
* Returns false if the resource is not genuinely anonymous and cannot be
* referred to using an ID. [6.6] idAttr ::= ' ID="' IDsymbol '"'
*/
private boolean wIdAttrOpt(Resource r) {
if (isGenuineAnon(r))
return true; // We have output resource (with nothing).
if (prettyWriter.sIdAttr)
return false;
if (r.isAnon())
return false;
if (isLocalReference(r)) {
// Try and use the reification rules if they apply.
// Issue: aren't we just about to list those statements explicitly.
if (wantReification(r))
return false;
// Can be an ID if not already output.
if (idDone.contains(r)) {
return false; // We have already output this one.
}
idDone.add(r);
print(" ");
printRdfAt("ID");
print("=");
print(quote(getLocalName(r)));
return true;
}
return false;
}
/*
* [6.7] aboutAttr ::= ' about="' URI-reference '"'
*/
private boolean wAboutAttr(Resource r) {
print(" ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -