Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | 3e703d134bbe |
children | e57816cf41d5 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java Fri Sep 28 12:14:59 2012 +0200 @@ -0,0 +1,160 @@ +package de.intevation.flys.collections; + +import java.util.List; +import java.util.Map; + +import javax.xml.xpath.XPathConstants; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import de.intevation.artifacts.ArtifactNamespaceContext; + +import de.intevation.artifactdatabase.state.DefaultOutput; +import de.intevation.artifactdatabase.state.Facet; +import de.intevation.artifactdatabase.state.Output; +import de.intevation.artifactdatabase.state.Settings; + +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.flys.artifacts.model.ManagedDomFacet; +import de.intevation.flys.exports.ChartSettings; + +/** + * Access parts of the Attribute parts of a FLYSCollections description + * document. + */ +public class AttributeParser { + + /** Constant XPath that points to the outputmodes of an artifact. */ + public static final String XPATH_ARTIFACT_OUTPUTMODES = + "/art:attribute/art:outputs/art:output"; + + + private static Logger logger = Logger.getLogger(AttributeParser.class); + + + protected Document attributeDocument; + + protected CollectionAttribute attribute; + + + public AttributeParser(Document attributeDocument) { + this.attributeDocument = attributeDocument; + } + + + public void parse() { + logger.debug("AttributeParser.parse"); + + attribute = new CollectionAttribute(); + + NodeList outs = (NodeList) XMLUtils.xpath( + attributeDocument, + XPATH_ARTIFACT_OUTPUTMODES, + XPathConstants.NODESET, + ArtifactNamespaceContext.INSTANCE); + + int num = outs != null ? outs.getLength() : 0; + + logger.debug("Attribute has " + num + " outputs."); + + for (int i = 0; i < num; i++) { + Node out = outs.item(i); + + parseOutput(out); + } + } + + + public CollectionAttribute getCollectionAttribute() { + if (attribute == null) { + parse(); + } + + return attribute; + } + + + public Document getAttributeDocument() { + return attributeDocument; + } + + + public Map<String, Output> getOuts() { + return attribute.getOutputs(); + } + + + /** + * Access all facets. + * @return list of all facets. + */ + public List<Facet> getFacets() { + return attribute.getFacets(); + } + + + protected void parseOutput(Node out) { + String name = XMLUtils.xpathString( + out, "@name", ArtifactNamespaceContext.INSTANCE); + + if (name == null || name.length() == 0) { + logger.warn("No Output name specified. Cancel parsing!"); + return; + } + + Output o = attribute.getOutput(name); + + if (o == null) { + logger.debug("Create new output: " + name); + + o = new DefaultOutput(name, null, null); + attribute.addOutput(name, o); + } + + parseSettings(out, name); + parseItems(out, name); + } + + + protected void parseSettings(Node out, String outname) { + Node settingsNode = (Node) XMLUtils.xpath( + out, "settings", + XPathConstants.NODE, + null); + + if (settingsNode == null) { + logger.debug("No Settings found for Output '" + outname + "'"); + return; + } + + Settings settings = ChartSettings.parse(settingsNode); + attribute.setSettings(outname, settings); + } + + + protected void parseItems(Node out, String outname) { + NodeList themes = (NodeList) XMLUtils.xpath( + out, "art:facet", + XPathConstants.NODESET, + ArtifactNamespaceContext.INSTANCE); + + int num = themes != null ? themes.getLength() : 0; + + logger.debug("Output has " + num + " themes."); + + String uri = ArtifactNamespaceContext.NAMESPACE_URI; + + for (int i = 0; i < num; i++) { + Element theme = (Element) themes.item(i); + + attribute.addFacet(outname, new ManagedDomFacet(theme)); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :