001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.harness;
007:
008: import java.io.File;
009: import java.io.FileInputStream;
010:
011: import java.util.Vector;
012: import java.util.Properties;
013:
014: import com.sun.portal.desktop.context.TemplateContext;
015:
016: import com.sun.portal.desktop.template.ParsedTagArray;
017:
018: public class HarnessTemplateContext implements TemplateContext {
019:
020: public HarnessTemplateContext() {
021: }
022:
023: public void init(int scaninterval) {
024: }
025:
026: public StringBuffer getTemplateSB(String base, String type,
027: String locale, String app, String provider,
028: String clientFilePath, String file) {
029:
030: File f = getTemplatePath(base, type, locale, app, provider,
031: clientFilePath, file);
032: if (f == null) {
033: return null;
034: }
035:
036: StringBuffer buf = new StringBuffer();
037:
038: try {
039: FileInputStream fis = new FileInputStream(f);
040: byte b[] = new byte[800];
041: for (;;) {
042: int len = fis.read(b);
043: if (len < 0) {
044: break;
045: }
046: buf.append(new String(b, 0, len));
047: }
048: } catch (Exception ex) {
049: return null;
050: }
051:
052: return buf;
053: }
054:
055: public ParsedTagArray getTemplate(String base, String type,
056: String locale, String app, String provider,
057: String clientFilePath, String file) {
058: return new ParsedTagArray(getTemplateSB(base, type, locale,
059: app, provider, clientFilePath, file));
060: }
061:
062: public File getTemplatePath(String base, String type,
063: String locale, String app, String provider,
064: String clientFilePath, String file) {
065: Vector v = new Vector();
066: appendPaths(v, type, locale, app, clientFilePath, file);
067: for (int i = 0; i < v.size(); ++i) {
068: File f = new File(base + (String) v.elementAt(i));
069: if (f.exists()) {
070: return f;
071: }
072: }
073: return null;
074: }
075:
076: public Properties getTemplateProperties(String base, String type,
077: String locale, String app, String provider,
078: String clientFilePath, String file) {
079: return null;
080: }
081:
082: public File getTemplateMostSpecificPath(String base, String type,
083: String locale, String app, String provider,
084: String clientFilePath, String file) {
085: return getTemplatePath(base, type, locale, app, provider,
086: clientFilePath, file);
087: }
088:
089: public File[] getTemplatePaths(String type, String locale,
090: String app, String clientFilePath, String file) {
091: Vector v = new Vector();
092: appendPaths(v, type, locale, app, clientFilePath, file);
093:
094: File f[] = new File[v.size()];
095: for (int i = 0; i < v.size(); ++i) {
096: f[i] = new File((String) v.elementAt(i));
097: }
098:
099: return f;
100: }
101:
102: private void appendPaths(Vector v, String type, String locale,
103: String app, String clientFilePath, String file) {
104:
105: int orglen = type.length();
106: String first = type;
107: if (locale != null && locale.length() > 0) {
108: first = first + "_" + locale;
109: }
110:
111: for (;;) {
112:
113: if (clientFilePath != null) {
114: v.add("/" + first + "/" + app + "/" + clientFilePath
115: + "/" + file);
116: }
117: v.add("/" + first + "/" + app + "/" + file);
118: v.add("/" + first + "/" + file);
119: int idx = first.lastIndexOf('_');
120: if (idx < orglen) {
121: break;
122: }
123: first = first.substring(0, idx);
124: }
125:
126: if (type.equals("default")) {
127: return;
128: }
129:
130: appendPaths(v, "default", locale, app, clientFilePath, file);
131: }
132: }
|