Search in sources :

Example 31 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class DefaultOfficeViewerScriptService method view.

@Override
public String view(AttachmentReference attachmentReference, Map<String, String> parameters) {
    // Clear previous caught exception.
    this.execution.getContext().removeProperty(OFFICE_VIEW_EXCEPTION);
    try {
        DocumentReference documentReference = attachmentReference.getDocumentReference();
        // Check whether current user has view rights on the document containing the attachment.
        if (!this.documentAccessBridge.isDocumentViewable(documentReference)) {
            throw new RuntimeException("Inadequate privileges.");
        }
        // Create the view and render the result.
        Syntax fromSyntax = this.documentAccessBridge.getTranslatedDocumentInstance(documentReference).getSyntax();
        Syntax toSyntax = Syntax.XHTML_1_0;
        return render(this.officeViewer.createView(attachmentReference, parameters), fromSyntax, toSyntax);
    } catch (Exception e) {
        // Save caught exception.
        this.execution.getContext().setProperty(OFFICE_VIEW_EXCEPTION, e);
        this.logger.error("Failed to view office document: " + attachmentReference, e);
        return null;
    }
}
Also used : Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 32 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class DefaultCoreConfiguration method getDefaultDocumentSyntax.

/**
 * @see CoreConfiguration#getDefaultDocumentSyntax()
 * @since 2.3M1
 */
@Override
public Syntax getDefaultDocumentSyntax() {
    // If the found value is an empty string then default to the configuration value in the main configuration
    // source.
    // TODO: In the future we would need the notion of initialized/not-initialized property values in the wiki.
    // When this is implemented modify the code below.
    String key = PREFIX + "defaultDocumentSyntax";
    String syntaxId = this.configuration.getProperty(key, String.class);
    if (StringUtils.isEmpty(syntaxId)) {
        syntaxId = this.xwikiPropertiesConfiguration.getProperty(key, Syntax.XWIKI_2_1.toIdString());
    }
    // Try to parse the syntax and if it fails defaults to the XWiki Syntax 2.1
    Syntax syntax;
    try {
        syntax = Syntax.valueOf(syntaxId);
    } catch (ParseException e) {
        this.logger.warn("Invalid default document Syntax [" + syntaxId + "], defaulting to [" + Syntax.XWIKI_2_1.toIdString() + "] instead", e);
        syntax = Syntax.XWIKI_2_1;
    }
    return syntax;
}
Also used : ParseException(org.xwiki.rendering.parser.ParseException) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 33 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class XWikiDocument method getDefaultDocumentSyntax.

/**
 * If there's no parser available for the specified syntax default to the XWiki 2.1 syntax.
 */
private Syntax getDefaultDocumentSyntax() {
    Syntax syntax = Utils.getComponent(CoreConfiguration.class).getDefaultDocumentSyntax();
    if (syntax == null || (!Utils.getComponentManager().hasComponent(Parser.class, syntax.toIdString()) && !Syntax.XWIKI_2_1.equals(syntax))) {
        LOGGER.warn("Failed to find parser for the default syntax [{}]. Defaulting to xwiki/2.1 syntax.", syntax);
        syntax = Syntax.XWIKI_2_1;
    }
    return syntax;
}
Also used : CoreConfiguration(com.xpn.xwiki.CoreConfiguration) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 34 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class XWiki method createUser.

/**
 * Create a new user.
 *
 * @param userName the name of the user (without the space)
 * @param map extra datas to add to user profile object
 * @param userRights the right of the user on his own profile page
 * @param context see {@link XWikiContext}
 * @return
 *         <ul>
 *         <li>1: ok</li>
 *         <li>-3: user already exists</li>
 *         </ul>
 * @throws XWikiException failed to create the new user
 */
