001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war;
034:
035: import com.flexive.shared.FxSharedUtils;
036: import com.flexive.shared.FxLanguage;
037: import com.flexive.shared.EJBLookup;
038: import com.flexive.shared.exceptions.FxApplicationException;
039: import com.flexive.shared.content.FxPK;
040:
041: import java.awt.*;
042: import java.util.regex.Pattern;
043:
044: import org.apache.commons.lang.StringUtils;
045:
046: /**
047: * Configuration helper that parses a URI for thumbnail configuration parameters
048: *
049: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: */
051: public class FxThumbnailURIConfigurator {
052:
053: private final static Pattern pPK = Pattern
054: .compile("^pk\\d+(\\.(\\d+|MAX|LIVE))?");
055: private final static Pattern pXPath = Pattern.compile("^xp.+");
056: private final static Pattern pSize = Pattern.compile("s[0123]");
057: private final static Pattern pLang = Pattern
058: .compile("^lang[a-zA-Z]{2}");
059: private final static Pattern pLangFallback = Pattern
060: .compile("^lfb[01]");
061:
062: private String URI;
063: private FxPK pk = null;
064: private String xp = null;
065: private String lang = null;
066: private Boolean langFallback = null;
067: private String err500 = null;
068: private String err404 = null;
069: private String err403 = null;
070: private int size = 0;
071: private int scaleWidth = -1, scaleHeight = -1;
072: private byte rot = 0;
073: private Boolean flipH = null, flipV = null;
074: private Rectangle crop = null;
075: private String filename = null; //filename is last parameter (optional) if it contains a dot and does not match another parameter
076:
077: public FxThumbnailURIConfigurator(String URI) {
078: this .URI = URI;
079: parse();
080: }
081:
082: /**
083: * Valid options are:
084: * <ul>
085: * <li><code>pk{n.m}</code> - id and version of the content, if no version is given the live version is used</li>
086: * <li><code>xp{path}</code> - URL encoded XPath of the property containing the image (optional, else default will be used)</li>
087: * <li><code>lang{lang}</code> - 2-digit ISO language code
088: * <li><code>lfb{0,1}</code> - language fallback: 0=generate error if language not found, 1=fall back to default language
089: * <li><code>e{3 digit error number}u{error url}</code> - URL encoded error url to redirect for http errors specified in {error number}, if no number is given then the url is a fallback for unclassified errors
090: * <li><code>s{0,1,2,3}</code> - use a predefined image/thumbnail size, 0=default</li>
091: * <li><code>w{n}</code> - scale to width</li>
092: * <li><code>h{n}</code> - scale to height</li>
093: * <li><code>rot{90,180,270}</code> - rotate 90, 180 or 270 degrees (rotate is always executed before flip operations)</li>
094: * <li><code>flip{h,v}</code> - flip horizontal or vertical (rotate is always executed before flip operations)</li>
095: * <li><code>cropx{x}y{y}w{w}h{h}</code> - crop a box from image defined by x,y,w(idth),h(eight), scaling applies to cropped image!</li>
096: * </ul>
097: */
098: private void parse() {
099: String[] elements = URI.split("\\/");
100: for (String element : elements) {
101: if (pk == null && pPK.matcher(element).matches()) {
102: pk = FxPK.fromString(element.substring(2));
103: }
104: if (xp == null && pXPath.matcher(element).matches()) {
105: xp = FxSharedUtils.decodeXPath(element.substring(2));
106: }
107: if (size == 0 && pSize.matcher(element).matches())
108: size = Integer.parseInt(element.substring(1));
109: if (lang == null && pLang.matcher(element).matches())
110: lang = element.substring(4);
111: if (langFallback == null
112: && pLangFallback.matcher(element).matches())
113: langFallback = "1".equals(element.substring(3));
114: }
115: if (xp == null)
116: xp = "/";
117: }
118:
119: public boolean hasPK() {
120: return pk != null;
121: }
122:
123: public String getXPath() {
124: return xp;
125: }
126:
127: public FxPK getPK() {
128: return pk;
129: }
130:
131: public int getSize() {
132: return size;
133: }
134:
135: public String getLanguageIso() {
136: return lang;
137: }
138:
139: public FxLanguage getLanguage() throws FxApplicationException {
140: return StringUtils.isNotBlank(lang) ? EJBLookup
141: .getLanguageEngine().load(lang) : null;
142: }
143: }
|