KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > query > ResultResolver


1 /*
2  *
3  * ====================================================================
4  *
5  * Copyright 2004 The Apache Software Foundation
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */

20 package org.apache.slide.projector.processor.query;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.slide.projector.Context;
29 import org.apache.slide.projector.Processor;
30 import org.apache.slide.projector.Projector;
31 import org.apache.slide.projector.Result;
32 import org.apache.slide.projector.URI;
33 import org.apache.slide.projector.descriptor.ArrayValueDescriptor;
34 import org.apache.slide.projector.descriptor.BooleanValueDescriptor;
35 import org.apache.slide.projector.descriptor.MapValueDescriptor;
36 import org.apache.slide.projector.descriptor.ParameterDescriptor;
37 import org.apache.slide.projector.descriptor.ResultDescriptor;
38 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
39 import org.apache.slide.projector.descriptor.StateDescriptor;
40 import org.apache.slide.projector.descriptor.StringValueDescriptor;
41 import org.apache.slide.projector.descriptor.URIValueDescriptor;
42 import org.apache.slide.projector.i18n.DefaultMessage;
43 import org.apache.slide.projector.i18n.ParameterMessage;
44 import org.apache.slide.projector.processor.SimpleProcessor;
45 import org.apache.slide.projector.processor.xml.XPathQuery;
46 import org.apache.slide.projector.value.ArrayValue;
47 import org.apache.slide.projector.value.BooleanValue;
48 import org.apache.slide.projector.value.DocumentValue;
49 import org.apache.slide.projector.value.MapValue;
50 import org.apache.slide.projector.value.MultipleStreamableValue;
51 import org.apache.slide.projector.value.NullValue;
52 import org.apache.slide.projector.value.StreamableValue;
53 import org.apache.slide.projector.value.Value;
54 import org.jdom.xpath.XPath;
55
56 /**
57  */

58 public class ResultResolver implements Processor {
59     private final static String JavaDoc URI_ENTRY = "uri";
60     private final static String JavaDoc INSTRUCTIONS = "instructions";
61     private final static String JavaDoc INCLUDE_CONTENT = "includeContent";
62     private final static String JavaDoc CONTENT_ENTRY = "content";
63
64     private final static ParameterDescriptor[] parameterDescriptors =
65         new ParameterDescriptor[] {
66             new ParameterDescriptor(
67                     SimpleProcessor.INPUT,
68                     new ParameterMessage("resultResolver/paramter/input"),
69                     new ArrayValueDescriptor(new MapValueDescriptor(
70                             new ParameterDescriptor(URI_ENTRY,
71                             new ParameterMessage("resultResolver/parameter/input/uri"),
72                             new URIValueDescriptor())))),
73             new ParameterDescriptor(
74                     INSTRUCTIONS,
75                     new ParameterMessage("resultResolver/paramter/instructions"),
76                     new MapValueDescriptor(new ParameterDescriptor(MapValueDescriptor.ALL,
77                                     new ParameterMessage("resultResolver/parameter/instruction/instruction"),
78                                     new StringValueDescriptor())), NullValue.NULL),
79             new ParameterDescriptor(
80                     INCLUDE_CONTENT,
81                     new ParameterMessage("resultResolver/paramter/include-content"),
82                     new BooleanValueDescriptor(), BooleanValue.TRUE)
83     };
84     
85     private final static ResultDescriptor resultDescriptor =
86         new ResultDescriptor(new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
87                 new ResultEntryDescriptor[] {
88                 new ResultEntryDescriptor(SimpleProcessor.OUTPUT,
89                         new DefaultMessage("resultResolver/result/output"),
90                         ArrayValue.CONTENT_TYPE, false) });
91     
92     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
93         Value []values = ((ArrayValue)parameter.get(SimpleProcessor.INPUT)).getArray();
94         boolean includeContent = ((BooleanValue)parameter.get(INCLUDE_CONTENT)).booleanValue();
95         Value instructions = (Value)parameter.get(INSTRUCTIONS);
96         List JavaDoc arrayEntries = new ArrayList JavaDoc();
97         for ( int i = 0; i < values.length; i++ ) {
98             Map JavaDoc map = ((MapValue)values[i]).getMap();
99             Map JavaDoc resultMap = new HashMap JavaDoc();
100             resultMap.putAll(map);
101             URI uri = (URI)map.get(URI_ENTRY);
102             Value content = Projector.getRepository().getResource(uri, context.getCredentials());
103             if ( content != null ) {
104                 if ( instructions instanceof MapValue ) {
105                     if ( includeContent ) {
106                         content = new MultipleStreamableValue((StreamableValue) content);
107                     }
108                     DocumentValue documentValue = new DocumentValue((StreamableValue)content);
109                     Map JavaDoc instructionMap = ((MapValue)instructions).getMap();
110                     for ( Iterator JavaDoc j = instructionMap.entrySet().iterator(); j.hasNext(); ) {
111                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)j.next();
112                         String JavaDoc key = (String JavaDoc) entry.getKey();
113                         XPath xPath = XPath.newInstance(entry.getValue().toString());
114                         List JavaDoc nodeList = xPath.selectNodes(documentValue.getRootElement());
115                         resultMap.put(key, XPathQuery.createValueFromNodeList(nodeList));
116                     }
117                 }
118                 if ( includeContent ) {
119                     resultMap.put(CONTENT_ENTRY, content);
120                 }
121             }
122             arrayEntries.add(new MapValue(resultMap));
123         }
124         return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, new ArrayValue((Value [])arrayEntries.toArray(new Value[arrayEntries.size()])));
125     }
126     
127     public ParameterDescriptor[] getParameterDescriptors() {
128         return parameterDescriptors;
129     }
130
131     public ResultDescriptor getResultDescriptor() {
132         return resultDescriptor;
133     }
134 }
Popular Tags