1 package spoon.support.processing; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.xml.sax.Attributes ; 7 import org.xml.sax.SAXException ; 8 import org.xml.sax.helpers.DefaultHandler ; 9 10 import spoon.AbstractLauncher; 11 import spoon.support.builder.CtResource; 12 13 16 public class SpoonletXmlHandler extends DefaultHandler { 17 18 private AbstractLauncher launcher; 19 20 private List <CtResource> spoonletIndex; 21 22 XmlProcessorProperties prop; 23 24 String propName; 25 26 List <Object > values; 27 28 String buffer; 29 30 35 public SpoonletXmlHandler(AbstractLauncher launcher, 36 List <CtResource> spoonletIndex) { 37 super(); 38 this.launcher = launcher; 39 this.spoonletIndex = spoonletIndex; 40 } 41 42 45 @Override 46 public void endElement(String uri, String localName, String qName) 47 throws SAXException { 48 if (localName.equals("processor")) { 49 launcher.getFactory().getEnvironment().setProcessorProperties( 50 prop.getProcessorName(), prop); 51 prop = null; 52 } else if (localName.equals("property")) { 53 if (values != null) { 54 prop.addProperty(propName, values); 55 } 56 values = null; 57 propName = null; 58 } else if (localName.equals("value")) { 59 values.add(buffer); 60 } 61 buffer = null; 62 super.endElement(uri, localName, qName); 63 } 64 65 68 @Override 69 public void characters(char[] ch, int start, int end) throws SAXException { 70 buffer = new String (ch, start, end); 71 } 72 73 76 @Override 77 public void startElement(String uri, String localName, String qName, 78 Attributes attributes) throws SAXException { 79 if (localName.equals("processor")) { 80 launcher.addProcessor(attributes.getValue("class")); 81 prop = new XmlProcessorProperties(launcher.getFactory(), attributes 82 .getValue("class")); 83 } else if (localName.equals("template")) { 84 String foldername = attributes.getValue("folder"); 85 for (CtResource r : spoonletIndex) { 86 if (r.getName().startsWith(foldername)) { 87 launcher.addTemplateResource(r); 88 } 89 } 90 } else if (localName.equals("property")) { 91 propName = attributes.getValue("name"); 92 if (attributes.getValue("value") != null) { 93 prop.addProperty(propName, attributes.getValue("value")); 94 } else { 95 values = new ArrayList <Object >(); 96 } 97 } 98 } 99 } 100 | Popular Tags |