001: //httpTemplate.java
002: //-------------------------------------
003: //(C) by Michael Peter Christen; mc@anomic.de
004: //first published on http://www.anomic.de
005: //Frankfurt, Germany, 2004
006: //last major change: 16.01.2005
007:
008: //$LastChangedDate: 2008-01-29 10:12:48 +0000 (Di, 29 Jan 2008) $
009: //$LastChangedRevision: 4414 $
010: //$LastChangedBy: orbiter $
011:
012: //extended for multi- and alternatives-templates by Alexander Schier
013:
014: //This program is free software; you can redistribute it and/or modify
015: //it under the terms of the GNU General Public License as published by
016: //the Free Software Foundation; either version 2 of the License, or
017: //(at your option) any later version.
018:
019: //This program is distributed in the hope that it will be useful,
020: //but WITHOUT ANY WARRANTY; without even the implied warranty of
021: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022: //GNU General Public License for more details.
023:
024: //You should have received a copy of the GNU General Public License
025: //along with this program; if not, write to the Free Software
026: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
027:
028: //Using this software in any meaning (reading, learning, copying, compiling,
029: //running) means that you agree that the Author(s) is (are) not responsible
030: //for cost, loss of data or any harm that may be caused directly or indirectly
031: //by usage of this softare or this documentation. The usage of this software
032: //is on your own risk. The installation and usage (starting/running) of this
033: //software may allow other people or application to access your computer and
034: //any attached devices and is highly dependent on the configuration of the
035: //software which must be done by the user of the software; the author(s) is
036: //(are) also not responsible for proper configuration and usage of the
037: //software, even if provoked by documentation provided together with
038: //the software.
039:
040: //Any changes to this file according to the GPL as documented in the file
041: //gpl.txt aside this file in the shipment you received can be done to the
042: //lines that follows this copyright notice here, but changes must not be
043: //done inside the copyright notice above. A re-distribution must contain
044: //the intact and unchanged copyright notice.
045: //Contributions and changes to the program code must be marked as such.
046:
047: package de.anomic.http;
048:
049: import java.io.BufferedReader;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.File;
053: import java.io.FileInputStream;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.io.InputStreamReader;
057: import java.io.OutputStream;
058: import java.io.PushbackInputStream;
059: import java.io.UnsupportedEncodingException;
060: import java.util.ArrayList;
061: import java.util.HashMap;
062: import java.util.List;
063:
064: import de.anomic.server.serverByteBuffer;
065: import de.anomic.server.serverFileUtils;
066: import de.anomic.server.logging.serverLog;
067:
068: /**
069: * A template engine, which substitutes patterns in strings<br>
070: *
071: * The template engine supports four types of templates:<br>
072: * <ol>
073: * <li>Normal templates: the template will be replaced by a string.</li>
074: * <li>Multi templates: the template will be used more than one time.<br>
075: * i.e. for lists</li>
076: * <li>3. Alternatives: the program chooses one of multiple alternatives.</li>
077: * <li>Includes: another file with templates will be included.</li>
078: * </ol>
079: *
080: * All these templates can be used recursivly.<p>
081: * <b>HTML-Example</b><br>
082: * <pre>
083: * <html><head></head><body>
084: * #{times}#
085: * Good #(daytime)#morning::evening#(/daytime)#, #[name]#!(#[num]#. Greeting)<br>
086: * #{/times}#
087: * </body></html>
088: * </pre>
089: * <p>
090: * The corresponding Hashtable to use this Template:<br>
091: * <b>Java Example</b><br>
092: * <pre>
093: * Hashtable pattern;
094: * pattern.put("times", 10); //10 greetings
095: * for(int i=0;i<=9;i++){
096: * pattern.put("times_"+i+"_daytime", 1); //index: 1, second Entry, evening
097: * pattern.put("times_"+i+"_name", "John Connor");
098: * pattern.put("times_"+i+"_num", (i+1));
099: * }
100: * </pre>
101: * <p>
102: * <b>Recursion</b><br>
103: * If you use recursive templates, the templates will be used from
104: * the external to the internal templates.
105: * In our Example, the Template in #{times}##{/times}# will be repeated ten times.<br>
106: * Then the inner Templates will be applied.
107: * <p>
108: * The inner templates have a prefix, so they may have the same name as a template on another level,
109: * or templates which are in another recursive template.<br>
110: * <b>The Prefixes:</b>
111: * <ul>
112: * <li>Multi templates: multitemplatename_index_</li>
113: * <li>Alterantives: alternativename_</li>
114: * </ul>
115: * So the Names in the Hashtable are:
116: * <ul>
117: * <li>Multi templates: multitemplatename_index_templatename</li>
118: * <li>Alterantives: alternativename_templatename</li>
119: * </ul>
120: * <i>#(alternative)#::#{repeat}##[test]##{/repeat}##(/alternative)#</i><br>
121: * would be adressed as "alternative_repeat_"+number+"_test"
122: */
123: public final class httpTemplate {
124:
125: public static final byte hash = (byte) '#';
126: private static final byte[] hasha = { hash };
127:
128: private static final byte dp = (byte) ':';
129: public static final byte[] dpdpa = { dp, dp };
130:
131: private static final byte lbr = (byte) '[';
132: private static final byte rbr = (byte) ']';
133: private static final byte[] pOpen = { hash, lbr };
134: private static final byte[] pClose = { rbr, hash };
135:
136: private static final byte lcbr = (byte) '{';
137: private static final byte rcbr = (byte) '}';
138: private static final byte[] mOpen = { hash, lcbr };
139: private static final byte[] mClose = { rcbr, hash };
140:
141: private static final byte lrbr = (byte) '(';
142: private static final byte rrbr = (byte) ')';
143: private static final byte[] aOpen = { hash, lrbr };
144: private static final byte[] aClose = { rrbr, hash };
145:
146: private static final byte ps = (byte) '%';
147: private static final byte[] iOpen = { hash, ps };
148: private static final byte[] iClose = { ps, hash };
149:
150: public static final byte[] slash = { (byte) '/' };
151:
152: public static final Object[] meta_quotation = new Object[] {
153: new Object[] { pOpen, pClose },
154: new Object[] { mOpen, mClose },
155: new Object[] { aOpen, aClose },
156: new Object[] { iOpen, iClose } };
157:
158: public static serverByteBuffer[] splitQuotations(
159: serverByteBuffer text) {
160: List<serverByteBuffer> l = splitQuotation(text, 0);
161: serverByteBuffer[] sbbs = new serverByteBuffer[l.size()];
162: for (int i = 0; i < l.size(); i++)
163: sbbs[i] = l.get(i);
164: return sbbs;
165: }
166:
167: public static List<serverByteBuffer> splitQuotation(
168: serverByteBuffer text, int qoff) {
169: ArrayList<serverByteBuffer> l = new ArrayList<serverByteBuffer>();
170: if (qoff >= meta_quotation.length) {
171: if (text.length() > 0)
172: l.add(text);
173: return l;
174: }
175: int p = -1, q;
176: byte[] left = (byte[]) ((Object[]) meta_quotation[qoff])[0];
177: byte[] right = (byte[]) ((Object[]) meta_quotation[qoff])[1];
178: qoff++;
179: while ((text.length() > 0) && ((p = text.indexOf(left)) >= 0)) {
180: q = text.indexOf(right, p + 1);
181: if (q >= 0) {
182: // found a pattern
183: l.addAll(splitQuotation(new serverByteBuffer(text
184: .getBytes(0, p)), qoff));
185: l.add(new serverByteBuffer(text.getBytes(p, q
186: + right.length - p)));
187: text = new serverByteBuffer(text.getBytes(q
188: + right.length));
189: } else {
190: // found only pattern start, no closing parantesis (a syntax error that is silently accepted here)
191: l.addAll(splitQuotation(new serverByteBuffer(text
192: .getBytes(0, p)), qoff));
193: l.addAll(splitQuotation(new serverByteBuffer(text
194: .getBytes(p)), qoff));
195: text.clear();
196: }
197: }
198:
199: // find double-points
200: while ((text.length() > 0) && ((p = text.indexOf(dpdpa)) >= 0)) {
201: l.addAll(splitQuotation(new serverByteBuffer(text.getBytes(
202: 0, p)), qoff));
203: l.add(new serverByteBuffer(dpdpa));
204: l.addAll(splitQuotation(new serverByteBuffer(text
205: .getBytes(p + 2)), qoff));
206: text.clear();
207: }
208:
209: // add remaining
210: if (text.length() > 0)
211: l.addAll(splitQuotation(text, qoff));
212: return l;
213: }
214:
215: /**
216: * transfer until a specified pattern is found; everything but the pattern is transfered so far
217: * the function returns true, if the pattern is found
218: */
219: private static boolean transferUntil(PushbackInputStream i,
220: OutputStream o, byte[] pattern) throws IOException {
221: int b, bb;
222: boolean equal;
223: while ((b = i.read()) > 0) {
224: if ((b & 0xFF) == pattern[0]) {
225: // read the whole pattern
226: equal = true;
227: for (int n = 1; n < pattern.length; n++) {
228: if (((bb = i.read()) & 0xFF) != pattern[n]) {
229: // push back all
230: i.unread(bb);
231: equal = false;
232: for (int nn = n - 1; nn > 0; nn--)
233: i.unread(pattern[nn]);
234: break;
235: }
236: }
237: if (equal)
238: return true;
239: }
240: o.write(b);
241: }
242: return false;
243: }
244:
245: public static void writeTemplate(InputStream in, OutputStream out,
246: HashMap<String, String> pattern, byte[] dflt)
247: throws IOException {
248: writeTemplate(in, out, pattern, dflt, new byte[0]);
249: }
250:
251: /**
252: * Reads a input stream, and writes the data with replaced templates on a output stream
253: */
254: public static byte[] writeTemplate(InputStream in,
255: OutputStream out, HashMap<String, String> pattern,
256: byte[] dflt, byte[] prefix) throws IOException {
257: PushbackInputStream pis = new PushbackInputStream(in, 100);
258: ByteArrayOutputStream keyStream;
259: byte[] key;
260: byte[] multi_key;
261: byte[] replacement;
262: int bb;
263: serverByteBuffer structure = new serverByteBuffer();
264:
265: while (transferUntil(pis, out, hasha)) {
266: bb = pis.read();
267: keyStream = new ByteArrayOutputStream();
268:
269: if ((bb & 0xFF) == lcbr) { //multi
270: if (transferUntil(pis, keyStream, mClose)) { //close tag
271: //multi_key = "_" + keyStream.toString(); //for _Key
272: bb = pis.read();
273: if ((bb & 0xFF) != 10) { //kill newline
274: pis.unread(bb);
275: }
276: multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here
277: keyStream = new ByteArrayOutputStream(); //reset stream
278:
279: /* DEBUG - print key + value
280: try{
281: System.out.println("Key: "+prefix+multi_key+ "; Value: "+pattern.get(prefix+multi_key));
282: }catch(NullPointerException e){
283: System.out.println("Key: "+prefix+multi_key);
284: }
285: */
286:
287: //this needs multi_key without prefix
288: if (transferUntil(pis, keyStream, appendBytes(
289: mOpen, slash, multi_key, mClose))) {
290: bb = pis.read();
291: if ((bb & 0xFF) != 10) { //kill newline
292: pis.unread(bb);
293: }
294:
295: byte[] text = keyStream.toByteArray(); //text between #{key}# an #{/key}#
296: int num = 0;
297: String patternKey = getPatternKey(prefix,
298: multi_key);
299: if (pattern.containsKey(patternKey)
300: && pattern.get(patternKey) != null) {
301: try {
302: num = Integer.parseInt((String) pattern
303: .get(patternKey)); // Key contains the iteration number as string
304: } catch (NumberFormatException e) {
305: num = 0;
306: }
307: //System.out.println(multi_key + ": " + num); //DEBUG
308: } else {
309: //0 interations - no display
310: //System.out.println("_"+new String(multi_key)+" is null or does not exist"); //DEBUG
311: }
312:
313: //Enumeration enx = pattern.keys(); while (enx.hasMoreElements()) System.out.println("KEY=" + enx.nextElement()); // DEBUG
314: structure.append("<".getBytes("UTF-8")).append(
315: multi_key).append(
316: " type=\"multi\" num=\""
317: .getBytes("UTF-8"))
318: .append(
319: Integer.toString(num).getBytes(
320: "UTF-8")).append(
321: "\">\n".getBytes("UTF-8"));
322: for (int i = 0; i < num; i++) {
323: PushbackInputStream pis2 = new PushbackInputStream(
324: new ByteArrayInputStream(text));
325: //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG
326: //System.out.println(text);
327: structure.append(writeTemplate(pis2, out,
328: pattern, dflt, newPrefix(prefix,
329: multi_key, i)));
330: }//for
331: structure.append("</".getBytes("UTF-8"))
332: .append(multi_key).append(
333: ">\n".getBytes("UTF-8"));
334: } else {//transferUntil
335: serverLog.logSevere("TEMPLATE",
336: "No Close Key found for #{"
337: + new String(multi_key) + "}#"); //prefix here?
338: }
339: }
340: } else if ((bb & 0xFF) == lrbr) { //alternatives
341: int others = 0;
342: serverByteBuffer text = new serverByteBuffer();
343: PushbackInputStream pis2;
344:
345: transferUntil(pis, keyStream, aClose);
346: key = keyStream.toByteArray(); //Caution: Key does not contain prefix
347:
348: /* DEBUG - print key + value
349: try{
350: System.out.println("Key: "+prefix+key+ "; Value: "+pattern.get(prefix+key));
351: }catch(NullPointerException e){
352: System.out.println("Key: "+prefix+key);
353: }
354: */
355:
356: keyStream = new ByteArrayOutputStream(); //clear
357:
358: boolean byName = false;
359: int whichPattern = 0;
360: byte[] patternName = new byte[0];
361: String patternKey = getPatternKey(prefix, key);
362: if (pattern.containsKey(patternKey)
363: && pattern.get(patternKey) != null) {
364: String patternId = (String) pattern.get(patternKey);
365: try {
366: whichPattern = Integer.parseInt(patternId); //index
367: } catch (NumberFormatException e) {
368: whichPattern = 0;
369: byName = true;
370: patternName = patternId.getBytes("UTF-8");
371: }
372: } else {
373: //System.out.println("Pattern \""+new String(prefix + key)+"\" is not set"); //DEBUG
374: }
375:
376: int currentPattern = 0;
377: boolean found = false;
378: keyStream = new ByteArrayOutputStream(); //reset stream
379: if (byName) {
380: //TODO: better Error Handling
381: transferUntil(pis, keyStream,
382: appendBytes("%%".getBytes("UTF-8"),
383: patternName, null, null));
384: if (pis.available() == 0) {
385: serverLog.logSevere("TEMPLATE",
386: "No such Template: %%" + patternName);
387: return structure.getBytes();
388: }
389: keyStream = new ByteArrayOutputStream();
390: transferUntil(pis, keyStream, "::".getBytes());
391: pis2 = new PushbackInputStream(
392: new ByteArrayInputStream(keyStream
393: .toByteArray()));
394: structure.append(writeTemplate(pis2, out, pattern,
395: dflt, newPrefix(prefix, key)));
396: transferUntil(pis, keyStream, appendBytes("#(/"
397: .getBytes("UTF-8"), key, ")#"
398: .getBytes("UTF-8"), null));
399: if (pis.available() == 0) {
400: serverLog.logSevere("TEMPLATE",
401: "No Close Key found for #("
402: + new String(key)
403: + ")# (by Name)");
404: }
405: } else {
406: while (!found) {
407: bb = pis.read();
408: if ((bb & 0xFF) == hash) {
409: bb = pis.read();
410: if ((bb & 0xFF) == lrbr) {
411: transferUntil(pis, keyStream, aClose);
412:
413: //reached the end. output last string.
414: if (java.util.Arrays.equals(keyStream
415: .toByteArray(), appendBytes(
416: slash, key, null, null))) {
417: pis2 = new PushbackInputStream(
418: new ByteArrayInputStream(
419: text.getBytes()));
420: //this maybe the wrong, but its the last
421: structure
422: .append(
423: "<"
424: .getBytes("UTF-8"))
425: .append(key)
426: .append(
427: " type=\"alternative\" which=\""
428: .getBytes("UTF-8"))
429: .append(
430: Integer
431: .toString(
432: whichPattern)
433: .getBytes(
434: "UTF-8"))
435: .append(
436: "\" found=\"0\">\n"
437: .getBytes("UTF-8"));
438: structure.append(writeTemplate(
439: pis2, out, pattern, dflt,
440: newPrefix(prefix, key)));
441: structure
442: .append(
443: "</"
444: .getBytes("UTF-8"))
445: .append(key)
446: .append(
447: ">\n"
448: .getBytes("UTF-8"));
449: found = true;
450: } else if (others > 0
451: && keyStream.toString()
452: .startsWith("/")) { //close nested
453: others--;
454: text
455: .append(
456: "#("
457: .getBytes("UTF-8"))
458: .append(
459: keyStream
460: .toByteArray())
461: .append(
462: ")#"
463: .getBytes("UTF-8"));
464: } else { //nested
465: others++;
466: text
467: .append(
468: "#("
469: .getBytes("UTF-8"))
470: .append(
471: keyStream
472: .toByteArray())
473: .append(
474: ")#"
475: .getBytes("UTF-8"));
476: }
477: keyStream = new ByteArrayOutputStream(); //reset stream
478: continue;
479: } else { //is not #(
480: pis.unread(bb);//is processed in next loop
481: bb = (hash);//will be added to text this loop
482: //text += "#";
483: }
484: } else if ((bb & 0xFF) == ':' && others == 0) {//ignore :: in nested Expressions
485: bb = pis.read();
486: if ((bb & 0xFF) == ':') {
487: if (currentPattern == whichPattern) { //found the pattern
488: pis2 = new PushbackInputStream(
489: new ByteArrayInputStream(
490: text.getBytes()));
491: structure
492: .append(
493: "<"
494: .getBytes("UTF-8"))
495: .append(key)
496: .append(
497: " type=\"alternative\" which=\""
498: .getBytes("UTF-8"))
499: .append(
500: Integer
501: .toString(
502: whichPattern)
503: .getBytes(
504: "UTF-8"))
505: .append(
506: "\" found=\"0\">\n"
507: .getBytes("UTF-8"));
508: structure.append(writeTemplate(
509: pis2, out, pattern, dflt,
510: newPrefix(prefix, key)));
511: structure
512: .append(
513: "</"
514: .getBytes("UTF-8"))
515: .append(key)
516: .append(
517: ">\n"
518: .getBytes("UTF-8"));
519:
520: transferUntil(
521: pis,
522: keyStream,
523: appendBytes(
524: "#(/"
525: .getBytes("UTF-8"),
526: key,
527: ")#"
528: .getBytes("UTF-8"),
529: null));//to #(/key)#.
530:
531: found = true;
532: }
533: currentPattern++;
534: text.clear();
535: continue;
536: } else {
537: text.append(":".getBytes("UTF-8"));
538: }
539: }
540: if (!found) {
541: text.append((byte) bb);
542: if (pis.available() == 0) {
543: serverLog.logSevere("TEMPLATE",
544: "No Close Key found for #("
545: + new String(key)
546: + ")# (by Index)");
547: found = true;
548: }
549: }
550: }//while
551: }//if(byName) (else branch)
552: } else if ((bb & 0xFF) == lbr) { //normal
553: if (transferUntil(pis, keyStream, pClose)) {
554: // pattern detected, write replacement
555: key = keyStream.toByteArray();
556: String patternKey = getPatternKey(prefix, key);
557: replacement = replacePattern(patternKey, pattern,
558: dflt); //replace
559: structure.append("<".getBytes("UTF-8")).append(key)
560: .append(
561: " type=\"normal\">\n"
562: .getBytes("UTF-8"));
563: structure.append(replacement);
564: structure.append("</".getBytes("UTF-8"))
565: .append(key)
566: .append(">\n".getBytes("UTF-8"));
567:
568: /* DEBUG
569: try{
570: System.out.println("Key: "+key+ "; Value: "+pattern.get(key));
571: }catch(NullPointerException e){
572: System.out.println("Key: "+key);
573: }
574: */
575:
576: serverFileUtils.write(replacement, out);
577: } else {
578: // inconsistency, simply finalize this
579: serverFileUtils.copy(pis, out);
580: return structure.getBytes();
581: }
582: } else if ((bb & 0xFF) == ps) { //include
583: serverByteBuffer include = new serverByteBuffer();
584: keyStream = new ByteArrayOutputStream(); //reset stream
585: if (transferUntil(pis, keyStream, iClose)) {
586: byte[] filename = keyStream.toByteArray();
587: //if(filename.startsWith( Character.toString((char)lbr) ) && filename.endsWith( Character.toString((char)rbr) )){ //simple pattern for filename
588: if ((filename[0] == lbr)
589: && (filename[filename.length - 1] == rbr)) { //simple pattern for filename
590: byte[] newFilename = new byte[filename.length - 2];
591: System.arraycopy(filename, 1, newFilename, 0,
592: newFilename.length);
593: String patternkey = getPatternKey(prefix,
594: newFilename);
595: filename = replacePattern(patternkey, pattern,
596: dflt);
597: }
598: if (filename.length > 0
599: && !java.util.Arrays.equals(filename, dflt)) {
600: BufferedReader br = null;
601: try {
602: //br = new BufferedReader(new InputStreamReader(new FileInputStream( filename ))); //Simple Include
603: br = new BufferedReader(
604: new InputStreamReader(
605: new FileInputStream(
606: httpdFileHandler
607: .getLocalizedFile(new String(
608: filename,
609: "UTF-8"))),
610: "UTF-8")); //YaCy (with Locales)
611: //Read the Include
612: String line = "";
613: while ((line = br.readLine()) != null) {
614: include
615: .append(line.getBytes("UTF-8"))
616: .append(
617: de.anomic.server.serverCore.CRLF_STRING
618: .getBytes("UTF-8"));
619: }
620: } catch (IOException e) {
621: //file not found?
622: serverLog.logSevere("FILEHANDLER",
623: "Include Error with file "
624: + new String(filename,
625: "UTF-8") + ": "
626: + e.getMessage());
627: } finally {
628: if (br != null)
629: try {
630: br.close();
631: br = null;
632: } catch (Exception e) {
633: }
634: }
635: PushbackInputStream pis2 = new PushbackInputStream(
636: new ByteArrayInputStream(include
637: .getBytes()));
638: structure.append(
639: "<fileinclude file=\""
640: .getBytes("UTF-8")).append(
641: filename).append(
642: ">\n".getBytes("UTF-8"));
643: structure.append(writeTemplate(pis2, out,
644: pattern, dflt, prefix));
645: structure.append("</fileinclude>\n"
646: .getBytes("UTF-8"));
647: }
648: }
649: } else { //no match, but a single hash (output # + bb)
650: byte[] tmp = new byte[2];
651: tmp[0] = hash;
652: tmp[1] = (byte) bb;
653: serverFileUtils.write(tmp, out);
654: }
655: }
656: //System.out.println(structure.toString()); //DEBUG
657: return structure.getBytes();
658: }
659:
660: public static byte[] replacePattern(String key,
661: HashMap<String, String> pattern, byte dflt[]) {
662: byte[] replacement;
663: Object value;
664: if (pattern.containsKey(key)) {
665: value = pattern.get(key);
666: try {
667: if (value instanceof byte[]) {
668: replacement = (byte[]) value;
669: } else if (value instanceof String) {
670: //replacement = ((String) value).getBytes();
671: replacement = ((String) value).getBytes("UTF-8");
672: } else {
673: //replacement = value.toString().getBytes();
674: replacement = value.toString().getBytes("UTF-8");
675: }
676: } catch (UnsupportedEncodingException e) {
677: replacement = dflt;
678: }
679: } else {
680: replacement = dflt;
681: }
682: return replacement;
683: }
684:
685: public static void main(String[] args) {
686: // arg1 = test input; arg2 = replacement for pattern 'test'; arg3 = default replacement
687: try {
688: InputStream i = new ByteArrayInputStream(args[0].getBytes());
689: HashMap<String, String> h = new HashMap<String, String>();
690: h.put("test", args[1]);
691: writeTemplate(new PushbackInputStream(i, 100), System.out,
692: h, args[2].getBytes());
693: System.out.flush();
694: } catch (Exception e) {
695: e.printStackTrace();
696: }
697: }
698:
699: /*
700: * loads all Files from path into a filename->content HashMap
701: */
702: public static HashMap<String, String> loadTemplates(File path) {
703: // reads all templates from a path
704: // we use only the folder from the given file path
705: HashMap<String, String> result = new HashMap<String, String>();
706: if (path == null)
707: return result;
708: if (!(path.isDirectory()))
709: path = path.getParentFile();
710: if ((path == null) || (!(path.isDirectory())))
711: return result;
712: String[] templates = path.list();
713: for (int i = 0; i < templates.length; i++) {
714: if (templates[i].endsWith(".template"))
715: try {
716: //System.out.println("TEMPLATE " + templates[i].substring(0, templates[i].length() - 9) + ": " + new String(buf, 0, c));
717: result.put(templates[i].substring(0, templates[i]
718: .length() - 9), new String(serverFileUtils
719: .read(new File(path, templates[i]))));
720: } catch (Exception e) {
721: }
722: }
723: return result;
724: }
725:
726: public static byte[] newPrefix(byte[] oldPrefix, byte[] key) {
727: serverByteBuffer newPrefix = new serverByteBuffer();
728: newPrefix.append(oldPrefix).append(key).append("_".getBytes());
729: return newPrefix.getBytes();
730: }
731:
732: public static byte[] newPrefix(byte[] oldPrefix, byte[] multi_key,
733: int i) {
734: serverByteBuffer newPrefix = new serverByteBuffer();
735: try {
736: newPrefix.append(oldPrefix).append(multi_key).append(
737: "_".getBytes()).append(
738: Integer.toString(i).getBytes("UTF-8")).append(
739: "_".getBytes());
740:
741: } catch (UnsupportedEncodingException e) {
742: }
743: return newPrefix.getBytes();
744: }
745:
746: public static String getPatternKey(byte[] prefix, byte[] key) {
747: serverByteBuffer patternKey = new serverByteBuffer();
748: patternKey.append(prefix).append(key);
749: try {
750: return new String(patternKey.getBytes(), "UTF-8");
751: } catch (UnsupportedEncodingException e) {
752: return null;
753: }
754: }
755:
756: public static byte[] appendBytes(byte[] b1, byte[] b2, byte[] b3,
757: byte[] b4) {
758: serverByteBuffer byteArray = new serverByteBuffer();
759: byteArray.append(b1).append(b2);
760: if (b3 != null)
761: byteArray.append(b3);
762: if (b4 != null)
763: byteArray.append(b4);
764: return byteArray.getBytes();
765: }
766:
767: }
|