comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 71:e4ecf3188bdf

Integrated FIS-Artifact gnv-artifacts/trunk@62 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Thu, 10 Sep 2009 13:11:24 +0000
parents 0035862b0295
children 504570de21fd
comparison
equal deleted inserted replaced
70:0035862b0295 71:e4ecf3188bdf
7 import java.util.Collection; 7 import java.util.Collection;
8 import java.util.HashMap; 8 import java.util.HashMap;
9 import java.util.Iterator; 9 import java.util.Iterator;
10 import java.util.Map; 10 import java.util.Map;
11 11
12 import javax.xml.XMLConstants;
13 import javax.xml.xpath.XPathConstants;
14
12 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
13 import org.w3c.dom.Document; 16 import org.w3c.dom.Document;
14 import org.w3c.dom.Element; 17 import org.w3c.dom.Element;
15 import org.w3c.dom.Node; 18 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList; 19 import org.w3c.dom.NodeList;
17 20
18 import de.intevation.artifactdatabase.Config; 21 import de.intevation.artifactdatabase.Config;
19 import de.intevation.artifactdatabase.DefaultArtifact; 22 import de.intevation.artifactdatabase.DefaultArtifact;
23 import de.intevation.artifactdatabase.XMLUtils;
20 import de.intevation.artifacts.ArtifactFactory; 24 import de.intevation.artifacts.ArtifactFactory;
21 import de.intevation.artifacts.ArtifactNamespaceContext; 25 import de.intevation.artifacts.ArtifactNamespaceContext;
22 import de.intevation.gnv.artifacts.context.GNVArtifactContext; 26 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
23 import de.intevation.gnv.transition.DefaultInputData; 27 import de.intevation.gnv.transition.DefaultInputData;
24 import de.intevation.gnv.transition.InputData; 28 import de.intevation.gnv.transition.InputData;
26 import de.intevation.gnv.transition.OutputMode; 30 import de.intevation.gnv.transition.OutputMode;
27 import de.intevation.gnv.transition.OutputTransition; 31 import de.intevation.gnv.transition.OutputTransition;
28 import de.intevation.gnv.transition.Transition; 32 import de.intevation.gnv.transition.Transition;
29 import de.intevation.gnv.transition.TransitionFactory; 33 import de.intevation.gnv.transition.TransitionFactory;
30 import de.intevation.gnv.transition.exception.TransitionException; 34 import de.intevation.gnv.transition.exception.TransitionException;
35 import de.intevation.gnv.utils.ArtifactXMLUtilities;
31 36
32 /** 37 /**
33 * @author Tim Englich <tim.englich@intevation.de> 38 * @author Tim Englich <tim.englich@intevation.de>
34 * 39 *
35 */ 40 */
67 /** 72 /**
68 * The Name of the Artifact 73 * The Name of the Artifact
69 */ 74 */
70 protected String name = null; 75 protected String name = null;
71 76
77 private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities();
78
72 /** 79 /**
73 * Constructor 80 * Constructor
74 */ 81 */
75 public GNVArtifactBase() { 82 public GNVArtifactBase() {
76 super(); 83 super();
79 /** 86 /**
80 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, java.lang.Object) 87 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, java.lang.Object)
81 */ 88 */
82 @Override 89 @Override
83 public Document advance(Document target, Object context) { 90 public Document advance(Document target, Object context) {
84 Document result = super.newDocument(); 91 Document result = XMLUtils.newDocument();
85 if (this.current != null){ 92 if (this.current != null){
86 String transitionName = this.readTransitionName(target); 93 String transitionName = this.readTransitionName(target);
87 if (this.current.isTransitionReachable(transitionName)){ 94 if (this.current.isTransitionReachable(transitionName)){
88 // 1. Prüfung ob Transition valide ist 95 // 1. Prüfung ob Transition valide ist
89 if (this.current.validate()){ 96 if (this.current.validate()){
134 /** 141 /**
135 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object) 142 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object)
136 */ 143 */
137 @Override 144 @Override
138 public Document feed(Document target, Object context) { 145 public Document feed(Document target, Object context) {
139 Document result = super.newDocument(); 146 Document result = XMLUtils.newDocument();
140 try { 147 try {
141 if (this.current != null){ 148 if (this.current != null){
142 this.current.putInputData(this.parseInputData(target)); 149 this.current.putInputData(this.parseInputData(target));
143 // TODO Ergebnisdokument erzeugen. 150 // TODO Ergebnisdokument erzeugen.
144 } 151 }
176 } 183 }
177 184
178 185
179 protected Document createDescibeOutput(){ 186 protected Document createDescibeOutput(){
180 log.debug("GNVArtifactBase.createDescibeOutput"); 187 log.debug("GNVArtifactBase.createDescibeOutput");
181 Document document = super.newDocument(); 188 Document document = XMLUtils.newDocument();
182 Element rootNode = this.createRootNode(document); 189 Element rootNode = this.createRootNode(document);
183 this.createHeader(rootNode, document, "describe"); 190 this.createHeader(rootNode, document, "describe");
184 this.createOutputs(rootNode, document); 191 this.createOutputs(rootNode, document);
185 this.createCurrentState(rootNode, document); 192 this.createCurrentState(rootNode, document);
186 this.createReachableStates(rootNode, document); 193 this.createReachableStates(rootNode, document);
189 196
190 return document; 197 return document;
191 } 198 }
192 199
193 protected Element createRootNode(Document document){ 200 protected Element createRootNode(Document document){
194 Element rootNode = createElement(document,"result"); 201 Element rootNode = xmlUtilities.createArtifactElement(document,"result");
195 document.appendChild(rootNode); 202 document.appendChild(rootNode);
196 return rootNode; 203 return rootNode;
197 } 204 }
198 205
199 protected void createHeader(Element parent, Document document, String documentType){ 206 protected void createHeader(Element parent, Document document, String documentType){
200 Element typeNode = createElement(document,"type"); 207 Element typeNode = xmlUtilities.createArtifactElement(document,"type");
201 typeNode.setAttribute("name", documentType); 208 typeNode.setAttribute("name", documentType);
202 parent.appendChild(typeNode); 209 parent.appendChild(typeNode);
203 210
204 Element uuidNode = createElement(document,"uuid"); 211 Element uuidNode = xmlUtilities.createArtifactElement(document,"uuid");
205 uuidNode.setAttribute("value", super.identifier); 212 uuidNode.setAttribute("value", super.identifier);
206 parent.appendChild(uuidNode); 213 parent.appendChild(uuidNode);
207 214
208 Element hashNode = createElement(document,"hash"); 215 Element hashNode = xmlUtilities.createArtifactElement(document,"hash");
209 hashNode.setAttribute("value", this.hash()); 216 hashNode.setAttribute("value", this.hash());
210 parent.appendChild(hashNode); 217 parent.appendChild(hashNode);
211 218
212 219
213 } 220 }
214 protected void createReachableStates(Element parent,Document document){ 221 protected void createReachableStates(Element parent,Document document){
215 Element stateNode = createElement(document,"reachable-states"); 222 Element stateNode = xmlUtilities.createArtifactElement(document,"reachable-states");
216 if (this.current != null){ 223 if (this.current != null){
217 Iterator<String> states = this.current.reachableTransitions().iterator(); 224 Iterator<String> states = this.current.reachableTransitions().iterator();
218 while(states.hasNext()){ 225 while(states.hasNext()){
219 String value = states.next(); 226 String value = states.next();
220 Element currentNode = createElement(document,"state"); 227 Element currentNode = xmlUtilities.createArtifactElement(document,"state");
221 currentNode.setAttribute("name", value); 228 currentNode.setAttribute("name", value);
222 currentNode.setAttribute("description", transitions.get(value).getDescription()); 229 currentNode.setAttribute("description", transitions.get(value).getDescription());
223 stateNode.appendChild(currentNode); 230 stateNode.appendChild(currentNode);
224 } 231 }
225 } 232 }
226 parent.appendChild(stateNode); 233 parent.appendChild(stateNode);
227 } 234 }
228 235
229 protected void createCurrentState(Element parent, Document document){ 236 protected void createCurrentState(Element parent, Document document){
230 Element stateNode = createElement(document,"state"); 237 Element stateNode = xmlUtilities.createArtifactElement(document,"state");
231 stateNode.setAttribute("name", this.current.getID()); 238 stateNode.setAttribute("name", this.current.getID());
232 stateNode.setAttribute("description", this.current.getDescription()); 239 stateNode.setAttribute("description", this.current.getDescription());
233 parent.appendChild(stateNode); 240 parent.appendChild(stateNode);
234 } 241 }
235 242
236 243
237 protected void createModel(Element parent, Document document){ 244 protected void createModel(Element parent, Document document){
238 Element modelNode = createElement(document,"model"); 245 Element modelNode = xmlUtilities.createArtifactElement(document,"model");
239 if (this.current != null){ 246 if (this.current != null){
240 Collection<InputValue> inputValues = this.current.getRequiredInputValues(); 247 Collection<InputValue> inputValues = this.current.getRequiredInputValues();
241 if (inputValues != null){ 248 if (inputValues != null){
242 Iterator<InputValue> it = inputValues.iterator(); 249 Iterator<InputValue> it = inputValues.iterator();
243 while(it.hasNext()){ 250 while(it.hasNext()){
244 InputValue inputValue = it.next(); 251 InputValue inputValue = it.next();
245 Element inputNode = createElement(document,"input"); 252 Element inputNode = xmlUtilities.createArtifactElement(document,"input");
246 inputNode.setAttribute("name", inputValue.getName()); 253 inputNode.setAttribute("name", inputValue.getName());
247 inputNode.setAttribute("type", inputValue.getType()); 254 inputNode.setAttribute("type", inputValue.getType());
248 modelNode.appendChild(inputNode); 255 modelNode.appendChild(inputNode);
249 } 256 }
250 } 257 }
251 } 258 }
252 parent.appendChild(modelNode); 259 parent.appendChild(modelNode);
253 } 260 }
254 261
255 protected void createUserInterface(Element parent, Document document){ 262 protected void createUserInterface(Element parent, Document document){
256 Element uiNode = createElement(document,"ui"); 263 Element uiNode = xmlUtilities.createArtifactElement(document,"ui");
257 264
258 if (this.current != null){ 265 if (this.current != null){
259 this.current.describe(document, uiNode); 266 this.current.describe(document, uiNode);
260 } 267 }
261 268
262 parent.appendChild(uiNode); 269 parent.appendChild(uiNode);
263 } 270 }
264 271
265 protected void createOutputs(Element parent, Document document){ 272 protected void createOutputs(Element parent, Document document){
266 log.debug("GNVArtifactBase.createOutputs"); 273 log.debug("GNVArtifactBase.createOutputs");
267 Element outputsNode = createElement(document,"outputs"); 274 Element outputsNode = xmlUtilities.createArtifactElement(document,"outputs");
268 if (this.current instanceof OutputTransition){ 275 if (this.current instanceof OutputTransition){
269 Collection<OutputMode> outputModes = ((OutputTransition)this.current).getOutputModes(); 276 Collection<OutputMode> outputModes = ((OutputTransition)this.current).getOutputModes();
270 if (outputModes != null){ 277 if (outputModes != null){
271 Iterator<OutputMode> it = outputModes.iterator(); 278 Iterator<OutputMode> it = outputModes.iterator();
272 while(it.hasNext()){ 279 while(it.hasNext()){
273 OutputMode outputMode = it.next(); 280 OutputMode outputMode = it.next();
274 log.debug("Write Outputnode for "+ outputMode.toString()); 281 log.debug("Write Outputnode for "+ outputMode.toString());
275 Element outputModeNode = createElement(document,"output"); 282 Element outputModeNode = xmlUtilities.createArtifactElement(document,"output");
276 outputModeNode.setAttribute("name", outputMode.getName()); 283 outputModeNode.setAttribute("name", outputMode.getName());
277 outputModeNode.setAttribute("description", outputMode.getDescription()); 284 outputModeNode.setAttribute("description", outputMode.getDescription());
278 outputModeNode.setAttribute("mime-type", outputMode.getMimeType()); 285 outputModeNode.setAttribute("mime-type", outputMode.getMimeType());
279 outputsNode.appendChild(outputModeNode); 286 outputsNode.appendChild(outputModeNode);
280 } 287 }
283 } 290 }
284 } 291 }
285 parent.appendChild(outputsNode); 292 parent.appendChild(outputsNode);
286 } 293 }
287 294
288 /**
289 * @param document
290 * @return
291 */
292 private Element createElement(Document document, String name) {
293 Element node = document.createElementNS(ArtifactNamespaceContext.NAMESPACE_URI, name);
294 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
295 return node;
296 }
297 295
298 protected Collection<InputData> parseInputData(Document document){ 296 protected Collection<InputData> parseInputData(Document document){
299 log.debug("GNVArtifactBase.parseInputData"); 297 log.debug("GNVArtifactBase.parseInputData");
300 Collection<InputData> returnValue = null; 298 Collection<InputData> returnValue = null;
301 299
302 NodeList inputElemets = Config.getNodeSetXPath(document, "action/data/input"); 300 log.debug(new ArtifactXMLUtilities().writeDocument2String(document));
301
302 NodeList inputElemets = (NodeList)XMLUtils.xpath(document, "/action/data/input",XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE);//Config.getNodeSetXPath(document, "");
303 if(inputElemets != null){ 303 if(inputElemets != null){
304 returnValue = new ArrayList<InputData>(inputElemets.getLength()); 304 returnValue = new ArrayList<InputData>(inputElemets.getLength());
305 for (int i = 0; i < inputElemets.getLength(); i++){ 305 for (int i = 0; i < inputElemets.getLength(); i++){
306 Node inputDataNode = inputElemets.item(i); 306 Node inputDataNode = inputElemets.item(i);
307 InputData inputData = new DefaultInputData(Config.getStringXPath(inputDataNode,"@name"), Config.getStringXPath(inputDataNode,"@value")); 307 InputData inputData = new DefaultInputData(Config.getStringXPath(inputDataNode,"@name"), Config.getStringXPath(inputDataNode,"@value"));

http://dive4elements.wald.intevation.org