private Object executeLike(PageContext pc, SQL sql, Query qr, ZExpression expression, int row) throws PageException { return Caster.toBoolean( like( sql, Caster.toString(executeExp(pc, sql, qr, expression.getOperand(0), row)), Caster.toString(executeExp(pc, sql, qr, expression.getOperand(1), row)))); }
/** * execute a plus operation * * @param sql * @param qr QueryResult to execute on it * @param expression * @param row row of resultset to execute * @return result * @throws PageException */ private Object executePlus(PageContext pc, SQL sql, Query qr, ZExpression expression, int row) throws PageException { Object left = executeExp(pc, sql, qr, expression.getOperand(0), row); Object right = executeExp(pc, sql, qr, expression.getOperand(1), row); try { return new Double(Caster.toDoubleValue(left) + Caster.toDoubleValue(right)); } catch (PageException e) { return Caster.toString(left) + Caster.toString(right); } }
private void doActionSetInfo() throws PageException, IOException, DocumentException { required("pdf", "setInfo", "info", info); required("pdf", "getInfo", "source", source); PDFDocument doc = toPDFDocument(source, password, null); PdfReader pr = doc.getPdfReader(); OutputStream os = null; try { if (destination == null) { if (doc.getResource() == null) throw new ApplicationException( "source is not based on a resource, destination file is required"); destination = doc.getResource(); } else if (destination.exists() && !overwrite) throw new ApplicationException("destination file [" + destination + "] already exists"); PdfStamper stamp = new PdfStamper(pr, os = destination.getOutputStream()); HashMap moreInfo = new HashMap(); Key[] keys = info.keys(); for (int i = 0; i < keys.length; i++) { moreInfo.put( StringUtil.ucFirst(keys[i].getLowerString()), Caster.toString(info.get(keys[i]))); } // author Object value = info.get("author", null); if (value != null) moreInfo.put("Author", Caster.toString(value)); // keywords value = info.get("keywords", null); if (value != null) moreInfo.put("Keywords", Caster.toString(value)); // title value = info.get("title", null); if (value != null) moreInfo.put("Title", Caster.toString(value)); // subject value = info.get("subject", null); if (value != null) moreInfo.put("Subject", Caster.toString(value)); // creator value = info.get("creator", null); if (value != null) moreInfo.put("Creator", Caster.toString(value)); // trapped value = info.get("Trapped", null); if (value != null) moreInfo.put("Trapped", Caster.toString(value)); // Created value = info.get("Created", null); if (value != null) moreInfo.put("Created", Caster.toString(value)); // Language value = info.get("Language", null); if (value != null) moreInfo.put("Language", Caster.toString(value)); stamp.setMoreInfo(moreInfo); stamp.close(); } finally { IOUtil.closeEL(os); pr.close(); } }
/** * cast a Object Array to a String Array * * @param array * @return String Array * @throws PageException */ public static String[] toStringArray(Array array) throws PageException { String[] arr = new String[array.size()]; for (int i = 0; i < arr.length; i++) { arr[i] = Caster.toString(array.get(i + 1, null)); } return arr; }
public static boolean call(PageContext pc, Object obj, boolean addNewLine, boolean doErrorStream) throws PageException { String string; if (Decision.isSimpleValue(obj)) string = Caster.toString(obj); else { try { string = Serialize.call(pc, obj); } catch (Throwable t) { string = obj.toString(); } } PrintStream stream = System.out; // string+=":"+Thread.currentThread().getId(); if (doErrorStream) stream = System.err; if (string != null) { if (StringUtil.indexOfIgnoreCase(string, "<print-stack-trace>") != -1) { String st = ExceptionUtil.getStacktrace(new Exception("Stack trace"), false); string = StringUtil.replace(string, "<print-stack-trace>", "\n" + st + "\n", true).trim(); } if (StringUtil.indexOfIgnoreCase(string, "<hash-code>") != -1) { String st = obj.hashCode() + ""; string = StringUtil.replace(string, "<hash-code>", st, true).trim(); } } if (addNewLine) stream.println(string); else stream.print(string); return true; }
public void checkAccess(String key, long timeNonce) throws PageException { if (previousNonces.containsKey(timeNonce)) { long now = System.currentTimeMillis(); long diff = timeNonce > now ? timeNonce - now : now - timeNonce; if (diff > 10) throw new ApplicationException("nonce was already used, same nonce can only be used once"); } long now = System.currentTimeMillis() + getTimeServerOffset(); if (timeNonce > (now + FIVE_SECONDS) || timeNonce < (now - FIVE_SECONDS)) throw new ApplicationException( "nonce is outdated (timserver offset:" + getTimeServerOffset() + ")"); previousNonces.put(timeNonce, ""); String[] keys = getAuthenticationKeys(); // check if one of the keys matching String hash; for (int i = 0; i < keys.length; i++) { try { hash = Hash.hash( keys[i], Caster.toString(timeNonce), Hash.ALGORITHM_SHA_256, Hash.ENCODING_HEX); if (hash.equals(key)) return; } catch (NoSuchAlgorithmException e) { throw Caster.toPageException(e); } } throw new ApplicationException("No access, no matching authentication key found"); }
/** * @param title * @param key * @param content * @param custom1 * @param custom2 * @param custom3 * @param custom4 * @return Document */ public static Document getDocument( String title, String key, String content, String urlpath, String custom1, String custom2, String custom3, String custom4) { // make a new, empty document Document doc = new Document(); doc.add(FieldUtil.UnIndexed("size", Caster.toString(content.length()))); doc.add(FieldUtil.Text("key", key)); FieldUtil.setMimeType(doc, "text/plain"); FieldUtil.setRaw(doc, content); FieldUtil.setContent(doc, content); FieldUtil.setSummary(doc, StringUtil.max(content, SUMMERY_SIZE), false); FieldUtil.setTitle(doc, title); FieldUtil.setURL(doc, urlpath); FieldUtil.setCustom(doc, custom1, 1); FieldUtil.setCustom(doc, custom2, 2); FieldUtil.setCustom(doc, custom3, 3); FieldUtil.setCustom(doc, custom4, 4); return doc; }
protected void _init(PageContext pc, Map<String, String> arguments) { this.pc = pc; // header HttpServletRequest req = pc.getHttpServletRequest(); header = new StringBuffer(); createHeader(header, "context-path", req.getContextPath()); createHeader(header, "remote-user", req.getRemoteUser()); createHeader(header, "remote-addr", req.getRemoteAddr()); createHeader(header, "remote-host", req.getRemoteHost()); createHeader( header, "script-name", StringUtil.emptyIfNull(req.getContextPath()) + StringUtil.emptyIfNull(req.getServletPath())); createHeader(header, "server-name", req.getServerName()); createHeader(header, "protocol", req.getProtocol()); createHeader(header, "server-port", Caster.toString(req.getServerPort())); createHeader( header, "path-info", StringUtil.replace( StringUtil.emptyIfNull(req.getRequestURI()), StringUtil.emptyIfNull(req.getServletPath()), "", true)); // createHeader(header,"path-translated",pc.getBasePageSource().getDisplayPath()); createHeader(header, "query-string", req.getQueryString()); createHeader(header, "unit", unitShortToString(unit)); createHeader(header, "min-time-nano", min + ""); content = new StringBuffer(); // directory String strDirectory = arguments.get("directory"); if (dir == null) { if (StringUtil.isEmpty(strDirectory)) { dir = getTemp(pc); } else { try { dir = ResourceUtil.toResourceNotExisting(pc, strDirectory, false); if (!dir.exists()) { dir.createDirectory(true); } else if (dir.isFile()) { err( pc, "can not create directory [" + dir + "], there is already a file with same name."); } } catch (Throwable t) { err(pc, t); dir = getTemp(pc); } } } file = dir.getRealResource((pc.getId()) + "-" + CreateUUID.call(pc) + ".exl"); file.createNewFile(); start = System.currentTimeMillis(); }
/** * @param opacity the opacity to set * @throws ApplicationException */ public void setOpacity(double opacity) throws ApplicationException { if (opacity < 0 || opacity > 10) throw new ApplicationException( "invalid opacity definition [" + Caster.toString(opacity) + "], value should be in range from 0 to 10"); this.opacity = (float) (opacity / 10); }
/** * cast a Object Array to a String Array * * @param array * @return String Array */ public static String[] toStringArrayEL(Array array) { String[] arr = new String[array.size()]; for (int i = 0; i < arr.length; i++) { arr[i] = Caster.toString(array.get(i + 1, null), null); } return arr; }
public static Array call(PageContext pc, QueryColumn column) throws PageException { Array arr = new ArrayImpl(); int size = column.size(); for (int i = 1; i <= size; i++) { arr.append(Caster.toString(column.get(i, null))); } return arr; }
public static String call(PageContext pc, Object name, Object image, double x, double y) throws PageException { if (name instanceof String) name = pc.getVariable(Caster.toString(name)); Image img = Image.toImage(name); img.drawImage(Image.createImage(pc, image, true, false, true), (int) x, (int) y); return null; }
/** * trim every single item of the array * * @param arr * @return * @throws PageException */ public static Array trimItems(Array arr) throws PageException { Key[] keys = arr.keys(); for (int i = 0; i < keys.length; i++) { arr.setEL(keys[i], Caster.toString(arr.get(keys[i], null)).trim()); } return arr; }
public static String[] listToStringArray(String list, char delimeter) { Array array = List.listToArrayRemoveEmpty(list, delimeter); String[] arr = new String[array.size()]; for (int i = 0; i < arr.length; i++) { arr[i] = Caster.toString(array.get(i + 1, ""), ""); } return arr; }
/** * cast a Object Array to a String Array * * @param array * @param defaultValue * @return String Array */ public static String[] toStringArray(Array array, String defaultValue) { String[] arr = new String[array.size()]; for (int i = 0; i < arr.length; i++) { arr[i] = Caster.toString(array.get(i + 1, defaultValue), defaultValue); } return arr; }
public String getXmlVersion() { // dynamic load to support jre 1.4 and 1.5 try { Method m = doc.getClass().getMethod("getXmlVersion", new Class[] {}); return Caster.toString(m.invoke(doc, ArrayUtil.OBJECT_EMPTY)); } catch (Exception e) { throw new PageRuntimeException(Caster.toPageException(e)); } }
private static String createMessage(String format, Object value) { if (Decision.isSimpleValue(value)) return "the value [" + Caster.toString(value, null) + "] is not in [" + format + "] format"; return "cannot convert object from type [" + Caster.toTypeName(value) + "] to a [" + format + "] format"; }
/** * Constructor of the class * * @param sqle */ @Override public CatchBlock getCatchBlock(Config config) { String strSQL = sql == null ? "" : sql.toString(); if (StringUtil.isEmpty(strSQL)) strSQL = Caster.toString(getAdditional().get("SQL", ""), ""); String datasourceName = datasource == null ? "" : datasource.getName(); if (StringUtil.isEmpty(datasourceName)) datasourceName = Caster.toString(getAdditional().get("DataSource", ""), ""); CatchBlock sct = super.getCatchBlock(config); sct.setEL("NativeErrorCode", new Double(errorcode)); sct.setEL("DataSource", datasourceName); sct.setEL("SQLState", sqlstate); sct.setEL("Sql", strSQL); sct.setEL("queryError", strSQL); sct.setEL("where", ""); return sct; }
public static String call(PageContext pc, Object name, double gain) throws PageException { if (name instanceof String) name = pc.getVariable(Caster.toString(name)); Image img = Image.toImage(name); if (gain < -1.0 || gain > 2.0) throw new FunctionException(pc, "ImageSharpen", 2, "gain", "value must be between 1 and 2"); img.sharpen((float) gain); return null; }
/** * convert Array Object to string list * * @param array array to convert * @param delimeter delimeter for the new list * @return list generated from string array * @throws PageException */ public static String arrayToList(Array array, String delimeter) throws PageException { if (array.size() == 0) return ""; StringBuffer sb = new StringBuffer(Caster.toString(array.getE(1))); int len = array.size(); for (int i = 2; i <= len; i++) { sb.append(delimeter); sb.append(array.get(i, "")); } return sb.toString(); }
public static String call(PageContext pc, String strQueryColumn, String delimeter) throws PageException { QueryColumn column = toColumn(pc, strQueryColumn); StringBuffer sb = new StringBuffer(); int size = column.size(); for (int i = 1; i <= size; i++) { if (i > 1) sb.append(delimeter); sb.append(Caster.toString(column.get(i))); } return sb.toString(); }
public static String getType(Property prop) { String type = prop.getType(); if (StringUtil.isEmpty(type) || "any".equalsIgnoreCase(type) || "object".equalsIgnoreCase(type)) { String fieldType = Caster.toString(prop.getMeta().get(FIELD_TYPE, null), null); if ("one-to-many".equalsIgnoreCase(fieldType) || "many-to-many".equalsIgnoreCase(fieldType)) { return "array"; } return "any"; } return type; }
/** @see railo.runtime.type.Collection#keysAsString() */ public String[] keysAsString() { Set keySet = component.keySet(access); keySet.add("this"); String[] arr = new String[keySet.size()]; Iterator it = keySet.iterator(); int index = 0; while (it.hasNext()) { arr[index++] = Caster.toString(it.next(), null); } return arr; }
/** * @param version the version to set * @throws ApplicationException */ public void setVersion(double version) throws ApplicationException { if (1.1 == version) this.version = '1'; else if (1.2 == version) this.version = PdfWriter.VERSION_1_2; else if (1.3 == version) this.version = PdfWriter.VERSION_1_3; else if (1.4 == version) this.version = PdfWriter.VERSION_1_4; else if (1.5 == version) this.version = PdfWriter.VERSION_1_5; else if (1.6 == version) this.version = PdfWriter.VERSION_1_6; else throw new ApplicationException( "invalid version definition [" + Caster.toString(version) + "], valid version definitions are " + "[1.1, 1.2, 1.3, 1.4, 1.5, 1.6]"); }
public static String call( PageContext pc, Object name, double x, double y, double width, double height, boolean filled) throws PageException { if (name instanceof String) name = pc.getVariable(Caster.toString(name)); Image img = Image.toImage(name); if (width < 0) throw new FunctionException( pc, "ImageDrawRect", 3, "width", "width must contain a none negative value"); if (height < 0) throw new FunctionException( pc, "ImageDrawRect", 4, "height", "width must contain a none negative value"); img.drawRect((int) x, (int) y, (int) width, (int) height, filled); return null; }
public static Object call(PageContext pc, Object[] objArr) throws PageException { if (objArr.length < 3) throw new ExpressionException("invalid call of a CFML Based built in function"); // translate arguments String filename = Caster.toString((((FunctionValue) objArr[0]).getValue())); Collection.Key name = KeyImpl.toKey((((FunctionValue) objArr[1]).getValue())); boolean isweb = Caster.toBooleanValue((((FunctionValue) objArr[2]).getValue())); UDF udf = loadUDF(pc, filename, name, isweb); Struct meta = udf.getMetaData(pc); boolean caller = meta == null ? false : Caster.toBooleanValue(meta.get(CALLER, Boolean.FALSE), false); Struct namedArguments = null; Object[] arguments = null; if (objArr.length <= 3) arguments = ArrayUtil.OBJECT_EMPTY; else if (objArr[3] instanceof FunctionValue) { FunctionValue fv; namedArguments = new StructImpl(); if (caller) namedArguments.setEL(CALLER, pc.undefinedScope().duplicate(false)); for (int i = 3; i < objArr.length; i++) { fv = toFunctionValue(name, objArr[i]); namedArguments.set(fv.getName(), fv.getValue()); } } else { int offset = (caller ? 2 : 3); arguments = new Object[objArr.length - offset]; if (caller) arguments[0] = pc.undefinedScope().duplicate(false); for (int i = 3; i < objArr.length; i++) { arguments[i - offset] = toObject(name, objArr[i]); } } // load UDF // execute UDF if (namedArguments == null) { return udf.call(pc, arguments, false); } return udf.callWithNamedValues(pc, namedArguments, false); }
public static String call(PageContext pc, Object oSrc, Object oDst) throws PageException { Resource src = Caster.toResource(pc, oSrc, false); if (!src.exists()) throw new FunctionException( pc, "FileMove", 1, "source", "source file [" + src + "] does not exist"); FileTag.actionMove( pc, pc.getConfig().getSecurityManager(), src, Caster.toString(oDst), FileUtil.NAMECONFLICT_UNDEFINED, null, null, -1, null); return null; }
/** * @param coll * @param value * @param all * @param buffer * @return * @throws PageException */ private static boolean getValues( PageContext pc, Array array, Collection coll, String value, boolean all, String path) throws PageException { // Key[] keys = coll.keys(); boolean abort = false; Key key; Iterator<Entry<Key, Object>> it = coll.entryIterator(); Entry<Key, Object> e; loop: while (it.hasNext()) { e = it.next(); if (abort) break loop; key = e.getKey(); Object o = e.getValue(); // Collection (this function search first for sub) if (o instanceof Collection) { abort = getValues( pc, array, ((Collection) o), value, all, StructFindKey.createKey(coll, path, key)); } // matching value if (!abort && !StructFindKey.isArray(coll)) { String target = Caster.toString(o, null); if ((target != null && target.equalsIgnoreCase( value)) /*|| (o instanceof Array && checkSub(array,((Array)o),value,all,path,abort))*/) { Struct sct = new StructImpl(); sct.setEL(KeyConstants._key, key.getString()); sct.setEL(KeyConstants._path, StructFindKey.createKey(coll, path, key)); sct.setEL(KeyConstants._owner, coll); array.append(sct); if (!all) abort = true; } } } return abort; }
protected void _release() { // execution time createHeader(header, "execution-time", Caster.toString(System.currentTimeMillis() - start)); header.append("\n"); // path StringBuffer sb = new StringBuffer(); Iterator<String> it = pathes.iterator(); int count = 0; while (it.hasNext()) { sb.append(count++); sb.append(":"); sb.append(it.next()); sb.append("\n"); } sb.append("\n"); try { IOUtil.write(file, header + sb.toString() + content.toString(), (Charset) null, false); } catch (IOException ioe) { err(pc, ioe); } }
static final void validate(String validate, Struct validateParams, Object obj) throws PageException { if (StringUtil.isEmpty(validate, true)) return; validate = validate.trim().toLowerCase(); if (!validate.equals("regex") && !Decision.isValid(validate, obj)) throw new ExpressionException(createMessage(validate, obj)); // range if (validateParams == null) return; if (validate.equals("integer") || validate.equals("numeric") || validate.equals("number")) { double min = Caster.toDoubleValue(validateParams.get(MIN, null), Double.NaN); double max = Caster.toDoubleValue(validateParams.get(MAX, null), Double.NaN); double d = Caster.toDoubleValue(obj); if (!Double.isNaN(min) && d < min) throw new ExpressionException( validate + " [" + Caster.toString(d) + "] is out of range, value must be more than or equal to [" + min + "]"); if (!Double.isNaN(max) && d > max) throw new ExpressionException( validate + " [" + Caster.toString(d) + "] is out of range, value must be less than or equal to [" + max + "]"); } else if (validate.equals("string")) { double min = Caster.toDoubleValue(validateParams.get(MIN_LENGTH, null), Double.NaN); double max = Caster.toDoubleValue(validateParams.get(MAX_LENGTH, null), Double.NaN); String str = Caster.toString(obj); int l = str.length(); if (!Double.isNaN(min) && l < ((int) min)) throw new ExpressionException( "string [" + str + "] is to short [" + l + "], the string must be at least [" + min + "] characters"); if (!Double.isNaN(max) && l > ((int) max)) throw new ExpressionException( "string [" + str + "] is to long [" + l + "], the string can have a maximal length of [" + max + "] characters"); } else if (validate.equals("regex")) { String pattern = Caster.toString(validateParams.get(PATTERN, null), null); String value = Caster.toString(obj); if (!StringUtil.isEmpty(pattern, true) && !IsValid.regex(value, pattern)) throw new ExpressionException( "the string [" + value + "] does not match the regular expression pattern [" + pattern + "]"); } }