1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.ByteArrayInputStream ; 5 6 import javax.xml.transform.Transformer ; 7 import javax.xml.transform.TransformerFactory ; 8 import javax.xml.transform.TransformerException ; 9 import javax.xml.transform.TransformerConfigurationException ; 10 import javax.xml.transform.stream.StreamSource ; 11 import javax.xml.transform.stream.StreamResult ; 12 import org.exoplatform.services.xml.querying.InvalidStatementException; 13 import org.exoplatform.services.xml.querying.QueryRunTimeException; 14 15 20 21 public class QueryProcessor { 22 23 private static TransformerFactory tFactory = TransformerFactory.newInstance(); 24 private Transformer transformer; 25 26 protected UniFormTree process(Command command, UniFormTree input) throws QueryRunTimeException 27 { 28 if( transformer == null ) 29 throw new QueryRunTimeException("QueryProcessor.process(): Query Run Time Exception. Transformer can not be NULL. Set init first! "); 30 31 try { 32 33 UniFormTreeFragment fragment = new UniFormTreeFragment (); 34 35 if(!input.isEmpty()) { 36 ByteArrayOutputStream os = new ByteArrayOutputStream (); 38 39 transformer.transform( new StreamSource (input.getAsInputStream()), new StreamResult ( os ) ); 40 41 fragment.init( new ByteArrayInputStream (os.toByteArray()) ); 42 43 44 } 46 return fragment; 47 48 } catch (Exception e) { 50 51 throw new QueryRunTimeException("QueryProcessor.process(): Query Run Time Exception: " + e); 52 } 53 54 } 55 56 public void init(Command command) throws InvalidStatementException 57 { 58 try { 59 60 transformer = tFactory.newTransformer(new StreamSource (command.getAsInputStream())); 61 64 65 } catch (Exception e) { 66 67 throw new InvalidStatementException("QueryProcessor.init(): failed Reason: " + e); 68 } 69 70 } 71 72 public void setParameter(String name, String value) throws InvalidStatementException 73 { 74 try { 75 76 transformer.setParameter(name, value); 77 78 } catch (Exception e) { 79 80 throw new InvalidStatementException("QueryProcessor.init(): failed Reason: " + e); 81 } 82 83 } 84 85 } 86 | Popular Tags |