KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > parser > MetaDataParser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata.parser;
13
14 import org.xml.sax.Attributes JavaDoc;
15 import org.xml.sax.helpers.AttributesImpl JavaDoc;
16 import org.xml.sax.helpers.DefaultHandler JavaDoc;
17
18
19 import org.xml.sax.InputSource JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import com.versant.core.common.JaxpUtils;
22
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import javax.xml.parsers.SAXParserFactory JavaDoc;
25  
26
27  
28
29 import com.versant.core.metadata.MDStatics;
30
31 import java.io.FileInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 import com.versant.core.common.BindingSupportImpl;
40
41 /**
42  * This produces a tree of JdoXXX objects representing the structure of a
43  * JDO meta data file (.jdo). All of our vendor extensions are valdidated
44  * (correct keys) as part of this process.
45  */

46 public class MetaDataParser extends DefaultHandler JavaDoc
47 {
48
49     public static final String JavaDoc VENDOR_NAME = "versant";
50     public static final String JavaDoc VENDOR_NAME_JDOGENIE = "jdogenie";
51
52
53     private SAXParserFactory JavaDoc parserFactory;
54     private SAXParser JavaDoc parser;
55
56     private int state;
57     private int extState;
58     private int extSkipState;
59     private String JavaDoc elementName;
60     //private int elementDepth;
61
private int extensionSkipDepth;
62
63     // these fields form a 'stack' of elements
64
private JdoRoot jdoRoot;
65     private JdoPackage jdoPackage;
66     private JdoClass jdoClass;
67     private JdoField jdoField;
68     private JdoCollection jdoCollection;
69     private JdoMap jdoMap;
70     private JdoArray jdoArray;
71     private JdoQuery jdoQuery;
72     private int extStackTop;
73     private JdoExtension[] extStack = new JdoExtension[32];
74     private ArrayList JavaDoc[] extStackList = new ArrayList JavaDoc[32];
75
76     private ArrayList JavaDoc packageList = new ArrayList JavaDoc();
77     private ArrayList JavaDoc packageExtList = new ArrayList JavaDoc();
78     private ArrayList JavaDoc packageClassList = new ArrayList JavaDoc();
79     private ArrayList JavaDoc classElementList = new ArrayList JavaDoc();
80 // private ArrayList fieldExtList = new ArrayList();
81
private ArrayList JavaDoc queryExtList = new ArrayList JavaDoc();
82     private ArrayList JavaDoc collectionExtList = new ArrayList JavaDoc();
83
84     private boolean doneFilter;
85     private boolean doneSql;
86     private boolean doneDeclarations;
87     private boolean doneResult;
88     private StringBuffer JavaDoc text;
89
90     private static final int START = 1;
91     private static final int JDO = 2;
92     private static final int PACKAGE = 3;
93     private static final int CLASS = 4;
94     private static final int FIELD = 5;
95     private static final int COLLECTION = 6;
96     private static final int EXTENSION = 7;
97     private static final int EXTENSION_SKIP = 8;
98     private static final int QUERY = 9;
99     private static final int EMBEDDED = 10;
100     private static final int EMBEDDED_FIELD = 11;
101
102     private static String JavaDoc[] STATE_STRING = {"ERROR", "START", "JDO", "PACKAGE", "CLASS",
103                                             "FIELD", "COLLECTION", "EXTENSION",
104                                             "EXTENSION_SKIP", "QUERY", "EMBEDDED",
105                                             "EMBEDDED_FIELD"};
106
107     public MetaDataParser() {
108
109         parserFactory = JaxpUtils.getSAXParserFactory();
110         parserFactory.setValidating(false);
111         parserFactory.setNamespaceAware(false);
112
113     }
114
115     /**
116      * Load all the jdoNames as resources using loader and return the JdoRoot's.
117      */

118     public JdoRoot[] parse(Collection JavaDoc jdoNames, ClassLoader JavaDoc loader) {
119         int n = jdoNames.size();
120         JdoRoot[] roots = new JdoRoot[n];
121         int c = 0;
122         for (Iterator JavaDoc i = jdoNames.iterator(); i.hasNext(); ) {
123             String JavaDoc name = (String JavaDoc)i.next();
124             InputStream JavaDoc in = null;
125             try {
126
127
128                 in = loader.getResourceAsStream(
129                         name.startsWith("/") ? name.substring(1) : name);
130                 if (in == null) {
131                     throw BindingSupportImpl.getInstance().runtime("Unable to load resource: " +
132                             name);
133                 }
134
135                 roots[c++] = parse(in, name);
136             } finally {
137                 if (in != null) {
138                     try {
139                         in.close();
140                     } catch (IOException JavaDoc x) {
141                         // ignore
142
}
143                 }
144             }
145         }
146         return roots;
147     }
148
149     /**
150      * Parse the supplied JDO meta data stream and create a JdoRoot tree.
151      */

152     public JdoRoot parse(InputStream JavaDoc in, String JavaDoc name) {
153         try {
154             init(name);
155
156             parser = JaxpUtils.createSAXParser(parserFactory);
157             parser.parse(in, this);
158             parser = null;
159
160
161
162             return jdoRoot;
163         } catch (Exception JavaDoc x) {
164             throw BindingSupportImpl.getInstance().runtime(
165                     name + ": " + x.getMessage(), x);
166         }
167     }
168
169
170
171
172     /**
173      * Resolve an external entity.
174      * <p/>
175      * We override this method so we don't go looking on the web for dtd's
176      */

177
178     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
179     {
180         StringReader JavaDoc reader = new StringReader JavaDoc("");
181         return new InputSource JavaDoc(reader);
182     }
183
184
185     /**
186      * Parse the supplied JDO meta data file and create a JdoRoot tree.
187      *
188      * @throws IOException if on errors opening filename
189      */

190     public JdoRoot parse(String JavaDoc filename) throws IOException JavaDoc {
191         FileInputStream JavaDoc in = null;
192         try {
193             return parse(in = new FileInputStream JavaDoc(filename), filename);
194         } finally {
195             if (in != null) {
196                 try {
197                     in.close();
198                 } catch (IOException JavaDoc x) {
199                     // ignore
200
}
201             }
202         }
203     }
204
205     /**
206      * Prepare this parser to receive SAX events. Normally one of the parse
207      * methods should be used. Use this only if you are getting SAX events
208      * from somewhere else.
209      *
210      * @see #parse
211      * @see #getJdoRoot
212      */

213     public void init(String JavaDoc name) {
214         jdoRoot = new JdoRoot();
215         jdoRoot.name = name;
216         state = START;
217         jdoPackage = null;
218         jdoClass = null;
219         jdoField = null;
220         jdoCollection = null;
221         jdoMap = null;
222         jdoArray = null;
223         extStackTop = -1;
224     }
225
226     /**
227      * Retrieve the last parsed JdoRoot.
228      */

229     public JdoRoot getJdoRoot() {
230         return jdoRoot;
231     }
232
233     /**
234      * Decide what to do with the element based on its name and our current
235      * state.
236      */

237     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name,
238             Attributes JavaDoc attr) throws SAXException JavaDoc {
239         try {
240             startElementImp(uri, localName, name, attr);
241         } catch (RuntimeException JavaDoc x) {
242             throw x;
243         }
244     }
245
246     /**
247      * Decide what to do with the element based on its name and our current
248      * state.
249      */

250     public void startElementImp(String JavaDoc uri, String JavaDoc localName, String JavaDoc name,
251             Attributes JavaDoc attr) throws SAXException JavaDoc {
252 // if (Debug.DEBUG) {
253
// StringBuffer s = new StringBuffer();
254
// s.append(STATE_STR[state]);
255
// s.append(SPACE.substring(0, elementDepth * 2 + 1));
256
// s.append('<');
257
// s.append(name);
258
// int n = attr.getLength();
259
// for (int i = 0; i < n; i++) {
260
// s.append(' ');
261
// s.append(attr.getQName(i));
262
// s.append("=\"");
263
// s.append(attr.getValue(i));
264
// s.append('"');
265
// }
266
// s.append('>');
267
// cat.debug(s.toString());
268
// elementDepth++;
269
// }
270
elementName = name;
271         switch (state) {
272
273             case START:
274                 if (name.equals("jdo") || name.equals("mapping")) {
275                     packageList.clear();
276                     state = JDO;
277                 } else {
278
279                     throwInvalidElement("<jdo>");
280  
281  
282                 }
283                 break;
284
285             case JDO:
286                 if (name.equals("package") || name.equals("namespace")) {
287                     startPackage(attr);
288                 } else {
289
290                     throwInvalidElement("<package>");
291  
292  
293                 }
294                 break;
295
296             case PACKAGE:
297                 if (name.equals("class")) {
298                     startClass(attr);
299                 } else if (name.equals("extension")) {
300                     startExtension(jdoPackage, packageExtList, attr);
301                 } else {
302                     throwInvalidElement("<class> or <extension>");
303                 }
304                 break;
305             case EMBEDDED:
306                 if (name.equals("field")) {
307                     startEmbeddedField(attr);
308                 } else {
309                     throwInvalidElement("<field>");
310                 }
311                 break;
312             case CLASS:
313                 if (name.equals("field")) {
314                     startField(attr);
315                 } else if (name.equals("extension")) {
316                     startExtension(jdoClass, classElementList, attr);
317                 } else if (name.equals("query")) {
318                     startQuery(attr);
319                 } else {
320                     throwInvalidElement("<field> or <extension>");
321                 }
322                 break;
323             case EMBEDDED_FIELD:
324             case FIELD:
325                 if (name.equals("extension")) {
326                     startExtension(jdoField, jdoField.extensionList, attr);
327                 } else if (name.equals("collection")) {
328                     startCollection(attr);
329                 } else if (name.equals("map")) {
330                     startMap(attr);
331                 } else if (name.equals("array")) {
332                     startArray(attr);
333                 } else if (name.equals("embedded")) {
334                     startEmbedded(attr);
335                 } else {
336                     throwInvalidElement(
337                             "<collection>, <map>, <array> or <extension>");
338                 }
339                 break;
340
341             case COLLECTION:
342                 if (name.equals("extension")) {
343                     JdoElement p;
344                     if (jdoCollection != null) {
345                         p = jdoCollection;
346                     } else if (jdoArray != null) {
347                         p = jdoArray;
348                     } else {
349                         p = jdoMap;
350                     }
351                     startExtension(p, collectionExtList, attr);
352                 } else {
353                     throwInvalidElement("<extension>");
354                 }
355                 break;
356
357             case EXTENSION:
358                 if (name.equals("extension")) {
359                     startExtension(extStack[extStackTop],
360                             extStackList[extStackTop], attr);
361                 } else {
362                     throwInvalidElement("<extension>");
363                 }
364                 break;
365
366             case EXTENSION_SKIP:
367                 extensionSkipDepth++;
368                 break;
369
370             case QUERY:
371                 if (name.equals("filter")) {
372                     if (doneFilter) throwDuplicateElement("filter");
373                     if (jdoQuery.filter != null) {
374                         throw BindingSupportImpl.getInstance().runtime("You may not have a filter attribute and " +
375                                 "element in " + getContext());
376                     }
377                     doneFilter = true;
378                     text = new StringBuffer JavaDoc();
379                     // no attributes to read
380
} else if (name.equals("sql")) {
381                     if (doneSql) throwDuplicateElement("sql");
382                     if (jdoQuery.sql != null) {
383                         throw BindingSupportImpl.getInstance().runtime("You may not have a sql attribute and " +
384                                 "element in " + getContext());
385                     }
386                     doneSql = true;
387                     text = new StringBuffer JavaDoc();
388                     // no attributes to read
389
} else if (name.equals("declare")) {
390                     if (doneDeclarations) throwDuplicateElement("declarations");
391                     doneDeclarations = true;
392                     jdoQuery.imports = attr.getValue("imports");
393                     jdoQuery.parameters = attr.getValue("parameters");
394                     jdoQuery.variables = attr.getValue("variables");
395                 } else if (name.equals("result")) {
396                     if (doneResult) throwDuplicateElement("result");
397                     doneResult = true;
398                     jdoQuery.resultClass = attr.getValue("class");
399                     jdoQuery.unique = getTriState(attr, "unique");
400                     jdoQuery.grouping = attr.getValue("grouping");
401                     text = new StringBuffer JavaDoc();
402                 } else if (name.equals("extension")) {
403                     startExtension(jdoQuery, queryExtList, attr);
404                 } else {
405                     throwInvalidElement("<filter>, <declarations>, <result> " +
406                             "or <extension>");
407                 }
408                 break;
409         }
410     }
411
412     private void startEmbedded(Attributes JavaDoc attr) {
413         jdoField.embedded = JdoField.TRUE;
414         state = EMBEDDED;
415     }
416
417     public void characters(char ch[], int start, int length)
418              throws SAXException JavaDoc {
419         if (text != null) text.append(ch, start, length);
420     }
421
422     private void throwDuplicateElement(String JavaDoc name) {
423         throw BindingSupportImpl.getInstance().runtime("Only one '" + elementName +
424                 "' element is allowed in " + getContext());
425     }
426
427     private void throwInvalidElement(String JavaDoc valid) {
428         throw BindingSupportImpl.getInstance().runtime("Invalid element '" + elementName +
429                 "' in " + getContext() + ", expected " + valid);
430     }
431
432     private void throwInvalidAttribute(String JavaDoc attrName, String JavaDoc value) {
433         throw BindingSupportImpl.getInstance().runtime("Invalid " + attrName + " attribute '" +
434                 value + "' in " + getContext());
435     }
436
437     private void startPackage(Attributes JavaDoc attr) {
438         jdoPackage = new JdoPackage();
439         jdoPackage.parent = jdoRoot;
440         packageList.add(jdoPackage);
441         jdoPackage.name = getReqAttr(attr, "name");
442         packageClassList.clear();
443         packageExtList.clear();
444         state = PACKAGE;
445     }
446
447     private void startClass(Attributes JavaDoc attr) {
448         jdoClass = new JdoClass();
449         jdoClass.parent = jdoPackage;
450         packageClassList.add(jdoClass);
451         jdoClass.name = getReqAttr(attr, "name");
452         String JavaDoc idt = attr.getValue("identity-type");
453         if (idt != null) {
454             if (idt.equals("datastore")) {
455                 jdoClass.identityType = JdoClass.IDENTITY_TYPE_DATASTORE;
456             } else if (idt.equals("application")) {
457                 jdoClass.identityType = JdoClass.IDENTITY_TYPE_APPLICATION;
458             } else if (idt.equals("nondurable")) {
459                 jdoClass.identityType = JdoClass.IDENTITY_TYPE_NONDURABLE;
460             } else {
461                 throwInvalidAttribute("indentity-type", idt);
462             }
463         }
464         jdoClass.objectIdClass = attr.getValue("objectid-class");
465         String JavaDoc re = attr.getValue("requires-extent");
466         if (re == null || re.equals("true")) {
467             jdoClass.requiresExtent = true;
468         } else if (re.equals("false")) {
469             jdoClass.requiresExtent = false;
470         } else {
471             throwInvalidAttribute("requires-extent", re);
472         }
473         jdoClass.pcSuperclass = attr.getValue("persistence-capable-superclass");
474         classElementList.clear();
475         state = CLASS;
476     }
477
478     private void startField(Attributes JavaDoc attr) {
479         jdoField = new JdoField();
480         jdoField.parent = jdoClass;
481         classElementList.add(jdoField);
482         jdoField.name = getReqAttr(attr, "name");
483         String JavaDoc pm = attr.getValue("persistence-modifier");
484         if (pm != null) {
485             if (pm.equals("persistent")) {
486                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_PERSISTENT;
487             } else if (pm.equals("transactional")) {
488                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_TRANSACTIONAL;
489             } else if (pm.equals("none")) {
490                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_NONE;
491             } else {
492                 throwInvalidAttribute("persistence-modifier", pm);
493             }
494         }
495         String JavaDoc pk = attr.getValue("primary-key");
496         if (pk == null || pk.equals("false")) {
497             jdoField.primaryKey = false;
498         } else if (pk.equals("true")) {
499             jdoField.primaryKey = true;
500         } else {
501             throwInvalidAttribute("primary-key", pk);
502         }
503         String JavaDoc nv = attr.getValue("null-value");
504         if (nv == null || nv.equals("none")) {
505             jdoField.nullValue = JdoField.NULL_VALUE_NONE;
506         } else if (nv.equals("exception")) {
507             jdoField.nullValue = JdoField.NULL_VALUE_EXCEPTION;
508         } else if (nv.equals("default")) {
509             jdoField.nullValue = JdoField.NULL_VALUE_DEFAULT;
510         } else {
511             throwInvalidAttribute("null-value", nv);
512         }
513         String JavaDoc df = attr.getValue("default-fetch-group");
514         if (df != null) {
515             if (df.equals("true")) {
516                 jdoField.defaultFetchGroup = JdoField.TRUE;
517             } else if (df.equals("false")) {
518                 jdoField.defaultFetchGroup = JdoField.FALSE;
519             } else {
520                 throwInvalidAttribute("default-fetch-group", df);
521             }
522         }
523         String JavaDoc em = attr.getValue("embedded");
524         if (em != null) {
525             if (em.equals("true")) {
526                 jdoField.embedded = JdoField.TRUE;
527             } else if (em.equals("false")) {
528                 jdoField.embedded = JdoField.FALSE;
529             } else {
530                 throwInvalidAttribute("embedded", em);
531             }
532         }
533         state = FIELD;
534     }
535
536     private void startEmbeddedField(Attributes JavaDoc attr) {
537         jdoField = new JdoField(jdoField);
538         jdoField.parent = jdoClass;
539         jdoField.name = getReqAttr(attr, "name");
540
541         String JavaDoc pm = attr.getValue("persistence-modifier");
542         if (pm != null) {
543             if (pm.equals("persistent")) {
544                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_PERSISTENT;
545             } else if (pm.equals("transactional")) {
546                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_TRANSACTIONAL;
547             } else if (pm.equals("none")) {
548                 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_NONE;
549             } else {
550                 throwInvalidAttribute("persistence-modifier", pm);
551             }
552         }
553
554         String JavaDoc nv = attr.getValue("null-value");
555         if (nv == null || nv.equals("none")) {
556             jdoField.nullValue = JdoField.NULL_VALUE_NONE;
557         } else if (nv.equals("exception")) {
558             jdoField.nullValue = JdoField.NULL_VALUE_EXCEPTION;
559         } else if (nv.equals("default")) {
560             jdoField.nullValue = JdoField.NULL_VALUE_DEFAULT;
561         } else {
562             throwInvalidAttribute("null-value", nv);
563         }
564         String JavaDoc df = attr.getValue("default-fetch-group");
565         if (df != null) {
566             if (df.equals("true")) {
567                 jdoField.defaultFetchGroup = JdoField.TRUE;
568             } else if (df.equals("false")) {
569                 jdoField.defaultFetchGroup = JdoField.FALSE;
570             } else {
571                 throwInvalidAttribute("default-fetch-group", df);
572             }
573         }
574         String JavaDoc em = attr.getValue("embedded");
575         if (em != null) {
576             if (em.equals("true")) {
577                 jdoField.embedded = JdoField.TRUE;
578             } else if (em.equals("false")) {
579                 jdoField.embedded = JdoField.FALSE;
580             } else {
581                 throwInvalidAttribute("embedded", em);
582             }
583         }
584         state = EMBEDDED_FIELD;
585     }
586
587     private void startCollection(Attributes JavaDoc attr) {
588         jdoCollection = new JdoCollection();
589         jdoCollection.parent = jdoField;
590         jdoField.collection = jdoCollection;
591         jdoCollection.elementType = attr.getValue("element-type");
592         String JavaDoc em = attr.getValue("embedded-element");
593         if (em != null) {
594             if (em.equals("true")) {
595                 jdoCollection.embeddedElement = JdoCollection.TRUE;
596             } else if (em.equals("false")) {
597                 jdoCollection.embeddedElement = JdoCollection.FALSE;
598             } else {
599                 throwInvalidAttribute("embedded-element", em);
600             }
601         }
602         collectionExtList.clear();
603         state = COLLECTION;
604     }
605
606     private void startMap(Attributes JavaDoc attr) {
607         jdoMap = new JdoMap();
608         jdoMap.parent = jdoField;
609         jdoField.map = jdoMap;
610         jdoMap.keyType = attr.getValue("key-type");
611         jdoMap.valueType = attr.getValue("value-type");
612         String JavaDoc em = attr.getValue("embedded-key");
613         if (em != null) {
614             if (em.equals("true")) {
615                 jdoMap.embeddedKey = JdoMap.TRUE;
616             } else if (em.equals("false")) {
617                 jdoMap.embeddedKey = JdoMap.FALSE;
618             } else {
619                 throwInvalidAttribute("embedded-key", em);
620             }
621         }
622         em = attr.getValue("embedded-value");
623         if (em != null) {
624             if (em.equals("true")) {
625                 jdoMap.embeddedValue = JdoMap.TRUE;
626             } else if (em.equals("false")) {
627                 jdoMap.embeddedValue = JdoMap.FALSE;
628             } else {
629                 throwInvalidAttribute("embedded-value", em);
630             }
631         }
632         collectionExtList.clear();
633         state = COLLECTION;
634     }
635
636     private void startArray(Attributes JavaDoc attr) {
637         jdoArray = new JdoArray();
638         jdoArray.parent = jdoField;
639         jdoField.array = jdoArray;
640         String JavaDoc em = attr.getValue("embedded-element");
641         if (em != null) {
642             if (em.equals("true")) {
643                 jdoArray.embeddedElement = JdoCollection.TRUE;
644             } else if (em.equals("false")) {
645                 jdoArray.embeddedElement = JdoCollection.FALSE;
646             } else {
647                 throwInvalidAttribute("embedded-element", em);
648             }
649         }
650         collectionExtList.clear();
651         state = COLLECTION;
652     }
653
654     private void startExtension(JdoElement parent, ArrayList JavaDoc list,
655             Attributes JavaDoc attr) {
656  
657
658         String JavaDoc vn = getReqAttr(attr, "vendor-name");
659
660         if (isOurVendorName(vn)) {
661             JdoExtension e = new JdoExtension();
662             e.parent = parent;
663             String JavaDoc key = getReqAttr(attr, "key");
664             int i = JdoExtension.parseKey(key);
665             if (i == Integer.MIN_VALUE) {
666                 throw BindingSupportImpl.getInstance().runtime("Invalid key '" + key + "' in " +
667                         getContext());
668             }
669             e.key = i;
670             e.value = attr.getValue("value");
671             extStack[++extStackTop] = e;
672             ArrayList JavaDoc a = extStackList[extStackTop];
673             if (a == null) {
674                 extStackList[extStackTop] = new ArrayList JavaDoc();
675             } else {
676                 a.clear();
677             }
678             if (state != EXTENSION) {
679                 extState = state;
680                 state = EXTENSION;
681             }
682             list.add(e);
683         } else {
684             extSkipState = state;
685             state = EXTENSION_SKIP;
686             extensionSkipDepth = 0;
687         }
688     }
689
690     private void startQuery(Attributes JavaDoc attr) {
691         jdoQuery = new JdoQuery();
692         jdoQuery.parent = jdoClass;
693         jdoClass.addJdoQuery(jdoQuery);
694         jdoQuery.name = getReqAttr(attr, "name");
695         jdoQuery.language = attr.getValue("language");
696         jdoQuery.ignoreCache = getTriState(attr, "ignore-cache");
697         jdoQuery.includeSubclasses = getTriState(attr, "include-subclasses");
698         jdoQuery.ordering = attr.getValue("ordering");
699         jdoQuery.filter = attr.getValue("filter");
700         jdoQuery.sql = attr.getValue("sql");
701         String JavaDoc r = attr.getValue("range");
702         if (r != null) {
703             try {
704                 int i = r.indexOf(',');
705                 jdoQuery.rangeStart = Integer.parseInt(r.substring(0, i));
706                 jdoQuery.rangeEnd = Integer.parseInt(r.substring(i + 1));
707             } catch (Exception JavaDoc e) {
708                 throwInvalidAttribute("range", r);
709             }
710         }
711         state = QUERY;
712         doneSql = false;
713     }
714
715     /**
716      * Decide what to do with the element based on our current state and its
717      * name.
718      */

