001: /* Utils.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Mar 14 15:30:49 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul.impl;
020:
021: import java.util.List;
022: import java.util.LinkedList;
023: import java.util.Iterator;
024:
025: import org.zkoss.lang.Strings;
026:
027: import org.zkoss.zk.ui.Desktop;
028: import org.zkoss.zk.ui.AbstractComponent;
029: import org.zkoss.zk.ui.WrongValueException;
030:
031: /**
032: * A collection of utilities.
033: *
034: * @author tomyeh
035: */
036: public class Utils {
037: /** Parse a list of numbers.
038: *
039: * @param defaultValue the value if a number is omitted. For example, ",2"
040: * means "1,2" if defafultValue is 1
041: * @return an array of int, or null if no integer at all
042: */
043: public static final int[] stringToInts(String numbers,
044: int defaultValue) throws WrongValueException {
045: if (numbers == null)
046: return null;
047:
048: List list = new LinkedList();
049: for (int j = 0;;) {
050: int k = numbers.indexOf(',', j);
051: final String s = (k >= 0 ? numbers.substring(j, k)
052: : numbers.substring(j)).trim();
053: if (s.length() == 0) {
054: if (k < 0)
055: break;
056: list.add(null);
057: } else {
058: try {
059: list.add(Integer.valueOf(s));
060: } catch (Throwable ex) {
061: throw new WrongValueException(
062: "Not a valid number list: " + numbers);
063: }
064: }
065:
066: if (k < 0)
067: break;
068: j = k + 1;
069: }
070:
071: int[] ary;
072: final int sz = list.size();
073: if (sz > 0) {
074: ary = new int[sz];
075: int j = 0;
076: for (Iterator it = list.iterator(); it.hasNext(); ++j) {
077: final Integer i = (Integer) it.next();
078: ary[j] = i != null ? i.intValue() : defaultValue;
079: }
080: } else {
081: ary = null;
082: }
083: return ary;
084: }
085:
086: /** Converts an array of numbers to a string.
087: */
088: public static final String intsToString(int[] ary) {
089: if (ary == null || ary.length == 0)
090: return "";
091:
092: final StringBuffer sb = new StringBuffer(50);
093: for (int j = 0; j < ary.length; ++j) {
094: if (j > 0)
095: sb.append(',');
096: sb.append(ary[j]);
097: }
098: return sb.toString();
099: }
100:
101: /** Parse a list of numbers.
102: *
103: * @param defaultValue the value used if an empty string is fund.
104: * For example, ",2" means "1,2" if defafultValue is "1"
105: * @return an array of string, or null if no data at all
106: */
107: public static final String[] stringToArray(String src,
108: String defaultValue) {
109: if (src == null)
110: return null;
111:
112: List list = new LinkedList();
113: for (int j = 0;;) {
114: int k = src.indexOf(',', j);
115: final String s = (k >= 0 ? src.substring(j, k) : src
116: .substring(j)).trim();
117: if (s.length() == 0) {
118: if (k < 0)
119: break;
120: list.add(defaultValue);
121: } else {
122: list.add(s);
123: }
124:
125: if (k < 0)
126: break;
127: j = k + 1;
128: }
129:
130: return (String[]) list.toArray(new String[list.size()]);
131: }
132:
133: /** Converts an array of objects to a string, by catenating them
134: * together and separated with comma.
135: */
136: public static final String arrayToString(Object[] ary) {
137: if (ary == null || ary.length == 0)
138: return "";
139:
140: final StringBuffer sb = new StringBuffer(50);
141: for (int j = 0; j < ary.length; ++j) {
142: if (j > 0)
143: sb.append(',');
144: if (ary[j] != null)
145: sb.append(ary[j]);
146: }
147: return sb.toString();
148: }
149:
150: /** Returns the encoded URL for the dynamic generated content, or empty
151: * the component doesn't belong to any desktop.
152: *
153: * @since 3.0.2
154: */
155: public static String getDynamicMediaURI(AbstractComponent comp,
156: int version, String name, String format) {
157: final Desktop desktop = comp.getDesktop();
158: if (desktop == null)
159: return ""; //no avail at client
160:
161: final StringBuffer sb = new StringBuffer(64).append('/');
162: Strings.encode(sb, version);
163: if (name != null || format != null) {
164: sb.append('/');
165: boolean bExtRequired = true;
166: if (name != null && name.length() != 0) {
167: sb.append(name.replace('\\', '/'));
168: bExtRequired = name.lastIndexOf('.') < 0;
169: } else {
170: sb.append(comp.getId());
171: }
172: if (bExtRequired && format != null)
173: sb.append('.').append(format);
174: }
175: return desktop.getDynamicMediaURI(comp, sb.toString()); //already encoded
176: }
177: }
|