Search in sources :

Example 6 with ScriptContextManager

use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.

the class DBTreeListClass method displayTree.

private String displayTree(String name, String prefix, List<String> selectlist, String mode, XWikiContext context) {
    ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
    ScriptContext scontext = scriptManager.getCurrentScriptContext();
    Map<String, ListItem> map = getMap(context);
    Map<String, List<ListItem>> treemap = getTreeMap(context);
    scontext.setAttribute("selectlist", selectlist, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("fieldname", prefix + name, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("tree", map, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("treelist", getTreeList(treemap, map, context), ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("treemap", treemap, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("mode", mode, ScriptContext.ENGINE_SCOPE);
    return context.getWiki().parseTemplate("treeview.vm", context);
}
Also used : ScriptContext(javax.script.ScriptContext) ArrayList(java.util.ArrayList) List(java.util.List) ScriptContextManager(org.xwiki.script.ScriptContextManager)

Example 7 with ScriptContextManager

use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.

the class XWiki method include.

public String include(String topic, boolean isForm, XWikiContext context) throws XWikiException {
    String database = null, incdatabase = null;
    String prefixedTopic, localTopic;
    // Save current documents in script context
    Document currentAPIdoc = null, currentAPIcdoc = null, currentAPItdoc = null;
    ScriptContextManager scritContextManager = Utils.getComponent(ScriptContextManager.class);
    ScriptContext scontext = scritContextManager.getScriptContext();
    String currentDocName = context.getWikiId() + ":" + context.getDoc().getFullName();
    if (scontext != null) {
        currentAPIdoc = (Document) scontext.getAttribute("doc");
        currentAPIcdoc = (Document) scontext.getAttribute("cdoc");
        currentAPItdoc = (Document) scontext.getAttribute("tdoc");
    }
    try {
        int i0 = topic.indexOf(':');
        if (i0 != -1) {
            incdatabase = topic.substring(0, i0);
            database = context.getWikiId();
            context.setWikiId(incdatabase);
            prefixedTopic = topic;
            localTopic = topic.substring(i0 + 1);
        } else {
            prefixedTopic = context.getWikiId() + ":" + topic;
            localTopic = topic;
        }
        XWikiDocument doc = null;
        try {
            LOGGER.debug("Including Topic " + topic);
            try {
                @SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
                if (includedDocs == null) {
                    includedDocs = new HashSet<String>();
                    context.put("included_docs", includedDocs);
                }
                if (includedDocs.contains(prefixedTopic) || currentDocName.equals(prefixedTopic)) {
                    LOGGER.warn("Error on too many recursive includes for topic " + topic);
                    return "Cannot make recursive include";
                }
                includedDocs.add(prefixedTopic);
            } catch (Exception e) {
            }
            // Get document to include
            DocumentReference targetDocumentReference = getCurrentMixedDocumentReferenceResolver().resolve(localTopic);
            doc = getDocument(targetDocumentReference, context);
            if (checkAccess("view", doc, context) == false) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "Access to this document is denied: " + doc);
            }
        } catch (XWikiException e) {
            LOGGER.warn("Exception Including Topic " + topic, e);
            return "Topic " + topic + " does not exist";
        }
        XWikiDocument contentdoc = doc.getTranslatedDocument(context);
        String result;
        if (isForm) {
            // We do everything in the context of the including document
            if (database != null) {
                context.setWikiId(database);
            }
            // Note: the Script macro in the new rendering checks for programming rights for the document in
            // the xwiki context.
            result = getRenderedContent(contentdoc, (XWikiDocument) context.get("doc"), context);
        } else {
            // We stay in the included document context
            // Since the Script macro checks for programming rights in the current document, we need to
            // temporarily set the contentdoc as the current doc before rendering it.
            XWikiDocument originalDoc = null;
            try {
                originalDoc = context.getDoc();
                context.put("doc", doc);
                result = getRenderedContent(contentdoc, doc, context);
            } finally {
                context.put("doc", originalDoc);
            }
        }
        try {
            @SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
            if (includedDocs != null) {
                includedDocs.remove(prefixedTopic);
            }
        } catch (Exception e) {
        }
        return result;
    } finally {
        if (database != null) {
            context.setWikiId(database);
        }
        if (currentAPIdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("doc", currentAPIdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
        if (currentAPIcdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("cdoc", currentAPIcdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
        if (currentAPItdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("tdoc", currentAPItdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ScriptContext(javax.script.ScriptContext) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document) ScriptContextManager(org.xwiki.script.ScriptContextManager) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 8 with ScriptContextManager

use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.

the class XWikiValidationStatus method addError.

public void addError(String className, String propName, String propPrettyName, String validationMessage, XWikiContext context) {
    getErrorObjects().add(className);
    getPropertyErrors().add(propName);
    if ((validationMessage != null) && (!validationMessage.trim().equals(""))) {
        getErrors().add(validationMessage);
    } else {
        ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
        ScriptContext scontext = scriptManager.getCurrentScriptContext();
        if (scontext == null) {
            getErrors().add("Validation error for property " + propPrettyName + " in class " + className);
        } else {
            scontext.setAttribute("className", className, ScriptContext.ENGINE_SCOPE);
            scontext.setAttribute("propName", propPrettyName, ScriptContext.ENGINE_SCOPE);
            String message = context.getMessageTool().get("validationerror", Collections.singletonList(propName));
            getErrors().add(message);
        }
    }
}
Also used : ScriptContext(javax.script.ScriptContext) ScriptContextManager(org.xwiki.script.ScriptContextManager)

Aggregations

ScriptContextManager (org.xwiki.script.ScriptContextManager)8 ScriptContext (javax.script.ScriptContext)4 SimpleScriptContext (javax.script.SimpleScriptContext)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Mockery (org.jmock.Mockery)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 Before (org.junit.Before)2 Execution (org.xwiki.context.Execution)2 WikiReference (org.xwiki.model.reference.WikiReference)2 ScriptMockSetup (org.xwiki.rendering.macro.script.ScriptMockSetup)2 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)1 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 Document (com.xpn.xwiki.api.Document)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)1