001    package org.kuali.maven.plugins.graph.sanitize;
002    
003    import java.util.List;
004    import java.util.Map;
005    
006    import org.kuali.maven.plugins.graph.pojo.MavenContext;
007    import org.kuali.maven.plugins.graph.pojo.State;
008    import org.kuali.maven.plugins.graph.tree.Node;
009    import org.kuali.maven.plugins.graph.tree.TreeHelper;
010    import org.slf4j.Logger;
011    import org.slf4j.LoggerFactory;
012    
013    public abstract class MavenContextSanitizer implements NodeSanitizer<MavenContext> {
014        private static final Logger logger = LoggerFactory.getLogger(MavenContextSanitizer.class);
015    
016        TreeHelper helper = new TreeHelper();
017    
018        Map<String, MavenContext> included;
019        State state;
020    
021        public MavenContextSanitizer() {
022            this(null, null);
023        }
024    
025        public MavenContextSanitizer(Map<String, MavenContext> included, State state) {
026            super();
027            this.included = included;
028            this.state = state;
029        }
030    
031        protected abstract void sanitize(MavenContext context, Map<String, MavenContext> included);
032    
033        @Override
034        public void sanitize(Node<MavenContext> node) {
035            List<MavenContext> contexts = helper.getList(node, state);
036            for (MavenContext context : contexts) {
037                State elementState = context.getState();
038                if (state.equals(elementState)) {
039                    sanitize(context, included);
040                }
041            }
042        }
043    
044        protected void warnAndSwitch(State switchTo, String artifactId, MavenContext context, String warning) {
045            logger.warn(getSwitchMessage(artifactId, switchTo));
046            logger.warn(warning);
047            context.setState(switchTo);
048        }
049    
050        protected String getSwitchMessage(String artifactId, State switched) {
051            return getState() + "->" + switched + " " + artifactId;
052        }
053    
054        public Map<String, MavenContext> getIncluded() {
055            return included;
056        }
057    
058        public void setIncluded(Map<String, MavenContext> included) {
059            this.included = included;
060        }
061    
062        public State getState() {
063            return state;
064        }
065    
066        public void setState(State state) {
067            this.state = state;
068        }
069    }