ingo@937: package de.intevation.flys.artifacts; ingo@937: ingo@937: import java.io.File; ingo@937: import java.util.ArrayList; ingo@937: import java.util.HashMap; ingo@937: import java.util.List; ingo@937: import java.util.Map; ingo@937: ingo@937: import javax.xml.xpath.XPathConstants; ingo@937: ingo@937: import org.apache.log4j.Logger; ingo@937: ingo@937: import org.w3c.dom.Document; ingo@937: import org.w3c.dom.Element; ingo@937: import org.w3c.dom.Node; ingo@937: import org.w3c.dom.NodeList; ingo@937: ingo@937: import de.intevation.artifacts.Artifact; ingo@937: import de.intevation.artifacts.ArtifactNamespaceContext; ingo@937: import de.intevation.artifacts.CallContext; ingo@937: import de.intevation.artifacts.Hook; ingo@937: ingo@937: import de.intevation.artifacts.common.utils.Config; ingo@937: import de.intevation.artifacts.common.utils.XMLUtils; ingo@940: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@937: ingo@937: ingo@937: public class CollectionMonitor implements Hook { ingo@937: ingo@940: public static final String XPATH_STATES = "output-defaults/state"; ingo@940: public static final String XPATH_RESULT = "/art:result"; ingo@937: ingo@937: ingo@940: protected Map> states; ingo@937: ingo@937: ingo@937: private static final Logger logger = ingo@937: Logger.getLogger(CollectionMonitor.class); ingo@937: ingo@937: ingo@937: @Override ingo@937: public void setup(Node cfg) { ingo@937: Element config = (Element) cfg; ingo@937: String xlink = config.getAttribute("xlink:href"); ingo@937: xlink = Config.replaceConfigDir(xlink); ingo@937: ingo@937: File file = new File(xlink); ingo@937: ingo@937: if (file == null || !file.exists()) { ingo@937: logger.error("The config file '" + xlink + "' does not exist."); ingo@937: return; ingo@937: } ingo@937: ingo@937: Document outputDefaults = XMLUtils.parseDocument(file); ingo@937: ingo@937: NodeList states = (NodeList) XMLUtils.xpath( ingo@937: outputDefaults, ingo@937: XPATH_STATES, ingo@937: XPathConstants.NODESET); ingo@937: ingo@937: int len = states != null ? states.getLength() : 0; ingo@937: ingo@940: this.states = new HashMap>(len); ingo@937: ingo@937: for (int i = 0; i < len; i++) { ingo@937: Element state = (Element) states.item(i); ingo@937: ingo@940: String stateId = state.getAttribute("id"); ingo@940: String factory = state.getAttribute("artifact-factory"); ingo@937: ingo@940: if (stateId != null && factory != null) { ingo@940: List factories = this.states.get(stateId); ingo@940: ingo@940: if (factories == null) { ingo@940: factories = new ArrayList(); ingo@940: this.states.put(stateId, factories); ingo@940: } ingo@940: ingo@940: factories.add(factory); ingo@937: } ingo@937: } ingo@937: } ingo@937: ingo@937: ingo@937: @Override ingo@940: public void execute(Artifact artifact, CallContext context, Document doc) { ingo@937: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@937: ingo@937: String stateId = flys.getCurrentStateId(); ingo@937: ingo@940: List factories = states.get(stateId); ingo@937: ingo@940: if (factories == null || factories.isEmpty()) { ingo@937: return; ingo@937: } ingo@937: ingo@940: logger.info("Found " + factories.size() + " recommended artifacts."); ingo@937: ingo@940: appendRecommendations(doc, factories); ingo@937: } ingo@937: ingo@937: ingo@940: protected void appendRecommendations(Document doc, List factories) { ingo@940: Element result = (Element) XMLUtils.xpath( ingo@940: doc, ingo@940: XPATH_RESULT, ingo@940: XPathConstants.NODE, ingo@940: ArtifactNamespaceContext.INSTANCE); ingo@937: ingo@940: ElementCreator creator = new ElementCreator( ingo@940: doc, ingo@940: ArtifactNamespaceContext.NAMESPACE_URI, ingo@940: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@937: ingo@940: Element recommended = creator.create("recommended-artifacts"); ingo@937: ingo@940: for (String factory: factories) { ingo@940: Element fac = creator.create("artifact-factory"); ingo@940: creator.addAttr(fac, "name", factory); ingo@940: ingo@940: recommended.appendChild(fac); ingo@937: } ingo@937: ingo@940: result.appendChild(recommended); ingo@937: } ingo@937: } ingo@937: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :