KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmldb > xupdate > lexus > XUpdateQueryParser


1 package org.xmldb.xupdate.lexus;
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.xml.sax.Attributes JavaDoc;
57 import org.xml.sax.ContentHandler JavaDoc;
58 import org.xml.sax.Locator JavaDoc;
59 import org.xml.sax.SAXException JavaDoc;
60 import org.xmldb.xupdate.lexus.commands.CommandConstants;
61
62 import java.util.HashMap JavaDoc;
63 import java.util.Hashtable JavaDoc;
64 import java.util.Vector JavaDoc;
65
66 /**
67  * This class parses the query-String by a SAXParser. The SAXEvents are
68  * converted to a list of Integers.
69  *
70  * @version $Revision: 1.6 $ $Date: 2003/05/21 13:27:50 $
71  * @author <a HREF="http://www.smb-tec.com">SMB</a>
72  * @author <a HREF="mailto:tdean@gr.com">Timothy M. Dean</a>
73  */

74 public class XUpdateQueryParser implements ContentHandler JavaDoc {
75   org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(XUpdateQueryParser.class);
76
77   /* The list of Integers. */
78   private Vector JavaDoc commands = null;
79   /* The list of all attributes as Hashtable for each element. */
80   private Vector JavaDoc attributes = null;
81   /* The list of all characters for each element. */
82   private Vector JavaDoc characters = null;
83   /* The list of mappings for namespace prefixes */
84   private HashMap JavaDoc namespaces = null;
85   /* The Integer representation of each XUpdate-command. */
86   private CommandConstants consts = null;
87   /* A flag indicating whether we are within an XUpdateOperation. */
88   private boolean xupdateInsertOperation = false;
89   /* The name of the last operation. */
90   private String JavaDoc lastOperation = "";
91
92   /**
93    * @param constants the Object containing the Integer representation.
94    */

95   public XUpdateQueryParser(CommandConstants constants) {
96     consts = constants;
97     commands = new Vector JavaDoc();
98     attributes = new Vector JavaDoc();
99     characters = new Vector JavaDoc();
100     namespaces = new HashMap JavaDoc();
101   }
102
103
104   /**
105    *
106    */

107   public void startDocument() {
108   }
109
110
111   /**
112    *
113    */

114   public void endDocument() {
115   }
116
117
118   /**
119    *
120    */

121   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
122                            String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
123     Hashtable JavaDoc attributesHash = new Hashtable JavaDoc();
124     for (int i = 0; i < atts.getLength(); i++) {
125       attributesHash.put(atts.getLocalName(i), atts.getValue(i));
126     }
127     if (namespaceURI.equals(XUpdateQueryImpl.NAMESPACE_URI)) {
128       int id = consts.idForString(localName);
129       if (id != 0) {
130         xupdateInsertOperation = consts.isInsertOperation(id) ? true : xupdateInsertOperation;
131         commands.addElement(new Integer JavaDoc(id));
132         if (consts.isInstruction(id) && !xupdateInsertOperation) {
133           throw new SAXException JavaDoc("instruction <" + qName + "> is not valid for operation <" + lastOperation + "> !");
134         }
135         if (!attributesHash.isEmpty()) {
136           commands.addElement(new Integer JavaDoc(consts.ATTRIBUTES));
137           attributes.addElement(attributesHash);
138         }
139       }
140     } else if (xupdateInsertOperation) {
141       log.debug(".startElement(): xupdateInsertOperation == true" + namespaceURI + " " + qName);
142       commands.addElement(new Integer JavaDoc(consts.INSTRUCTION_ELEMENT));
143       Hashtable JavaDoc temp = new Hashtable JavaDoc();
144       temp.put("name", qName);
145       temp.put("namespace", namespaceURI);
146       commands.addElement(new Integer JavaDoc(consts.ATTRIBUTES));
147       attributes.addElement(temp);
148       for (int i = 0; i < atts.getLength(); i++) {
149         commands.addElement(new Integer JavaDoc(consts.INSTRUCTION_ATTRIBUTE));
150         temp = new Hashtable JavaDoc();
151         temp.put("name", atts.getQName(i));
152         if (atts.getURI(i) != null) {
153           temp.put("namespace", atts.getURI(i));
154         }
155         commands.addElement(new Integer JavaDoc(consts.ATTRIBUTES));
156         attributes.addElement(temp);
157         commands.addElement(new Integer JavaDoc(consts.CHARACTERS));
158         characters.addElement(atts.getValue(i));
159         commands.addElement(new Integer JavaDoc(-consts.INSTRUCTION_ATTRIBUTE));
160       }
161     } else {
162       log.error(".startElement(): " + "no insert-operation for element <" + qName + "> or wrong XUpdate-Namespace !");
163       throw new SAXException JavaDoc("no insert-operation for element <" + qName + "> or wrong XUpdate-Namespace !");
164     }
165     lastOperation = qName;
166   }
167
168
169   /**
170    *
171    */

172   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) {
173     if (namespaceURI.equals(XUpdateQueryImpl.NAMESPACE_URI)) {
174       int id = consts.idForString(localName);
175       if (id != 0) {
176         commands.addElement(new Integer JavaDoc(-id));
177         xupdateInsertOperation = consts.isInsertOperation(id) ? false : xupdateInsertOperation;
178       }
179     } else if (xupdateInsertOperation) {
180       commands.addElement(new Integer JavaDoc(-consts.INSTRUCTION_ELEMENT));
181     }
182   }
183
184
185   /**
186    * If a new namespace mapping will be found we save these information
187    * for further processing.
188    *
189    * @param prefix
190    * @param uri
191    */

192   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
193     if ((prefix != null) && (prefix.length() > 0)) {
194       namespaces.put(prefix, uri);
195     } else {
196       namespaces.put(null, uri);
197     }
198   }
199
200
201   /**
202    *
203    * @param prefix The namespace prefix to be ended.
204    */

205   public void endPrefixMapping(String JavaDoc prefix) {
206   }
207
208
209   /**
210    *
211    */

212   public void characters(char[] ch, int start, int length) {
213     if (!commands.isEmpty()) {
214       int lastInteger = ((Integer JavaDoc) commands.lastElement()).intValue();
215       if (xupdateInsertOperation && lastInteger < 0) {
216         commands.addElement(new Integer JavaDoc(consts.INSTRUCTION_TEXT));
217       }
218       commands.addElement(new Integer JavaDoc(consts.CHARACTERS));
219       characters.addElement(new String JavaDoc(ch, start, length));
220       if (xupdateInsertOperation && lastInteger < 0) {
221         commands.addElement(new Integer JavaDoc(-consts.INSTRUCTION_TEXT));
222       }
223     }
224   }
225
226
227   /**
228    *
229    */

230   public void ignorableWhitespace(char[] ch, int start, int length) {
231   }
232
233
234   /**
235    *
236    */

237   public void processingInstruction(String JavaDoc target, String JavaDoc data) {
238   }
239
240
241   /**
242    *
243    */

244   public void setDocumentLocator(Locator JavaDoc locator) {
245   }
246
247
248   /**
249    *
250    */

251   public void skippedEntity(String JavaDoc name) {
252   }
253
254
255   /**
256    * Returns the cached query String as a representation of three Vectors.
257    *
258    * @return The cached query String.
259    */

260   public Vector JavaDoc[] getCachedQuery() {
261     return new Vector JavaDoc[]{commands, attributes, characters};
262   }
263
264   /**
265    * Returns a Map of all namespace prefixes and their proper
266    * namespace URIs.
267    *
268    * @return All saved namespace mappings.
269    * @since XML:DB Lexus 0.3
270    */

271   public HashMap JavaDoc getNamespaceMappings() {
272     return namespaces;
273   }
274
275 }
276
277
Popular Tags