Project

General

Profile

1
// No copyright, no warranty; use as you will.
2
// Written by Ronald Bourret, Technical University of Darmstadt, 1998-9
3

    
4
import de.tudarmstadt.ito.schemas.dtd.DTD;
5
import de.tudarmstadt.ito.schemas.converters.DDMLToDTD;
6
import de.tudarmstadt.ito.schemas.converters.DTDToDDML;
7
import de.tudarmstadt.ito.schemas.converters.SubsetToDTD;
8
import java.io.File;
9
import java.io.FileOutputStream;
10
import java.io.OutputStream;
11
import java.util.Hashtable;
12
import oracle.xml.parser.XMLParser;
13
import org.xml.sax.InputSource;
14
import org.xml.sax.Parser;
15

    
16
/**
17
 * This application converts a schema document to a DTD and vice versa.
18
 * Currently, it only supports conversions from DDML documents to DTDs and
19
 * vice versa. To use other schema languages, you need to write the appropriate
20
 * converters. (It should take a day or less for each direction assuming you
21
 * know the schema language well and don't support the more exotic stuff such
22
 * as schema reuse features.)
23
 *
24
 * <P>The command line syntax for this application is:</P>
25
 * <PRE>
26
 *    java GenerateMap <input-file>
27
 * </PRE>
28
 */
29

    
30
public class ConvertSchema
31
{
32
   public static void main (String[] argv)
33
   {
34
      try
35
      {
36
         if (argv.length != 1)
37
         {
38
            System.out.println("\nUsage: java ConvertSchema <schema-file>\n");
39
            return;
40
         }
41

    
42
         convert(argv[0]);
43
      }
44
      catch (Exception e)
45
      {
46
         e.printStackTrace();
47
      }
48
   }
49

    
50
   static void convert(String filename)
51
      throws Exception
52
   {
53
      InputSource src;
54
      String      extension, basename;
55

    
56
      // Create an InputSource over the file.
57

    
58
      src = new InputSource(getFileURL(filename));
59

    
60
      // Get the base name and file extension.
61

    
62
      extension = getExtension(filename);
63
      basename = getBasename(filename);
64

    
65
      // Determine the document type from its extension: .DDM for DDML and .DTD
66
      // for external subsets.
67

    
68
      if (extension.equals("DDM"))
69
      {
70
         // DDML Document
71
         convertDDMLToDTD(src, basename);
72
      }
73
      else if (extension.equals("DTD"))
74
      {
75
         // External subset (DTD)
76
         convertDTDToDDML(src, basename);
77
      }
78
      else
79
      {
80
         throw new Exception("Unrecognized file extension: " + extension);
81
      }
82
   }
83

    
84
   static void convertDDMLToDTD(InputSource src, String basename)
85
      throws Exception
86
   {
87
      DDMLToDTD        ddmlToDTD;
88
      DTD              dtd;
89
      FileOutputStream dtdFile;
90

    
91
      // Create the DTD file.
92

    
93
      dtdFile = new FileOutputStream(basename + ".dtd");
94

    
95
      // Convert the DDML document to a DTD object, then serialize
96
      // the DTD object to the DTD file.
97
      //
98
      // Note that this is hard-coded to use the Oracle version 1 parser.
99

    
100
      ddmlToDTD = new DDMLToDTD(new XMLParser());
101
      dtd = ddmlToDTD.convert(src);
102
      dtd.serialize(dtdFile, true);
103

    
104
      // Close the DTD file.
105

    
106
      dtdFile.close();
107
   }
108

    
109
   static void convertDTDToDDML(InputSource src, String basename)
110
      throws Exception
111
   {
112
      SubsetToDTD      subsetToDTD;
113
      DTDToDDML        dtdToDDML;
114
      DTD              dtd;
115
      FileOutputStream ddmlFile;
116
      Hashtable        namespaceURIs = null;
117

    
118
      // Create the DDML file.
119

    
120
      ddmlFile = new FileOutputStream(basename + ".ddm");
121

    
122
      // Convert the DTD to a DTD object, then serialize the DTD object
123
      // as a DDML document.
124

    
125
      // If there are namespace prefixes in your DTD, you need to declare
126
      // them here, putting each one in the namespaceURIs hashtable with
127
      // the prefix as the key and the namespace URI as the value. For
128
      // example, the following commented-out lines state that the DDML
129
      // prefix corresponds to the http://www.purl.org/NET/ddml/v1 URI.
130
      // Note that the default namespace can be declared by adding an
131
      // empty string ("") as a key.
132

    
133
//      namespaceURIs = new Hashtable();
134
//      namespaceURIs.put("DDML", "http://www.purl.org/NET/ddml/v1");
135

    
136
      subsetToDTD = new SubsetToDTD();
137
      dtd = subsetToDTD.convertExternalSubset(src, namespaceURIs);
138
      dtdToDDML = new DTDToDDML();
139
      dtdToDDML.convert(dtd, ddmlFile, true, 3);
140

    
141
      // Close the DDML file.
142

    
143
      ddmlFile.close();
144
   }
145

    
146
   static String getFileURL(String fileName)
147
   {
148
      File   file;
149
      String path, separator;
150

    
151
      // Construct a file URL for the file.
152

    
153
      file = new File(fileName);
154
      return "file:///" + file.getAbsolutePath();
155
   }
156

    
157
   static String getBasename(String filename)
158
   {
159
      int    period;
160

    
161
      // Get the basename of the file.
162

    
163
      period = filename.lastIndexOf('.', filename.length());
164
      if (period == -1)
165
      {
166
         return filename;
167
      }
168
      else
169
      {
170
         return filename.substring(0, period);
171
      }
172
   }
173

    
174
   static String getExtension(String filename)
175
   {
176
      int    period;
177

    
178
      // Get the file extension.
179

    
180
      period = filename.lastIndexOf('.', filename.length());
181
      if (period == -1)
182
      {
183
         return "";
184
      }
185
      else
186
      {
187
         return filename.substring(period + 1, filename.length()).toUpperCase();
188
      }
189
   }
190

    
191
}
192

    
(2-2/14)