KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > xml > XPathQuery


1 package org.apache.slide.projector.processor.xml;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.apache.slide.projector.ConfigurableProcessor;
8 import org.apache.slide.projector.ConfigurationException;
9 import org.apache.slide.projector.ContentType;
10 import org.apache.slide.projector.Context;
11 import org.apache.slide.projector.ProcessException;
12 import org.apache.slide.projector.descriptor.ParameterDescriptor;
13 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
14 import org.apache.slide.projector.descriptor.StringValueDescriptor;
15 import org.apache.slide.projector.i18n.DefaultMessage;
16 import org.apache.slide.projector.i18n.ErrorMessage;
17 import org.apache.slide.projector.i18n.ParameterMessage;
18 import org.apache.slide.projector.processor.SimpleProcessor;
19 import org.apache.slide.projector.value.ArrayValue;
20 import org.apache.slide.projector.value.ElementValue;
21 import org.apache.slide.projector.value.StreamableValue;
22 import org.apache.slide.projector.value.StringValue;
23 import org.apache.slide.projector.value.Value;
24 import org.apache.slide.projector.value.XMLValue;
25 import org.jdom.Attribute;
26 import org.jdom.CDATA;
27 import org.jdom.Comment;
28 import org.jdom.Element;
29 import org.jdom.ProcessingInstruction;
30 import org.jdom.Text;
31 import org.jdom.xpath.XPath;
32
33 /**
34  * @version $Revision: 1.3 $
35  */

36
37 public class XPathQuery extends SimpleProcessor implements ConfigurableProcessor {
38     private Element rootElement;
39
40     public Value process(Value input, Context context) throws Exception JavaDoc {
41         XPath xPath = XPath.newInstance(((StringValue)input).toString());
42         List JavaDoc nodeList = xPath.selectNodes(rootElement);
43         return createValueFromNodeList(nodeList);
44     }
45
46     public ParameterDescriptor getParameterDescriptor() {
47         return new ParameterDescriptor(INPUT, new ParameterMessage("xPathQuery/parameter/input"), new StringValueDescriptor());
48     }
49
50     public ResultEntryDescriptor getResultEntryDescriptor() {
51         return new ResultEntryDescriptor(OUTPUT, new DefaultMessage("xPathQuery/result/output"), ContentType.DYNAMIC, true);
52     }
53
54     public void configure(StreamableValue config) throws ConfigurationException {
55         if ( config instanceof XMLValue ) {
56             rootElement = ((XMLValue)config).getRootElement();
57         } else {
58             throw new ConfigurationException(new ErrorMessage("xpathQuery/xmlResourceRequired"));
59         }
60     }
61
62     public static Value createValueFromNodeList(List JavaDoc nodeList) throws ProcessException {
63         if ( nodeList.size() > 1 ) {
64             List JavaDoc resources = new ArrayList JavaDoc();
65             for ( Iterator JavaDoc i = nodeList.iterator(); i.hasNext(); ) {
66                 resources.add(createValueFromNode(i.next()));
67             }
68             Value[] array = new Value[resources.size()];
69             return new ArrayValue((Value [])resources.toArray(array));
70         } else {
71             return createValueFromNode(nodeList.get(0));
72         }
73     }
74     
75     public static Value createValueFromNode(Object JavaDoc node) throws ProcessException {
76         if ( node instanceof Element ) {
77             return new ElementValue((Element)node);
78         } else if ( node instanceof Attribute ) {
79             return new StringValue(((Attribute)node).getValue());
80         } else if ( node instanceof Text ) {
81             return new StringValue(((Text)node).getText());
82         } else if ( node instanceof Comment ) {
83             return new StringValue(((Comment)node).getText());
84         } else if ( node instanceof CDATA ) {
85             return new StringValue(((CDATA)node).toString());
86         } else if ( node instanceof ProcessingInstruction ) {
87             return new StringValue(((ProcessingInstruction)node).getData());
88         }
89         throw new ProcessException(new ErrorMessage("unconvertableJDomNode"));
90     }
91 }
Popular Tags