1 package jsdsi;
2
3 import java.io.EOFException;
4 import java.io.FileInputStream;
5 import java.io.InputStreamReader;
6 import java.io.OutputStreamWriter;
7
8 import jsdsi.sexp.SexpInput;
9 import jsdsi.sexp.SexpInputStream;
10 import jsdsi.sexp.SexpOutput;
11 import jsdsi.sexp.SexpOutputStream;
12
13 import jsdsi.xml.XmlReader;
14 import jsdsi.xml.XmlWriter;
15
16 /***
17 * Reads a file containing S-expressions or XML and outputs its contents
18 * in canonical, readable, transport, or XML format to stdout.
19 *
20 * XML input currently fails on cache1.xml because XML doesn't allow
21 * multiple top-level elements in a file. Try it on cert.xml instead.
22 *
23 * @author Sameer Ajmani
24 * @version $Revision: 1.2.6.1 $ $Date: 2005/11/08 03:12:52 $
25 */
26 class S2X {
27 public static void main(String[] args) throws Exception
28 {
29 if (args.length < 3) {
30 System.err.println("usage: java S2X input-file "
31 + "[sexp|xml] [xml|canon|read|xprt]");
32 return;
33 }
34 FileInputStream fin = new FileInputStream(args[0]);
35 final String inFormat = args[1];
36 final String outFormat = args[2];
37 SexpInput in = null;
38 if (inFormat.equals("sexp")) {
39 in = new SexpInputStream(fin);
40 } else if (inFormat.equals("xml")) {
41 in = new XmlReader(new InputStreamReader(fin));
42 } else {
43 throw new Error("Unrecognized inFormat: "+inFormat);
44 }
45 SexpOutput out = null;
46 if (outFormat.equals("xml")) {
47 out = new XmlWriter(new OutputStreamWriter(System.out));
48 } else if (outFormat.equals("canon")) {
49 out = new SexpOutputStream(System.out).toCanonical();
50 } else if (outFormat.equals("read")) {
51 out = new SexpOutputStream(System.out).toReadable(0, 72, 0);
52 } else if (outFormat.equals("xprt")) {
53 out = new SexpOutputStream(System.out).toTransport();
54 } else {
55 throw new Error("Unrecognized outFormat: "+outFormat);
56 }
57 try {
58 while (true) {
59 out.writeSexp(in.readSexp());
60 out.flush();
61 System.out.println();
62 // looping causes an IOException for xml input,
63 // because it only expects one document per file.
64 if (inFormat.equals("xml")) break;
65 }
66 } catch (EOFException e) {
67 out.flush();
68 }
69 System.err.println("done");
70 }
71 }
This page was automatically generated by Maven