Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVProductArtifactFactory.java @ 592:3c939c95c477
Removed accidental checkin of Test-Shapefiles
gnv-artifacts/trunk@647 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 29 Jan 2010 13:28:28 +0000 |
parents | d47b478e662b |
children | 5b9b74c08bbb |
line wrap: on
line source
package de.intevation.gnv.artifacts; import de.intevation.artifactdatabase.DefaultArtifactFactory; import de.intevation.artifactdatabase.ProxyArtifact; import de.intevation.artifactdatabase.XMLUtils; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.ArtifactFactory; import de.intevation.gnv.artifacts.fis.SelectProductArtifact; import de.intevation.gnv.artifacts.fis.product.DefaultProduct; import de.intevation.gnv.state.DefaultInputData; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> */ public class GNVProductArtifactFactory extends DefaultArtifactFactory { public static final String XPATH_REPLACE_NAME = "IDENTIFIER"; public static final String XPATH_PRODUCT_ARTIFACT = "artifact-database/artifacts/artifact[@name='"+XPATH_REPLACE_NAME+"']"; public static final String XPATH_PRODUCT = "products/product"; public static final String XPATH_PRODUCT_NAME = "@name"; public static final String XPATH_PRODUCT_FACTORY = "artifact-factory"; public static final String XPATH_PRODUCT_PARAMETER = "parameters/parameter"; private Logger logger = Logger.getLogger(GNVProductArtifactFactory.class); private Map productFactories; private Map products; public GNVProductArtifactFactory() { } @Override public Artifact createArtifact(String identifier, Object context) { // create a SelectProductArtifact for initial product selection Artifact artifact = super.createArtifact(identifier, context); if (artifact instanceof ProxyArtifact) { SelectProductArtifact select = new SelectProductArtifact(); select.setProducts(products); select.setup(identifier, this, context); ((ProxyArtifact) artifact).setProxied(select); } return artifact; } @Override public void setup(Document document, Node factoryNode) { super.setup(document, factoryNode); String xpath = XPATH_PRODUCT_ARTIFACT.replace(XPATH_REPLACE_NAME, name); Node artifact = (Node) XMLUtils.xpath( document, xpath, XPathConstants.NODE); NodeList productNodes = (NodeList) XMLUtils.xpath( artifact, XPATH_PRODUCT, XPathConstants.NODESET); if (productNodes == null) { logger.warn("No products found for fis: " + name); return; } int productLength = productNodes.getLength(); if (productLength != 0) { productFactories = new HashMap(productLength); products = new HashMap(productLength); parseProductFactories(document, productNodes); } } public ArtifactFactory getArtifactFactoryByName(String name) { return (ArtifactFactory) productFactories.get(name); } protected void parseProductFactories(Document document, NodeList products) { int items = products.getLength(); for(int i = 0; i < items; i++) { try { Node product = products.item(i); String name = (String) XMLUtils.xpath( product, XPATH_PRODUCT_NAME, XPathConstants.STRING); Node factoryNode = (Node) XMLUtils.xpath( product, XPATH_PRODUCT_FACTORY, XPathConstants.NODE); String factoryClass = factoryNode.getTextContent(); if (factoryClass == null || factoryClass.equals("")) { logger.warn("No artifact factory class found for " + name); continue; } Class cls = Class.forName(factoryClass); ArtifactFactory factory = (ArtifactFactory) cls.newInstance(); factory.setup(document, factoryNode); productFactories.put(name, factory); initializeProducts(product); } catch (ClassNotFoundException cnfe) { logger.warn(cnfe, cnfe); } catch (InstantiationException ie) { logger.warn(ie, ie); } catch (IllegalAccessException iae) { logger.warn(iae, iae); } } } protected void initializeProducts(Node productNode) { String productName = (String) XMLUtils.xpath( productNode, "@name", XPathConstants.STRING); NodeList parameterNodes = (NodeList) XMLUtils.xpath( productNode, XPATH_PRODUCT_PARAMETER, XPathConstants.NODESET ); if (parameterNodes != null) { int items = parameterNodes.getLength(); Collection inputParameter = new ArrayList(items); for (int j = 0; j < items; j++) { Node parameterNode = (Node) parameterNodes.item(j); String name = (String) XMLUtils.xpath( parameterNode, "@name", XPathConstants.STRING); String value = (String) XMLUtils.xpath( parameterNode, "@value", XPathConstants.STRING); inputParameter.add(new DefaultInputData(name, value)); } products.put( productName, new DefaultProduct(productName, inputParameter, this.name) ); } } }