Coverage Report - org.eclipse.swtbot.generator.XmlConfigurator
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlConfigurator
0%
0/32
0%
0/4
2
XmlConfigurator$1
0%
0/8
0%
0/2
2
 
 1  0
 /**
 2  
  * This file is derived from Hamcrest's  {@link org.hamcrest.generator.config.XmlConfigurator} class.
 3  
  * Portions modified by Ketan Padegaonkar are provided under the EPL.
 4  
  *
 5  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 6  
  * All rights reserved. This program and the accompanying materials
 7  
  * are made available under the terms of the Eclipse Public License v1.0
 8  
  * which accompanies this distribution, and is available at
 9  
  * http://www.eclipse.org/legal/epl-v10.html
 10  
  *
 11  
  * Contributors:
 12  
  *     Ketan Padegaonkar - initial API and implementation
 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")) { //$NON-NLS-1$
 46  0
                                         String className = attributes.getValue("class"); //$NON-NLS-1$
 47  
                                         try {
 48  0
                                                 addClass(className);
 49  0
                                         } catch (ClassNotFoundException e) {
 50  0
                                                 throw new SAXException("Cannot find Matcher class : " + className); //$NON-NLS-1$
 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"; //$NON-NLS-1$
 65  0
                 int dotIndex = fullClassName.lastIndexOf("."); //$NON-NLS-1$
 66  0
                 String packageName = dotIndex == -1 ? "" : fullClassName.substring(0, dotIndex); //$NON-NLS-1$
 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()); //$NON-NLS-1$
 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); //$NON-NLS-1$
 85  0
                         sugarGenerator.generate();
 86  0
                 } finally {
 87  0
                         sugarGenerator.close();
 88  0
                 }
 89  0
         }
 90  
 }