Search in sources :

Example 6 with IconException

use of org.xwiki.icon.IconException in project xwiki-platform by xwiki.

the class DefaultIconSetLoader method loadIconSet.

@Override
public IconSet loadIconSet(Reader input, String name) throws IconException {
    try {
        IconSet iconSet = new IconSet(name);
        Properties properties = new Properties();
        properties.load(input);
        // Load all the keys
        for (String key : properties.stringPropertyNames()) {
            String value = properties.getProperty(key);
            if (key.equals(CSS_PROPERTY_NAME)) {
                iconSet.setCss(value);
            } else if (key.equals(SSX_PROPERTY_NAME)) {
                iconSet.setSsx(value);
            } else if (key.equals(JSX_PROPERTY_NAME)) {
                iconSet.setJsx(value);
            } else if (key.equals(RENDER_WIKI_PROPERTY_NAME)) {
                iconSet.setRenderWiki(value);
            } else if (key.equals(RENDER_HTML_PROPERTY_NAME)) {
                iconSet.setRenderHTML(value);
            } else if (key.equals(ICON_TYPE_PROPERTY_NAME)) {
                iconSet.setType(IconType.valueOf(value.toUpperCase()));
            } else {
                Icon icon = new Icon();
                icon.setValue(properties.getProperty(key));
                iconSet.addIcon(key, icon);
            }
        }
        // return
        return iconSet;
    } catch (IOException e) {
        throw new IconException(String.format(ERROR_MSG, name), e);
    }
}
Also used : Icon(org.xwiki.icon.Icon) IOException(java.io.IOException) Properties(java.util.Properties) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException)

Example 7 with IconException

use of org.xwiki.icon.IconException in project xwiki-platform by xwiki.

the class DefaultIconSetLoader method loadIconSet.

@Override
public IconSet loadIconSet(DocumentReference iconSetReference) throws IconException {
    try {
        // Get the document
        DocumentModelBridge doc = documentAccessBridge.getDocumentInstance(iconSetReference);
        String content = doc.getContent();
        // The name of the icon set is stored in the IconThemesCode.IconThemeClass XObject of the document
        DocumentReference iconClassRef = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), "IconThemesCode", "IconThemeClass");
        String name = (String) documentAccessBridge.getProperty(iconSetReference, iconClassRef, "name");
        // Load the icon set
        return loadIconSet(new StringReader(content), name);
    } catch (Exception e) {
        throw new IconException(String.format(ERROR_MSG, iconSetReference), e);
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) StringReader(java.io.StringReader) DocumentReference(org.xwiki.model.reference.DocumentReference) IOException(java.io.IOException) IconException(org.xwiki.icon.IconException) IconException(org.xwiki.icon.IconException)

Example 8 with IconException

use of org.xwiki.icon.IconException in project xwiki-platform by xwiki.

the class DefaultIconSetManager method getIconSet.

@Override
public IconSet getIconSet(String name) throws IconException {
    // Special case: the default icon theme
    if (DEFAULT_ICONSET_NAME.equals(name)) {
        return getDefaultIconSet();
    }
    // Get the icon set from the cache
    IconSet iconSet = iconSetCache.get(name, wikiDescriptorManager.getCurrentWikiId());
    // Load it if it is not loaded yet
    if (iconSet == null) {
        try {
            // Search by name
            String xwql = "FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name";
            Query query = queryManager.createQuery(xwql, Query.XWQL);
            query.bindValue("name", name);
            List<String> results = query.execute();
            if (results.isEmpty()) {
                return null;
            }
            // Get the first result
            String docName = results.get(0);
            DocumentReference docRef = documentReferenceResolver.resolve(docName);
            // Load the icon theme
            iconSet = iconSetLoader.loadIconSet(docRef);
            // Put it in the cache
            iconSetCache.put(docRef, iconSet);
            iconSetCache.put(name, wikiDescriptorManager.getCurrentWikiId(), iconSet);
        } catch (QueryException e) {
            throw new IconException(String.format("Failed to load the icon set [%s].", name), e);
        }
    }
    // Return the icon set
    return iconSet;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException)

Example 9 with IconException

use of org.xwiki.icon.IconException in project xwiki-platform by xwiki.

the class VelocityRenderer method render.

/**
 * Render a velocity code without messing with the document context and namespace.
 * @param code code to render
 * @return the rendered code
 * @throws IconException if problem occurs
 */
public String render(String code) throws IconException {
    // The macro namespace to use by the velocity engine, see afterwards.
    String namespace = "IconVelocityRenderer_" + Thread.currentThread().getId();
    // Create the output writer
    StringWriter output = new StringWriter();
    VelocityEngine engine = null;
    try {
        // Get the velocity engine
        engine = velocityManager.getVelocityEngine();
        // Use a new macro namespace to prevent the code redefining existing macro.
        // We use the thread name to have a unique id.
        engine.startedUsingMacroNamespace(namespace);
        // Create a new VelocityContext to prevent the code creating variables in the current context.
        // See https://jira.xwiki.org/browse/XWIKI-11400.
        // We set the current context as inner context of the new one to be able to read existing variables.
        // See https://jira.xwiki.org/browse/XWIKI-11426.
        VelocityContext context = new VelocityContext(velocityManager.getVelocityContext());
        // Render the code
        if (engine.evaluate(context, output, "DefaultIconRenderer", code)) {
            return output.toString();
        } else {
            // I don't know how to check the velocity runtime log
            throw new IconException("Failed to render the icon. See the Velocity runtime log.", null);
        }
    } catch (XWikiVelocityException e) {
        throw new IconException("Failed to render the icon.", e);
    } finally {
        // Do not forget to close the macro namespace we have created previously
        if (engine != null) {
            engine.stoppedUsingMacroNamespace(namespace);
        }
    }
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) IconException(org.xwiki.icon.IconException)

Example 10 with IconException

use of org.xwiki.icon.IconException in project xwiki-platform by xwiki.

the class DefaultIconRendererTest method renderWithException.

@Test
public void renderWithException() throws Exception {
    IconSet iconSet = new IconSet("default");
    iconSet.setRenderWiki("image:$icon.png");
    iconSet.addIcon("test", new Icon("blabla"));
    IconException exception = new IconException("exception", null);
    when(velocityRenderer.render(any())).thenThrow(exception);
    // Test
    IconException caughtException = null;
    try {
        mocker.getComponentUnderTest().render("test", iconSet);
    } catch (IconException e) {
        caughtException = e;
    }
    // Verify
    assertNotNull(caughtException);
    assertEquals(exception, caughtException);
}
Also used : Icon(org.xwiki.icon.Icon) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException) Test(org.junit.Test)

Aggregations

IconException (org.xwiki.icon.IconException)14 Test (org.junit.Test)8 QueryException (org.xwiki.query.QueryException)5 IOException (java.io.IOException)4 IconSet (org.xwiki.icon.IconSet)4 MalformedURLException (java.net.MalformedURLException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 InputStreamReader (java.io.InputStreamReader)2 VelocityContext (org.apache.velocity.VelocityContext)2 Icon (org.xwiki.icon.Icon)2 Query (org.xwiki.query.Query)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Properties (java.util.Properties)1