# HG changeset patch # User Ingo Weinzierl # Date 1311071877 0 # Node ID 59ae2a823e73cff68007c746155e9b9c1a14e2e1 # Parent c256061287d74a14c890c21007e09073186d1da9 Use the Facet.toXML() method to write facet elements into the DESCRIBE document of the Collection. flys-artifacts/trunk@2360 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue Jul 19 10:37:57 2011 +0000 @@ -1,3 +1,41 @@ +2011-07-19 Ingo Weinzierl + + * src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java: + Override the toXML() method. Subclasses can now write their own XML + representation. + + * src/main/java/de/intevation/flys/artifacts/model/ManagedDomFacet.java: + New. This ManagedFacet uses an Element (DOM) to store the information + about a facet. The intent of this facet type is to represent a facet + stored in an Collection attribute. Different facets can have different + attributes that we need to parse, but the only thing ManagedFacets need + to do, is to adjust the attributes "active" and "position". So, those + values are set directly on the Element, the other attributes aren't + touched. + + * src/main/java/de/intevation/flys/artifacts/model/ManagedFacetAdapter.java: + New. This facet is a wrapper for another facet. This subclass of a + ManagedFacet overrides the toXML() method. The XML representation is + defined by the inner facet that is stored as member variable. The + ManagedFacet specific attributes "artifact", "facet", "pos" and "active" + are added manually. + + * src/main/java/de/intevation/flys/collections/AttributeWriter.java: Uses + the toXML() method to write a facet node into the attribute document. + + * src/main/java/de/intevation/flys/collections/AttributeParser.java: Uses + the ManagedDomFacet to save the information of a facet which is + contained in the attribute part of a Collection's DESCRIBE document. + + * src/main/java/de/intevation/flys/collections/OutputParser.java: Uses the + ManagedFacetAdapter to save a facet, because we want to keep the + specific facet to be able to write its specific XML representation into + the Collection's DESCRIBE document. + + * src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java: + Adapted the XPath of facets stored in the attribute part of the + DESCRIBE. + 2011-07-18 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Added a diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedDomFacet.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedDomFacet.java Tue Jul 19 10:37:57 2011 +0000 @@ -0,0 +1,133 @@ +package de.intevation.flys.artifacts.model; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import de.intevation.artifacts.ArtifactNamespaceContext; + + +public class ManagedDomFacet extends ManagedFacet { + + protected Element facet; + + + public ManagedDomFacet(Element facet) { + super(null, -1, null, null, -1, -1); + + this.facet = facet; + } + + + @Override + public int getIndex() { + if (this.index < 0) { + String index = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "index"); + + if (index != null && index.length() > 0) { + this.index = Integer.parseInt(index); + } + } + + return this.index; + } + + + @Override + public String getName() { + if (this.name == null || this.name.length() == 0) { + String name = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "facet"); + + this.name = name; + } + + return this.name; + } + + + @Override + public String getDescription() { + if (this.description == null || this.description.length() == 0) { + String description = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "description"); + + this.description = description; + } + + return this.description; + } + + + @Override + public int getPosition() { + if (this.position < 0) { + String position = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "pos"); + + if (position != null && position.length() > 0) { + this.position = Integer.parseInt(position); + } + } + + return this.position; + } + + + @Override + public void setPosition(int position) { + this.position = position; + + facet.setAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, + "pos", + String.valueOf(position)); + } + + + @Override + public int getActive() { + if (this.active < 0) { + String active = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "active"); + + if (active != null && active.length() > 0) { + this.active = Integer.parseInt(active); + } + } + + return this.active; + } + + + @Override + public void setActive(int active) { + this.active = active; + + facet.setAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, + "active", + String.valueOf(active)); + } + + + @Override + public String getArtifact() { + if (this.uuid == null || this.uuid.length() == 0) { + String uuid = facet.getAttributeNS( + ArtifactNamespaceContext.NAMESPACE_URI, "artifact"); + + this.uuid = uuid; + } + + return this.uuid; + } + + + @Override + public Node toXML(Document doc) { + return doc.importNode(facet, true); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java Tue Jul 19 10:37:57 2011 +0000 @@ -1,5 +1,13 @@ package de.intevation.flys.artifacts.model; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import de.intevation.artifacts.ArtifactNamespaceContext; + +import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; + import de.intevation.artifactdatabase.state.DefaultFacet; @@ -57,5 +65,23 @@ public String getArtifact() { return uuid; } + + + public Node toXML(Document doc) { + ElementCreator ec = new ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element facet = ec.create("theme"); + ec.addAttr(facet, "artifact", getArtifact(), true); + ec.addAttr(facet, "facet", getName(), true); + ec.addAttr(facet, "pos", String.valueOf(getPosition()), true); + ec.addAttr(facet, "active", String.valueOf(getActive()), true); + ec.addAttr(facet, "index", String.valueOf(getIndex()), true); + ec.addAttr(facet, "description", getDescription(), true); + + return facet; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedFacetAdapter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ManagedFacetAdapter.java Tue Jul 19 10:37:57 2011 +0000 @@ -0,0 +1,47 @@ +package de.intevation.flys.artifacts.model; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import de.intevation.artifacts.ArtifactNamespaceContext; + +import de.intevation.artifactdatabase.state.Facet; + +import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; + + +public class ManagedFacetAdapter extends ManagedFacet { + + protected Facet facet; + + + public ManagedFacetAdapter(Facet facet, String uuid, int pos, int active) { + super( + facet.getName(), + facet.getIndex(), + facet.getDescription(), + uuid, + pos, + active); + + this.facet = facet; + } + + + @Override + public Node toXML(Document doc) { + ElementCreator ec = new ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element e = (Element) facet.toXML(doc); + ec.addAttr(e, "artifact", getArtifact(), true); + ec.addAttr(e, "facet", getName(), true); + ec.addAttr(e, "pos", String.valueOf(getPosition()), true); + ec.addAttr(e, "active", String.valueOf(getActive()), true); + + return e; + } +} diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java Tue Jul 19 10:37:57 2011 +0000 @@ -20,6 +20,7 @@ import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.flys.artifacts.model.ManagedFacet; +import de.intevation.flys.artifacts.model.ManagedDomFacet; public class AttributeParser { @@ -92,7 +93,7 @@ protected void parseItems(Node out, String outname) { NodeList themes = (NodeList) XMLUtils.xpath( - out, "art:theme", + out, "art:facet", XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); @@ -105,39 +106,7 @@ for (int i = 0; i < num; i++) { Element theme = (Element) themes.item(i); - String name = theme.getAttributeNS(uri, "facet"); - if (name == null || name.length() == 0) { - continue; - } - - String uuid = theme.getAttributeNS(uri, "artifact"); - if (uuid == null || uuid.length() == 0) { - continue; - } - - String pos = theme.getAttributeNS(uri, "pos"); - if (pos == null || pos.length() == 0) { - continue; - } - - String index = theme.getAttributeNS(uri, "index"); - if (index == null || index.length() == 0) { - continue; - } - - String active = theme.getAttributeNS(uri, "active"); - if (active == null || active.length() == 0) { - continue; - } - - String description = theme.getAttributeNS(uri, "description"); - - ManagedFacet item = new ManagedFacet( - name, Integer.parseInt(index), description, uuid, - Integer.parseInt(pos), - Integer.parseInt(active)); - - addItem(outname, item); + addItem(outname, new ManagedDomFacet(theme)); } } } diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java Tue Jul 19 10:37:57 2011 +0000 @@ -55,7 +55,7 @@ Output a = newAttr.get(outName); Output b = oldAttr.get(outName); -// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : + writeOutput(doc, outs, cr, a, b); } @@ -99,7 +99,11 @@ ManagedFacet fA = (ManagedFacet) a.get(i); if (!mergeFacets(doc, cr, output, fA, b)) { - writeFacet(doc, cr, output, fA); + Node n = fA.toXML(doc); + + if (n != null) { + output.appendChild(n); + } } } } @@ -112,7 +116,7 @@ ManagedFacet a, List list) { - String name = a.getName() + a.getIndex(); + String nameA = a.getName() + a.getIndex(); if (list == null) { logger.debug("No old facets found."); @@ -120,31 +124,20 @@ } for (Facet facet: list) { - if (name.equals(facet.getName() + facet.getIndex())) { - writeFacet(doc, cr, output, (ManagedFacet) facet); + String nameB = facet.getName() + facet.getIndex(); + + if (nameA.equals(nameB)) { + Node n = facet.toXML(doc); + + if (n != null) { + output.appendChild(n); + } + return true; } } return false; } - - - protected void writeFacet( - Document doc, - ElementCreator cr, - Node output, - ManagedFacet f) - { - Element theme = cr.create("theme"); - cr.addAttr(theme, "artifact", f.getArtifact(), true); - cr.addAttr(theme, "facet", f.getName(), true); - cr.addAttr(theme, "pos", Integer.toString(f.getPosition()), true); - cr.addAttr(theme, "active", Integer.toString(f.getActive()), true); - cr.addAttr(theme, "index", Integer.toString(f.getIndex()), true); - cr.addAttr(theme, "description", f.getDescription(), true); - - output.appendChild(theme); - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Tue Jul 19 10:37:57 2011 +0000 @@ -708,7 +708,7 @@ protected void parse(Document output) { NodeList themes = (NodeList) XMLUtils.xpath( output, - "art:output/art:theme", + "art:output/art:facet", XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); diff -r c256061287d7 -r 59ae2a823e73 flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java Mon Jul 18 17:09:00 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java Tue Jul 19 10:37:57 2011 +0000 @@ -17,7 +17,7 @@ import de.intevation.artifactdatabase.state.Output; import de.intevation.flys.artifacts.FLYSArtifact; -import de.intevation.flys.artifacts.model.ManagedFacet; +import de.intevation.flys.artifacts.model.ManagedFacetAdapter; public class OutputParser { @@ -92,11 +92,7 @@ List newFacets = new ArrayList(old.size()); for (Facet f: old) { - newFacets.add(new ManagedFacet( - f.getName(), - f.getIndex(), - f.getDescription(), - uuid, pos++, 1)); + newFacets.add(new ManagedFacetAdapter(f, uuid, pos++, 1)); } return newFacets;