1 46 package org.codehaus.groovy.tools.xml; 47 48 import java.io.File ; 49 import java.io.FileWriter ; 50 import java.io.IOException ; 51 import java.io.PrintWriter ; 52 import java.net.URL ; 53 54 import javax.xml.parsers.DocumentBuilder ; 55 import javax.xml.parsers.DocumentBuilderFactory ; 56 57 import junit.framework.TestCase; 58 59 import org.w3c.dom.Document ; 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.SAXException ; 62 63 67 public class DomToGroovyTest extends TestCase { 68 69 protected DocumentBuilder builder; 70 protected DomToGroovy converter; 71 protected File dir = new File ("target/generated-groovyxml"); 72 73 public void testConversion() throws Exception { 74 convert("test1.xml", "test1.groovy"); 75 convert("po.xsd", "poSchema.groovy"); 76 convert("swing.xml", "swing.groovy"); 77 } 78 79 protected void convert(String name, String output) throws Exception { 80 Document document = parse(name); 81 82 PrintWriter writer = new PrintWriter (new FileWriter (new File (dir, output))); 83 converter = new DomToGroovy(writer); 84 85 writer.println("#!/bin/groovy"); 86 writer.println(); 87 writer.println("// generated from " + name); 88 writer.println(); 89 converter.print(document); 90 writer.close(); 91 } 92 93 protected Document parse(String name) throws SAXException , IOException { 94 URL resource = getClass().getResource(name); 95 assertTrue("Could not find resource: " + name, resource != null); 96 return builder.parse(new InputSource (resource.toString())); 97 } 98 99 protected void setUp() throws Exception { 100 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 101 factory.setNamespaceAware(true); 102 builder = factory.newDocumentBuilder(); 103 104 dir.mkdirs(); 105 } 106 107 } 108 | Popular Tags |