001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml;
021:
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.StringTokenizer;
026:
027: import org.apache.log4j.Logger;
028:
029: import de.schlund.pfixxml.resources.FileResource;
030: import de.schlund.pfixxml.resources.ResourceUtil;
031:
032: /**
033: * ImageGeometry.java
034: *
035: *
036: * Created: Tue Apr 16 23:43:02 2002
037: *
038: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
039: *
040: *
041: */
042:
043: public class ImageGeometry {
044: private static Map<String, ImageGeometryData> imageinfo = new HashMap<String, ImageGeometryData>();
045: private final static Logger LOG = Logger
046: .getLogger(ImageGeometry.class);
047:
048: public static int getHeight(String path) {
049: ImageGeometryData data = getImageGeometryData(path);
050: if (data == null) {
051: return -1;
052: } else {
053: return data.getHeight();
054: }
055: }
056:
057: public static int getWidth(String path) {
058: ImageGeometryData data = getImageGeometryData(path);
059: if (data == null) {
060: return -1;
061: } else {
062: return data.getWidth();
063: }
064: }
065:
066: public static String getType(String path) {
067: ImageGeometryData data = getImageGeometryData(path);
068: if (data == null) {
069: return null;
070: } else {
071: return data.getType();
072: }
073: }
074:
075: public static String getStyleStringForImage(String path,
076: String userStyle, String userWidth, String userHeight) {
077: ImageGeometryData data = getImageGeometryData(path);
078: int targetWidth = -1;
079: int targetHeight = -1;
080: String targetWidthUnit = "px";
081: String targetHeightUnit = "px";
082:
083: if (userWidth != null && userWidth.length() > 0) {
084: userWidth = userWidth.trim();
085: if (userWidth.endsWith("%")) {
086: targetWidthUnit = "%";
087: userWidth = userWidth.substring(0,
088: userWidth.length() - 1);
089: }
090: try {
091: targetWidth = Integer.parseInt(userWidth);
092: } catch (NumberFormatException e) {
093: LOG
094: .error("*** Image "
095: + path
096: + " supplied invalid data for width parameter: "
097: + userWidth);
098: targetWidth = -1;
099: }
100: } else {
101: if (data != null)
102: targetWidth = data.getWidth();
103: }
104: if (userHeight != null && userHeight.length() > 0) {
105: userHeight = userHeight.trim();
106: if (userHeight.endsWith("%")) {
107: targetHeightUnit = "%";
108: userHeight = userHeight.substring(0, userHeight
109: .length() - 1);
110: }
111: try {
112: targetHeight = Integer.parseInt(userHeight);
113: } catch (NumberFormatException e) {
114: LOG
115: .error("*** Image "
116: + path
117: + " supplied invalid data for height parameter: "
118: + userHeight);
119: targetHeight = -1;
120: }
121: } else {
122: if (data != null)
123: targetHeight = data.getHeight();
124: }
125:
126: boolean haveWidth = false, haveHeight = false;
127:
128: if (userStyle == null) {
129: userStyle = "";
130: }
131: StringBuffer genStyle = new StringBuffer(userStyle.trim());
132:
133: StringTokenizer st = new StringTokenizer(userStyle, ";");
134: while (st.hasMoreTokens()) {
135: String token = st.nextToken();
136: String propName = token.substring(0, token.indexOf(':'));
137: propName = propName.trim().toLowerCase();
138: if (propName.equals("width")) {
139: haveWidth = true;
140: } else if (propName.equals("height")) {
141: haveHeight = true;
142: }
143: }
144:
145: if (!haveWidth && targetWidth != -1) {
146: if (genStyle.length() > 0
147: && genStyle.charAt(genStyle.length() - 1) != ';') {
148: genStyle.append(';');
149: }
150: genStyle.append("width:");
151: genStyle.append(targetWidth);
152: genStyle.append(targetWidthUnit + ";");
153: }
154:
155: if (!haveHeight && targetHeight != -1) {
156: if (genStyle.length() > 0
157: && genStyle.charAt(genStyle.length() - 1) != ';') {
158: genStyle.append(';');
159: }
160: genStyle.append("height:");
161: genStyle.append(targetHeight);
162: genStyle.append(targetHeightUnit + ";");
163: }
164:
165: return genStyle.toString();
166: }
167:
168: private static ImageGeometryData getImageGeometryData(String path) {
169: synchronized (imageinfo) {
170: FileResource img = ResourceUtil
171: .getFileResourceFromDocroot(path);
172: if (img.exists() && img.canRead() && img.isFile()) {
173: long mtime = img.lastModified();
174: ImageGeometryData tmp = imageinfo.get(path);
175: if (tmp == null || mtime > tmp.lastModified()) {
176: // LOG.debug("Cache miss or outdated for: " + path);
177: try {
178: tmp = new ImageGeometryData(img);
179: } catch (IOException e) {
180: LOG.error("*** Couldn't get geometry for "
181: + path, e);
182: return null;
183: }
184: if (!tmp.isOK()) {
185: LOG
186: .error("*** Image data wasn't recognized for "
187: + path);
188: return null;
189: }
190: imageinfo.put(path, tmp);
191: } else {
192: // CAT.debug("Cache hit and uptodate for: " + path);
193: }
194: return tmp;
195: }
196: return null;
197: }
198: }
199:
200: }// ImageGeometry
|