List of usage examples for org.apache.wicket.request.resource PackageResource getResourceStream
@Override
public IResourceStream getResourceStream()
From source file:org.hippoecm.frontend.skin.IconUtil.java
License:Apache License
public static String svgAsString(PackageResourceReference reference, String... cssClasses) throws ResourceStreamNotFoundException, IOException { final PackageResource resource = reference.getResource(); final IResourceStream resourceStream = resource.getResourceStream(); if (resourceStream == null) { throw new ResourceStreamNotFoundException("Cannot find SVG icon " + resource); }/*from w ww. ja v a 2 s . c o m*/ String svgAsString = IOUtils.toString(resourceStream.getInputStream()); int rootIndex = svgAsString.indexOf("<svg"); if (rootIndex == -1) { throw new IllegalArgumentException("Cannot find SVG root element in " + resource); } //skip everything (comments, xml declaration and dtd definition) before <svg element svgAsString = svgAsString.substring(rootIndex); //append css classes if present final String cssClassesAsString = cssClassesAsString(cssClasses); if (StringUtils.isNotEmpty(cssClassesAsString)) { //check if class attribute is present and part of <svg element final int classAttributeIndex = svgAsString.indexOf("class=\""); if (classAttributeIndex > -1 && classAttributeIndex < svgAsString.indexOf(">")) { int insertCssClassesAt = classAttributeIndex + 7; svgAsString = svgAsString.substring(0, insertCssClassesAt) + cssClassesAsString + " " + svgAsString.substring(insertCssClassesAt); } else { svgAsString = "<svg class=\"" + cssClassesAsString + "\"" + svgAsString.substring(4); } } return svgAsString; }