"Cannot process expression '" + getExpression() + "' on a non-content context: " + context.getdocument().getDocumentKey());
return errorReturnContext;
}
WGStructEntry struct = content.getStructEntry();
WGDatabase db = context.getdocument().getDatabase();
if (contextExpression.equals("parent")) {
if (content.isDummy()) {
context.setLastError("Cannot retrieve parent of dummy content");
return errorReturnContext;
}
if (struct.isRoot()) {
context.setLastError("Cannot get parent of root document");
return errorReturnContext;
}
else {
WGContent parentContent = navigator.getParentContent(content);
if (parentContent != null) {
return context.getTMLContextForDocument(parentContent);
}
else {
context.setLastError(
"Could not retrieve parent content: "
+ content.getTitle()
+ " ("
+ content.getContentKey().toString()
+ ")");
return errorReturnContext;
}
}
}
else if (contextExpression.equals("root")) {
WGContent rootContent = null;
if (!content.isDummy()) {
rootContent = navigator.getRootContent(content);
}
else {
rootContent = content.getDatabase().getFirstReleasedContent(_languageChooser, true);
}
if (rootContent != null) {
return context.getTMLContextForDocument(rootContent);
}
else {
context.setLastError("Could not retrieve root content");
return errorReturnContext;
}
}
else if (contextExpression.equals("main") || contextExpression.equals("currentdocument")) {
TMLContext mainContext = context.getmaincontext();
return new TMLContext(mainContext.getdocument(), context);
}
else if (contextExpression.startsWith("children")) {
if (content.isDummy()) {
context.setLastError("Cannot retrieve children of dummy content");
return errorReturnContext;
}
String idxString = contextExpression.substring(contextExpression.indexOf("[") + 1, contextExpression.indexOf("]"));
int idx;
try {
idx = Integer.parseInt(idxString);
}
catch (NumberFormatException exc) {
context.setLastError("Can't parse children index: " + contextExpression);
return errorReturnContext;
}
WGContent childContent = navigator.getChildContent(content, idx, WGContent.SEARCHORDER_ASCENDING);
if (childContent != null) {
return context.getTMLContextForDocument(childContent);
}
else {
context.setLastError("Cant retrieve child content idx " + idx);
return errorReturnContext;
}
}
else if (contextExpression.equals("selectedchild")) {
if (content.isDummy()) {
context.setLastError("Cannot retrieve children of dummy content");
return errorReturnContext;
}
WGContent mainContentParent = context.getmaincontext().content();
while (mainContentParent != null && !mainContentParent.isDummy() && !mainContentParent.getStructEntry().isRoot()) {
if (mainContentParent.getStructEntry().getParentEntry().equals(struct)) {
return context.getTMLContextForDocument(mainContentParent);
}
else {
mainContentParent = navigator.getParentContent(mainContentParent);
}
}
context.setLastError("Could not retrieve selected child");
return errorReturnContext;
}
else if (contextExpression.startsWith("siblings")) {
if (content.isDummy()) {
context.setLastError("Cannot retrieve siblings of dummy content");
return errorReturnContext;
}
String idxString =
contextExpression.substring(contextExpression.indexOf("[") + 1, contextExpression.indexOf("]")).trim();
boolean relative = (idxString.charAt(0) == '+' || idxString.charAt(0) == '-' ? true : false);
if (idxString.charAt(0) == '+') {
idxString = idxString.substring(1);
}
int idx = 0;
try {
idx = Integer.parseInt(idxString);
}
catch (NumberFormatException exc) {
context.setLastError("Can't parse siblings index: " + contextExpression);
return errorReturnContext;
}
WGContent sibling = navigator.getSiblingContent(content, idx, relative);
if (sibling != null) {
return context.getTMLContextForDocument(sibling);
}
else {
context.setLastError("Could not retrieve sibling " + idxString);
return errorReturnContext;
}
}
else if (contextExpression.equals("this")) {
if (!_explicitLanguageChoice) {
return context;
}
if (content.isDummy()) {
WGLanguage lang = _languageChooser.selectDatabaseLanguage(db);
if (lang != null) {
return context.getTMLContextForDocument(db.getDummyContent(lang.getName()));
}
else {
return errorReturnContext;
}
}