Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/ProtocolUtils.java @ 473:d0ac790a6c89 dive4elements-move
Moved directories to org.dive4elements
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 10:57:18 +0200 |
parents | artifact-database/src/main/java/de/intevation/artifactdatabase/ProtocolUtils.java@f367be55dd35 |
children | 415df0fc4fa1 |
comparison
equal
deleted
inserted
replaced
472:783cc1b6b615 | 473:d0ac790a6c89 |
---|---|
1 /* | |
2 * Copyright (c) 2011 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 package de.intevation.artifactdatabase; | |
9 | |
10 import java.util.List; | |
11 | |
12 import org.w3c.dom.Document; | |
13 import org.w3c.dom.Element; | |
14 import org.w3c.dom.Node; | |
15 | |
16 import de.intevation.artifacts.ArtifactNamespaceContext; | |
17 | |
18 import de.intevation.artifacts.common.utils.XMLUtils; | |
19 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
20 | |
21 import de.intevation.artifactdatabase.state.Facet; | |
22 import de.intevation.artifactdatabase.state.Output; | |
23 import de.intevation.artifactdatabase.state.State; | |
24 | |
25 | |
26 /** | |
27 * This class provides methods that help creating the artifact protocol | |
28 * documents describe, feed, advance and out. | |
29 * | |
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
31 */ | |
32 public class ProtocolUtils { | |
33 | |
34 /** | |
35 * It should not be necessary to create instances of this class. | |
36 */ | |
37 private ProtocolUtils() {} | |
38 | |
39 | |
40 /** | |
41 * This method creates a node that might be used for the artifact protocol. | |
42 * | |
43 * @param creator The ElementCreator that is used to create the node. | |
44 * @param nodeName The node name. | |
45 * @param attrName The names of optional attributes. | |
46 * @param value The values for the optional attributes. | |
47 * | |
48 * @return the created node. | |
49 */ | |
50 public static Element createArtNode( | |
51 XMLUtils.ElementCreator creator, | |
52 String nodeName, String[] attrName, String[] value) | |
53 { | |
54 Element typeNode = creator.create(nodeName); | |
55 | |
56 if (attrName != null && value != null) { | |
57 for (int i = 0; i < attrName.length; i++) { | |
58 if (i < value.length) { | |
59 creator.addAttr(typeNode, attrName[i], value[i], true); | |
60 } | |
61 else { | |
62 break; | |
63 } | |
64 } | |
65 } | |
66 | |
67 return typeNode; | |
68 } | |
69 | |
70 | |
71 /** | |
72 * This method creates the root node for all artifact protocol documents. | |
73 * | |
74 * @param creator The ElementCreator used to create new elements. | |
75 * | |
76 * @return the root node for the artifact protocol document. | |
77 */ | |
78 public static Element createRootNode(XMLUtils.ElementCreator creator) { | |
79 return createArtNode(creator, "result", null, null); | |
80 } | |
81 | |
82 | |
83 /** | |
84 * This method appends the three necessary nodes <i>type</i>, <i>uuid</i> | |
85 * and <i>hash</i> of the describe document to <i>root</i> node. | |
86 * | |
87 * @param creator The ElementCreator that is used to create new nodes. | |
88 * @param root The root node of the describe document. | |
89 * @param uuid The UUID of the artifact. | |
90 * @param hash The hash if the artifact. | |
91 */ | |
92 public static void appendDescribeHeader( | |
93 XMLUtils.ElementCreator creator, Element root, String uuid, String hash) | |
94 { | |
95 root.appendChild(createArtNode( | |
96 creator, | |
97 "type", | |
98 new String[] {"name"}, | |
99 new String[] {"describe"})); | |
100 | |
101 root.appendChild(createArtNode( | |
102 creator, | |
103 "uuid", | |
104 new String[] {"value"}, | |
105 new String[] {uuid})); | |
106 | |
107 root.appendChild(createArtNode( | |
108 creator, | |
109 "hash", | |
110 new String[] {"value"}, | |
111 new String[] {hash})); | |
112 } | |
113 | |
114 | |
115 /** | |
116 * This method appends a node that describes the current state to | |
117 * <i>root</i>. | |
118 * | |
119 * @param creator The ElementCreator used to create new elements. | |
120 * @param root The parent node for new elements. | |
121 * @param state The state to be appended. | |
122 */ | |
123 public static void appendState( | |
124 XMLUtils.ElementCreator creator, Element root, State state) | |
125 { | |
126 root.appendChild(createArtNode( | |
127 creator, "state", | |
128 new String[] { "description", "name" }, | |
129 new String[] { state.getDescription(), state.getID() })); | |
130 } | |
131 | |
132 | |
133 /** | |
134 * This method appends a node with reachable states to <i>root</i>. | |
135 * | |
136 * @param creator The ElementCreator used to create new elements. | |
137 * @param root The parent node for new elements. | |
138 * @param states The reachable states to be appended. | |
139 */ | |
140 public static void appendReachableStates( | |
141 XMLUtils.ElementCreator creator, | |
142 Element root, | |
143 List<State> states) | |
144 { | |
145 Element reachable = createArtNode( | |
146 creator, "reachable-states", null, null); | |
147 | |
148 for (State s: states) { | |
149 appendState(creator, reachable, s); | |
150 } | |
151 | |
152 root.appendChild(reachable); | |
153 } | |
154 | |
155 | |
156 /** | |
157 * This method appends a node for each Output in the <i>outputs</i> list to | |
158 * <i>out</i>. Note: an output node includes its provided facets! | |
159 * | |
160 * @param doc The document to which to add new elements. | |
161 * @param out The parent node for new elements. | |
162 * @param outputs The list of reachable outputs. | |
163 */ | |
164 public static void appendOutputModes( | |
165 Document doc, | |
166 Element out, | |
167 List<Output> outputs) | |
168 { | |
169 ElementCreator creator = new ElementCreator( | |
170 doc, | |
171 ArtifactNamespaceContext.NAMESPACE_URI, | |
172 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
173 | |
174 for (Output o: outputs) { | |
175 Element newOut = createArtNode( | |
176 creator, | |
177 "output", | |
178 new String[] {"name", "description", "mime-type", "type"}, | |
179 new String[] { | |
180 o.getName(), | |
181 o.getDescription(), | |
182 o.getMimeType(), | |
183 o.getType() }); | |
184 | |
185 Element facets = createArtNode(creator, "facets", null, null); | |
186 appendFacets(doc, facets, o.getFacets()); | |
187 | |
188 newOut.appendChild(facets); | |
189 out.appendChild(newOut); | |
190 } | |
191 } | |
192 | |
193 | |
194 /** | |
195 * This method appends a node for each Facet in the <i>facets</i> list to | |
196 * <i>facet</i>. | |
197 * | |
198 * @param doc The document to wich to add new elements. | |
199 * @param facet The root node for new elements. | |
200 * @param facets The list of facets. | |
201 */ | |
202 public static void appendFacets( | |
203 Document doc, | |
204 Element facet, | |
205 List<Facet> facets) | |
206 { | |
207 if (facets == null || facets.size() == 0) { | |
208 return; | |
209 } | |
210 | |
211 ElementCreator creator = new ElementCreator( | |
212 doc, | |
213 ArtifactNamespaceContext.NAMESPACE_URI, | |
214 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
215 | |
216 for (Facet f: facets) { | |
217 Node node = f.toXML(doc); | |
218 | |
219 if (node != null) { | |
220 facet.appendChild(node); | |
221 } | |
222 } | |
223 } | |
224 } | |
225 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |