KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attr JavaDoc;
57 import org.w3c.dom.Element JavaDoc;
58 import org.w3c.dom.NamedNodeMap JavaDoc;
59 import org.w3c.dom.Node JavaDoc;
60
61 import java.util.Hashtable JavaDoc;
62
63 /**
64  *
65  * @version $Id: InsertCommand.java,v 1.7 2003/05/21 13:27:50 jbreedveld Exp $
66  * @author <a HREF="http://www.smb-tec.com">SMB</a>
67  */

68 public abstract class InsertCommand extends CommandObject {
69
70   /* */
71   private Node JavaDoc current = null;
72   /* */
73   protected Node JavaDoc result = null;
74   /* */
75   private int state = -1;
76   /* */
77   private InsertStates[] states = null;
78   /* */
79   private static final int STATE_COUNT = 7;
80
81   /**
82    *
83    */

84   public InsertCommand(Node JavaDoc contextNode) throws Exception JavaDoc {
85     super(contextNode);
86     states = new InsertStates[STATE_COUNT];
87     states[0] = new InsertElement();
88     states[1] = new InsertAttribute();
89     states[2] = new InsertComment();
90     states[3] = new InsertText();
91     states[4] = new InsertCDATA();
92     states[5] = new InsertProcessingInstruction();
93     states[6] = new InsertVariable();
94   }
95
96
97   /**
98    *
99    */

100   public void submitAttributes(Hashtable JavaDoc attributes) {
101     if (state < 0) {
102       super.submitAttributes(attributes);
103     } else {
104       states[state].submitAttributes(attributes);
105     }
106   }
107
108
109   /**
110    *
111    */

112   public void submitCharacters(String JavaDoc data) {
113     if (state < 0) {
114       super.submitCharacters(data);
115     } else {
116       states[state].submitCharacters(data);
117     }
118   }
119
120
121   /**
122    *
123    */

124   public boolean submitInstruction(int instruction) throws Exception JavaDoc {
125     if (result == null) {
126       result = document.createElementNS(null, "temporaryXUpdateTree");
127       current = result;
128       document.getDocumentElement().appendChild(result);
129     }
130     if (state >= 0) {
131       current = states[state].execute(current);
132     }
133     switch (instruction) {
134       case CommandConstants.INSTRUCTION_ELEMENT:
135         state = 0;
136         break;
137       case CommandConstants.INSTRUCTION_ATTRIBUTE:
138         state = 1;
139         break;
140       case CommandConstants.INSTRUCTION_COMMENT:
141         state = 2;
142         break;
143       case CommandConstants.INSTRUCTION_TEXT:
144         state = 3;
145         break;
146       case CommandConstants.INSTRUCTION_CDATA:
147         state = 4;
148         break;
149       case CommandConstants.INSTRUCTION_PROCESSING_INSTRUCTION:
150         state = 5;
151         break;
152       case CommandConstants.INSTRUCTION_VALUE_OF:
153         state = 6;
154         break;
155       default:
156         state = -1;
157     }
158     if (state >= 0) {
159       states[state].reset();
160     }
161     return state >= 0;
162   }
163
164
165   /**
166    *
167    */

168   public boolean executeInstruction() throws Exception JavaDoc {
169     if (state >= 0) {
170       current = states[state].execute(current);
171       state = -1;
172     }
173     if (!current.equals(result)) {
174       switch (current.getNodeType()) {
175         case Node.ATTRIBUTE_NODE:
176           current = ((Attr JavaDoc) current).getOwnerElement();
177           return true;
178         default:
179           current = current.getParentNode();
180           return true;
181       }
182     }
183     return false;
184   }
185
186
187   /**
188    *
189    */

190   protected void insertAttributes(NamedNodeMap JavaDoc attributes, Node JavaDoc node) throws Exception JavaDoc {
191     if (attributes == null) {
192       return;
193     }
194     if (node.getNodeType() != Node.ELEMENT_NODE) {
195       throw new Exception JavaDoc("can't append attribute to !");
196     }
197     int attributesLength = attributes.getLength();
198     for (int j = 0; j < attributesLength; j++) {
199       Attr JavaDoc attribute = (Attr JavaDoc) attributes.item(j);
200       String JavaDoc namespaceURI = attribute.getNamespaceURI();
201       ((Element JavaDoc) node).setAttributeNS(namespaceURI, attribute.getNodeName(), attribute.getNodeValue());
202     }
203   }
204
205
206   /**
207    *
208    */

209   public abstract Node JavaDoc execute() throws Exception JavaDoc;
210 }
211
212
Popular Tags