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);
}
}
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);
}
}
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;
}
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);
}
}
}
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);
}
Aggregations