1 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 ; 38 import java.io.InputStream ; 39 import java.util.*; 40 41 45 public class SimpleXmlExtractor extends AbstractPropertyExtractor implements Configurable { 46 47 protected List instructions = new ArrayList(); 48 49 public SimpleXmlExtractor(String uri, String contentType, String namespace) { 50 super(uri, contentType, namespace); 51 } 52 53 public Map extract(InputStream 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 propertyValue = filter(nodeList, instruction); 63 if (propertyValue != null) { 64 properties.put(instruction.getPropertyName(), propertyValue); 65 } 66 } 67 } catch (IOException 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 90 protected Object 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 ) { 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 property = instruction.getAttribute("property"); 110 String 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 |