All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.delivery.sax.SAXElementVisitorMap Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU Lesser General Public License for more details:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.delivery.sax;

import org.milyn.delivery.ContentHandlerConfigMap;
import org.milyn.delivery.VisitLifecycleCleanable;
import org.milyn.delivery.sax.annotation.StreamResultWriter;
import org.milyn.delivery.sax.annotation.TextConsumer;
import org.milyn.util.ClassUtil;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.xpath.SelectorStep;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
 * SAXElement visitor Map.
 * 
 * @author [email protected]
 */
public class SAXElementVisitorMap {

    private List> visitBefores;
    private List> childVisitors;
    private List> visitAfters;
    private List> visitCleanables;
    private boolean accumulateText = false;
    private SAXVisitor acquireWriterFor = null;

    public List> getVisitBefores() {
        return visitBefores;
    }

    public void setVisitBefores(List> visitBefores) {
        this.visitBefores = visitBefores;
    }

    public List> getChildVisitors() {
        return childVisitors;
    }

    public void setChildVisitors(List> childVisitors) {
        this.childVisitors = childVisitors;
    }

    public List> getVisitAfters() {
        return visitAfters;
    }

    public void setVisitAfters(List> visitAfters) {
        this.visitAfters = visitAfters;
    }

    public List> getVisitCleanables() {
        return visitCleanables;
    }

    public void setVisitCleanables(List> visitCleanables) {
        this.visitCleanables = visitCleanables;
    }

    public boolean accumulateText() {
        return accumulateText;
    }

    public SAXVisitor acquireWriterFor() {
        return acquireWriterFor;
    }

    public void initAccumulateText() {
    	// If any of the before/after handlers are marked as text consumers...
        if(getAnnotatedHandler(visitBefores, TextConsumer.class, false) != null) {
            accumulateText = true;
        	return;
        }
        if(getAnnotatedHandler(visitAfters, TextConsumer.class, false) != null) {
            accumulateText = true;
        	return;
        }
    	
    	// If any of the selector steps need access to the fragment text...
        if(visitAfters == null) {
            return;
        }
        for(ContentHandlerConfigMap contentHandlerMap : visitAfters) {
            SmooksResourceConfiguration resourceConfig = contentHandlerMap.getResourceConfig();
            SelectorStep selectorStep = resourceConfig.getSelectorStep();

            if(selectorStep.accessesText()) {
                accumulateText = true;
                break;
            }
        }
    }

    public void initAccumulateText(SAXElementVisitorMap srcMap) {
    	this.accumulateText = (this.accumulateText || srcMap.accumulateText);
    }
    
    public void initAcquireWriterFor(SAXElementVisitorMap srcMap) {
    	if(this.acquireWriterFor == null) {
    		this.acquireWriterFor = srcMap.acquireWriterFor;
    	}
    }
    
    public void initAcquireWriterFor() {
    	acquireWriterFor = getAnnotatedHandler(visitBefores, StreamResultWriter.class, true);
    	if(acquireWriterFor == null) {
        	acquireWriterFor = getAnnotatedHandler(visitAfters, StreamResultWriter.class, true);
    	}
    }

    public SAXElementVisitorMap merge(SAXElementVisitorMap map) {
    	if(map == null) {
    		// No need to merge...
    		return this;
    	}    	
    	
    	SAXElementVisitorMap merge = new SAXElementVisitorMap();
    	
        merge.visitBefores = new ArrayList>();
        merge.childVisitors = new ArrayList>();
        merge.visitAfters = new ArrayList>();
        merge.visitCleanables = new ArrayList>();
        
        merge.visitBefores.addAll(visitBefores);
        merge.visitBefores.addAll(map.visitBefores);
        merge.childVisitors.addAll(childVisitors);
        merge.childVisitors.addAll(map.childVisitors);
        merge.visitAfters.addAll(visitAfters);
        merge.visitAfters.addAll(map.visitAfters);
        merge.visitCleanables.addAll(visitCleanables);
        merge.visitCleanables.addAll(map.visitCleanables);
        
        merge.accumulateText = (accumulateText || merge.accumulateText);

        return merge;
    }

	private  T getAnnotatedHandler(List> handlerMaps, Class annotationClass, boolean checkFields) {
		if(handlerMaps == null) {
			return null;
		}
		
		for(ContentHandlerConfigMap handlerMap : handlerMaps) {
        	T contentHandler = handlerMap.getContentHandler();
			if(contentHandler.getClass().isAnnotationPresent(annotationClass)) {
        		return contentHandler;
        	} else if(checkFields && !ClassUtil.getAnnotatedFields(contentHandler.getClass(), annotationClass).isEmpty()) {
        		return contentHandler;
        	}
        }
		
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy