KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > s2b > FormatDtd


1 package s2b;
2
3 import java.util.regex.Pattern JavaDoc;
4 import java.util.regex.Matcher JavaDoc;
5 import java.io.BufferedReader JavaDoc;
6 import java.io.BufferedWriter JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.File JavaDoc;
10
11 /*
12  * FormatDtd.java
13  *
14  * Created on June 13, 2002, 2:29 PM
15  */

16
17 /**
18  *
19  * @author sridatta
20  */

21 public class FormatDtd {
22
23     /** Creates a new instance of FormatDtd */
24     public FormatDtd() {
25     }
26
27     private static final String JavaDoc USAGE =
28         "java s2b.FormatDtd <originaldtd> <newdtd>";
29     
30     public static void usage() {
31         System.out.println(USAGE);
32         System.exit(1);
33     }
34     
35     private static void generate(String JavaDoc origDtd, String JavaDoc genDtd) {
36         BufferedReader JavaDoc fis = null;
37         BufferedWriter JavaDoc fos = null;
38         try {
39             FileReader JavaDoc f = new FileReader JavaDoc(origDtd);
40             fis = new BufferedReader JavaDoc(f);
41             FileWriter JavaDoc outF = new FileWriter JavaDoc(genDtd);
42             fos = new BufferedWriter JavaDoc(outF);
43             String JavaDoc line = null;
44             String JavaDoc outLine = null;
45             while((line=fis.readLine()) != null) {
46                 //remove entities
47
if(line == null) {
48                     continue;
49                 }
50                 if (line.startsWith("<!ENTITY")) {
51                     if (line.endsWith(">")){ // Entity on one line -
52
// skip it
53
continue;
54                     } else { // Entity on 2 lines skip this and next
55
fis.readLine();
56                         continue;
57                     }
58                 }
59                 
60                 outLine = line;
61                 outLine = line.replaceAll("property", "element-property");
62                 outLine = outLine.replaceAll("system-element-property", "system-property");
63                 outLine = outLine.replaceAll("%.*;", "CDATA ");
64                 outLine = outLine.replaceAll("\t", " ");
65 // outLine = outLine.replaceAll("CDATA(\\s+)(\")([\\S&&[^\"]]+)(\\s)([\\S&&[^\"]]+)(.*)(>)", "CDATA \"FIXME_NOW\">");
66
// outLine = outLine.replaceAll("CDATA(\\s+)(\")([\\S&&[^\"]]+)(\\s)([\\S&&[^\"]]+)(.*)", "CDATA \"FIXME_NOW\"");
67
fos.write(outLine);
68                 fos.write("\n");
69             }
70         } catch(Exception JavaDoc e) {
71             e.printStackTrace();
72         } finally {
73             try {
74                 fis.close();
75             } catch(Exception JavaDoc e){}
76             
77             try {
78                 fos.close();
79             } catch(Exception JavaDoc e){}
80         }
81     }
82     /**
83     * @param args the command line arguments
84     */

85     public static void main (String JavaDoc args[]) {
86
87         if(args.length != 2)
88             usage();
89
90         String JavaDoc origDtd = args[0];
91         String JavaDoc genDtd = args[1];
92         
93         System.out.println("origdtd " + origDtd);
94         System.out.println("path " + genDtd);
95         generate(origDtd, genDtd);
96     }
97
98 }
99
Popular Tags