Source Code Cross Referenced for SqlMapParser.java in  » Database-ORM » iBATIS » com » ibatis » sqlmap » engine » builder » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » iBATIS » com.ibatis.sqlmap.engine.builder.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ibatis.sqlmap.engine.builder.xml;
002:
003:        import com.ibatis.common.logging.Log;
004:        import com.ibatis.common.logging.LogFactory;
005:        import com.ibatis.common.resources.Resources;
006:        import com.ibatis.common.xml.Nodelet;
007:        import com.ibatis.common.xml.NodeletException;
008:        import com.ibatis.common.xml.NodeletParser;
009:        import com.ibatis.common.xml.NodeletUtils;
010:
011:        import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
012:        import com.ibatis.sqlmap.engine.cache.CacheModel;
013:        import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
014:        import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMapping;
015:        import com.ibatis.sqlmap.engine.mapping.result.*;
016:        import com.ibatis.sqlmap.engine.mapping.statement.*;
017:        import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
018:        import com.ibatis.sqlmap.engine.type.TypeHandler;
019:        import org.w3c.dom.Node;
020:
021:        import java.io.InputStream;
022:        import java.io.Reader;
023:        import java.util.ArrayList;
024:        import java.util.List;
025:        import java.util.Properties;
026:        import java.util.StringTokenizer;
027:        import java.util.Iterator;
028:
029:        public class SqlMapParser extends BaseParser {
030:
031:            private static final Log log = LogFactory
032:                    .getLog(SqlMapParser.class);
033:
034:            protected final NodeletParser parser = new NodeletParser();
035:
036:            public SqlMapParser(Variables vars) {
037:                super (vars);
038:                parser.setValidation(true);
039:                parser.setEntityResolver(new SqlMapClasspathEntityResolver());
040:
041:                addSqlMapNodelets();
042:                addSqlNodelets();
043:                addTypeAliasNodelets();
044:                addCacheModelNodelets();
045:                addParameterMapNodelets();
046:                addResultMapNodelets();
047:                addStatementNodelets();
048:
049:            }
050:
051:            public void parse(Reader reader) throws NodeletException {
052:                parser.parse(reader);
053:            }
054:
055:            public void parse(InputStream inputStream) throws NodeletException {
056:                parser.parse(inputStream);
057:            }
058:
059:            private void addSqlMapNodelets() {
060:                parser.addNodelet("/sqlMap", new Nodelet() {
061:                    public void process(Node node) throws Exception {
062:                        Properties attributes = NodeletUtils.parseAttributes(
063:                                node, vars.properties);
064:                        vars.currentNamespace = attributes
065:                                .getProperty("namespace");
066:                    }
067:                });
068:                parser.addNodelet("/sqlMap/end()", new Nodelet() {
069:                    public void process(Node node) throws Exception {
070:                        Iterator names = vars.delegate.getResultMapNames();
071:                        while (names.hasNext()) {
072:                            String name = (String) names.next();
073:                            ResultMap rm = vars.delegate.getResultMap(name);
074:                            Discriminator disc = rm.getDiscriminator();
075:                            if (disc != null) {
076:                                disc.bindSubMaps();
077:                            }
078:                        }
079:                    }
080:                });
081:            }
082:
083:            private void addSqlNodelets() {
084:                parser.addNodelet("/sqlMap/sql", new Nodelet() {
085:                    public void process(Node node) throws Exception {
086:                        Properties attributes = NodeletUtils.parseAttributes(
087:                                node, vars.properties);
088:                        String id = attributes.getProperty("id");
089:                        if (vars.useStatementNamespaces) {
090:                            id = applyNamespace(id);
091:                        }
092:                        if (vars.sqlIncludes.containsKey(id)) {
093:                            // To be upgraded to throwing of a RuntimeException later on
094:                            log.warn("Duplicate <sql>-include '" + id
095:                                    + "' found.");
096:                        } else {
097:                            vars.sqlIncludes.put(id, node);
098:                        }
099:                    }
100:                });
101:            }
102:
103:            private void addTypeAliasNodelets() {
104:                parser.addNodelet("/sqlMap/typeAlias", new Nodelet() {
105:                    public void process(Node node) throws Exception {
106:                        Properties prop = NodeletUtils.parseAttributes(node,
107:                                vars.properties);
108:                        String alias = prop.getProperty("alias");
109:                        String type = prop.getProperty("type");
110:                        vars.typeHandlerFactory.putTypeAlias(alias, type);
111:                    }
112:                });
113:            }
114:
115:            private void addCacheModelNodelets() {
116:                parser.addNodelet("/sqlMap/cacheModel", new Nodelet() {
117:                    public void process(Node node) throws Exception {
118:                        vars.currentCacheModel = new CacheModel();
119:                        vars.currentProperties = new Properties();
120:                    }
121:                });
122:                parser.addNodelet("/sqlMap/cacheModel/end()", new Nodelet() {
123:                    public void process(Node node) throws Exception {
124:                        vars.errorCtx.setActivity("building a cache model");
125:
126:                        Properties attributes = NodeletUtils.parseAttributes(
127:                                node, vars.properties);
128:                        String id = applyNamespace(attributes.getProperty("id"));
129:                        String type = attributes.getProperty("type");
130:                        type = vars.typeHandlerFactory.resolveAlias(type);
131:
132:                        String readOnly = attributes.getProperty("readOnly");
133:                        if (readOnly != null && readOnly.length() > 0) {
134:                            vars.currentCacheModel.setReadOnly("true"
135:                                    .equals(readOnly));
136:                        } else {
137:                            vars.currentCacheModel.setReadOnly(true);
138:                        }
139:
140:                        String serialize = attributes.getProperty("serialize");
141:                        if (serialize != null && serialize.length() > 0) {
142:                            vars.currentCacheModel.setSerialize("true"
143:                                    .equals(serialize));
144:                        } else {
145:                            vars.currentCacheModel.setSerialize(false);
146:                        }
147:
148:                        vars.errorCtx.setObjectId(id + " cache model");
149:
150:                        vars.errorCtx
151:                                .setMoreInfo("Check the cache model type.");
152:                        vars.currentCacheModel.setId(id);
153:                        vars.currentCacheModel.setResource(vars.errorCtx
154:                                .getResource());
155:
156:                        try {
157:                            vars.currentCacheModel.setControllerClassName(type);
158:                        } catch (Exception e) {
159:                            throw new RuntimeException(
160:                                    "Error setting Cache Controller Class.  Cause: "
161:                                            + e, e);
162:                        }
163:
164:                        vars.errorCtx
165:                                .setMoreInfo("Check the cache model configuration.");
166:                        vars.currentCacheModel
167:                                .configure(vars.currentProperties);
168:
169:                        if (vars.client.getDelegate().isCacheModelsEnabled()) {
170:                            vars.client.getDelegate().addCacheModel(
171:                                    vars.currentCacheModel);
172:                        }
173:
174:                        vars.errorCtx.setMoreInfo(null);
175:                        vars.errorCtx.setObjectId(null);
176:                        vars.currentProperties = null;
177:                        vars.currentCacheModel = null;
178:                    }
179:                });
180:                parser.addNodelet("/sqlMap/cacheModel/property", new Nodelet() {
181:                    public void process(Node node) throws Exception {
182:                        vars.errorCtx
183:                                .setMoreInfo("Check the cache model properties.");
184:                        Properties attributes = NodeletUtils.parseAttributes(
185:                                node, vars.properties);
186:                        String name = attributes.getProperty("name");
187:                        String value = NodeletUtils.parsePropertyTokens(
188:                                attributes.getProperty("value"),
189:                                vars.properties);
190:                        vars.currentProperties.put(name, value);
191:                    }
192:                });
193:                parser.addNodelet("/sqlMap/cacheModel/flushOnExecute",
194:                        new Nodelet() {
195:                            public void process(Node node) throws Exception {
196:                                vars.errorCtx
197:                                        .setMoreInfo("Check the cache model flush on statement elements.");
198:                                Properties childAttributes = NodeletUtils
199:                                        .parseAttributes(node, vars.properties);
200:                                vars.currentCacheModel
201:                                        .addFlushTriggerStatement(childAttributes
202:                                                .getProperty("statement"));
203:                            }
204:                        });
205:                parser.addNodelet("/sqlMap/cacheModel/flushInterval",
206:                        new Nodelet() {
207:                            public void process(Node node) throws Exception {
208:                                Properties childAttributes = NodeletUtils
209:                                        .parseAttributes(node, vars.properties);
210:                                long t = 0;
211:                                try {
212:                                    vars.errorCtx
213:                                            .setMoreInfo("Check the cache model flush interval.");
214:                                    String milliseconds = childAttributes
215:                                            .getProperty("milliseconds");
216:                                    String seconds = childAttributes
217:                                            .getProperty("seconds");
218:                                    String minutes = childAttributes
219:                                            .getProperty("minutes");
220:                                    String hours = childAttributes
221:                                            .getProperty("hours");
222:                                    if (milliseconds != null)
223:                                        t += Integer.parseInt(milliseconds);
224:                                    if (seconds != null)
225:                                        t += Integer.parseInt(seconds) * 1000;
226:                                    if (minutes != null)
227:                                        t += Integer.parseInt(minutes) * 60 * 1000;
228:                                    if (hours != null)
229:                                        t += Integer.parseInt(hours) * 60 * 60 * 1000;
230:                                    if (t < 1)
231:                                        throw new RuntimeException(
232:                                                "A flush interval must specify one or more of milliseconds, seconds, minutes or hours.");
233:                                    vars.currentCacheModel.setFlushInterval(t);
234:                                } catch (NumberFormatException e) {
235:                                    throw new RuntimeException(
236:                                            "Error building cache '"
237:                                                    + vars.currentCacheModel
238:                                                            .getId()
239:                                                    + "' in '"
240:                                                    + "resourceNAME"
241:                                                    + "'.  Flush interval milliseconds must be a valid long integer value.  Cause: "
242:                                                    + e, e);
243:                                }
244:                            }
245:                        });
246:            }
247:
248:            private void addParameterMapNodelets() {
249:                parser.addNodelet("/sqlMap/parameterMap/end()", new Nodelet() {
250:                    public void process(Node node) throws Exception {
251:
252:                        vars.currentParameterMap
253:                                .setParameterMappingList(vars.parameterMappingList);
254:
255:                        vars.client.getDelegate().addParameterMap(
256:                                vars.currentParameterMap);
257:
258:                        vars.errorCtx.setMoreInfo(null);
259:                        vars.errorCtx.setObjectId(null);
260:                    }
261:                });
262:                parser.addNodelet("/sqlMap/parameterMap", new Nodelet() {
263:                    public void process(Node node) throws Exception {
264:                        vars.errorCtx.setActivity("building a parameter map");
265:
266:                        vars.currentParameterMap = new BasicParameterMap(
267:                                vars.client.getDelegate());
268:
269:                        Properties attributes = NodeletUtils.parseAttributes(
270:                                node, vars.properties);
271:                        String id = applyNamespace(attributes.getProperty("id"));
272:                        String parameterClassName = attributes
273:                                .getProperty("class");
274:                        parameterClassName = vars.typeHandlerFactory
275:                                .resolveAlias(parameterClassName);
276:
277:                        vars.currentParameterMap.setId(id);
278:                        vars.currentParameterMap.setResource(vars.errorCtx
279:                                .getResource());
280:
281:                        vars.errorCtx.setObjectId(id + " parameter map");
282:
283:                        Class parameterClass = null;
284:                        try {
285:                            vars.errorCtx
286:                                    .setMoreInfo("Check the parameter class.");
287:                            parameterClass = Resources
288:                                    .classForName(parameterClassName);
289:                        } catch (Exception e) {
290:                            //TODO: Why is this commented out?
291:                            //throw new SqlMapException("Error configuring ParameterMap.  Could not set ParameterClass.  Cause: " + e, e);
292:                        }
293:
294:                        vars.currentParameterMap
295:                                .setParameterClass(parameterClass);
296:
297:                        vars.parameterMappingList = new ArrayList();
298:
299:                        vars.errorCtx
300:                                .setMoreInfo("Check the parameter mappings.");
301:                    }
302:                });
303:                parser.addNodelet("/sqlMap/parameterMap/parameter",
304:                        new Nodelet() {
305:                            public void process(Node node) throws Exception {
306:                                Properties childAttributes = NodeletUtils
307:                                        .parseAttributes(node, vars.properties);
308:                                String propertyName = childAttributes
309:                                        .getProperty("property");
310:                                String jdbcType = childAttributes
311:                                        .getProperty("jdbcType");
312:                                String type = childAttributes
313:                                        .getProperty("typeName");
314:                                String javaType = childAttributes
315:                                        .getProperty("javaType");
316:                                String resultMap = childAttributes
317:                                        .getProperty("resultMap");
318:                                String nullValue = childAttributes
319:                                        .getProperty("nullValue");
320:                                String mode = childAttributes
321:                                        .getProperty("mode");
322:                                String callback = childAttributes
323:                                        .getProperty("typeHandler");
324:                                String numericScale = childAttributes
325:                                        .getProperty("numericScale");
326:
327:                                callback = vars.typeHandlerFactory
328:                                        .resolveAlias(callback);
329:                                javaType = vars.typeHandlerFactory
330:                                        .resolveAlias(javaType);
331:                                resultMap = applyNamespace(resultMap);
332:
333:                                vars.errorCtx.setObjectId(propertyName
334:                                        + " mapping of the "
335:                                        + vars.currentParameterMap.getId()
336:                                        + " parameter map");
337:
338:                                TypeHandler handler = null;
339:                                if (callback != null) {
340:                                    vars.errorCtx
341:                                            .setMoreInfo("Check the parameter mapping typeHandler attribute '"
342:                                                    + callback
343:                                                    + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
344:                                    try {
345:                                        Object impl = Resources.classForName(
346:                                                callback).newInstance();
347:                                        if (impl instanceof  TypeHandlerCallback) {
348:                                            handler = new CustomTypeHandler(
349:                                                    (TypeHandlerCallback) impl);
350:                                        } else if (impl instanceof  TypeHandler) {
351:                                            handler = (TypeHandler) impl;
352:                                        } else {
353:                                            throw new RuntimeException(
354:                                                    "The class '"
355:                                                            + callback
356:                                                            + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
357:                                        }
358:                                    } catch (Exception e) {
359:                                        throw new RuntimeException(
360:                                                "Error occurred during custom type handler configuration.  Cause: "
361:                                                        + e, e);
362:                                    }
363:                                } else {
364:                                    vars.errorCtx
365:                                            .setMoreInfo("Check the parameter mapping property type or name.");
366:                                    handler = resolveTypeHandler(vars.client
367:                                            .getDelegate()
368:                                            .getTypeHandlerFactory(),
369:                                            vars.currentParameterMap
370:                                                    .getParameterClass(),
371:                                            propertyName, javaType, jdbcType);
372:                                }
373:
374:                                BasicParameterMapping mapping = new BasicParameterMapping();
375:                                mapping.setPropertyName(propertyName);
376:                                mapping.setJdbcTypeName(jdbcType);
377:                                mapping.setTypeName(type);
378:                                mapping.setResultMapName(resultMap);
379:                                mapping.setNullValue(nullValue);
380:                                if (mode != null && mode.length() > 0) {
381:                                    mapping.setMode(mode);
382:                                }
383:                                mapping.setTypeHandler(handler);
384:                                try {
385:                                    if (javaType != null
386:                                            && javaType.length() > 0) {
387:                                        mapping.setJavaType(Resources
388:                                                .classForName(javaType));
389:                                    }
390:                                } catch (ClassNotFoundException e) {
391:                                    throw new RuntimeException(
392:                                            "Error setting javaType on parameter mapping.  Cause: "
393:                                                    + e);
394:                                }
395:
396:                                if (numericScale != null) {
397:                                    try {
398:                                        Integer scale = Integer
399:                                                .valueOf(numericScale);
400:                                        if (scale.intValue() < 0) {
401:                                            throw new RuntimeException(
402:                                                    "Error setting numericScale on parameter mapping.  Cause: scale must be greater than or equal to zero");
403:                                        }
404:
405:                                        mapping.setNumericScale(scale);
406:                                    } catch (NumberFormatException e) {
407:                                        throw new RuntimeException(
408:                                                "Error setting numericScale on parameter mapping.  Cause: "
409:                                                        + numericScale
410:                                                        + " is not a valid integer");
411:                                    }
412:                                }
413:
414:                                vars.parameterMappingList.add(mapping);
415:
416:                            }
417:                        });
418:            }
419:
420:            private void addResultMapNodelets() {
421:                parser.addNodelet("/sqlMap/resultMap/end()", new Nodelet() {
422:                    public void process(Node node) throws Exception {
423:
424:                        if (vars.resultMappingList.size() == 0) {
425:                            throw new RuntimeException("resultMap "
426:                                    + vars.currentResultMap.getId()
427:                                    + " must have at least one result mapping");
428:                        }
429:
430:                        vars.currentResultMap
431:                                .setResultMappingList(vars.resultMappingList);
432:
433:                        vars.currentResultMap
434:                                .setDiscriminator(vars.discriminator);
435:                        vars.discriminator = null;
436:
437:                        vars.client.getDelegate().addResultMap(
438:                                vars.currentResultMap);
439:
440:                        vars.errorCtx.setMoreInfo(null);
441:
442:                        vars.errorCtx.setObjectId(null);
443:                    }
444:                });
445:                parser.addNodelet("/sqlMap/resultMap", new Nodelet() {
446:                    public void process(Node node) throws Exception {
447:                        vars.errorCtx.setActivity("building a result map");
448:
449:                        vars.currentResultMap = new BasicResultMap(vars.client
450:                                .getDelegate());
451:
452:                        Properties attributes = NodeletUtils.parseAttributes(
453:                                node, vars.properties);
454:                        String id = applyNamespace(attributes.getProperty("id"));
455:                        String resultClassName = attributes
456:                                .getProperty("class");
457:                        String extended = applyNamespace(attributes
458:                                .getProperty("extends"));
459:                        String xmlName = attributes.getProperty("xmlName");
460:                        String groupBy = attributes.getProperty("groupBy");
461:                        resultClassName = vars.typeHandlerFactory
462:                                .resolveAlias(resultClassName);
463:
464:                        vars.errorCtx.setObjectId(id + " result map");
465:
466:                        vars.currentResultMap.setId(id);
467:                        vars.currentResultMap.setXmlName(xmlName);
468:                        vars.currentResultMap.setResource(vars.errorCtx
469:                                .getResource());
470:
471:                        if (groupBy != null && groupBy.length() > 0) {
472:                            StringTokenizer parser = new StringTokenizer(
473:                                    groupBy, ", ", false);
474:                            while (parser.hasMoreTokens()) {
475:                                vars.currentResultMap.addGroupByProperty(parser
476:                                        .nextToken());
477:                            }
478:                        }
479:
480:                        Class resultClass = null;
481:                        try {
482:                            vars.errorCtx
483:                                    .setMoreInfo("Check the result class.");
484:                            resultClass = Resources
485:                                    .classForName(resultClassName);
486:                        } catch (Exception e) {
487:                            throw new RuntimeException(
488:                                    "Error configuring Result.  Could not set ResultClass.  Cause: "
489:                                            + e, e);
490:
491:                        }
492:
493:                        vars.currentResultMap.setResultClass(resultClass);
494:
495:                        vars.resultMappingList = new ArrayList();
496:
497:                        vars.errorCtx
498:                                .setMoreInfo("Check the extended result map.");
499:                        if (extended != null) {
500:                            BasicResultMap extendedResultMap = (BasicResultMap) vars.client
501:                                    .getDelegate().getResultMap(extended);
502:                            ResultMapping[] resultMappings = extendedResultMap
503:                                    .getResultMappings();
504:                            for (int i = 0; i < resultMappings.length; i++) {
505:                                vars.resultMappingList.add(resultMappings[i]);
506:                            }
507:
508:                            List nestedResultMappings = extendedResultMap
509:                                    .getNestedResultMappings();
510:                            if (nestedResultMappings != null) {
511:                                Iterator iter = nestedResultMappings.iterator();
512:                                while (iter.hasNext()) {
513:                                    vars.currentResultMap
514:                                            .addNestedResultMappings((ResultMapping) iter
515:                                                    .next());
516:                                }
517:                            }
518:
519:                            if (groupBy == null || groupBy.length() == 0) {
520:                                if (extendedResultMap.hasGroupBy()) {
521:                                    Iterator i = extendedResultMap
522:                                            .groupByProps();
523:                                    while (i.hasNext()) {
524:                                        vars.currentResultMap
525:                                                .addGroupByProperty((String) i
526:                                                        .next());
527:                                    }
528:                                }
529:                            }
530:                        }
531:
532:                        vars.errorCtx.setMoreInfo("Check the result mappings.");
533:                        vars.resultMappingIndex = vars.resultMappingList.size();
534:
535:                    }
536:                });
537:                parser.addNodelet("/sqlMap/resultMap/result", new Nodelet() {
538:                    public void process(Node node) throws Exception {
539:                        Properties childAttributes = NodeletUtils
540:                                .parseAttributes(node, vars.properties);
541:                        String propertyName = childAttributes
542:                                .getProperty("property");
543:                        String nullValue = childAttributes
544:                                .getProperty("nullValue");
545:                        String jdbcType = childAttributes
546:                                .getProperty("jdbcType");
547:                        String javaType = childAttributes
548:                                .getProperty("javaType");
549:                        String columnName = childAttributes
550:                                .getProperty("column");
551:                        String columnIndex = childAttributes
552:                                .getProperty("columnIndex");
553:                        String statementName = childAttributes
554:                                .getProperty("select");
555:                        String resultMapName = childAttributes
556:                                .getProperty("resultMap");
557:                        String callback = childAttributes
558:                                .getProperty("typeHandler");
559:
560:                        callback = vars.typeHandlerFactory
561:                                .resolveAlias(callback);
562:                        javaType = vars.typeHandlerFactory
563:                                .resolveAlias(javaType);
564:
565:                        vars.errorCtx
566:                                .setObjectId(propertyName + " mapping of the "
567:                                        + vars.currentResultMap.getId()
568:                                        + " result map");
569:
570:                        TypeHandler handler = null;
571:                        if (callback != null) {
572:                            vars.errorCtx
573:                                    .setMoreInfo("Check the result mapping typeHandler attribute '"
574:                                            + callback
575:                                            + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
576:                            try {
577:                                Object impl = Resources.classForName(callback)
578:                                        .newInstance();
579:                                if (impl instanceof  TypeHandlerCallback) {
580:                                    handler = new CustomTypeHandler(
581:                                            (TypeHandlerCallback) impl);
582:                                } else if (impl instanceof  TypeHandler) {
583:                                    handler = (TypeHandler) impl;
584:                                } else {
585:                                    throw new RuntimeException(
586:                                            "The class '"
587:                                                    + callback
588:                                                    + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
589:                                }
590:                            } catch (Exception e) {
591:                                throw new RuntimeException(
592:                                        "Error occurred during custom type handler configuration.  Cause: "
593:                                                + e, e);
594:                            }
595:                        } else {
596:                            vars.errorCtx
597:                                    .setMoreInfo("Check the result mapping property type or name.");
598:                            handler = resolveTypeHandler(vars.client
599:                                    .getDelegate().getTypeHandlerFactory(),
600:                                    vars.currentResultMap.getResultClass(),
601:                                    propertyName, javaType, jdbcType, true);
602:                        }
603:
604:                        BasicResultMapping mapping = new BasicResultMapping();
605:                        mapping.setPropertyName(propertyName);
606:                        mapping.setColumnName(columnName);
607:                        mapping.setJdbcTypeName(jdbcType);
608:                        mapping.setTypeHandler(handler);
609:                        mapping.setNullValue(nullValue);
610:                        mapping.setStatementName(statementName);
611:                        mapping.setNestedResultMapName(resultMapName);
612:
613:                        if (resultMapName != null && resultMapName.length() > 0) {
614:                            vars.currentResultMap
615:                                    .addNestedResultMappings(mapping);
616:                        }
617:
618:                        try {
619:                            if (javaType != null && javaType.length() > 0) {
620:                                mapping.setJavaType(Resources
621:                                        .classForName(javaType));
622:                            }
623:                        } catch (ClassNotFoundException e) {
624:                            throw new RuntimeException(
625:                                    "Error setting javaType on result mapping.  Cause: "
626:                                            + e);
627:                        }
628:
629:                        if (columnIndex != null && columnIndex.length() > 0) {
630:                            mapping.setColumnIndex(Integer
631:                                    .parseInt(columnIndex));
632:                        } else {
633:                            vars.resultMappingIndex++;
634:                            mapping.setColumnIndex(vars.resultMappingIndex);
635:                        }
636:
637:                        vars.resultMappingList.add(mapping);
638:                    }
639:                });
640:
641:                parser.addNodelet("/sqlMap/resultMap/discriminator/subMap",
642:                        new Nodelet() {
643:                            public void process(Node node) throws Exception {
644:                                if (vars.discriminator == null) {
645:                                    throw new RuntimeException(
646:                                            "The discriminator is null, but somehow a subMap was reached.  This is a bug.");
647:                                }
648:                                Properties childAttributes = NodeletUtils
649:                                        .parseAttributes(node, vars.properties);
650:                                String value = childAttributes
651:                                        .getProperty("value");
652:                                String resultMap = childAttributes
653:                                        .getProperty("resultMap");
654:                                vars.discriminator.addSubMap(value,
655:                                        applyNamespace(resultMap));
656:                            }
657:                        });
658:
659:                parser.addNodelet("/sqlMap/resultMap/discriminator",
660:                        new Nodelet() {
661:                            public void process(Node node) throws Exception {
662:                                Properties childAttributes = NodeletUtils
663:                                        .parseAttributes(node, vars.properties);
664:                                String nullValue = childAttributes
665:                                        .getProperty("nullValue");
666:                                String jdbcType = childAttributes
667:                                        .getProperty("jdbcType");
668:                                String javaType = childAttributes
669:                                        .getProperty("javaType");
670:                                String columnName = childAttributes
671:                                        .getProperty("column");
672:                                String columnIndex = childAttributes
673:                                        .getProperty("columnIndex");
674:                                String callback = childAttributes
675:                                        .getProperty("typeHandler");
676:
677:                                callback = vars.typeHandlerFactory
678:                                        .resolveAlias(callback);
679:                                javaType = vars.typeHandlerFactory
680:                                        .resolveAlias(javaType);
681:
682:                                TypeHandler handler = null;
683:                                if (callback != null) {
684:                                    vars.errorCtx
685:                                            .setMoreInfo("Check the result mapping typeHandler attribute '"
686:                                                    + callback
687:                                                    + "' (must be a TypeHandlerCallback implementation).");
688:                                    try {
689:                                        Object impl = Resources.classForName(
690:                                                callback).newInstance();
691:                                        if (impl instanceof  TypeHandlerCallback) {
692:                                            handler = new CustomTypeHandler(
693:                                                    (TypeHandlerCallback) impl);
694:                                        } else if (impl instanceof  TypeHandler) {
695:                                            handler = (TypeHandler) impl;
696:                                        } else {
697:                                            throw new RuntimeException(
698:                                                    "The class '' is not a valid implementation of TypeHandler or TypeHandlerCallback");
699:                                        }
700:                                    } catch (Exception e) {
701:                                        throw new RuntimeException(
702:                                                "Error occurred during custom type handler configuration.  Cause: "
703:                                                        + e, e);
704:                                    }
705:                                } else {
706:                                    vars.errorCtx
707:                                            .setMoreInfo("Check the result mapping property type or name.");
708:                                    handler = resolveTypeHandler(vars.client
709:                                            .getDelegate()
710:                                            .getTypeHandlerFactory(),
711:                                            vars.currentResultMap
712:                                                    .getResultClass(), "",
713:                                            javaType, jdbcType, true);
714:                                }
715:
716:                                BasicResultMapping mapping = new BasicResultMapping();
717:                                mapping.setColumnName(columnName);
718:                                mapping.setJdbcTypeName(jdbcType);
719:                                mapping.setTypeHandler(handler);
720:                                mapping.setNullValue(nullValue);
721:
722:                                try {
723:                                    if (javaType != null
724:                                            && javaType.length() > 0) {
725:                                        mapping.setJavaType(Resources
726:                                                .classForName(javaType));
727:                                    }
728:                                } catch (ClassNotFoundException e) {
729:                                    throw new RuntimeException(
730:                                            "Error setting javaType on result mapping.  Cause: "
731:                                                    + e);
732:                                }
733:
734:                                if (columnIndex != null
735:                                        && columnIndex.length() > 0) {
736:                                    mapping.setColumnIndex(Integer
737:                                            .parseInt(columnIndex));
738:                                }
739:
740:                                vars.discriminator = new Discriminator(
741:                                        vars.delegate, mapping);
742:                            }
743:                        });
744:            }
745:
746:            protected void addStatementNodelets() {
747:                parser.addNodelet("/sqlMap/statement", new Nodelet() {
748:                    public void process(Node node) throws Exception {
749:                        vars.currentStatement = new SqlStatementParser(vars)
750:                                .parseGeneralStatement(node,
751:                                        new GeneralStatement());
752:                        vars.delegate.addMappedStatement(vars.currentStatement);
753:                    }
754:                });
755:                parser.addNodelet("/sqlMap/insert", new Nodelet() {
756:                    public void process(Node node) throws Exception {
757:                        vars.currentStatement = new SqlStatementParser(vars)
758:                                .parseGeneralStatement(node,
759:                                        new InsertStatement());
760:                        vars.delegate.addMappedStatement(vars.currentStatement);
761:                    }
762:                });
763:                parser.addNodelet("/sqlMap/update", new Nodelet() {
764:                    public void process(Node node) throws Exception {
765:                        vars.currentStatement = new SqlStatementParser(vars)
766:                                .parseGeneralStatement(node,
767:                                        new UpdateStatement());
768:                        vars.delegate.addMappedStatement(vars.currentStatement);
769:                    }
770:                });
771:                parser.addNodelet("/sqlMap/delete", new Nodelet() {
772:                    public void process(Node node) throws Exception {
773:                        vars.currentStatement = new SqlStatementParser(vars)
774:                                .parseGeneralStatement(node,
775:                                        new DeleteStatement());
776:                        vars.delegate.addMappedStatement(vars.currentStatement);
777:                    }
778:                });
779:                parser.addNodelet("/sqlMap/select", new Nodelet() {
780:                    public void process(Node node) throws Exception {
781:                        vars.currentStatement = new SqlStatementParser(vars)
782:                                .parseGeneralStatement(node,
783:                                        new SelectStatement());
784:                        vars.delegate.addMappedStatement(vars.currentStatement);
785:                    }
786:                });
787:                parser.addNodelet("/sqlMap/procedure", new Nodelet() {
788:                    public void process(Node node) throws Exception {
789:                        vars.currentStatement = new SqlStatementParser(vars)
790:                                .parseGeneralStatement(node,
791:                                        new ProcedureStatement());
792:                        vars.delegate.addMappedStatement(vars.currentStatement);
793:                    }
794:                });
795:            }
796:
797:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.