public int createUser(String userName, Map<String, ?> map, String userRights, XWikiContext context) throws XWikiException {
    BaseClass userClass = getUserClass(context);
    String content = "";
    Syntax syntax = getDefaultDocumentSyntaxInternal();
    return createUser(userName, map, new EntityReference(userClass.getDocumentReference().getName(), EntityType.DOCUMENT), content, syntax, userRights, context);
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) EntityReference(org.xwiki.model.reference.EntityReference) RegexEntityReference(org.xwiki.model.reference.RegexEntityReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 35 with Syntax

use of org.xwiki.rendering.syntax.Syntax in project xwiki-platform by xwiki.

the class XWiki method createUser.

public int createUser(boolean withValidation, String userRights, XWikiContext context) throws XWikiException {
    try {
        XWikiRequest request = context.getRequest();
        Map<String, String[]> map = Util.getObject(request, "register");
        String content = "";
        Syntax syntax = getDefaultDocumentSyntaxInternal();
        // Read the values from the request.
        String xwikiname = request.getParameter("xwikiname");
        String password2 = request.getParameter("register2_password");
        String password = (map.get("password"))[0];
        String email = (map.get("email"))[0];
        String template = request.getParameter("template");
        String parent = request.getParameter("parent");
        String validkey = null;
        // Validate the values.
        if (XWikiRightService.SUPERADMIN_USER.equalsIgnoreCase(xwikiname)) {
            return -8;
        }
        try {
            if (!context.getUtil().match(getConfiguration().getProperty("xwiki.validusername", "/^[a-zA-Z0-9_]+$/"), xwikiname)) {
                return -4;
            }
        } catch (RuntimeException ex) {
            LOGGER.warn("Invalid regular expression for xwiki.validusername", ex);
            if (!context.getUtil().match("/^[a-zA-Z0-9_]+$/", xwikiname)) {
                return -4;
            }
        }
        if ((!password.equals(password2)) || (password.trim().equals(""))) {
            // TODO: throw wrong password exception
            return -2;
        }
        if ((template != null) && (!template.equals(""))) {
            XWikiDocument tdoc = getDocument(template, context);
            if ((!tdoc.isNew())) {
                // FIXME: This ignores template objects, attachments, etc.
                content = tdoc.getContent();
                syntax = tdoc.getSyntax();
            }
        }
        if ((parent == null) || (parent.equals(""))) {
            parent = "XWiki.XWikiUsers";
        }
        // Mark the user as active or waiting email validation.
        if (withValidation) {
            map.put("active", new String[] { "0" });
            validkey = generateValidationKey(16);
            map.put("validkey", new String[] { validkey });
        } else {
            // Mark user active
            map.put("active", new String[] { "1" });
        }
        // Create the user.
        int result = createUser(xwikiname, map, getRelativeEntityReferenceResolver().resolve(parent, EntityType.DOCUMENT), content, syntax, userRights, context);
        // Send validation mail, if needed.
        if ((result > 0) && (withValidation)) {
            // Send the validation email
            try {
                sendValidationEmail(xwikiname, password, email, validkey, "validation_email_content", context);
            } catch (XWikiException e) {
                LOGGER.warn("User created. Failed to send the mail to the created user.", e);
                return -11;
            }
        }
        return result;
    } catch (XWikiException e) {
        LOGGER.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_CREATE_USER, "Exception while creating user", e, null);
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) Syntax(org.xwiki.rendering.syntax.Syntax) 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)

Aggregations

Syntax (org.xwiki.rendering.syntax.Syntax)35 Test (org.junit.Test)16 DocumentReference (org.xwiki.model.reference.DocumentReference)15 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)11 Expectations (org.jmock.Expectations)10 SyntaxType (org.xwiki.rendering.syntax.SyntaxType)7 ParseException (org.xwiki.rendering.parser.ParseException)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 IOException (java.io.IOException)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 Parser (org.xwiki.rendering.parser.Parser)4 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 HashMap (java.util.HashMap)3 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)3 XDOM (org.xwiki.rendering.block.XDOM)3 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 AbstractInstanceFilterStreamTest (com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)2 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2