package net.sf.plugfy.verifier.spring;

import net.sf.plugfy.verifier.VerificationContext;
import net.sf.plugfy.verifier.violations.SpringViolation;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * Helper class for parsing the spring bean configurations
 *  
 * @author markus
 */
class SpringVerifierContentHandler extends DefaultHandler {

    private VerificationContext context;
    
    private String file;

    /**
     * Create a handler that relies for checks on the given context
     * 
     * @param context
     * @param name 
     */
    public SpringVerifierContentHandler(VerificationContext context, String name) {
        this.context = context;
        this.file = name;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if("bean".equals(qName)) {
            // check if bean class is available
            checkAvailabilityOfBeanClass(attributes);
            // register bean in context
            String beanId = attributes.getValue("id");
            String beanClazz = attributes.getValue("class");
            this.context.registerBean(beanId, beanClazz);
        }
        if("property".equals(qName)) {
            // check referenced bean in context
            String beanId = attributes.getValue("ref");
            this.context.requireBean(SpringViolation.create(this.file, beanId, null));
        }
    }

    protected void checkAvailabilityOfBeanClass(Attributes attributes) {
        String beanClazz = attributes.getValue("class");
        String beanId = attributes.getValue("id");
        if(beanClazz != null) {
            try {
                context.getClassLoader().loadClass(beanClazz);
            } catch (ClassNotFoundException e) {
                context.getResult().add(SpringViolation.create(this.file, beanId, beanClazz));
            }
        }
    }
    
}

@@ -1281,32 +1281,42 @@
 z);%0D%0A        %7D%0D%0A
+        %0D%0A
         if(%22prop
@@ -1540,24 +1540,465 @@
 %0A        %7D%0D%0A
+        %0D%0A    %7D%0D%0A%0D%0A    protected void checkAvailabilityOfBeanClass(Attributes attributes) %7B%0D%0A        checkBeanClass(attributes);%0D%0A        checkBeanFactory(attributes);%0D%0A    %7D%0D%0A%0D%0A    protected void checkBeanFactory(Attributes attributes) %7B%0D%0A        String beanFactory = attributes.getValue(%22factory-bean%22);%0D%0A        if(beanFactory != null) %7B%0D%0A            this.context.requireBean(SpringViolation.create(file, beanFactory, null));%0D%0A        %7D%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2014,38 +2014,24 @@
 d void check
-AvailabilityOf
 BeanClass(At

handle bean-factory attribute