Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVProductArtifactFactory.java @ 1102:aaacced2a0cc
Fixed an ArrayIndexOutOfBounds exception that occured while initializing the ParameterMatrix (issue281).
gnv-artifacts/trunk@1228 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 28 Jun 2010 10:25:55 +0000 |
parents | 41234fcf4c7e |
children | f953c9a559d8 |
line wrap: on
line source
package de.intevation.gnv.artifacts; 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; 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; /** * This factory is called to create a {@link * de.intevation.gnv.artifacts.fis.SelectProductArtifact} which lets the user * select a product supported by the selected fis. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ 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; /** * Constructor. */ public GNVProductArtifactFactory() { } /** * Method to create an artifact. If the created artifact is from type * <code>ProxyArtifact</code>, a further ProductSelectArtifact is created * which becomes embedded into the <code>ProxyArtifact</code>. * @param identifier UUID. * @param context CallContext. * @param data Some data. * @return The created artifact. */ @Override public Artifact createArtifact(String identifier, Object context, Document data) { // create a SelectProductArtifact for initial product selection Artifact artifact = super.createArtifact(identifier, context, data); if (artifact instanceof ProxyArtifact) { SelectProductArtifact select = new SelectProductArtifact(); select.setProducts(products); select.setup(identifier, this, context, data); ((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().trim(); 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); } } } /** * Initialize all products supported by the current fis. */ 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) ); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :