KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.xml.querying.impl.xtas;
2 import org.exoplatform.services.xml.querying.InvalidSourceException;
3 import org.exoplatform.services.xml.querying.InvalidStatementException;
4 import org.exoplatform.services.xml.querying.QueryRunTimeException;
5 import org.exoplatform.services.xml.querying.UniFormTransformationException;
6 import org.exoplatform.services.xml.querying.impl.xtas.resource.Resource;
7 import org.exoplatform.services.xml.querying.impl.xtas.resource.ResourceResolver;
8
9 /**
10  * Resource processing based instruction
11  * @version $Id: ResourceInstruction.java 566 2005-01-25 12:50:49Z kravchuk $
12  */

13 public class ResourceInstruction extends Instruction {
14
15     private Object JavaDoc context;
16
17     /**
18      * Copy constructor
19      */

20     public ResourceInstruction(Instruction instr)
21     {
22        super(instr);
23     }
24
25     public ResourceInstruction(String JavaDoc type, String JavaDoc match, UniFormTree newValue) throws InvalidStatementException
26     {
27         super(type, match, newValue);
28     }
29
30     public String JavaDoc getAsString()
31     {
32         String JavaDoc matchStr = "";
33         if( match != null )
34             matchStr = " resource=\"" + match + "\"";
35
36         return "<" + type + matchStr + ">" +
37                 newValue.getAsString() +
38                "</" + type + ">";
39     }
40
41     /**
42     * Compiles instruction (does nothing for ResourceInstruction)
43     *
44     */

45     public void compile() throws InvalidStatementException
46     {
47        return;
48     }
49
50     /**
51     * Executes instruction
52     *
53     * */

54     public UniFormTree execute(UniFormTree input) throws InvalidSourceException, QueryRunTimeException
55     {
56
57
58        try {
59
60           Resource res = ResourceResolver.getInstance().getResource( match );
61           res.setContext( context );
62
63           if ( type == QueryType.CREATE ) {
64
65               // New Value for initial tree MUST be well-formed.
66
// MAY BE <resource name="name"> - REQUIRED
67
// <dtd systemId="systemId" publicId="publicId">
68
// [!CDATA]
69
// </dtd>
70
// </resource>
71

72
73                WellFormedUniFormTree initTree = UniFormConverter.toWellForm(newValue);
74 // initTree.setOmitXmlDeclaration(false);
75
res.create( initTree );
76
77                return newValue;
78
79           }
80
81           if ( type == QueryType.DROP ) {
82              res.drop();
83              return null;
84           }
85        } catch (UniFormTransformationException e) {
86
87             throw new InvalidSourceException("Bad Resource Instruction (possible newValue is not well-formed) : " + e);
88
89        } catch (Exception JavaDoc e) {
90
91             throw new QueryRunTimeException("Query Run Time Exception: " + e);
92        }
93
94        return null;
95
96     }
97
98     public void setContext(Object JavaDoc resourceContext)
99     {
100        this.context = resourceContext;
101     }
102
103 }
104
Popular Tags