KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmldb > xupdate > lexus > commands > CommandConstants


1 package org.xmldb.xupdate.lexus.commands;
2
3 /*
4  * The XML:DB Initiative Software License, Version 1.0
5  *
6  *
7  * Copyright (c) 2000-2003 The XML:DB Initiative. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * XML:DB Initiative (http://www.xmldb.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The name "XML:DB Initiative" must not be used to endorse or
30  * promote products derived from this software without prior written
31  * permission. For written permission, please contact info@xmldb.org.
32  *
33  * 5. Products derived from this software may not be called "XML:DB",
34  * nor may "XML:DB" appear in their name, without prior written
35  * permission of the XML:DB Initiative.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the XML:DB Initiative. For more information
53  * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
54  */

55
56 import org.w3c.dom.Node JavaDoc;
57
58 import java.util.Hashtable JavaDoc;
59
60 /**
61  * This class represents all XUpdate commands as Integer values. It returns
62  * the Integer for the given String and also the Commandobject for the given
63  * Integer.
64  *
65  * @version $Id: CommandConstants.java,v 1.5 2003/05/21 13:27:50 jbreedveld Exp $
66  * @author <a HREF="http://www.smb-tec.com">SMB</a>
67  */

68 public class CommandConstants extends Object JavaDoc {
69
70   /* represents <code>xupdate:modifications</code>, the root element*/
71   public static final int ROOT_ELEMENT = 0;
72   /* */
73   public static final int COMMAND_COUNT = 7;
74   /* represents <code>xupdate:remove</code> */
75   public static final int COMMAND_REMOVE = 1;
76   /* represents <code>xupdate:rename</code> */
77   public static final int COMMAND_RENAME = 2;
78   /* represents <code>xupdate:update</code> */
79   public static final int COMMAND_UPDATE = 3;
80   /* represents <code>xupdate:variable</code> */
81   public static final int COMMAND_VARIABLE = 4;
82   /* represents <code>xupdate:append</code> */
83   public static final int COMMAND_APPEND = 5;
84   /* represents <code>xupdate:insert-before</code> */
85   public static final int COMMAND_INSERT_BEFORE = 6;
86   /* represents <code>xupdate:insert-after</code> */
87   public static final int COMMAND_INSERT_AFTER = 7;
88   /* */
89   protected static final int FIRST_INSTRUCTION = 100;
90   /* represents <code>xupdate:element</code> */
91   public static final int INSTRUCTION_ELEMENT = 101;
92   /* represents <code>xupdate:attribute</code> */
93   public static final int INSTRUCTION_ATTRIBUTE = 102;
94   /* represents <code>xupdate:comment</code> */
95   public static final int INSTRUCTION_COMMENT = 103;
96   /* represents <code>xupdate:text</code> */
97   public static final int INSTRUCTION_TEXT = 104;
98   /* represents <code>xupdate:cdata</code> */
99   public static final int INSTRUCTION_CDATA = 105;
100   /* represents <code>xupdate:processing-instruction</code> */
101   public static final int INSTRUCTION_PROCESSING_INSTRUCTION = 106;
102   /* represents <code>xupdate:value-of</code> */
103   public static final int INSTRUCTION_VALUE_OF = 107;
104   /* */
105   protected static final int LAST_INSTRUCTION = 200;
106   /* */
107   public static final int ATTRIBUTES = 200;
108   /* */
109   public static final int CHARACTERS = 300;
110
111   /* Contains the allocation of String and Integer. */
112   private Hashtable JavaDoc ids = null;
113   /* Contains the Command-object for each COMMAND. */
114   private CommandObject[] command = null;
115   /* A Hashtable containing all variable names and its selected nodes. */
116   public static TempTree tempTree = null;
117
118   /**
119    *
120    */

121   public CommandConstants() {
122     tempTree = new TempTree();
123     initTable();
124   }
125
126
127   /**
128    * Maps the given String to a Integer value.
129    * @param localName the name of a XUpdate-command.
130    * @return the Integer value for this String. If there is no such XUpdate
131    * command, it returns 0.
132    */

133   public int idForString(String JavaDoc localName) {
134     Integer JavaDoc result = (Integer JavaDoc) ids.get(localName);
135     if (result == null) {
136       return 0;
137     }
138     return result.intValue();
139   }
140
141
142   /**
143    * Maps the given integer to a COMMAND object.
144    * @param id the ID for a XUpdate command.
145    * @return the object which performs the XUpdate operation. If there is a
146    * no such ID or the given ID stands for an instruction it returns
147    * null.
148    */

149   public CommandObject commandForID(int id) {
150     switch (id) {
151       case COMMAND_REMOVE:
152         return command[0];
153       case COMMAND_RENAME:
154         return command[1];
155       case COMMAND_UPDATE:
156         return command[2];
157       case COMMAND_VARIABLE:
158         return command[3];
159       case COMMAND_APPEND:
160         return command[4];
161       case COMMAND_INSERT_BEFORE:
162         return command[5];
163       case COMMAND_INSERT_AFTER:
164         return command[6];
165     }
166     return null;
167   }
168
169
170   /**
171    * Sets the contextNode which is to be updated and initializes the objects
172    * for each XUpdate operation.
173    * @param node the contextNode which is to be updated.
174    * @throws IllegalArgumentException if node is null.
175    */

176   public void setContextNode(Node JavaDoc node) throws Exception JavaDoc {
177     if (node == null) {
178       throw new IllegalArgumentException JavaDoc("context node must not be null !");
179     }
180
181     command = new CommandObject[COMMAND_COUNT];
182     command[0] = new RemoveCommand(node);
183     command[1] = new RenameCommand(node);
184     command[2] = new UpdateCommand(node);
185     command[3] = new VariableCommand(node);
186     command[4] = new AppendCommand(node);
187     command[5] = new InsertBeforeCommand(node);
188     command[6] = new InsertAfterCommand(node);
189   }
190
191
192   /**
193    * @return true if the given id is an insert operation
194    */

195   public boolean isInsertOperation(int id) {
196     return id == COMMAND_INSERT_BEFORE
197             || id == COMMAND_INSERT_AFTER
198             || id == COMMAND_APPEND;
199   }
200
201
202   /**
203    * @return true if the given id is an instruction
204    */

205   public boolean isInstruction(int id) {
206     return FIRST_INSTRUCTION < id && id < LAST_INSTRUCTION;
207   }
208
209
210   /**
211    * Initializes the Hashtable which converts the String representation
212    * of the XUpdate operation and instruction to the Integer representation.
213    */

214   protected void initTable() {
215     ids = new Hashtable JavaDoc();
216     //root element
217
ids.put("modifications", new Integer JavaDoc(ROOT_ELEMENT));
218     //commands
219
ids.put("remove", new Integer JavaDoc(COMMAND_REMOVE));
220     ids.put("rename", new Integer JavaDoc(COMMAND_RENAME));
221     ids.put("update", new Integer JavaDoc(COMMAND_UPDATE));
222     ids.put("variable", new Integer JavaDoc(COMMAND_VARIABLE));
223     ids.put("append", new Integer JavaDoc(COMMAND_APPEND));
224     ids.put("insert-before", new Integer JavaDoc(COMMAND_INSERT_BEFORE));
225     ids.put("insert-after", new Integer JavaDoc(COMMAND_INSERT_AFTER));
226     //instructions
227
ids.put("element", new Integer JavaDoc(INSTRUCTION_ELEMENT));
228     ids.put("attribute", new Integer JavaDoc(INSTRUCTION_ATTRIBUTE));
229     ids.put("comment", new Integer JavaDoc(INSTRUCTION_COMMENT));
230     ids.put("text", new Integer JavaDoc(INSTRUCTION_TEXT));
231     ids.put("cdata", new Integer JavaDoc(INSTRUCTION_CDATA));
232     ids.put("value-of", new Integer JavaDoc(INSTRUCTION_VALUE_OF));
233     ids.put("processing-instruction",
234             new Integer JavaDoc(INSTRUCTION_PROCESSING_INSTRUCTION));
235
236   }
237 }
238
239
Popular Tags