//SECTION(Begin): Import class libraries //SECTION(Begin): Declares the class libraries which will be imported import java.io.*; import org.apache.regexp.*; //SECTION(End): Import class libraries //SECTION(Begin): Declares main class and method public class xml2gen { // Create a StringBuffer object which will eventually be // used as the final string. This needs to be declared // here so that it is viewable by all of the methods // at all times static StringBuffer msdatDoc = new StringBuffer(); public static void main(String[] args) throws IOException { //SECTION(End): Declares main class and method //SECTION(Begin): Command Line Options //SECTION(Begin): See also: GetOpt.java //SECTION(Begin): This section gets command line options and take some action //(cont.) depending on which options were used boolean inputFileFlag = false; boolean outputFileFlag = false; GetOpt go = new GetOpt("hi:o:"); char c; String inputFile = ""; String outputFile = ""; while ( ( c = go.getopt(args) ) != 0 ) { switch (c) { case 'h': helpAndExit(0); break; case 'i': inputFile = go.optarg(); inputFileFlag = true; break; case 'o': outputFile = go.optarg(); outputFileFlag = true; break; default: System.err.println( "Unknown option in " + args[go.getOptInd() - 1] ); helpAndExit(1); } } // The string buffer "msdatDoc" will be manipulated until it looks like a // correct msdat file if (! inputFileFlag ) helpAndExit(1); //SECTION(End): Command Line Options //SECTION(Begin): Set up variables //SECTION(Begin): This section sets up variables which will be used throughout //(cont.) the rest of the program // Use a BufferedReader to read the input file BufferedReader input_str = new BufferedReader(new FileReader(inputFile)); // The first line of the file is not important for the msdat schema // It's only important for XML // Read it in and then... String inputLine = input_str.readLine(); // Read the first line of input which may be important to the msdat schema inputLine = input_str.readLine(); // Regular Expression variables need to be declcared (and used) in a try/catch block try { // RE for XML comments (Is this still needed? I think so. Add code?) RE comment_re = new RE("^\\s*\\s*$"); // RE for root tag RE msdat_re = new RE("\\s*\\s*"); // RE for a line that contains only white space RE ws_re = new RE("^\\s*$"); // RE for starting Schema Version tag RE sv_re = new RE("^\\s*\\s*$"); // RE for a Table Type tag RE tt_re = new RE("^\\s*\\s*$"); // RE for a Table Attribute tag RE ta_re = new RE("^\\s*\\s*$"); //SECTION(End): Set up variables //SECTION(Begin): Read Lines, Find Patterns, Manipulate String Buffer //SECTION(Begin): Start reading through the lines, looking for patterns, //(cont.) and manipulate the string buffer // This loop will finish when the EOF has been reached in the XML file do { //SUBSECTION(Begin): Table Attribute // See if this is a Table Attribute if ( ta_re.match(inputLine) ) { // If this is a Table Attribute read in the next 7 lines // and send these lines to the tableAttribute method for processing String TAid = input_str.readLine(); String TTid = input_str.readLine(); String TAName = input_str.readLine(); String TAaltName = input_str.readLine(); String TAFormat = input_str.readLine(); String TAIsKey = input_str.readLine(); String Comments = input_str.readLine(); tableAttribute(TAid, TTid, TAName, TAaltName, TAFormat, TAIsKey, Comments); // The next line should be , there is no need to process it inputLine = input_str.readLine(); } //SUBSECTION(End): Table Attribute //SUBSECTION(Begin): Table Type // See if this is a Table Type if ( tt_re.match(inputLine) ) { // If this is a Table Type read in the next 4 lines // and send these lines to the tableType method for processing String TTid = input_str.readLine(); String TTname = input_str.readLine(); String TTAbrev = input_str.readLine(); String Comments = input_str.readLine(); tableType(TTid, TTname, TTAbrev, Comments); // The next line should be , there is no need to process it inputLine = input_str.readLine(); } //SUBSECTION(End): Table Type //SUBSECTION(Begin): Comments // If this is a separate XML comment create a single line comment in // the msdat file. Note: The XML comment should be in the form // of a C-style comment within an XML-style comment to be processed // correctly by chgen if ( comment_re.match(inputLine) ) { msdatDoc.append( regexRepFirstParenPair(inputLine,"^\\s*\\s*$" ) + "\n"); inputLine = input_str.readLine(); } //SUBSECTION(End): Comments //SUBSECTION(Begin): Ignore first and last tag // If it's the or line, discard it if ( msdat_re.match(inputLine) ) { inputLine = input_str.readLine(); continue; } //SUBSECTION(End): Ignore first and last tag //SUBSECTION(Begin): Ignore white space lines // If it only contains white space, discard it if ( ws_re.match(inputLine) ) { inputLine = input_str.readLine(); continue; } //SUBSECTION(End): Ignore white space lines //SUBSECTION(Begin): Schema Version // See if this is a Schema Version if ( sv_re.match(inputLine) ) { // If this is a Schema Version read in the next 7 lines // and send these lines to the SchemaVer method for processing String SVid = input_str.readLine(); String PJid = input_str.readLine(); String schemaFile = input_str.readLine(); String chgen = input_str.readLine(); String chgenVer = input_str.readLine(); String chgenAlt = input_str.readLine(); String Comments = input_str.readLine(); schemaVer(SVid, PJid, schemaFile, chgen, chgenVer, chgenAlt, Comments); // The next line should be , there is no need to process it inputLine = input_str.readLine(); } //SUBSECTION(End): Schema Version // Read in the next line of input from the input file inputLine = input_str.readLine(); // If all of the lines have been read in break out of the loop } while (inputLine != null ); // End of the try/catch block needed to use Regular Expressions } catch (RESyntaxException e) { System.out.println("Caught " + e + "\n"); } //SECTION(End): Read Lines, Find Patterns, Manipulate String Buffer //SECTION(Begin): Finalize Output if ( outputFileFlag ) { BufferedOutputStream xmlOutput = new BufferedOutputStream( new FileOutputStream(outputFile) ); for (int i=0; i < msdatDoc.toString().length(); i++) { xmlOutput.write( msdatDoc.toString().charAt(i) ); } xmlOutput.close(); } else { for (int i=0; i < msdatDoc.toString().length(); i++) { System.out.print( msdatDoc.toString().charAt(i) ); } } //SECTION(End): Finalize Output // end of main } //SECTION(Begin): schemaVer Method protected static void schemaVer(String SVid, String PJid, String schemaFile, String chgen, String chgenVer, String chgenAlt, String Comments) { msdatDoc.append( regexRepFirstParenPair( SVid, "\\s*(SV.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( PJid, "\\s*(PJ.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( schemaFile, "\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( chgen, "\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( chgenVer, "\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( chgenAlt, "\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair( Comments, "\\s*(.*)" ) + "\n"); } //SECTION(End): schemaVer Method //SECTION(Begin): tableType Method protected static void tableType(String TTid, String TTname, String TTAbrev, String Comments) { msdatDoc.append( regexRepFirstParenPair(TTid,"\\s*(TT.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TTname,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TTAbrev,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(Comments,"\\s*(.*)" ) + "\n"); } //SECTION(End): tableType Method //SECTION(Begin): tableAttribute Method protected static void tableAttribute(String TAid, String TTid, String TAName, String TAaltName, String TAFormat, String TAIsKey, String Comments) { msdatDoc.append( regexRepFirstParenPair(TAid,"\\s*(TA.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TTid,"\\s*(TT.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TAName,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TAaltName,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TAFormat,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(TAIsKey,"\\s*(.*)" ) + " "); msdatDoc.append( regexRepFirstParenPair(Comments,"\\s*(.*)" ) + "\n"); } //SECTION(End): tableAttribute Method //SECTION(Begin): regexRepFirstParenPair method protected static String regexRepFirstParenPair(String wholeStr, String matchPat ) { String outputStr = null; try { RE str_re = new RE(matchPat); if ( str_re.match(wholeStr) ) { outputStr = str_re.subst( wholeStr, str_re.getParen(1) ); } } catch ( RESyntaxException e) { System.out.println("Caught " + e + "\n"); } return outputStr; } //SECTION(End): regexRepFirstParenPair method //SECTION(Begin): Help and Exit Method protected static void helpAndExit(int returnValue) { System.err.println("java genxml [-h] [-o outputFile.msdat] -i inputFile.xml"); System.exit(returnValue); } //SECTION(End): Help and Exit Method //End of class }