1 22 23 package org.xquark.xml.xqueryevaluator.eval; 24 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.parsers.SAXParserFactory ; 32 33 import org.xml.sax.*; 34 import org.xquark.mediator.DOMUtils.BufferTuple; 35 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 36 import org.xquark.mediator.plan.DynamicContext; 37 import org.xquark.mediator.runtime.MediatorException; 38 import org.xquark.schema.SchemaManager; 39 import org.xquark.schema.validation.SchemaValidationContext; 40 import org.xquark.schema.validation.ValidatingSchemaFilter; 41 import org.xquark.schema.validation.ValidatingSchemaHandler; 42 import org.xquark.xquery.parser.*; 43 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionDOC; 44 45 48 public class EvalFilter { 49 50 54 private static final String RCSRevision = "$Revision: 1.10 $"; 55 private static final String RCSName = "$Name: $"; 56 57 61 private static SAXParserFactory saxParserFactory = null; 62 static 63 { 64 saxParserFactory = SAXParserFactory.newInstance(); 66 saxParserFactory.setNamespaceAware(true); 67 saxParserFactory.setValidating(false); 68 } 69 70 private XMLReader saxReader = null; 71 private InputSource inputSource = null; 72 private XPPReader xppReader = null; 73 private BufferTuple buftuples = null; 74 private EvaluationVisitor evalVisitor = null; 75 76 80 public EvalFilter(BufferTuple buftuples, DynamicContext context, ArrayList paths, FLWRExpression flwr) throws MediatorException { 81 this(buftuples, context, paths, flwr, true); 82 } 83 84 public EvalFilter(BufferTuple buftuples, DynamicContext context, ArrayList paths, FLWRExpression flwr, boolean useXPP) throws MediatorException { 85 this.buftuples = buftuples; 86 try { 87 SchemaValidationContext svc = new SchemaValidationContext(new SchemaManager(context.getSchemaManager())); 88 SAX2TypedDOM handler = new SAX2TypedDOM(flwr,buftuples,context,paths,svc); 89 Variable var = (Variable)flwr.getVariables().get(0); 90 String doc = null; 91 XQueryExpression arg = null; 92 if (var.getExpression() instanceof LocatedExpression) 93 arg = ((FunctionDOC)((Step)var.getExpression().getSteps().get(0)).getExpression()).getArgument(0); 94 else if (var.getExpression() instanceof FunctionDOC) 95 arg = ((FunctionDOC)var.getExpression()).getArgument(0); 96 97 if (arg instanceof ValueString) 98 doc = ((ValueString)arg).getStringValue(); 99 else { 100 if (evalVisitor == null) 101 evalVisitor = new EvaluationVisitor(context.getSchemaManager(),context.getCurrentTuples()); 102 else 103 evalVisitor.reset(context.getCurrentTuples()); 104 evalVisitor.setReturnType(EvaluationVisitor.VALUE_TYPE); 105 try { 106 arg.accept(evalVisitor); 107 } catch (XQueryException xqe) { 108 throw new MediatorException("Incorrect argument of function doc : " + arg.getStringValue()); 109 } 110 Comparable comp = evalVisitor.getResValue(); 111 if (comp != null) 112 doc = comp.toString(); 113 } 114 if (doc == null) 115 throw new MediatorException("Incorrect argument of function doc : " + arg.getStringValue()); 116 if (doc.startsWith("*:")) doc = doc.substring(2); 117 URL url; 118 if (context.getBaseURI() != null) url = new URL (new URL (context.getBaseURI()),doc); 119 else url = new URL (doc); 120 if (useXPP) { 121 xppReader = new XPPReader(); 122 ValidatingSchemaHandler filter = new ValidatingSchemaHandler(svc, false); 123 filter.setContentHandler(handler); 124 filter.setLexicalHandler(handler); 125 filter.setErrorHandler( 126 new ErrorHandler() { 127 public void warning(SAXParseException ex) throws SAXException { 128 } 129 130 public void error(SAXParseException ex) throws SAXException { 131 throw ex; 132 } 133 134 public void fatalError(SAXParseException ex) throws SAXException { 135 throw ex; 136 } 137 }); 138 xppReader.setContentHandler(filter); 139 xppReader.setLexicalHandler(filter); 140 xppReader.setErrorHandler(filter); 141 xppReader.setInput(url.toString(), url.openStream()); 142 } else { 143 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader(); 144 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, svc, false); 145 filter.setContentHandler(handler); 146 filter.setLexicalHandler(handler); 147 filter.setErrorHandler( 148 new ErrorHandler() { 149 public void warning(SAXParseException ex) throws SAXException { 150 } 151 152 public void error(SAXParseException ex) throws SAXException { 153 throw ex; 154 } 155 156 public void fatalError(SAXParseException ex) throws SAXException { 157 throw ex; 158 } 159 }); 160 saxReader = filter; 161 inputSource = new InputSource(url.toString()); 162 } 163 } catch (SAXException e) { 164 throw new MediatorException(e.getMessage()); 165 } catch (MalformedURLException e) { 166 throw new MediatorException(e.getMessage()); 167 } catch (IOException e) { 168 throw new MediatorException(e.getMessage()); 169 } catch (ParserConfigurationException e) { 170 throw new MediatorException(e.getMessage()); 171 } 172 } 173 177 public boolean evaluate() throws MediatorException { 178 if (xppReader != null) return evaluateXPP(); 179 else return evaluateSAX(); 180 } 181 182 public boolean evaluateSAX() throws MediatorException { 183 try { 184 saxReader.parse(inputSource); 185 return true; 186 } 187 catch (IOException e) { 188 throw new MediatorException(e.getMessage()); 189 } 190 catch (SAXException e) { 191 throw new MediatorException(e.getMessage()); 192 } 193 } 194 195 public boolean evaluateXPP() throws MediatorException { 196 try { 197 boolean isDone = false; 198 if (xppReader.isAtStart()) xppReader.getContentHandler().startDocument(); 199 while (buftuples.isEmpty() && !isDone) { 200 isDone = xppReader.parseNextElement(); 201 } 202 if (isDone) xppReader.getContentHandler().endDocument(); 203 return isDone; 204 } catch (IOException e) { 205 throw new MediatorException(e.getMessage()); 206 } catch (SAXException e) { 207 throw new MediatorException(e.getMessage()); 208 } 209 } 210 211 } 212 | Popular Tags |