719     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
720              throws SAXException JavaDoc {
721         endElementImp(uri, localName, name);
722     }
723
724     /**
725      * Decide what to do with the element based on our current state and its
726      * name.
727      */

728     public void endElementImp(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
729              throws SAXException JavaDoc {
730 // if (cat.isDebugEnabled()) {
731
// elementDepth--;
732
// StringBuffer s = new StringBuffer();
733
// s.append(STATE_STR[state]);
734
// s.append(SPACE.substring(0, elementDepth * 2 + 1));
735
// s.append("</");
736
// s.append(name);
737
// s.append('>');
738
// cat.debug(s.toString());
739
// }
740
elementName = name;
741         int n;
742         switch (state) {
743
744             case JDO:
745                 jdoRoot.packages = new JdoPackage[packageList.size()];
746                 packageList.toArray(jdoRoot.packages);
747                 state = START;
748                 break;
749
750             case PACKAGE:
751                 n = packageClassList.size();
752                 jdoPackage.classes = new JdoClass[n];
753                 packageClassList.toArray(jdoPackage.classes);
754                 n = packageExtList.size();
755                 if (n > 0) {
756                     jdoPackage.extensions = new JdoExtension[n];
757                     packageExtList.toArray(jdoPackage.extensions);
758                 }
759                 jdoPackage = null;
760                 state = JDO;
761                 break;
762
763             case CLASS:
764                 jdoClass.elements = new JdoElement[classElementList.size()];
765                 classElementList.toArray(jdoClass.elements);
766                 jdoClass = null;
767                 state = PACKAGE;
768                 break;
769             case EMBEDDED_FIELD:
770                 n = jdoField.extensionList.size();
771                 if (n > 0) {
772                     jdoField.extensions = new JdoExtension[n];
773                     jdoField.extensionList.toArray(jdoField.extensions);
774                 }
775                 jdoField = jdoField.parentField;
776                 state = EMBEDDED;
777                 break;
778             case FIELD:
779                 n = jdoField.extensionList.size();
780                 if (n > 0) {
781                     jdoField.extensions = new JdoExtension[n];
782                     jdoField.extensionList.toArray(jdoField.extensions);
783                 }
784                 jdoField = null;
785                 jdoCollection = null;
786                 jdoArray = null;
787                 jdoMap = null;
788                 state = CLASS;
789                 break;
790
791             case COLLECTION:
792                 n = collectionExtList.size();
793                 if (n > 0) {
794                     JdoExtension[] e = new JdoExtension[n];
795                     collectionExtList.toArray(e);
796                     if (jdoCollection != null) {
797                         jdoCollection.extensions = e;
798                         jdoCollection = null;
799                     } else if (jdoArray != null) {
800                         jdoArray.extensions = e;
801                         jdoArray = null;
802                     } else {
803                         jdoMap.extensions = e;
804                         jdoMap = null;
805                     }
806                 }
807                 state = FIELD;
808                 break;
809
810             case EXTENSION:
811                 endExtension();
812                 break;
813
814             case EXTENSION_SKIP:
815                 if (extensionSkipDepth == 0) {
816                     state = extSkipState;
817                 } else {
818                     extensionSkipDepth--;
819                 }
820                 break;
821
822             case QUERY:
823                 if (elementName.equals("query")) {
824                     jdoQuery.extensions = new JdoExtension[queryExtList.size()];
825                     queryExtList.toArray(jdoQuery.extensions);
826                     jdoQuery = null;
827                     doneFilter = false;
828                     doneDeclarations = false;
829                     doneResult = false;
830                     state = CLASS;
831                 } else if (elementName.equals("filter")) {
832                     if (text != null) jdoQuery.filter = text.toString().trim();
833                     text = null;
834                 } else if (elementName.equals("result")) {
835                     if (text != null) jdoQuery.result = text.toString().trim();
836                     text = null;
837                 } else if (elementName.equals("sql")) {
838                     if (text != null) jdoQuery.sql = text.toString().trim();
839                     text = null;
840                 }
841                 break;
842             case EMBEDDED:
843                 if (jdoField.parentField == null) state = FIELD;
844                 else state = EMBEDDED_FIELD;
845                 break;
846         }
847     }
848
849     private void endExtension() {
850         JdoExtension top = extStack[extStackTop];
851         ArrayList JavaDoc a = extStackList[extStackTop];
852         int n = a.size();
853         if (n > 0) {
854             top.nested = new JdoExtension[n];
855             a.toArray(top.nested);
856         }
857         if (--extStackTop < 0) state = extState;
858     }
859
860     /**
861      * Get information about the current parser context in a String. This is
862      * used to construct error messages.
863      */

864     private String JavaDoc getContext() {
865         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
866         s.append(jdoRoot.name);
867         if (jdoPackage != null) {
868             s.append(":package[");
869             s.append(jdoPackage.name);
870             s.append(']');
871             if (jdoClass != null) {
872                 s.append("/class[");
873                 s.append(jdoClass.name);
874                 s.append(']');
875                 if (jdoField != null) {
876                     s.append("/field[");
877                     s.append(jdoField.name);
878                     s.append(']');
879                     if (jdoCollection != null) {
880                         s.append("/collection");
881                     }
882                     if (jdoMap != null) {
883                         s.append("/map");
884                     }
885                     if (jdoArray != null) {
886                         s.append("/array");
887                     }
888                 }
889                 if (jdoQuery != null) {
890                     s.append("/query");
891                 }
892             }
893         }
894         for (int i = 0; i <= extStackTop; i++) {
895             s.append("/extension[");
896             JdoExtension e = extStack[i];
897             s.append(JdoExtension.toKeyString(e.key));
898             s.append('=');
899             s.append(e.value);
900             s.append(']');
901         }
902         return s.toString();
903     }
904
905     /**
906      * Get the value of the Attribute with name.
907      */

908     private String JavaDoc getReqAttr(Attributes JavaDoc attr, String JavaDoc name)
909             /*throws JDOFatalUserException*/ {
910         String JavaDoc v = attr.getValue(name);
911         if (v == null) {
912             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
913             s.append("Expected attribute '");
914             s.append(name);
915             s.append("' in ");
916             s.append(getContext());
917             s.append('.');
918             s.append(elementName);
919             throw BindingSupportImpl.getInstance().runtime(s.toString());
920         }
921         return v;
922     }
923
924     /**
925      * Get the value of the int Attribute with name. Returns -1 if not found.
926      */

927     private int getIntAttr(Attributes JavaDoc attr, String JavaDoc name) {
928         String JavaDoc v = attr.getValue(name);
929         if (v == null) return -1;
930         try {
931             return Integer.parseInt(v);
932         } catch (NumberFormatException JavaDoc e) {
933             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
934             s.append("Expected int attribute '");
935             s.append(name);
936             s.append("' found '");
937             s.append(v);
938             s.append("' in ");
939             s.append(getContext());
940             s.append('.');
941             s.append(elementName);
942             throw BindingSupportImpl.getInstance().runtime(s.toString());
943         }
944     }
945
946     /**
947      * Get an optional boolean attribute.
948      *
949      * @see MDStatics.NOT_SET
950      * @see MDStatics.FALSE
951      * @see MDStatics.TRUE
952      */

953     private int getTriState(Attributes JavaDoc attr, String JavaDoc name)
954             /*throws JDOFatalUserException*/ {
955         String JavaDoc v = attr.getValue(name);
956         if (v == null) return MDStatics.NOT_SET;
957         if ("true".equals(v)) return MDStatics.TRUE;
958         if ("false".equals(v)) return MDStatics.FALSE;
959         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
960         s.append("Expected 'true' or 'false' for attribute '");
961         s.append(name);
962         s.append("', got '");
963         s.append(v);
964         s.append("' in ");
965         s.append(getContext());
966         s.append('.');
967         s.append(elementName);
968         throw BindingSupportImpl.getInstance().runtime(s.toString());
969     }
970
971     /**
972      * Does s match our vendor name?
973      */

974     public static boolean isOurVendorName(String JavaDoc s) {
975         return VENDOR_NAME.equals(s) || VENDOR_NAME_JDOGENIE.equals(s);
976     }
977
978 }
979
Popular Tags