ingo@346: package de.intevation.flys.collections;
ingo@346: 
ingo@346: import java.util.HashMap;
ingo@346: import java.util.Map;
ingo@346: 
ingo@346: import javax.xml.xpath.XPathConstants;
ingo@346: 
ingo@346: import org.apache.log4j.Logger;
ingo@346: 
ingo@346: import org.w3c.dom.Document;
ingo@694: import org.w3c.dom.Element;
ingo@346: import org.w3c.dom.Node;
ingo@346: import org.w3c.dom.NodeList;
ingo@346: 
ingo@346: import de.intevation.artifacts.ArtifactNamespaceContext;
ingo@346: 
ingo@346: import de.intevation.artifactdatabase.state.DefaultOutput;
ingo@346: import de.intevation.artifactdatabase.state.Output;
ingo@346: 
ingo@346: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@346: 
ingo@346: import de.intevation.flys.artifacts.model.ManagedFacet;
ingo@945: import de.intevation.flys.artifacts.model.ManagedDomFacet;
ingo@346: 
ingo@346: 
ingo@346: public class AttributeParser {
ingo@346: 
felix@1624:     /** Constant XPath that points to the outputmodes of an artifact. */
ingo@346:     public static final String XPATH_ARTIFACT_OUTPUTMODES =
ingo@638:         "/art:attribute/art:outputs/art:output";
ingo@346: 
ingo@346: 
ingo@346:     private static Logger logger = Logger.getLogger(AttributeParser.class);
ingo@346: 
ingo@346: 
ingo@346:     protected Map<String, Output> outs;
ingo@346: 
ingo@346: 
ingo@346:     public AttributeParser() {
ingo@346:         this.outs = new HashMap<String, Output>();
ingo@346:     }
ingo@346: 
ingo@346: 
ingo@346:     public void parse(Document doc) {
ingo@346:         logger.debug("AttributeParser.parse");
ingo@346: 
ingo@346:         NodeList outs = (NodeList) XMLUtils.xpath(
ingo@346:             doc,
ingo@346:             XPATH_ARTIFACT_OUTPUTMODES,
ingo@346:             XPathConstants.NODESET,
ingo@346:             ArtifactNamespaceContext.INSTANCE);
ingo@346: 
ingo@346:         int num = outs != null ? outs.getLength() : 0;
ingo@346: 
ingo@346:         logger.debug("Attribute has " + num + " outputs.");
ingo@346: 
ingo@346:         for (int i = 0; i < num; i++) {
ingo@346:             Node out = outs.item(i);
ingo@346: 
ingo@346:             parseOutput(out);
ingo@346:         }
ingo@346:     }
ingo@346: 
ingo@346: 
ingo@346:     public Map<String, Output> getOuts() {
ingo@346:         return outs;
ingo@346:     }
ingo@346: 
ingo@346: 
ingo@346:     protected void addItem(String out, ManagedFacet item) {
ingo@346:         Output o = outs.get(out);
ingo@346: 
ingo@346:         if (o != null) {
ingo@346:             o.addFacet(item);
ingo@346:         }
ingo@346:     }
ingo@346: 
ingo@346: 
ingo@346:     protected void parseOutput(Node out) {
ingo@346:         String name = XMLUtils.xpathString(
ingo@346:             out, "@name", ArtifactNamespaceContext.INSTANCE);
ingo@346: 
ingo@346:         if (outs.get(name) == null) {
ingo@346:             logger.debug("Create new output: " + name);
ingo@346: 
ingo@346:             Output o = new DefaultOutput(name, null, null);
ingo@346:             outs.put(name, o);
ingo@346:         }
ingo@346: 
ingo@346:         parseItems(out, name);
ingo@346:     }
ingo@346: 
ingo@346: 
ingo@346:     protected void parseItems(Node out, String outname) {
ingo@346:         NodeList themes = (NodeList) XMLUtils.xpath(
ingo@945:             out, "art:facet",
ingo@346:             XPathConstants.NODESET,
ingo@346:             ArtifactNamespaceContext.INSTANCE);
ingo@346: 
ingo@346:         int num = themes != null ? themes.getLength() : 0;
ingo@346: 
ingo@346:         logger.debug("Output has " + num + " themes.");
ingo@346: 
ingo@694:         String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@346: 
ingo@694:         for (int i = 0; i < num; i++) {
ingo@694:             Element theme = (Element) themes.item(i);
ingo@638: 
ingo@945:             addItem(outname, new ManagedDomFacet(theme));
ingo@346:         }
ingo@346:     }
ingo@346: }
ingo@346: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :