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 org.apache.log4j.Logger;
023:
024: import de.schlund.pfixxml.resources.FileResource;
025: import de.schlund.pfixxml.resources.ResourceUtil;
026: import de.schlund.pfixxml.targets.TargetGenerator;
027: import de.schlund.pfixxml.targets.TargetGeneratorFactory;
028: import de.schlund.pfixxml.targets.VirtualTarget;
029: import de.schlund.pfixxml.util.XsltContext;
030:
031: /**
032: * Describe class ImageThemedSrc here.
033: *
034: *
035: * Created: Wed Mar 23 17:15:43 2005
036: *
037: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
038: * @version 1.0
039: */
040: public class ImageThemedSrc {
041: private final static Logger LOG = Logger
042: .getLogger(ImageThemedSrc.class);
043:
044: /** xslt extension */
045: public static String getSrc(XsltContext context, String src,
046: String themed_path, String themed_img,
047: String parent_part_in, String parent_product_in,
048: String targetGen, String targetKey) throws Exception {
049:
050: String[] themes = null;
051: FileResource tgen_path = ResourceUtil
052: .getFileResource(targetGen);
053: TargetGenerator gen = TargetGeneratorFactory.getInstance()
054: .createGenerator(tgen_path);
055:
056: if (!targetKey.equals("__NONE__")) {
057: VirtualTarget target = (VirtualTarget) gen
058: .getTarget(targetKey);
059: themes = target.getThemes().getThemesArr();
060: }
061: if (themes == null) {
062: themes = gen.getGlobalThemes().getThemesArr();
063: }
064:
065: if (isSimpleSrc(src, themed_path, themed_img)) {
066: if (src.startsWith("/")) {
067: src = src.substring(1);
068: }
069: LOG.debug(" -> Register image src '" + src + "'");
070: DependencyTracker.logImage(context, src, parent_part_in,
071: parent_product_in, targetGen, targetKey, "image");
072: return src;
073: } else if (isThemedSrc(src, themed_path, themed_img)) {
074: if (themed_path.startsWith("/")) {
075: themed_path = themed_path.substring(1);
076: }
077:
078: String testsrc = null;
079: for (int i = 0; i < themes.length; i++) {
080: String currtheme = themes[i];
081: testsrc = themed_path + "/" + currtheme + "/"
082: + themed_img;
083: LOG.info(" -> Trying to find image src '" + testsrc
084: + "'");
085: if (existsImage(testsrc)) {
086: LOG.info(" -> Found src '" + testsrc + "'");
087: DependencyTracker.logImage(context, testsrc,
088: parent_part_in, parent_product_in,
089: targetGen, targetKey, "image");
090: return testsrc;
091: }
092: if (i < (themes.length - 1)) {
093: // FIXME: the next commented line should be used sometime so we can discriminate between
094: // "real" missing and "missing, but we found a better version" -- but make sure editor copes with it.
095: //DependencyTracker.logImage(context, testsrc, parent_part_in, parent_product_in, targetGen, targetKey, "shadow");
096: DependencyTracker.logImage(context, testsrc,
097: parent_part_in, parent_product_in,
098: targetGen, targetKey, "image");
099: LOG.info(" -> Image src '" + testsrc
100: + "' not found, trying next theme");
101: } else {
102: DependencyTracker.logImage(context, testsrc,
103: parent_part_in, parent_product_in,
104: targetGen, targetKey, "image");
105: LOG.warn(" -> No themed image found!");
106: }
107: }
108: return testsrc;
109: } else {
110: throw new XMLException(
111: "Need to have one of 'src' XOR both 'themed-path' and 'themed-img' given!");
112: }
113: }
114:
115: private static boolean isSimpleSrc(String src, String path,
116: String img) {
117: return (src != null && !src.equals("")
118: && (path == null || path.equals("")) && (img == null || img
119: .equals("")));
120: }
121:
122: private static boolean isThemedSrc(String src, String path,
123: String img) {
124: return ((src == null || src.equals("")) && path != null
125: && !path.equals("") && img != null && !img.equals(""));
126: }
127:
128: private static boolean existsImage(String path) {
129: FileResource img = ResourceUtil
130: .getFileResourceFromDocroot(path);
131: return (img.exists() && img.canRead() && img.isFile());
132: }
133:
134: }
|