KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > map > MapLoader


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
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 OBJECTSTYLE GROUP 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 and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.map;
57
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.util.Map JavaDoc;
61 import java.util.TreeMap JavaDoc;
62
63 import org.apache.log4j.Logger;
64 import org.objectstyle.cayenne.conf.Configuration;
65 import org.objectstyle.cayenne.dba.TypesMapping;
66 import org.objectstyle.cayenne.exp.Expression;
67 import org.objectstyle.cayenne.project.DataMapFile;
68 import org.objectstyle.cayenne.util.ResourceLocator;
69 import org.objectstyle.cayenne.util.Util;
70 import org.xml.sax.Attributes JavaDoc;
71 import org.xml.sax.InputSource JavaDoc;
72 import org.xml.sax.SAXException JavaDoc;
73 import org.xml.sax.SAXParseException JavaDoc;
74 import org.xml.sax.XMLReader JavaDoc;
75 import org.xml.sax.helpers.DefaultHandler JavaDoc;
76
77 /**
78  * Default MapLoader. Its responsibilities include reading DataMaps
79  * from XML files and saving DataMap objects back to XML.
80  *
81  * @author Misha Shengaout
82  * @author Andrei Adamchik
83  * @author Andriy Shapochka
84  */

85 public class MapLoader extends DefaultHandler JavaDoc {
86     private static final Logger logObj = Logger.getLogger(MapLoader.class);
87
88     public static final String JavaDoc DATA_MAP_TAG = "data-map";
89     public static final String JavaDoc PROPERTY_TAG = "property";
90     public static final String JavaDoc DB_ENTITY_TAG = "db-entity";
91     public static final String JavaDoc OBJ_ENTITY_TAG = "obj-entity";
92     public static final String JavaDoc DB_ATTRIBUTE_TAG = "db-attribute";
93     public static final String JavaDoc DB_ATTRIBUTE_DERIVED_TAG = "db-attribute-derived";
94     public static final String JavaDoc DB_ATTRIBUTE_REF_TAG = "db-attribute-ref";
95     public static final String JavaDoc OBJ_ATTRIBUTE_TAG = "obj-attribute";
96     public static final String JavaDoc OBJ_RELATIONSHIP_TAG = "obj-relationship";
97     public static final String JavaDoc DB_RELATIONSHIP_TAG = "db-relationship";
98     public static final String JavaDoc DB_RELATIONSHIP_REF_TAG = "db-relationship-ref";
99     public static final String JavaDoc DB_ATTRIBUTE_PAIR_TAG = "db-attribute-pair";
100     public static final String JavaDoc PROCEDURE_TAG = "procedure";
101     public static final String JavaDoc PROCEDURE_PARAMETER_TAG = "procedure-parameter";
102
103     // Query-related
104
public static final String JavaDoc QUERY_TAG = "query";
105     public static final String JavaDoc QUERY_RESULT_COLUMN_TAG = "result-column";
106     public static final String JavaDoc QUERY_SQL_TAG = "sql";
107     public static final String JavaDoc QUERY_QUALIFIER_TAG = "qualifier";
108     public static final String JavaDoc QUERY_ORDERING_TAG = "ordering";
109     public static final String JavaDoc QUERY_PREFETCH_TAG = "prefetch";
110
111     public static final String JavaDoc TRUE = "true";
112     public static final String JavaDoc FALSE = "false";
113
114     public static final String JavaDoc DB_KEY_GENERATOR_TAG = "db-key-generator";
115     public static final String JavaDoc DB_GENERATOR_TYPE_TAG = "db-generator-type";
116     public static final String JavaDoc DB_GENERATOR_NAME_TAG = "db-generator-name";
117     public static final String JavaDoc DB_KEY_CACHE_SIZE_TAG = "db-key-cache-size";
118
119     // Reading from XML
120
private DataMap dataMap;
121     private DbEntity dbEntity;
122     private ObjEntity objEntity;
123     private DbRelationship dbRelationship;
124     private ObjRelationship objRelationship;
125     private DbAttribute attrib;
126     private Procedure procedure;
127     private QueryBuilder queryBuilder;
128     private String JavaDoc sqlKey;
129     private String JavaDoc descending;
130     private String JavaDoc ignoreCase;
131
132     private String JavaDoc currentTag;
133     private StringBuffer JavaDoc charactersBuffer;
134     private Map JavaDoc mapProperties;
135
136     /**
137      * Loads a DataMap from XML input source.
138      */

139     public synchronized DataMap loadDataMap(InputSource JavaDoc src) throws DataMapException {
140         if (src == null) {
141             throw new NullPointerException JavaDoc("Null InputSource.");
142         }
143
144         try {
145             String JavaDoc mapName = mapNameFromLocation(src.getSystemId());
146             dataMap = new DataMap(mapName);
147             XMLReader JavaDoc parser = Util.createXmlReader();
148
149             parser.setContentHandler(this);
150             parser.setErrorHandler(this);
151             parser.parse(src);
152         }
153         catch (SAXException JavaDoc e) {
154             dataMap = null;
155             throw new DataMapException(
156                 "Wrong DataMap format, last processed tag: <" + currentTag,
157                 Util.unwindException(e));
158         }
159         catch (Exception JavaDoc e) {
160             dataMap = null;
161             throw new DataMapException(
162                 "Error loading DataMap, last processed tag: <" + currentTag,
163                 Util.unwindException(e));
164         }
165         return dataMap;
166     }
167
168     /**
169      * Loads DataMap from file specified by <code>uri</code> parameter.
170      *
171      * @throws DataMapException if source URI does not resolve to a valid map files
172      */

173     public DataMap loadDataMap(String JavaDoc uri) throws DataMapException {
174         // configure resource locator
175
ResourceLocator locator = configLocator();
176         InputStream JavaDoc in = locator.findResourceStream(uri);
177         if (in == null) {
178             throw new DataMapException("Can't find data map " + uri);
179         }
180
181         try {
182             InputSource JavaDoc inSrc = new InputSource JavaDoc(in);
183             inSrc.setSystemId(uri);
184             return loadDataMap(inSrc);
185         }
186         finally {
187             try {
188                 in.close();
189             }
190             catch (IOException JavaDoc ioex) {
191             }
192         }
193     }
194
195     /**
196      * Helper method to guess the map name from its location.
197      */

198     protected String JavaDoc mapNameFromLocation(String JavaDoc location) {
199         if (location == null) {
200             return "Untitled";
201         }
202
203         int lastSlash = location.lastIndexOf('/');
204         if (lastSlash < 0) {
205             lastSlash = location.lastIndexOf('\\');
206         }
207
208         if (lastSlash >= 0 && lastSlash + 1 < location.length()) {
209             location = location.substring(lastSlash + 1);
210         }
211
212         if (location.endsWith(DataMapFile.LOCATION_SUFFIX)) {
213             location =
214                 location.substring(
215                     0,
216                     location.length() - DataMapFile.LOCATION_SUFFIX.length());
217         }
218
219         return location;
220     }
221
222     /**
223      * Creates, configures and returns ResourceLocator object used
224      * to lookup DataMap files.
225      */

226     protected ResourceLocator configLocator() {
227         ResourceLocator locator = new ResourceLocator();
228         locator.setSkipAbsolutePath(true);
229         locator.setSkipClasspath(false);
230         locator.setSkipCurrentDirectory(false);
231         locator.setSkipHomeDirectory(false);
232
233         // Configuration superclass statically defines what
234
// ClassLoader to use for resources. This
235
// allows applications to control where resources
236
// are loaded from.
237
locator.setClassLoader(Configuration.getResourceLoader());
238
239         return locator;
240     }
241
242     public void startElement(
243         String JavaDoc namespaceUri,
244         String JavaDoc localName,
245         String JavaDoc qName,
246         Attributes JavaDoc attributes)
247         throws SAXException JavaDoc {
248         
249         rememberCurrentTag(localName);
250         if (localName.equals(DATA_MAP_TAG)) {
251         }
252         else if (localName.equals(DB_ENTITY_TAG)) {
253             processStartDbEntity(attributes);
254         }
255         else if (localName.equals(DB_ATTRIBUTE_TAG)) {
256             processStartDbAttribute(attributes);
257         }
258         else if (localName.equals(DB_ATTRIBUTE_DERIVED_TAG)) {
259             processStartDerivedDbAttribute(attributes);
260         }
261         else if (localName.equals(DB_ATTRIBUTE_REF_TAG)) {
262             processStartDbAttributeRef(attributes);
263         }
264         else if (localName.equals(OBJ_ENTITY_TAG)) {
265             processStartObjEntity(attributes);
266         }
267         else if (localName.equals(OBJ_ATTRIBUTE_TAG)) {
268             processStartObjAttribute(attributes);
269         }
270         else if (localName.equals(DB_RELATIONSHIP_TAG)) {
271             processStartDbRelationship(attributes);
272         }
273         else if (localName.equals(DB_ATTRIBUTE_PAIR_TAG)) {
274             processStartDbAttributePair(attributes);
275         }
276         else if (localName.equals(OBJ_RELATIONSHIP_TAG)) {
277             processStartObjRelationship(attributes);
278         }
279         else if (localName.equals(DB_RELATIONSHIP_REF_TAG)) {
280             processStartDbRelationshipRef(attributes);
281         }
282         else if (localName.equals(PROCEDURE_PARAMETER_TAG)) {
283             processStartProcedureParameter(attributes);
284         }
285         else if (localName.equals(PROCEDURE_TAG)) {
286             processStartProcedure(attributes);
287         }
288         else if (localName.equals(QUERY_TAG)) {
289             processStartQuery(attributes);
290         }
291         else if (localName.equals(QUERY_RESULT_COLUMN_TAG)) {
292             processStartQueryResultColumn(attributes);
293         }
294         else if (localName.equals(QUERY_SQL_TAG)) {
295             charactersBuffer = new StringBuffer JavaDoc();
296             processStartQuerySQL(attributes);
297         }
298         else if (localName.equals(QUERY_ORDERING_TAG)) {
299             charactersBuffer = new StringBuffer JavaDoc();
300             processStartQueryOrdering(attributes);
301         }
302         else if (localName.equals(QUERY_PREFETCH_TAG)) {
303             charactersBuffer = new StringBuffer JavaDoc();
304         }
305         else if (localName.equals(QUERY_QUALIFIER_TAG)) {
306             charactersBuffer = new StringBuffer JavaDoc();
307         }
308         else if (localName.equals(DB_KEY_GENERATOR_TAG)) {
309             processStartDbKeyGenerator(attributes);
310         }
311         else if (localName.equals(DB_GENERATOR_TYPE_TAG)) {
312             charactersBuffer = new StringBuffer JavaDoc();
313         }
314         else if (localName.equals(DB_GENERATOR_NAME_TAG)) {
315             charactersBuffer = new StringBuffer JavaDoc();
316         }
317         else if (localName.equals(DB_KEY_CACHE_SIZE_TAG)) {
318             charactersBuffer = new StringBuffer JavaDoc();
319         }
320         // properties can belong to query or DataMap
321
else if (localName.equals(PROPERTY_TAG)) {
322             if (queryBuilder != null) {
323                 processStartQueryProperty(attributes);
324             }
325             else {
326                 processStartDataMapProperty(attributes);
327             }
328         }
329     }
330
331     public void endElement(String JavaDoc namespaceURI, String JavaDoc local_name, String JavaDoc qName)
332         throws SAXException JavaDoc {
333         if (local_name.equals(DATA_MAP_TAG)) {
334             processEndDataMap();
335         }
336         else if (local_name.equals(DB_ENTITY_TAG)) {
337             processEndDbEntity();
338         }
339         else if (local_name.equals(OBJ_ENTITY_TAG)) {
340             processEndObjEntity();
341         }
342         else if (local_name.equals(DB_ATTRIBUTE_TAG)) {
343             processEndDbAttribute();
344         }
345         else if (local_name.equals(DB_ATTRIBUTE_DERIVED_TAG)) {
346             processEndDbAttribute();
347         }
348         else if (local_name.equals(DB_RELATIONSHIP_TAG)) {
349             processEndDbRelationship();
350         }
351         else if (local_name.equals(OBJ_RELATIONSHIP_TAG)) {
352             processEndObjRelationship();
353         }
354         else if (local_name.equals(DB_KEY_GENERATOR_TAG)) {
355         }
356         else if (local_name.equals(DB_GENERATOR_TYPE_TAG)) {
357             processEndDbGeneratorType();
358         }
359         else if (local_name.equals(DB_GENERATOR_NAME_TAG)) {
360             processEndDbGeneratorName();
361         }
362         else if (local_name.equals(DB_KEY_CACHE_SIZE_TAG)) {
363             processEndDbKeyCacheSize();
364         }
365         else if (local_name.equals(PROCEDURE_PARAMETER_TAG)) {
366             processEndProcedureParameter();
367         }
368         else if (local_name.equals(PROCEDURE_TAG)) {
369             processEndProcedure();
370         }
371         else if (local_name.equals(QUERY_TAG)) {
372             processEndQuery();
373         }
374         else if (local_name.equals(QUERY_SQL_TAG)) {
375             processEndQuerySQL();
376         }
377         else if (local_name.equals(QUERY_QUALIFIER_TAG)) {
378             processEndQualifier();
379         }
380         else if (local_name.equals(QUERY_ORDERING_TAG)) {
381             processEndQueryOrdering();
382         }
383         else if (local_name.equals(QUERY_PREFETCH_TAG)) {
384             processEndQueryPrefetch();
385         }
386
387         resetCurrentTag();
388         charactersBuffer = null;
389     }
390
391     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
392         logObj.warn(
393             "**Parsing warning**\n"
394                 + "Line:"
395                 + e.getLineNumber()
396                 + "\nMessage:"
397                 + e.getMessage());
398         throw new SAXException JavaDoc("Warning!");
399     }
400
401     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
402         logObj.error(
403             "**Parsing error**\n"
404                 + "Line:"
405                 + e.getLineNumber()
406                 + "\nMessage:"
407                 + e.getMessage());
408         throw new SAXException JavaDoc("Warning!");
409     }
410
411     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
412         logObj.fatal(
413             "**Parsing fatal error**\n"
414                 + "Line:"
415                 + e.getLineNumber()
416                 + "\nMessage:"
417                 + e.getMessage());
418         throw new SAXException JavaDoc("Warning!");
419     }
420
421     private void processStartDbEntity(Attributes JavaDoc atts) throws SAXException JavaDoc {
422         String JavaDoc name = atts.getValue("", "name");
423         String JavaDoc parentName = atts.getValue("", "parentName");
424
425         if (parentName != null) {
426             dbEntity = new DerivedDbEntity(name);
427             ((DerivedDbEntity) dbEntity).setParentEntityName(parentName);
428         }
429         else {
430             dbEntity = new DbEntity(name);
431         }
432
433         if (!(dbEntity instanceof DerivedDbEntity)) {
434             dbEntity.setSchema(atts.getValue("", "schema"));
435             dbEntity.setCatalog(atts.getValue("", "catalog"));
436         }
437
438         dataMap.addDbEntity(dbEntity);
439     }
440
441     private void processStartDbAttributeRef(Attributes JavaDoc atts) throws SAXException JavaDoc {
442         String JavaDoc name = atts.getValue("", "name");
443         if ((attrib instanceof DerivedDbAttribute)
444             && (dbEntity instanceof DerivedDbEntity)) {
445             DbEntity parent = ((DerivedDbEntity) dbEntity).getParentEntity();
446             DbAttribute ref = (DbAttribute) parent.getAttribute(name);
447             ((DerivedDbAttribute) attrib).addParam(ref);
448         }
449         else {
450             throw new SAXException JavaDoc(
451                 "Referenced attributes are not supported by regular DbAttributes. "
452                     + " Offending attribute name '"
453                     + attrib.getName()
454                     + "'.");
455         }
456     }
457
458     private void processStartDbAttribute(Attributes JavaDoc atts) throws SAXException JavaDoc {
459         String JavaDoc name = atts.getValue("", "name");
460         String JavaDoc type = atts.getValue("", "type");
461
462         attrib = new DbAttribute(name);
463         attrib.setType(TypesMapping.getSqlTypeByName(type));
464         dbEntity.addAttribute(attrib);
465
466         String JavaDoc length = atts.getValue("", "length");
467         if (length != null) {
468             attrib.setMaxLength(Integer.parseInt(length));
469         }
470
471         String JavaDoc precision = atts.getValue("", "precision");
472         if (precision != null) {
473             attrib.setPrecision(Integer.parseInt(precision));
474         }
475
476         attrib.setPrimaryKey(TRUE.equalsIgnoreCase(atts.getValue("", "isPrimaryKey")));
477         attrib.setMandatory(TRUE.equalsIgnoreCase(atts.getValue("", "isMandatory")));
478         attrib.setGenerated(TRUE.equalsIgnoreCase(atts.getValue("", "isGenerated")));
479     }
480
481     private void processStartDerivedDbAttribute(Attributes JavaDoc atts) throws SAXException JavaDoc {
482         String JavaDoc name = atts.getValue("", "name");
483         String JavaDoc type = atts.getValue("", "type");
484         String JavaDoc spec = atts.getValue("", "spec");
485
486         attrib = new DerivedDbAttribute(name);
487         attrib.setType(TypesMapping.getSqlTypeByName(type));
488         ((DerivedDbAttribute) attrib).setExpressionSpec(spec);
489         dbEntity.addAttribute(attrib);
490
491         String JavaDoc temp = atts.getValue("", "length");
492         if (temp != null) {
493             attrib.setMaxLength(Integer.parseInt(temp));
494         }
495         temp = atts.getValue("", "precision");
496         if (temp != null) {
497             attrib.setPrecision(Integer.parseInt(temp));
498         }
499         temp = atts.getValue("", "isPrimaryKey");
500         if (temp != null && temp.equalsIgnoreCase(TRUE)) {
501             attrib.setPrimaryKey(true);
502         }
503         temp = atts.getValue("", "isMandatory");
504         if (temp != null && temp.equalsIgnoreCase(TRUE)) {
505             attrib.setMandatory(true);
506         }
507
508         temp = atts.getValue("", "isGroupBy");
509         if (temp != null && temp.equalsIgnoreCase(TRUE)) {
510             ((DerivedDbAttribute) attrib).setGroupBy(true);
511         }
512     }
513
514     private void processStartDbKeyGenerator(Attributes JavaDoc atts) throws SAXException JavaDoc {
515         DbKeyGenerator pkGenerator = new DbKeyGenerator();
516         dbEntity.setPrimaryKeyGenerator(pkGenerator);
517     }
518     
519     private void processStartQuerySQL(Attributes JavaDoc atts) throws SAXException JavaDoc {
520         this.sqlKey = atts.getValue("", "adapter-class");
521     }
522
523     private void processStartObjEntity(Attributes JavaDoc atts) {
524         objEntity = new ObjEntity(atts.getValue("", "name"));
525         objEntity.setClassName(atts.getValue("", "className"));
526         objEntity.setClientClassName(atts.getValue("", "clientClassName"));
527
528         String JavaDoc readOnly = atts.getValue("", "readOnly");
529         objEntity.setReadOnly(TRUE.equalsIgnoreCase(readOnly));
530
531         String JavaDoc lockType = atts.getValue("", "lock-type");
532         if ("optimistic".equals(lockType)) {
533             objEntity.setDeclaredLockType(ObjEntity.LOCK_TYPE_OPTIMISTIC);
534         }
535
536         String JavaDoc superEntityName = atts.getValue("", "superEntityName");
537         if (superEntityName != null) {
538             objEntity.setSuperEntityName(superEntityName);
539         }
540         else {
541             objEntity.setDbEntityName(atts.getValue("", "dbEntityName"));
542             objEntity.setSuperClassName(atts.getValue("", "superClassName"));
543             objEntity.setClientSuperClassName(atts.getValue("", "clientSuperClassName"));
544         }
545
546         dataMap.addObjEntity(objEntity);
547     }
548
549     private void processStartObjAttribute(Attributes JavaDoc atts) {
550         String JavaDoc name = atts.getValue("", "name");
551         String JavaDoc type = atts.getValue("", "type");
552
553         String JavaDoc lock = atts.getValue("", "lock");
554
555         ObjAttribute oa = new ObjAttribute(name);
556         oa.setType(type);
557         oa.setUsedForLocking(TRUE.equalsIgnoreCase(lock));
558         objEntity.addAttribute(oa);
559         String JavaDoc dbPath = atts.getValue("", "db-attribute-path");
560         if (dbPath == null) {
561             dbPath = atts.getValue("", "db-attribute-name");
562         }
563         oa.setDbAttributePath(dbPath);
564     }
565
566     private void processStartDbRelationship(Attributes JavaDoc atts) throws SAXException JavaDoc {
567         String JavaDoc name = atts.getValue("", "name");
568         if (name == null) {
569             throw new SAXException JavaDoc(
570                 "MapLoaderImpl::processStartDbRelationship(),"
571                     + " Unable to parse name. Attributes:\n"
572                     + printAttributes(atts).toString());
573         }
574
575         String JavaDoc sourceName = atts.getValue("", "source");
576         if (sourceName == null) {
577             throw new SAXException JavaDoc("MapLoaderImpl::processStartDbRelationship() - null source entity");
578         }
579
580         DbEntity source = dataMap.getDbEntity(sourceName);
581         if (source == null) {
582             logObj.debug(
583                 "MapLoaderImpl::processStartDbRelationship() - Unable to find source "
584                     + sourceName);
585             return;
586         }
587
588         String JavaDoc toManyString = atts.getValue("", "toMany");
589         boolean toMany = toManyString != null && toManyString.equalsIgnoreCase(TRUE);
590
591         String JavaDoc toDependPkString = atts.getValue("", "toDependentPK");
592         boolean toDependentPK =
593             toDependPkString != null && toDependPkString.equalsIgnoreCase(TRUE);
594
595         dbRelationship = new DbRelationship(name);
596         dbRelationship.setSourceEntity(source);
597         dbRelationship.setTargetEntityName(atts.getValue("", "target"));
598         dbRelationship.setToMany(toMany);
599         dbRelationship.setToDependentPK(toDependentPK);
600
601         source.addRelationship(dbRelationship);
602     }
603
604     private void processStartDbRelationshipRef(Attributes JavaDoc atts) throws SAXException JavaDoc {
605         // db-relationship-ref element is deprecated and is supported for backwards
606
// compatibility only
607

608         String JavaDoc name = atts.getValue("", "name");
609         if (name == null) {
610             throw new SAXException JavaDoc(
611                 "MapLoaderImpl::processStartDbRelationshipRef()"
612                     + ", Null DbRelationship name for "
613                     + objRelationship.getName());
614         }
615
616         String JavaDoc path = objRelationship.getDbRelationshipPath();
617         path = (path != null) ? path + "." + name : name;
618         objRelationship.setDbRelationshipPath(path);
619     }
620
621     private void processStartDbAttributePair(Attributes JavaDoc atts) throws SAXException JavaDoc {
622         DbJoin join = new DbJoin(dbRelationship);
623         join.setSourceName(atts.getValue("", "source"));
624         join.setTargetName(atts.getValue("", "target"));
625         dbRelationship.addJoin(join);
626     }
627
628     private void processStartObjRelationship(Attributes JavaDoc atts) throws SAXException JavaDoc {
629         String JavaDoc name = atts.getValue("", "name");
630         if (null == name) {
631             throw new SAXException JavaDoc(
632                 "MapLoaderImpl::processStartObjRelationship(),"
633                     + " Unable to parse target. Attributes:\n"
634                     + printAttributes(atts).toString());
635         }
636
637         String JavaDoc sourceName = atts.getValue("", "source");
638         if (sourceName == null) {
639             throw new SAXException JavaDoc(
640                 "MapLoaderImpl::processStartObjRelationship(),"
641                     + " Unable to parse source. Attributes:\n"
642                     + printAttributes(atts).toString());
643         }
644
645         ObjEntity source = dataMap.getObjEntity(sourceName);
646         if (source == null) {
647             throw new SAXException JavaDoc(
648                 "MapLoaderImpl::processStartObjRelationship(),"
649                     + " Unable to find source "
650                     + sourceName);
651         }
652
653         String JavaDoc deleteRuleName = atts.getValue("", "deleteRule");
654         int deleteRule =
655             (deleteRuleName != null)
656                 ? DeleteRule.deleteRuleForName(deleteRuleName)
657                 : DeleteRule.NO_ACTION;
658
659         objRelationship = new ObjRelationship(name);
660         objRelationship.setSourceEntity(source);
661         objRelationship.setTargetEntityName(atts.getValue("", "target"));
662         objRelationship.setDeleteRule(deleteRule);
663         objRelationship.setUsedForLocking(
664             TRUE.equalsIgnoreCase(atts.getValue("", "lock")));
665         objRelationship.setDbRelationshipPath((atts.getValue("", "db-relationship-path")));
666         source.addRelationship(objRelationship);
667     }
668
669     private void processStartProcedure(Attributes JavaDoc attributes) throws SAXException JavaDoc {
670
671         String JavaDoc name = attributes.getValue("", "name");
672         if (null == name) {
673             throw new SAXException JavaDoc(
674                 "MapLoaderImpl::processStartProcedure()," + " no procedure name.");
675         }
676
677         String JavaDoc schema = attributes.getValue("", "schema");
678         String JavaDoc catalog = attributes.getValue("", "catalog");
679         String JavaDoc returningValue = attributes.getValue("", "returningValue");
680
681         procedure = new Procedure(name);
682         procedure.setReturningValue(
683             returningValue != null && returningValue.equalsIgnoreCase(TRUE));
684         procedure.setSchema(schema);
685         procedure.setCatalog(catalog);
686         dataMap.addProcedure(procedure);
687     }
688
689     private void processStartProcedureParameter(Attributes JavaDoc attributes)
690         throws SAXException JavaDoc {
691
692         String JavaDoc name = attributes.getValue("", "name");
693         if (name == null) {
694             throw new SAXException JavaDoc(
695                 "MapLoaderImpl::processStartProcedureParameter(),"
696                     + " no procedure parameter name.");
697         }
698
699         ProcedureParameter parameter = new ProcedureParameter(name);
700
701         String JavaDoc type = attributes.getValue("", "type");
702         if (type != null) {
703             parameter.setType(TypesMapping.getSqlTypeByName(type));
704         }
705
706         String JavaDoc length = attributes.getValue("", "length");
707         if (length != null) {
708             parameter.setMaxLength(Integer.parseInt(length));
709         }
710
711         String JavaDoc precision = attributes.getValue("", "precision");
712         if (precision != null) {
713             parameter.setPrecision(Integer.parseInt(precision));
714         }
715
716         String JavaDoc direction = attributes.getValue("", "direction");
717         if ("in".equals(direction)) {
718             parameter.setDirection(ProcedureParameter.IN_PARAMETER);
719         }
720         else if ("out".equals(direction)) {
721             parameter.setDirection(ProcedureParameter.OUT_PARAMETER);
722         }
723         else if ("in_out".equals(direction)) {
724             parameter.setDirection(ProcedureParameter.IN_OUT_PARAMETER);
725         }
726
727         procedure.addCallParameter(parameter);
728     }
729
730     private void processStartQuery(Attributes JavaDoc attributes) throws SAXException JavaDoc {
731         String JavaDoc name = attributes.getValue("", "name");
732         if (null == name) {
733             throw new SAXException JavaDoc("MapLoader::processStartQuery(), no query name.");
734         }
735
736         String JavaDoc builder = attributes.getValue("", "factory");
737
738         // TODO: this is a hack to migrate between 1.1M6 and 1.1M7...
739
// remove this at some point
740
if (builder == null) {
741             builder = SelectQueryBuilder.class.getName();
742         }
743         else if (builder.equals("org.objectstyle.cayenne.query.SelectQueryBuilder")) {
744             builder = SelectQueryBuilder.class.getName();
745         }
746
747         try {
748             queryBuilder = (QueryBuilder) Class.forName(builder).newInstance();
749         }
750         catch (Exception JavaDoc ex) {
751             throw new SAXException JavaDoc(
752                     "MapLoader::processStartQuery(), invalid query builder: " + builder);
753         }
754
755         String JavaDoc rootType = attributes.getValue("", "root");
756         String JavaDoc rootName = attributes.getValue("", "root-name");
757         String JavaDoc resultType = attributes.getValue("", "result-type");
758         String JavaDoc selecting = attributes.getValue("", "selecting");
759
760         queryBuilder.setName(name);
761         queryBuilder.setRoot(dataMap, rootType, rootName);
762         queryBuilder.setSelecting(selecting);
763         queryBuilder.setResultType(resultType);
764     }
765
766     private void processStartQueryProperty(Attributes JavaDoc attributes) throws SAXException JavaDoc {
767         String JavaDoc name = attributes.getValue("", "name");
768         if (null == name) {
769             throw new SAXException JavaDoc("MapLoader::processStartQueryProperty(), no property name.");
770         }
771
772         String JavaDoc value = attributes.getValue("", "value");
773         if (null == value) {
774             throw new SAXException JavaDoc("MapLoader::processStartQueryProperty(), no property value.");
775         }
776
777         queryBuilder.addProperty(name, value);
778     }
779     
780     private void processStartDataMapProperty(Attributes JavaDoc attributes) throws SAXException JavaDoc {
781         String JavaDoc name = attributes.getValue("", "name");
782         if (null == name) {
783             throw new SAXException JavaDoc("MapLoader::processStartDataMapProperty(), no property name.");
784         }
785
786         String JavaDoc value = attributes.getValue("", "value");
787         if (null == value) {
788             throw new SAXException JavaDoc("MapLoader::processStartDataMapProperty(), no property value.");
789         }
790
791         if(mapProperties == null) {
792             mapProperties = new TreeMap JavaDoc();
793         }
794         
795         mapProperties.put(name, value);
796     }
797
798     private void processStartQueryResultColumn(Attributes JavaDoc attributes)
799         throws SAXException JavaDoc {
800         String JavaDoc label = attributes.getValue("", "label");
801         if (label == null) {
802             throw new SAXException JavaDoc("MapLoader::processStartQueryResultColumn(), no label.");
803         }
804
805         String JavaDoc dbType = attributes.getValue("", "db-type");
806         if (dbType == null) {
807             throw new SAXException JavaDoc("MapLoader::processStartQueryResultColumn(), no db-type.");
808         }
809
810         String JavaDoc javaType = attributes.getValue("", "java-type");
811         if (javaType == null) {
812             throw new SAXException JavaDoc("MapLoader::processStartQueryResultColumn(), no java-type.");
813         }
814
815         queryBuilder.addResultColumn(label, dbType, javaType);
816     }
817
818     private void processEndQueryPrefetch() throws SAXException JavaDoc {
819         queryBuilder.addPrefetch(charactersBuffer.toString());
820     }
821
822     private void processStartQueryOrdering(Attributes JavaDoc attributes) throws SAXException JavaDoc {
823         descending = attributes.getValue("", "descending");
824         ignoreCase = attributes.getValue("", "ignore-case");
825     }
826
827     private void processEndQuery() throws SAXException JavaDoc {
828         dataMap.addQuery(queryBuilder.getQuery());
829         queryBuilder = null;
830     }
831
832     private void processEndQuerySQL() throws SAXException JavaDoc {
833         queryBuilder.addSql(charactersBuffer.toString(), sqlKey);
834         sqlKey = null;
835     }
836
837     private void processEndQualifier() throws SAXException JavaDoc {
838         String JavaDoc qualifier = charactersBuffer.toString();
839         if (qualifier.trim().length() == 0) {
840             return;
841         }
842
843         // qualifier can belong to ObjEntity or a query
844
if (objEntity != null) {
845             objEntity.setDeclaredQualifier(Expression.fromString(qualifier));
846         }
847         else {
848             queryBuilder.setQualifier(qualifier);
849         }
850     }
851
852     private void processEndQueryOrdering() throws SAXException JavaDoc {
853         String JavaDoc path = charactersBuffer.toString();
854         queryBuilder.addOrdering(path, descending, ignoreCase);
855     }
856
857     private void processEndDbAttribute() throws SAXException JavaDoc {
858         attrib = null;
859     }
860
861     private void processEndDbEntity() {
862         dbEntity = null;
863     }
864
865     private void processEndProcedure() {
866         procedure = null;
867     }
868
869     private void processEndProcedureParameter() {
870     }
871
872     private void processEndDbGeneratorType() {
873         if (dbEntity == null)
874             return;
875         DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator();
876         if (pkGenerator == null)
877             return;
878         pkGenerator.setGeneratorType(charactersBuffer.toString());
879         if (pkGenerator.getGeneratorType() == null) {
880             dbEntity.setPrimaryKeyGenerator(null);
881         }
882     }
883
884     private void processEndDbGeneratorName() {
885         if (dbEntity == null)
886             return;
887         DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator();
888         if (pkGenerator == null)
889             return;
890         pkGenerator.setGeneratorName(charactersBuffer.toString());
891     }
892
893     private void processEndDbKeyCacheSize() {
894         if (dbEntity == null)
895             return;
896         DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator();
897         if (pkGenerator == null)
898             return;
899         try {
900             pkGenerator.setKeyCacheSize(new Integer JavaDoc(charactersBuffer.toString().trim()));
901         }
902         catch (Exception JavaDoc ex) {
903             pkGenerator.setKeyCacheSize(null);
904         }
905     }
906     
907     private void processEndDataMap() {
908         if(mapProperties != null) {
909             dataMap.initWithProperties(mapProperties);
910         }
911         
912         mapProperties = null;
913     }
914
915     private void processEndObjEntity() {
916         objEntity = null;
917     }
918
919     private void processEndDbRelationship() {
920         dbRelationship = null;
921     }
922
923     private void processEndObjRelationship() {
924         objRelationship = null;
925     }
926
927     /** Prints the attributes. Used for error reporting purposes.*/
928     private StringBuffer JavaDoc printAttributes(Attributes JavaDoc atts) {
929         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
930         String JavaDoc name, value;
931         for (int i = 0; i < atts.getLength(); i++) {
932             value = atts.getQName(i);
933             name = atts.getValue(i);
934             sb.append("Name: " + name + "\tValue: " + value + "\n");
935         }
936         return sb;
937     }
938
939     public void characters(char[] text, int start, int length)
940         throws org.xml.sax.SAXException JavaDoc {
941         if (charactersBuffer != null) {
942             charactersBuffer.append(text, start, length);
943         }
944     }
945
946     private void rememberCurrentTag(String JavaDoc tag) {
947         currentTag = tag;
948     }
949
950     private void resetCurrentTag() {
951         currentTag = null;
952     }
953 }
954
Popular Tags