KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import java.util.HashMap JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import org.exoplatform.services.xml.querying.InvalidSourceException;
8 import org.exoplatform.services.xml.querying.InvalidStatementException;
9 import org.exoplatform.services.xml.querying.QueryRunTimeException;
10 import org.exoplatform.services.xml.querying.UniFormTransformationException;
11
12 /**
13  * Statement instruction (action to do)
14  * For now only one instruction for query will proccessed
15  * @version $Id: Instruction.java 566 2005-01-25 12:50:49Z kravchuk $
16  */

17 abstract public class Instruction {
18
19     private final static String JavaDoc XTAS_XSL = "/xtas.xsl";
20
21     protected static InstructionCompiler compiler;
22
23     protected String JavaDoc match;
24     protected UniFormTree newValue;
25     protected String JavaDoc type;
26
27     static {
28
29           try {
30
31              String JavaDoc xtasXsl = System.getProperty("xtas.xsl");
32              InputStream JavaDoc is = null;
33              if( xtasXsl == null || xtasXsl.equals("") )
34                  is = Instruction.class.getResourceAsStream(XTAS_XSL);
35               else
36                  is = new FileInputStream JavaDoc(xtasXsl);
37
38               compiler = new InstructionCompiler( is );
39
40          } catch (Exception JavaDoc e) {
41
42              // @ todo Log system!
43
System.out.println("Instruction() create ERROR: " + e);
44
45          }
46
47     }
48
49
50     protected Instruction(String JavaDoc type, String JavaDoc match, UniFormTree newValue) throws InvalidStatementException
51     {
52
53         if( type == null || type.trim() == "")
54             throw new InvalidStatementException( "Query Type can not be NULL !" );
55
56         this.type = type.toLowerCase();
57         if( ! QueryType.exists(this.type) )
58             throw new InvalidStatementException("Illegal query type '"+this.type+"' !");
59
60         if( match == null || match.trim() == "")
61               throw new InvalidStatementException( "Match attribute is required for '"+type+"' statement");
62
63         this.match = match;
64
65         try {
66
67             if (newValue == null) {
68
69                this.newValue = new UniFormTreeFragment ();
70                ((UniFormTreeFragment)this.newValue).init( new ByteArrayInputStream JavaDoc( "".getBytes() ) );
71
72             } else
73                this.newValue = newValue;
74
75         } catch (UniFormTransformationException e) {
76
77             // @ todo Log system!
78
System.out.println("Instruction() create error(UniFormTransformationException) : " + e);
79
80         }
81
82     }
83
84     /**
85      * Copy constructor
86      */

87     protected Instruction(Instruction instr)
88     {
89
90         this.type = instr.type;
91         this.match = instr.match;
92         this.newValue = instr.newValue;
93
94     }
95
96     public UniFormTree getNewValue()
97     {
98         return newValue;
99     }
100
101     public String JavaDoc getMatch()
102     {
103         return match;
104     }
105
106     public String JavaDoc getType()
107     {
108         return type;
109     }
110
111     public boolean isAtResource()
112     {
113         return QueryType.isAtResource( type );
114     }
115
116     public boolean isAtXml()
117     {
118         return QueryType.isAtXml( type );
119     }
120
121      /**
122      * Gets query statement as String
123      * */

124     public String JavaDoc toString()
125     {
126         return getAsString();
127     }
128
129
130      /**
131      * Gets instruction as String
132      * */

133     public abstract String JavaDoc getAsString();
134
135      /**
136      * Executes instruction
137      * */

138     public abstract UniFormTree execute(UniFormTree input) throws InvalidSourceException, QueryRunTimeException;
139
140      /**
141      * Compiles instruction
142      *
143      * */

144     public abstract void compile() throws InvalidStatementException;
145
146
147      /**
148      * Sets context
149      *
150      * */

151     public abstract void setContext(Object JavaDoc resourceContext);
152
153 }
154
Popular Tags