KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > extractor > SimpleXmlExtractor


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

23
24 package org.apache.slide.extractor;
25
26 import org.apache.slide.common.PropertyName;
27 import org.apache.slide.util.conf.Configurable;
28 import org.apache.slide.util.conf.Configuration;
29 import org.apache.slide.util.conf.ConfigurationException;
30 import org.jdom.Attribute;
31 import org.jdom.Document;
32 import org.jdom.JDOMException;
33 import org.jdom.Text;
34 import org.jdom.input.SAXBuilder;
35 import org.jdom.xpath.XPath;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.util.*;
40
41 /**
42  * The SimpleXmlExtractor class
43  *
44  */

45 public class SimpleXmlExtractor extends AbstractPropertyExtractor implements Configurable {
46
47     protected List instructions = new ArrayList();
48
49     public SimpleXmlExtractor(String JavaDoc uri, String JavaDoc contentType, String JavaDoc namespace) {
50         super(uri, contentType, namespace);
51     }
52
53     public Map extract(InputStream JavaDoc content) throws ExtractorException {
54         Map properties = new HashMap();
55         try {
56             SAXBuilder saxBuilder = new SAXBuilder();
57             Document document = saxBuilder.build(content);
58             for (Iterator i = instructions.iterator(); i.hasNext();) {
59                 Instruction instruction = (Instruction) i.next();
60                 XPath xPath = instruction.getxPath();
61                 List nodeList = xPath.selectNodes(document);
62                 Object JavaDoc propertyValue = filter(nodeList, instruction);
63                 if (propertyValue != null) {
64                     properties.put(instruction.getPropertyName(), propertyValue);
65                 }
66             }
67         } catch (IOException JavaDoc e) {
68             throw new ExtractorException("Exception while retrieving content");
69         } catch (JDOMException e) {
70             throw new ExtractorException("Exception while parsing content. XML document must be wellformed.");
71         }
72         return properties;
73     }
74
75     public void configure(Configuration configuration) throws ConfigurationException {
76         Enumeration instructions = configuration.getConfigurations("instruction");
77         while (instructions.hasMoreElements()) {
78             Configuration instruction = (Configuration) instructions.nextElement();
79             addInstruction(createInstruction(instruction));
80         }
81     }
82
83     /**
84      * Allow subclasses to apply filtering to property values before they are written.
85      * Returning null signals that the extractor ignors this value.
86      *
87      * @param text the Node List identified by the xpath instruction.
88      * @return the property value to be set, <code>null</codee> if to be ignored.
89      */

90     protected Object JavaDoc filter(List nodeList, Instruction instruction) throws ExtractorException {
91         if (nodeList.size() > 0) {
92             if (nodeList.get(0) instanceof Text) {
93                 return ((Text) nodeList.get(0)).getText();
94             } else if (nodeList.get(0) instanceof Attribute) {
95                 return ((Attribute) nodeList.get(0)).getValue();
96             } else if (nodeList.get(0) instanceof String JavaDoc) {
97                 return nodeList.get(0);
98             }
99         }
100         return null;
101     }
102
103     protected void addInstruction(Instruction instruction) {
104         instructions.add(instruction);
105     }
106
107     protected Instruction createInstruction(Configuration instruction) throws ConfigurationException {
108         try {
109             String JavaDoc property = instruction.getAttribute("property");
110             String JavaDoc namespace = instruction.getAttribute("namespace", "DAV:");
111             XPath xPath = XPath.newInstance(instruction.getAttribute("xpath"));
112             return new Instruction(xPath, new PropertyName(property, namespace));
113         } catch (JDOMException e) {
114             throw new ConfigurationException("Could not create xPath from given attribute", instruction);
115         }
116     }
117     
118     protected static class Instruction {
119
120         private XPath xPath;
121         private PropertyName propertyName;
122
123         public Instruction(XPath xPath, PropertyName property) {
124             this.xPath = xPath;
125             this.propertyName = property;
126         }
127
128         public XPath getxPath() {
129             return xPath;
130         }
131
132         public PropertyName getPropertyName() {
133             return propertyName;
134         }
135     }
136 }
137
Popular Tags