Search in sources :

Example 1 with ObjectPropertyReference

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;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with ObjectPropertyReference

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;
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with ObjectPropertyReference

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();
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) VelocityContext(org.apache.velocity.VelocityContext) LESSCompiler(org.xwiki.lesscss.compiler.LESSCompiler) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) LESSResourceReferenceFactory(org.xwiki.lesscss.resources.LESSResourceReferenceFactory) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference)

Example 4 with ObjectPropertyReference

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);
            }
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with ObjectPropertyReference

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;
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) XWiki(com.xpn.xwiki.XWiki) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)16 DocumentReference (org.xwiki.model.reference.DocumentReference)10 BaseObject (com.xpn.xwiki.objects.BaseObject)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Test (org.junit.Test)5 ObjectReference (org.xwiki.model.reference.ObjectReference)5 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)4 ArrayList (java.util.ArrayList)3 EntityReference (org.xwiki.model.reference.EntityReference)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseProperty (com.xpn.xwiki.objects.BaseProperty)2 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 Property (org.xwiki.rest.model.jaxb.Property)2 ObjectPropertyResource (org.xwiki.rest.resources.objects.ObjectPropertyResource)2 IntegerProperty (com.xpn.xwiki.objects.IntegerProperty)1 StringProperty (com.xpn.xwiki.objects.StringProperty)1