001: // nxTools.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: //
007: // $LastChangedDate: 2007-12-27 17:56:59 +0000 (Do, 27 Dez 2007) $
008: // $LastChangedRevision: 4292 $
009: // $LastChangedBy: orbiter $
010: //
011: // This program is free software; you can redistribute it and/or modify
012: // it under the terms of the GNU General Public License as published by
013: // the Free Software Foundation; either version 2 of the License, or
014: // (at your option) any later version.
015: //
016: // This program is distributed in the hope that it will be useful,
017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: // GNU General Public License for more details.
020: //
021: // You should have received a copy of the GNU General Public License
022: // along with this program; if not, write to the Free Software
023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: //
025: // Using this software in any meaning (reading, learning, copying, compiling,
026: // running) means that you agree that the Author(s) is (are) not responsible
027: // for cost, loss of data or any harm that may be caused directly or indirectly
028: // by usage of this softare or this documentation. The usage of this software
029: // is on your own risk. The installation and usage (starting/running) of this
030: // software may allow other people or application to access your computer and
031: // any attached devices and is highly dependent on the configuration of the
032: // software which must be done by the user of the software; the author(s) is
033: // (are) also not responsible for proper configuration and usage of the
034: // software, even if provoked by documentation provided together with
035: // the software.
036: //
037: // Any changes to this file according to the GPL as documented in the file
038: // gpl.txt aside this file in the shipment you received can be done to the
039: // lines that follows this copyright notice here, but changes must not be
040: // done inside the copyright notice above. A re-distribution must contain
041: // the intact and unchanged copyright notice.
042: // Contributions and changes to the program code must be marked as such.
043:
044: package de.anomic.tools;
045:
046: import java.io.UnsupportedEncodingException;
047: import java.util.ArrayList;
048: import java.util.Enumeration;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Vector;
052:
053: public class nxTools {
054:
055: public static HashMap<String, String> table(Vector<String> list) {
056: Enumeration<String> i = list.elements();
057: int pos;
058: String line;
059: HashMap<String, String> props = new HashMap<String, String>(
060: list.size());
061: while (i.hasMoreElements()) {
062: line = ((String) i.nextElement()).trim();
063: pos = line.indexOf("=");
064: if (pos > 0)
065: props.put(line.substring(0, pos).trim(), line
066: .substring(pos + 1).trim());
067: }
068: return props;
069: }
070:
071: public static HashMap<String, String> table(byte[] a,
072: String encoding) {
073: return table(strings(a, encoding));
074: }
075:
076: public static HashMap<String, String> table(ArrayList<String> list) {
077: if (list == null)
078: return new HashMap<String, String>();
079: Iterator<String> i = list.iterator();
080: int pos;
081: String line;
082: HashMap<String, String> props = new HashMap<String, String>(
083: list.size());
084: while (i.hasNext()) {
085: line = ((String) i.next()).trim();
086: if (line.startsWith("#"))
087: continue; // exclude comments
088: //System.out.println("NXTOOLS_PROPS - LINE:" + line);
089: pos = line.indexOf("=");
090: if (pos > 0)
091: props.put(line.substring(0, pos).trim(), line
092: .substring(pos + 1).trim());
093: }
094: return props;
095: }
096:
097: public static Vector<String> grep(Vector<String> list,
098: int afterContext, String pattern) {
099: Enumeration<String> i = list.elements();
100: int ac = 0;
101: String line;
102: Vector<String> result = new Vector<String>();
103: while (i.hasMoreElements()) {
104: line = (String) i.nextElement();
105: if (line.indexOf(pattern) >= 0) {
106: result.add(line);
107: ac = afterContext + 1;
108: } else if (ac > 0) {
109: result.add(line);
110: }
111: ac--;
112: }
113: return result;
114: }
115:
116: public static ArrayList<String> grep(ArrayList<String> list,
117: int afterContext, String pattern) {
118: Iterator<String> i = list.iterator();
119: int ac = 0;
120: String line;
121: ArrayList<String> result = new ArrayList<String>();
122: while (i.hasNext()) {
123: line = (String) i.next();
124: if (line.indexOf(pattern) >= 0) {
125: result.add(line);
126: ac = afterContext + 1;
127: } else if (ac > 0) {
128: result.add(line);
129: }
130: ac--;
131: }
132: return result;
133: }
134:
135: public static String tail1(Vector<String> list) {
136: if ((list == null) || (list.size() == 0))
137: return "";
138: return (String) list.lastElement();
139: }
140:
141: public static String tail1(ArrayList<String> list) {
142: if ((list == null) || (list.size() == 0))
143: return "";
144: return (String) list.get(list.size() - 1);
145: }
146:
147: public static String awk(String sentence, String separator,
148: int count) {
149: // returns the nth word of sentence, where count is the counter and the first word has the number 1
150: // the words are separated by the separator
151: if ((sentence == null) || (separator == null) || (count < 1))
152: return null;
153: int pos;
154: while ((count >= 1) && (sentence.length() > 0)) {
155: pos = sentence.indexOf(separator);
156: if (pos < 0) {
157: if (count == 1)
158: return sentence;
159: else
160: return null;
161: } else {
162: if (count == 1)
163: return sentence.substring(0, pos);
164: sentence = sentence.substring(pos + separator.length());
165: count--;
166: }
167: }
168: return null;
169: }
170:
171: public static ArrayList<String> strings(byte[] a) {
172: return strings(a, null);
173: }
174:
175: public static ArrayList<String> strings(byte[] a, String encoding) {
176: if (a == null)
177: return new ArrayList<String>();
178: int s = 0;
179: int e;
180: ArrayList<String> v = new ArrayList<String>();
181: byte b;
182: while (s < a.length) {
183: // find eol
184: e = s;
185: while (e < a.length) {
186: b = a[e];
187: if ((b == 10) || (b == 13) || (b == 0))
188: break;
189: e++;
190: }
191:
192: // read line
193: if (encoding == null) {
194: v.add(new String(a, s, e - s));
195: } else
196: try {
197: v.add(new String(a, s, e - s, encoding));
198: } catch (UnsupportedEncodingException xcptn) {
199: return v;
200: }
201:
202: // eat up additional eol bytes
203: s = e + 1;
204: while (s < a.length) {
205: b = a[s];
206: if ((b != 10) && (b != 13))
207: break;
208: s++;
209: }
210: }
211: return v;
212: }
213:
214: /**
215: * This function shorten URL Strings<br>
216: *
217: * Example returns:<br>
218: * <dl><dt>normal domain:</dt><dd>http://domain.net/leftpath..rightpath</dd>
219: * <dt>long domain:</dt><dd>http://very_very_long_domain.net/le..</dd></dl>
220: * @param String like a URL
221: * @return the shorten or the old String
222: */
223: public static String shortenURLString(String url, int len) {
224: // This is contributed by Thomas Quella (borg-0300)
225: if (url == null) {
226: return null;
227: }
228: int la = url.length();
229: if (la > len) {
230: int cpos;
231: cpos = url.indexOf("://");
232: if (cpos >= 0) {
233: cpos = url.indexOf("/", cpos + 3);
234: if (cpos >= 0) {
235: if (cpos < len - (len / 3)) { // at least 1/3 characters for the path
236: final int lb = ((len - cpos) / 2) - 1;
237: if (lb * 2 + 2 + cpos < len) {
238: la--;
239: } // if smaller(odd), half right path + 1
240: return url.substring(0, cpos + lb).concat("..")
241: .concat(url.substring(la - lb));
242: } else {
243: return url.substring(0, len - 2).concat("..");
244: }
245: } else { // very crazy domain or very short len
246: return url.substring(0, len - 2).concat("..");
247: } // no slash at end
248: } // NO URL !?
249: } // URL < len
250: return url;
251: }
252:
253: }
|