| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package org.eclipse.swtbot.generator; |
| 15 | |
|
| 16 | |
import java.io.File; |
| 17 | |
import java.io.FileWriter; |
| 18 | |
import java.io.IOException; |
| 19 | |
|
| 20 | |
import javax.xml.parsers.ParserConfigurationException; |
| 21 | |
import javax.xml.parsers.SAXParser; |
| 22 | |
import javax.xml.parsers.SAXParserFactory; |
| 23 | |
|
| 24 | |
import org.xml.sax.Attributes; |
| 25 | |
import org.xml.sax.InputSource; |
| 26 | |
import org.xml.sax.SAXException; |
| 27 | |
import org.xml.sax.helpers.DefaultHandler; |
| 28 | |
|
| 29 | |
public class XmlConfigurator { |
| 30 | |
|
| 31 | |
private final SugarGenerator sugarConfiguration; |
| 32 | |
private final SAXParserFactory saxParserFactory; |
| 33 | |
|
| 34 | 0 | public XmlConfigurator(SugarGenerator sugarConfiguration, ClassLoader classLoader) { |
| 35 | 0 | this.sugarConfiguration = sugarConfiguration; |
| 36 | 0 | saxParserFactory = SAXParserFactory.newInstance(); |
| 37 | 0 | saxParserFactory.setNamespaceAware(true); |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public void load(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException { |
| 41 | 0 | SAXParser saxParser = saxParserFactory.newSAXParser(); |
| 42 | 0 | saxParser.parse(inputSource, new DefaultHandler() { |
| 43 | |
@Override |
| 44 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 45 | 0 | if (localName.equals("widget")) { |
| 46 | 0 | String className = attributes.getValue("class"); |
| 47 | |
try { |
| 48 | 0 | addClass(className); |
| 49 | 0 | } catch (ClassNotFoundException e) { |
| 50 | 0 | throw new SAXException("Cannot find Matcher class : " + className); |
| 51 | |
} |
| 52 | |
} |
| 53 | 0 | } |
| 54 | |
}); |
| 55 | 0 | } |
| 56 | |
|
| 57 | 0 | private void addClass(String className) throws ClassNotFoundException { |
| 58 | 0 | sugarConfiguration.addFactoryMethods(new SWTBotGeneratorFactoryReader(className)); |
| 59 | 0 | sugarConfiguration.addImports(new SWTBotGeneratorFactoryReader(className)); |
| 60 | 0 | } |
| 61 | |
|
| 62 | |
public static void main(String configFile, String fullClassName, String superClass, File outputDir) throws Exception { |
| 63 | |
|
| 64 | 0 | String fileName = fullClassName.replace('.', File.separatorChar) + ".java"; |
| 65 | 0 | int dotIndex = fullClassName.lastIndexOf("."); |
| 66 | 0 | String packageName = dotIndex == -1 ? "" : fullClassName.substring(0, dotIndex); |
| 67 | 0 | String shortClassName = fullClassName.substring(dotIndex + 1); |
| 68 | |
|
| 69 | 0 | if (!outputDir.isDirectory()) { |
| 70 | 0 | System.err.println("Output directory not found : " + outputDir.getAbsolutePath()); |
| 71 | 0 | System.exit(-1); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | File outputFile = new File(outputDir, fileName); |
| 75 | 0 | outputFile.getParentFile().mkdirs(); |
| 76 | |
|
| 77 | 0 | SugarGenerator sugarGenerator = new SugarGenerator(); |
| 78 | |
try { |
| 79 | 0 | sugarGenerator.addWriter(new HamcrestFactoryWriter(packageName, shortClassName, superClass, new FileWriter(outputFile))); |
| 80 | 0 | sugarGenerator.addWriter(new QuickReferenceWriter(System.out)); |
| 81 | |
|
| 82 | 0 | XmlConfigurator xmlConfigurator = new XmlConfigurator(sugarGenerator, XmlConfigurator.class.getClassLoader()); |
| 83 | 0 | xmlConfigurator.load(new InputSource(configFile)); |
| 84 | 0 | System.out.println("Generating " + fullClassName); |
| 85 | 0 | sugarGenerator.generate(); |
| 86 | 0 | } finally { |
| 87 | 0 | sugarGenerator.close(); |
| 88 | 0 | } |
| 89 | 0 | } |
| 90 | |
} |