comparison artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java @ 5912:81bdb5c4414d

New exporter and facet for wsplgen calculation result.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 07 May 2013 12:45:57 +0200
parents
children c35323148b98
comparison
equal deleted inserted replaced
5911:b96a293d30f3 5912:81bdb5c4414d
1 package org.dive4elements.river.exports;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.util.List;
8 import java.util.zip.ZipOutputStream;
9
10 import org.apache.log4j.Logger;
11 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
12 import org.dive4elements.artifactdatabase.state.Settings;
13 import org.dive4elements.artifacts.Artifact;
14 import org.dive4elements.artifacts.CallContext;
15 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
16 import org.dive4elements.artifacts.common.utils.FileTools;
17 import org.dive4elements.artifacts.common.utils.XMLUtils;
18 import org.dive4elements.river.collections.D4EArtifactCollection;
19 import org.w3c.dom.Document;
20
21 import au.com.bytecode.opencsv.CSVWriter;
22
23
24 public class ShapeExporter implements OutGenerator
25 {
26 private static final String XPATH_FACET = "/art:action/@art:type";
27 private static Logger logger = Logger.getLogger(ShapeExporter.class);
28 private Artifact master;
29 private Document request;
30 private OutputStream out;
31 private CallContext context;
32 private D4EArtifactCollection collection;
33 private String facet;
34 private File dir;
35
36 @Override
37 public void init(Document request, OutputStream out, CallContext context) {
38 this.request = request;
39 this.out = out;
40 this.context = context;
41 }
42
43 @Override
44 public void setMasterArtifact(Artifact master) {
45 this.master = master;
46 }
47
48 @Override
49 public void setCollection(D4EArtifactCollection collection) {
50 this.collection = collection;
51 }
52
53 @Override
54 public void doOut(ArtifactAndFacet bundle, Document attr, boolean visible) {
55 String name = bundle.getFacetName();
56
57 if (!isFacetValid(name)) {
58 logger.debug("Facet '" + name + "' is not valid for this exporter!");
59 return;
60 }
61
62 addData(bundle.getData(context));
63 }
64
65 private void addData(Object data) {
66 if (data instanceof File) {
67 this.dir = (File)data;
68 }
69 }
70
71 private boolean isFacetValid(String name) {
72 String thisFacet = getFacet();
73 if (thisFacet == null || thisFacet.length() == 0) {
74 return false;
75 }
76 else if (facet == null || facet.length() == 0) {
77 return false;
78 }
79 else {
80 return thisFacet.equals(facet);
81 }
82 }
83
84
85 /**
86 * Returns the name of the desired facet.
87 *
88 * @return the name of the desired facet.
89 */
90 protected String getFacet() {
91 if (facet == null) {
92 facet = getFacetFromRequest();
93 }
94
95 return facet;
96 }
97
98 @Override
99 public void generate() throws IOException {
100 FileFilter filter = new FileFilter() {
101 @Override
102 public boolean accept(File pathname) {
103 if (pathname.getName().startsWith("wsplgen") &&
104 !pathname.getName().endsWith(".par")) {
105 return true;
106 }
107 else {
108 return false;
109 }
110 }
111 };
112 FileTools.createZipArchive(this.dir, out, filter);
113 out.close();
114 }
115
116 @Override
117 public void setSettings(Settings settings) {
118 //Do nothing.
119 }
120
121 @Override
122 public Settings getSettings() {
123 // This exporter has no settings.
124 return null;
125 }
126
127 /**
128 * Extracts the name of the requested facet from request document.
129 *
130 * @return the name of the requested facet.
131 */
132 protected String getFacetFromRequest() {
133 return XMLUtils.xpathString(
134 request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
135 }
136 }

http://dive4elements.wald.intevation.org