File indexing completed on 2024-12-15 04:19:55

0001 package mso.generator;
0002 
0003 import java.io.File;
0004 import java.io.FileInputStream;
0005 import java.io.IOException;
0006 import java.io.InputStream;
0007 import java.util.Properties;
0008 
0009 import javax.xml.parsers.DocumentBuilderFactory;
0010 import javax.xml.parsers.ParserConfigurationException;
0011 import javax.xml.transform.Source;
0012 import javax.xml.transform.stream.StreamSource;
0013 import javax.xml.validation.Schema;
0014 import javax.xml.validation.SchemaFactory;
0015 import javax.xml.validation.Validator;
0016 
0017 import mso.generator.utils.MSO;
0018 
0019 import org.w3c.dom.Document;
0020 import org.xml.sax.SAXException;
0021 
0022 public class ParserGeneratorRunner {
0023 
0024     static final String version = getVersion();
0025 
0026     private static String getVersion() {
0027         String version = "unknown";
0028         Properties p = new Properties();
0029         try {
0030             InputStream in = open("version.properties");
0031             p.load(in);
0032             in.close();
0033             version = p.getProperty("version");
0034         } catch (Exception e) {
0035             e.printStackTrace();
0036         }
0037         return version;
0038     }
0039 
0040     public static void main(String[] args) throws Exception {
0041         final String xsdfilename = "mso.xsd";
0042 
0043         new File("src/mso/javaparser").mkdirs();
0044 
0045         final Validator v = createValidator(xsdfilename);
0046 
0047         // pgr.generate(v, "pdf.xml", "PDF", "cpp");
0048         generate(v, "mso.xml", "MSO", "cpp");
0049     }
0050 
0051     private static void generate(Validator v, String xmlfilename,
0052             String namespace, String cppdir) throws SAXException, IOException,
0053                     ParserConfigurationException, Exception {
0054         v.validate(new StreamSource(open(xmlfilename)));
0055         final Document dom = DocumentBuilderFactory.newInstance()
0056                 .newDocumentBuilder().parse(open(xmlfilename));
0057         MSO mso = MSO.parse(dom);
0058         new File(cppdir).mkdirs();
0059         generateJavaParser(mso, namespace);
0060         generateQtParser(mso, "", "generatedclasses", cppdir);
0061         generateSimpleQtParser(mso, namespace, "simpleParser", cppdir);
0062         generateApi(mso, namespace, "api", cppdir);
0063     }
0064 
0065     private static void generateJavaParser(MSO mso, String namespace)
0066             throws IOException {
0067         final JavaParserGenerator g = new JavaParserGenerator();
0068         String l = namespace.toLowerCase();
0069         String il = namespace.substring(0, 1) + l.substring(1);
0070         g.generate(mso, "src", l + ".javaparser", "Generated" + il + "Parser");
0071     }
0072 
0073     private static void generateQtParser(MSO mso, String namespace,
0074             String basename, String outputdir) throws IOException {
0075         // generate a parser with introspection
0076         final QtParserGenerator g = new QtParserGenerator(namespace, basename,
0077                 outputdir);
0078         g.config.createHeader = false;
0079         g.config.enableIntrospection = true;
0080         g.config.enableXml = true;
0081         g.config.enableWriting = true;
0082         g.config.enableToString = true;
0083         g.config.enableStyleTextPropAtomFix = false;
0084         g.generate(mso);
0085     }
0086 
0087     private static void generateSimpleQtParser(MSO mso, String namespace,
0088             String basename, String outputdir) throws IOException {
0089         // generate a parser with introspection
0090         final QtParserGenerator g = new QtParserGenerator(namespace, basename,
0091                 outputdir);
0092         g.config.createHeader = true;
0093         g.config.enableIntrospection = false;
0094         g.config.enableXml = false;
0095         g.config.enableWriting = false;
0096         g.config.enableToString = false;
0097         g.config.enableStyleTextPropAtomFix = true;
0098         g.generate(mso);
0099     }
0100 
0101     private static void generateApi(MSO mso, String namespace, String basename,
0102             String outputdir) throws IOException {
0103         // generate a minimal parser but with a public header
0104         final QtApiGenerator g = new QtApiGenerator(namespace, basename,
0105                 outputdir);
0106         g.config.createHeader = true;
0107         g.generate(mso);
0108     }
0109 
0110     private static InputStream open(String name) throws Exception {
0111         File f = new File(name);
0112         if (f.canRead()) {
0113             System.out.println("Reading " + f.getAbsolutePath());
0114             return new FileInputStream(f.getAbsolutePath());
0115         }
0116         f = new File("src/" + name);
0117         if (f.canRead()) {
0118             System.out.println("Reading " + f.getAbsolutePath());
0119             return new FileInputStream(f.getAbsolutePath());
0120         }
0121         System.out.println("Reading " + name + " from jar file.");
0122         return ParserGeneratorRunner.class.getResourceAsStream("/" + name);
0123     }
0124 
0125     private static Validator createValidator(String xsdfilename)
0126             throws Exception {
0127         // define the type of schema - we use W3C:
0128         String schemaLang = "http://www.w3.org/2001/XMLSchema";
0129 
0130         // get validation driver:
0131         SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
0132 
0133         Source sources[] = new Source[1];
0134         sources[0] = new StreamSource(open(xsdfilename));
0135         Schema schema = factory.newSchema(sources);
0136         return schema.newValidator();
0137     }
0138 }