use of org.xwiki.model.reference.ObjectPropertyReference in project xwiki-platform by xwiki.
the class WikiSkinUtils method getSkinResourceFromDocumentSkin.
private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin) {
if (skinDocument != null) {
// Try to find a XWikiSkinFileOverrideClass object
BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
if (obj != null) {
ObjectPropertyReference reference = new ObjectPropertyReference(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, obj.getReference());
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT));
}
// Try parsing the object property
BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
if (property != null) {
ObjectPropertyReference reference = property.getReference();
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue());
}
// Try parsing a document attachment
// Convert "/" into "." in order to support wiki skin attachments to override some resources located in
// subdirectories.
String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
if (attachment != null) {
AttachmentReference reference = attachment.getReference();
return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), this.xcontextProvider);
}
}
return null;
}
use of org.xwiki.model.reference.ObjectPropertyReference in project xwiki-platform by xwiki.
the class WikiSkinUtils method getSkinResourceProperty.
private BaseProperty<ObjectPropertyReference> getSkinResourceProperty(String resource, XWikiDocument skinDocument) {
// Try parsing the object property
BaseObject skinObject = skinDocument.getXObject(SKINCLASS_REFERENCE);
if (skinObject != null) {
BaseProperty<ObjectPropertyReference> resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(resource);
// If not found try by replacing '/' with '.'
if (resourceProperty == null) {
String escapedTemplateName = StringUtils.replaceChars(resource, '/', '.');
resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(escapedTemplateName);
}
if (resourceProperty != null) {
Object value = resourceProperty.getValue();
if (value instanceof String && StringUtils.isNotEmpty((String) value)) {
return resourceProperty;
}
}
}
return null;
}
use of org.xwiki.model.reference.ObjectPropertyReference in project xwiki-platform by xwiki.
the class SxDocumentSource method getContent.
@Override
public String getContent() {
StringBuilder resultBuilder = new StringBuilder();
List<BaseObject> objects = this.document.getObjects(this.extension.getClassName());
if (objects != null) {
for (BaseObject sxObj : objects) {
if (sxObj == null) {
continue;
}
String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME);
int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME);
if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) {
LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class);
LESSResourceReferenceFactory lessResourceReferenceFactory = Utils.getComponent(LESSResourceReferenceFactory.class);
ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference(CONTENT_PROPERTY_NAME, sxObj.getReference());
LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
try {
sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false);
} catch (LESSCompilerException e) {
// Set the error message in a CSS comment to help the developer understand why its SSX is not
// working (it will work only if the CSS minifier is not used).
sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */", sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e));
}
} else if (parse == 1) {
try {
StringWriter writer = new StringWriter();
VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
VelocityContext vcontext = velocityManager.getVelocityContext();
velocityManager.getVelocityEngine().evaluate(vcontext, writer, this.document.getPrefixedFullName(), sxContent);
sxContent = writer.toString();
} catch (XWikiVelocityException ex) {
LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ", this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex));
}
}
// Also add a newline, in case the different object contents don't end with a blank
// line, and could cause syntax errors when concatenated.
resultBuilder.append(sxContent + "\n");
}
}
return resultBuilder.toString();
}
use of org.xwiki.model.reference.ObjectPropertyReference in project xwiki-platform by xwiki.
the class SSXListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
XWikiDocument document = (XWikiDocument) source;
String currentWiki = wikiDescriptorManager.getCurrentWikiId();
DocumentReference ssxDocRef = new DocumentReference(currentWiki, "XWiki", "StyleSheetExtension");
List<BaseObject> ssxObjects = document.getXObjects(ssxDocRef);
if (ssxObjects != null && !ssxObjects.isEmpty()) {
for (BaseObject obj : ssxObjects) {
if (obj == null) {
continue;
}
if ("LESS".equals(obj.getStringValue("contentType"))) {
ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference("code", new BaseObjectReference(ssxDocRef, obj.getNumber(), document.getDocumentReference()));
LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
lessResourcesCache.clearFromLESSResource(lessResourceReference);
colorThemeCache.clearFromLESSResource(lessResourceReference);
}
}
}
}
use of org.xwiki.model.reference.ObjectPropertyReference in project xwiki-platform by xwiki.
the class SkinAction method renderFileFromObjectField.
/**
* Tries to serve the content of an XWikiSkins object field as a skin file.
*
* @param filename The name of the skin file that should be rendered.
* @param doc The skin {@link XWikiDocument document}.
* @param context The current {@link XWikiContext request context}.
* @return <tt>true</tt> if the object exists, and the field is set to a non-empty value, and its content was
* successfully sent.
* @throws IOException If the response cannot be sent.
*/
public boolean renderFileFromObjectField(String filename, XWikiDocument doc, final XWikiContext context) throws IOException {
LOGGER.debug("... as object property");
BaseObject object = doc.getObject("XWiki.XWikiSkins");
String content = null;
if (object != null) {
content = object.getStringValue(filename);
}
if (!StringUtils.isBlank(content)) {
XWiki xwiki = context.getWiki();
// Evaluate the file only if it's of a supported type.
String mimetype = xwiki.getEngineContext().getMimeType(filename.toLowerCase());
if (isCssMimeType(mimetype) || isJavascriptMimeType(mimetype)) {
final ObjectPropertyReference propertyReference = new ObjectPropertyReference(filename, object.getReference());
// Evaluate the content with the rights of the document's author.
content = evaluateVelocity(content, propertyReference, doc.getAuthorReference(), context);
}
// Prepare the response.
XWikiResponse response = context.getResponse();
// Since object fields are read as unicode strings, the result does not depend on the wiki encoding. Force
// the output to UTF-8.
response.setCharacterEncoding(ENCODING);
// Write the content to the response's output stream.
byte[] data = content.getBytes(ENCODING);
setupHeaders(response, mimetype, doc.getDate(), data.length);
response.getOutputStream().write(data);
return true;
} else {
LOGGER.debug("Object field not found or empty");
}
return false;
}
Aggregations