1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.NodeData; 20 import info.magnolia.cms.gui.misc.FileProperties; 21 import info.magnolia.cms.util.Resource; 22 import info.magnolia.context.MgnlContext; 23 24 import java.awt.Color ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.text.CharacterIterator ; 31 import java.text.StringCharacterIterator ; 32 import java.util.Calendar ; 33 import java.util.GregorianCalendar ; 34 import java.util.TimeZone ; 35 36 import javax.jcr.AccessDeniedException; 37 import javax.jcr.PathNotFoundException; 38 import javax.jcr.PropertyType; 39 import javax.jcr.RepositoryException; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.jsp.PageContext ; 42 import javax.servlet.jsp.tagext.SimpleTagSupport ; 43 44 import org.apache.commons.io.IOUtils; 45 import org.apache.commons.lang.StringUtils; 46 import org.devlib.schmidt.imageinfo.ImageInfo; 47 import org.slf4j.Logger; 48 import org.slf4j.LoggerFactory; 49 50 51 54 public abstract class BaseImageTag extends SimpleTagSupport { 55 56 59 protected static final String PROPERTIES_EXTENSION_VALUE = "PNG"; 60 61 64 protected static final String PROPERTIES_CONTENTTYPE_VALUE = "image/png"; 65 66 69 private static Logger log = LoggerFactory.getLogger(BaseImageTag.class); 70 71 74 protected String parentContentNodeName; 75 76 81 protected String imageContentNodeName; 82 83 87 public void setImageContentNodeName(String imageContentNodeName) { 88 this.imageContentNodeName = imageContentNodeName; 89 } 90 91 95 public void setParentContentNodeName(String parentContentNodeName) { 96 this.parentContentNodeName = parentContentNodeName; 97 } 98 99 protected abstract String getFilename(); 100 101 protected HttpServletRequest getRequest() { 102 return (HttpServletRequest ) ((PageContext ) this.getJspContext()).getRequest(); 103 } 104 105 110 protected Content getImageContentNode() throws PathNotFoundException, RepositoryException, 111 info.magnolia.cms.security.AccessDeniedException { 112 113 Content imageContentNode; 114 Content currentActivePage = Resource.getCurrentActivePage(getRequest()); 115 Content paragraph = Resource.getLocalContentNode(getRequest()); 116 Content parentContentNode = null; 117 if (StringUtils.isEmpty(this.parentContentNodeName)) { 119 parentContentNode = paragraph != null ? paragraph : currentActivePage; 120 } 121 else { 122 123 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE); 124 if (this.parentContentNodeName.startsWith("/")) { 127 parentContentNode = hm.getContent(this.parentContentNodeName); 128 } 129 else { 130 parentContentNode = hm.getContent(currentActivePage.getHandle() + "/" + this.parentContentNodeName); 131 } 132 } 133 imageContentNode = null; 135 if (StringUtils.isEmpty(this.imageContentNodeName)) { 136 imageContentNode = parentContentNode; 137 } 138 else if (parentContentNode.hasContent(this.imageContentNodeName)) { 139 imageContentNode = parentContentNode.getContent(this.imageContentNodeName); 140 } 141 else { 142 imageContentNode = parentContentNode.createContent(this.imageContentNodeName, ItemType.CONTENTNODE); 143 } 144 return imageContentNode; 145 } 146 147 151 public String convertToSimpleString(String string) { 152 153 final StringBuffer result = new StringBuffer (); 154 155 final StringCharacterIterator iterator = new StringCharacterIterator (string); 156 char character = iterator.current(); 157 while (character != CharacterIterator.DONE) { 158 int charType = Character.getType(character); 159 if (charType == Character.SPACE_SEPARATOR) { 160 result.append("-"); 161 } 162 else if ((charType != Character.UPPERCASE_LETTER) 163 && (charType != Character.LOWERCASE_LETTER) 164 && (charType != Character.DECIMAL_DIGIT_NUMBER) 165 && (charType != Character.CONNECTOR_PUNCTUATION) 166 && (charType != Character.DASH_PUNCTUATION)) { 167 result.append("u" + (int) character); 168 169 } 170 else { 171 result.append(character); 174 } 175 character = iterator.next(); 176 } 177 return result.toString(); 178 } 179 180 184 public int[] convertHexToRGB(String hex) { 185 hex.trim(); 186 if (hex.startsWith("#")) { 187 hex = hex.substring(1); 188 } 189 if (hex.length() == 3) { 190 hex = String.valueOf(hex.charAt(0)) 192 + String.valueOf(hex.charAt(0)) 193 + String.valueOf(hex.charAt(1)) 194 + String.valueOf(hex.charAt(1)) 195 + String.valueOf(hex.charAt(2)) 196 + String.valueOf(hex.charAt(2)); 197 } 198 199 int[] rgb = new int[3]; 200 try { 201 rgb[0] = Integer.parseInt(hex.substring(0, 2), 16); 203 rgb[1] = Integer.parseInt(hex.substring(2, 4), 16); 204 rgb[2] = Integer.parseInt(hex.substring(4), 16); 205 } 206 catch (NumberFormatException e) { 207 log.error("NumberFormatException occured during text-to-image conversion: " 208 + "Attempting to convert Hex [" 209 + hex 210 + "] color to RGB color: " 211 + e.getMessage(), e); 212 rgb = new int[]{255, 0, 0}; } 214 return rgb; 215 } 216 217 public Color convertHexToColor(String hex) { 218 int[] rgb = convertHexToRGB(hex); 219 Color color = new Color (rgb[0], rgb[1], rgb[2]); 220 return color; 221 } 222 223 231 protected void createImageNode(File imageFile, Content imageNode) throws PathNotFoundException, 232 AccessDeniedException, RepositoryException, FileNotFoundException , IOException { 233 234 NodeData data; 236 data = imageNode.getNodeData(getFilename()); 237 238 if (!data.isExist()) { 239 data = imageNode.createNodeData(getFilename(), PropertyType.BINARY); 240 } 241 242 InputStream iis = new FileInputStream (imageFile); 243 data.setValue(iis); 244 IOUtils.closeQuietly(iis); 245 246 data.setAttribute(FileProperties.PROPERTY_FILENAME, getFilename()); 247 248 data.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, PROPERTIES_CONTENTTYPE_VALUE); 249 250 Calendar value = new GregorianCalendar (TimeZone.getDefault()); 251 data.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, value); 252 253 data.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(imageFile.length())); 254 255 data.setAttribute(FileProperties.PROPERTY_EXTENSION, PROPERTIES_EXTENSION_VALUE); 256 257 InputStream raf = null; 258 try { 259 ImageInfo ii = new ImageInfo(); 260 raf = new FileInputStream (imageFile); 261 ii.setInput(raf); 262 if (ii.check()) { 263 data.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth())); 264 data.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight())); 265 266 } 267 } 268 catch (FileNotFoundException e) { 269 log.error("FileNotFoundException caught when parsing {}, image data will not be available", imageFile 270 .getAbsolutePath()); 271 } 272 finally { 273 IOUtils.closeQuietly(raf); 274 } 275 276 imageFile.delete(); 278 279 imageNode.save(); 281 } 282 } 283 | Popular Tags |