001: /*
002: * ExportFile.java
003: *
004: * Created on November 12, 2001, 10:47 AM
005: */
006:
007: package com.sun.portal.desktop.deployment;
008:
009: import java.io.BufferedReader;
010: import java.io.FileReader;
011: import java.io.StringReader;
012:
013: import java.net.URLDecoder;
014:
015: import java.util.Vector;
016: import java.util.StringTokenizer;
017:
018: import org.w3c.dom.Document;
019:
020: /**
021: *
022: * Obtain information from the file that goes with the "export" utility - lists of files,
023: * autoextract op, and so on.
024: *
025: * @author yabob
026: * @version
027: */
028: public class ExportFile {
029:
030: // Private constructor so we can catch exceptions. fn is a "filename" for error
031: // messages, and may safely be null.
032:
033: private ExportFile(String fn, BufferedReader rdr, String dpnode,
034: ProviderPackageContext ctx) throws ParFileException {
035:
036: m_LineNumber = 0;
037:
038: try {
039: processReader(rdr, dpnode, ctx);
040: } catch (ParFileException ex) {
041:
042: if (m_LineNumber == 0) {
043: throw ex;
044: }
045:
046: Object tok[] = { "" + m_LineNumber, fn };
047:
048: throw new ParFileException("errorExportFile", ex, tok);
049: }
050: }
051:
052: // public static to make an export file.
053:
054: public static ExportFile makeExportFile(String fn, String dpnode,
055: ProviderPackageContext ctx) throws ParFileException {
056: BufferedReader rdr = null;
057:
058: try {
059: if (fn.toLowerCase().startsWith("from:")) {
060: StringBuffer buf = new StringBuffer();
061: StringTokenizer toks = new StringTokenizer(fn, ";");
062: while (toks.hasMoreTokens()) {
063: buf.append(toks.nextToken());
064: buf.append("\n");
065: }
066: rdr = new BufferedReader(new StringReader(buf
067: .toString()));
068: } else {
069: rdr = new BufferedReader(new FileReader(fn));
070: }
071: } catch (Exception ex) {
072: throw new ParFileException("errorExportOpen", ex);
073: }
074:
075: return new ExportFile(fn, rdr, dpnode, ctx);
076: }
077:
078: public static ExportFile makeExportFile(BufferedReader rdr,
079: String dpnode, ProviderPackageContext ctx)
080: throws ParFileException {
081: return new ExportFile(null, rdr, dpnode, ctx);
082: }
083:
084: public void addEntry(ParFileBuilder pfb) throws ParFileException {
085: pfb.addDPEntry(m_Doc, m_PPF, m_Op);
086: }
087:
088: public String getDPEntryName() throws ParFileException {
089: return Par.findDPEntryName(m_Doc);
090: }
091:
092: private void processReader(BufferedReader rdr, String dpnode,
093: ProviderPackageContext ctx) throws ParFileException {
094:
095: m_PPF.clear();
096: m_Op = null;
097: m_Doc = null;
098:
099: String objname = null;
100: String entryname = null;
101: int fromTypes = 0;
102: int fileDefType = ExtractOp.TYPE_CHANNEL;
103: int classDefType = ExtractOp.TYPE_PROVIDER;
104: StringBuffer dbuf = new StringBuffer();
105: String dpfx = "";
106: boolean defop = true;
107: String rec;
108:
109: for (m_LineNumber = 1; (rec = readLine(rdr)) != null; ++m_LineNumber) {
110:
111: StringTokenizer st = new StringTokenizer(rec, ": \t");
112: if (!st.hasMoreTokens()) {
113: continue;
114: }
115:
116: String label = st.nextToken();
117:
118: if (label.startsWith("#")) {
119: continue;
120: }
121:
122: if (label.equalsIgnoreCase("from")) {
123: fromTypes = typeArg(st, 0);
124:
125: // We default file and directory to type "channel" if no type is given,
126: // according to spec, but if channel type isn't present that makes no sense.
127: // class default type is the other way around.
128:
129: fileDefType = (fromTypes & ExtractOp.TYPE_CHANNEL) != 0 ? ExtractOp.TYPE_CHANNEL
130: : ExtractOp.TYPE_PROVIDER;
131: classDefType = (fromTypes & ExtractOp.TYPE_PROVIDER) != 0 ? ExtractOp.TYPE_PROVIDER
132: : ExtractOp.TYPE_CHANNEL;
133: try {
134: objname = URLDecoder.decode(nextToken(st));
135: } catch (Exception ex) {
136: throw new ParFileException("errorExportURLSyntax",
137: ex);
138: }
139: ctx.setObject(objname, fromTypes);
140: boolean dobasic = true;
141: if (st.hasMoreTokens()) {
142: String x = st.nextToken();
143: if (x.equalsIgnoreCase("nobasic")) {
144: dobasic = false;
145: }
146: }
147: if (dobasic) {
148: ctx.addBasicFiles(m_PPF);
149: }
150: continue;
151: }
152:
153: if (label.equalsIgnoreCase("entry")) {
154: entryname = nextToken(st);
155: continue;
156: }
157:
158: if (label.equalsIgnoreCase("desc")) {
159:
160: // Might be eliminating ":" from description, and losing
161: // multiple whitespace characters. We'll accept that.
162:
163: while (st.hasMoreTokens()) {
164: dbuf.append(dpfx);
165: dbuf.append(st.nextToken());
166: dpfx = " ";
167: }
168: dpfx = "\n";
169:
170: continue;
171: }
172:
173: if (label.equalsIgnoreCase("auto")) {
174:
175: String opstr = nextToken(st);
176: defop = false;
177: if (!opstr.equalsIgnoreCase("none")) {
178: m_Op = ExtractOp.makeOpFromArgument(dpnode, opstr);
179: }
180:
181: continue;
182: }
183:
184: // The rest of these entries depend on the objname having been specified.
185:
186: if (objname == null) {
187: throw new ParFileException("errorExportNoFrom");
188: }
189:
190: if (label.equalsIgnoreCase("file")) {
191:
192: String rootprop = nextToken(st);
193: String path = nextToken(st);
194: int pbty = typeArg(st, fileDefType);
195:
196: if (rootprop.equals(".")) {
197: m_PPF.add(ctx.getStaticFile(path, pbty));
198: } else {
199: m_PPF.add(ctx.getPropLocFile(rootprop, path, pbty));
200: }
201:
202: continue;
203: }
204:
205: if (label.equalsIgnoreCase("class")) {
206:
207: String fullname = nextToken(st);
208: int cty = typeArg(st, classDefType);
209:
210: m_PPF.add(ctx.getClassFile(fullname, cty));
211:
212: continue;
213: }
214:
215: if (label.equalsIgnoreCase("directory")) {
216:
217: String prop = nextToken(st);
218: String dir = nextToken(st);
219: String how = nextToken(st);
220: int types = typeArg(st, fileDefType);
221:
222: if (prop.equals(".")) {
223: prop = null;
224: }
225:
226: if (dir.equals(".")) {
227: dir = null;
228: }
229:
230: if (how.equals(".")) {
231: //addfiles is set to true
232: //ctx.addDirectory(prop,dir,false,false,null,types,null,m_PPF);
233: ctx.addDirectory(prop, dir, false, true, null,
234: types, null, m_PPF);
235: continue;
236: }
237:
238: if (how.equals("+")) {
239: //addfiles is set to true
240: //ctx.addDirectory(prop,dir,true,false,null,types,null,m_PPF);
241: ctx.addDirectory(prop, dir, true, true, null,
242: types, null, m_PPF);
243:
244: continue;
245: }
246:
247: ctx.addDirectory(prop, dir, true, false, how, types,
248: null, m_PPF);
249: continue;
250: }
251:
252: throw new ParFileException("errorExportUnrecognized");
253: }
254:
255: if (objname == null) {
256: throw new ParFileException("errorExportNoFrom");
257: }
258:
259: if (entryname == null) {
260: entryname = objname;
261: }
262:
263: // make sure our entry name doesn't contain characters that are incompatible
264: // with file syntax, since this will ultimately become an xml file. Whitespace
265: // looks stupid, too.
266:
267: StringTokenizer toks = new StringTokenizer(entryname,
268: " \t?[]/\\=+:;\",.");
269: String pfx = "";
270: StringBuffer buf = new StringBuffer();
271: while (toks.hasMoreTokens()) {
272: buf.append(pfx);
273: pfx = "_";
274: buf.append(toks.nextToken());
275: }
276: entryname = buf.toString();
277:
278: if (defop) {
279: if ((fromTypes & ExtractOp.TYPE_PROVIDER) != 0) {
280: m_Op = new ExOProvider(dpnode);
281: if ((fromTypes & ExtractOp.TYPE_CHANNEL) != 0) {
282: String cname = ctx.getContainerName(objname);
283: ExOChannel cop = new ExOChannel(dpnode, cname);
284: cop.setAvail(cname);
285: m_Op.add(cop);
286: }
287: } else {
288: if ((fromTypes & ExtractOp.TYPE_CHANNEL) != 0) {
289: m_Op = new ExOChannel(dpnode, ctx
290: .getContainerName(objname));
291: }
292: }
293: }
294:
295: m_Doc = ctx.getParEntry(entryname, dbuf.length() > 0 ? dbuf
296: .toString() : null);
297: }
298:
299: private static String readLine(BufferedReader rdr)
300: throws ParFileException {
301: try {
302: return rdr.readLine();
303: } catch (Exception ex) {
304: throw new ParFileException("errorExportInput", ex);
305: }
306: }
307:
308: private String nextToken(StringTokenizer st)
309: throws ParFileException {
310: if (!st.hasMoreTokens()) {
311: throw new ParFileException("errorExportTokenCount");
312: }
313:
314: return st.nextToken();
315: }
316:
317: private int typeArg(StringTokenizer st, int def)
318: throws ParFileException {
319:
320: if (!st.hasMoreTokens() && def != 0) {
321: return def;
322: }
323:
324: StringTokenizer typst = new StringTokenizer(nextToken(st), ",+");
325: int ty = 0;
326:
327: while (typst.hasMoreTokens()) {
328: String ts = typst.nextToken();
329: if (ts.equalsIgnoreCase("provider")) {
330: ty |= ExtractOp.TYPE_PROVIDER;
331: continue;
332: }
333: if (ts.equalsIgnoreCase("channel")) {
334: ty |= ExtractOp.TYPE_CHANNEL;
335: continue;
336: }
337: throw new ParFileException("errorExportOp");
338: }
339:
340: return ty;
341: }
342:
343: private Vector m_PPF = new Vector();
344: private ExtractOp m_Op = null;
345: private Document m_Doc = null;
346: private int m_LineNumber = 0;
347: }
|