001: /*
002: * cksoif - Reads in templates from stdin and prints whether or not
003: * it's a legal SOIF template.
004: *
005: * Usage: cksoif
006: */
007: package com.sun.portal.search.soif;
008:
009: import com.sun.portal.search.util.*;
010: import java.io.*;
011: import java.util.*;
012:
013: class CheckSOIF {
014:
015: static void usage() {
016: System.out
017: .print("Usage: CheckSOIF [-qacvio] [infile] [outfile]\n"
018: + " -q quiet\n"
019: + " -a don't dump attributes\n"
020: + " -c don't dump stats (attribute counts, content size, etc)\n"
021: + " -v dump attribute values\n"
022: + " -i input SOIF character encoding\n"
023: + " -o output SOIF character encoding\n");
024: }
025:
026: public static void main(String[] args) throws Exception {
027:
028: String ifn = null;
029: String ofn = null;
030:
031: SOIFInputStream cx = null;
032: SOIFOutputStream ocx = null;
033: SOIF s;
034:
035: int n = 0, nok = 0;
036: boolean cntflag = true, attrsflag = true, quiet = false, valflag = false;
037: String iencoding = SOIF.defaultEncoding;
038: String oencoding = SOIF.defaultEncoding;
039:
040: Getopt gopt = new Getopt(args, "qacvi:o:");
041:
042: int c;
043: while ((c = gopt.getopt()) != -1) {
044: switch (c) {
045: case 'q':
046: quiet = true;
047: break;
048: case 'a':
049: attrsflag = false;
050: break;
051: case 'c':
052: cntflag = false;
053: break;
054: case 'v':
055: valflag = true;
056: break;
057: case 'i':
058: iencoding = gopt.optArg;
059: break;
060: case 'o':
061: oencoding = gopt.optArg;
062: break;
063: default:
064: usage();
065: System.exit(1);
066: }
067: }
068:
069: // do we have an input file name?
070: if ((args.length - gopt.optInd) > 0)
071: ifn = args[gopt.optInd++];
072:
073: // do we have an output file name?
074: if ((args.length - gopt.optInd) > 0)
075: ofn = args[gopt.optInd++];
076:
077: if (args.length - gopt.optInd > 0) {
078: usage();
079: System.exit(1);
080: }
081:
082: if (ifn != null)
083: if (ifn.equals("-"))
084: cx = new SOIFInputStream(System.in, iencoding);
085: else
086: cx = new SOIFInputStream(ifn, iencoding);
087:
088: if (ofn != null)
089: if (ofn.equals("-"))
090: ocx = new SOIFOutputStream(System.out, oencoding);
091: else
092: ocx = new SOIFOutputStream(ofn, oencoding);
093:
094: n = nok = 0;
095:
096: for (;;) {
097:
098: n++;
099:
100: try {
101: s = cx.readSOIF();
102: } catch (Exception e) {
103: System.out.println("Attempt " + n + ":\t " + e);
104: continue;
105: }
106:
107: if (s == null)
108: break;
109:
110: if (!quiet) {
111: System.out.println("Attempt " + n + ":\tValid SOIF");
112: System.out
113: .println("Attempt " + n + ":\tSOIF analysis:");
114: System.out.println(" URL:\t" + s.getURL());
115: System.out.println(" Schema-Name:\t"
116: + s.getSchemaName());
117:
118: if (cntflag) {
119: int ac = s.getAttributeCount();
120: //int as = s.getAttributeSize());
121: //int vc = s.getValueCount());
122: //int vs = s.getValueSize());
123: //int ts = s.getTotalSize());
124: int cs = s.contentSize();
125:
126: if (!quiet) {
127: System.out.println(" AttributeCount:\t" + ac);
128: //System.out.println(" AttributeSize:\t" + as);
129: //System.out.println(" ValueCount:\t" + vc);
130: //System.out.println(" ValueSize:\t" + vs);
131: //System.out.println(" TotalSize:\t" + ts);
132: System.out.println(" ContentSize:\t" + cs);
133: }
134: }
135: if (attrsflag) {
136: Set attrs;
137: int cnt;
138: if (!quiet)
139: System.out.println(" Attributes:\t");
140: if ((attrs = s.getAttributes()) != null) {
141: cnt = attrs.size();
142: if (cnt == 0) {
143: if (!quiet)
144: System.out.println("EMPTY");
145: } else {
146: //Arrays.sort(attrs, cnt+1, sizeof(String ), qsort_cmp);
147: for (Iterator i = attrs.iterator(); i
148: .hasNext();) {
149: String att = (String) i.next();
150: if (!quiet)
151: System.out.print(att + " ");
152: if (valflag)
153: System.out.println("= "
154: + s.getValue(att));
155: }
156: if (!quiet)
157: System.out.println();
158: }
159: } else {
160: if (!quiet)
161: System.out.println("EMPTY");
162: }
163: }
164: }
165:
166: if (ocx != null)
167: ocx.write(s);
168: String pt = s.getValue("partial-text");
169: nok++;
170: }
171: System.out.println("Successfully parsed " + nok
172: + " SOIF objects, in " + (n - 1) + " attempts");
173: if (ocx != null)
174: ocx.close();
175: }
176:
177: }
|