KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > InstructionCompiler


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import javax.xml.transform.Transformer JavaDoc;
8 import javax.xml.transform.TransformerFactory JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.stream.StreamSource JavaDoc;
11 import javax.xml.transform.stream.StreamResult JavaDoc;
12 import org.exoplatform.services.xml.querying.InvalidStatementException;
13
14
15 /**
16  * Prepares query's instruction part of statement
17  * for executing
18  * (TrAX based transformation)
19  * @version $Id: InstructionCompiler.java 566 2005-01-25 12:50:49Z kravchuk $
20  */

21 public class InstructionCompiler {
22
23     protected Transformer JavaDoc queryResolver;
24
25     public InstructionCompiler(InputStream JavaDoc xslStream) throws InstructionCompilerException
26     {
27
28         try {
29
30             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
31             queryResolver = tFactory.newTransformer( new StreamSource JavaDoc( xslStream ) );
32
33         } catch (Exception JavaDoc e) {
34
35             throw new InstructionCompilerException("Can not instantiate an InstructionCompiler Reason: " + e);
36         }
37
38     }
39
40     /**
41      * Compiles instruction and returns it as Command object
42      *
43      */

44     public Command compile( String JavaDoc instruction ) throws InvalidStatementException
45     {
46         try {
47
48             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
49
50             StreamResult JavaDoc strRes = new StreamResult JavaDoc( os );
51
52             queryResolver.transform(new StreamSource JavaDoc(
53                  new ByteArrayInputStream JavaDoc(instruction.getBytes()) ), strRes);
54
55             return new Command( os.toByteArray() );
56
57         } catch (TransformerException JavaDoc e) {
58
59             throw new InvalidStatementException("Can not init XSLTStatement Reason: " + e);
60
61         }
62     }
63 }
64
Popular Tags