1 37 package org.webharvest.runtime.processors; 38 39 import java.io.StringReader ; 40 41 import javax.xml.transform.stream.StreamSource ; 42 43 import net.sf.saxon.Configuration; 44 import net.sf.saxon.om.Item; 45 import net.sf.saxon.om.SequenceIterator; 46 import net.sf.saxon.query.*; 47 import net.sf.saxon.trans.XPathException; 48 49 import org.webharvest.definition.XPathDef; 50 import org.webharvest.exception.ScraperXPathException; 51 import org.webharvest.runtime.Scraper; 52 import org.webharvest.runtime.ScraperContext; 53 import org.webharvest.runtime.templaters.BaseTemplater; 54 import org.webharvest.runtime.variables.*; 55 import org.webharvest.utils.CommonUtil; 56 57 60 public class XPathProcessor extends BaseProcessor { 61 62 private XPathDef xpathDef; 63 64 public XPathProcessor(XPathDef xpathDef) { 65 super(xpathDef); 66 this.xpathDef = xpathDef; 67 } 68 69 public IVariable execute(Scraper scraper, ScraperContext context) { 70 IVariable xml = getBodyTextContent(xpathDef, scraper, context); 71 String expression = BaseTemplater.execute( xpathDef.getExpression(), scraper.getScriptEngine() ); 72 73 try { 74 String input = xml.toString(); 75 76 final Configuration config = new Configuration(); 77 final StaticQueryContext sqc = new StaticQueryContext(config); 78 final XQueryExpression exp = sqc.compileQuery(expression); 79 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 80 81 StringReader reader = new StringReader (input); 82 dynamicContext.setContextItem(sqc.buildDocument(new StreamSource (reader))); 83 final SequenceIterator iter = exp.iterator(dynamicContext); 84 85 ListVariable listVariable = new ListVariable(); 86 while (true) { 87 Item item = iter.next(); 88 if (item == null) { 89 break; 90 } 91 92 String value = CommonUtil.serializeItem(item, config); 93 listVariable.addVariable( new NodeVariable(value) ); 94 } 95 96 return listVariable; 97 } catch (XPathException e) { 98 e.printStackTrace(); 99 throw new ScraperXPathException("Error parsing XPath expression (XPath = [" + expression + "])!", e); 100 } 101 } 102 103 } | Popular Tags |