Source Code Cross Referenced for Transformers.java in  » Database-ORM » MMBase » org » mmbase » util » transformers » 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 » MMBase » org.mmbase.util.transformers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util.transformers;
011:
012:        import java.util.Iterator;
013:        import org.mmbase.util.functions.Parameters;
014:        import org.mmbase.util.logging.Logger;
015:        import org.mmbase.util.logging.Logging;
016:
017:        /**
018:         * Utitilies related to the tranformers of this package.
019:         *
020:         * @author Michiel Meeuwissen
021:         * @since MMBase-1.7
022:         */
023:
024:        public class Transformers {
025:            private static final Logger log = Logging
026:                    .getLoggerInstance(Transformers.class);
027:
028:            /**
029:             * This method instatiates a CharTransformer by use of reflection.
030:             * @param name   The class name for the CharTransformer to be returned
031:             * @param config A configuration string for this transformer. At the moment this can be parsed
032:             *               as an integer, or the name of a integer constant of the transformer's class.
033:             *               Likely, other ways to configure a transformer will be available.
034:             * @param errorId  If something goes wrong, an error message is logged, in which this String is
035:             *                 used, to clear things up.
036:             * @param back   If true, the Transformer will be wrapped in a InverseCharTransformer, so the
037:             *               transformation will do the inverse thing.
038:             * @return A CharTransformer instance or null in case of an error.
039:             */
040:
041:            public static CharTransformer getCharTransformer(String name,
042:                    String config, String errorId, boolean back) {
043:                Class<?> clazz;
044:                try {
045:                    clazz = Class.forName(name);
046:                } catch (ClassNotFoundException ex) {
047:                    log.error("Class " + name + " specified for " + errorId
048:                            + " could not be found");
049:                    return null;
050:                }
051:
052:                if (!Transformer.class.isAssignableFrom(clazz)
053:                        && !ParameterizedTransformerFactory.class
054:                                .isAssignableFrom(clazz)) {
055:                    log.error("The class " + clazz + " specified for "
056:                            + errorId + " is not a Transformer");
057:                    return null;
058:                }
059:                Object t;
060:                try {
061:                    t = clazz.newInstance();
062:                } catch (Exception ex) {
063:                    log.error("Error instantiating a " + clazz + ": "
064:                            + ex.toString());
065:                    return null;
066:                }
067:                if (t instanceof  ParameterizedTransformerFactory) {
068:                    ParameterizedTransformerFactory pt = (ParameterizedTransformerFactory) t;
069:                    Parameters params = pt.createParameters();
070:                    Iterator<String> it = org.mmbase.util.StringSplitter.split(
071:                            config).iterator();
072:                    int i = 0;
073:                    while (it.hasNext()) {
074:                        params.set(i++, it.next());
075:                    }
076:                    config = "";
077:                    t = pt.createTransformer(params);
078:                }
079:                CharTransformer ct;
080:
081:                if (t instanceof  CharTransformer) {
082:                    ct = (CharTransformer) t;
083:                } else if (t instanceof  ByteToCharTransformer) {
084:                    log.debug("making a ByteCharTransformer");
085:                    ct = new ByteCharTransformer((ByteToCharTransformer) t);
086:                } else {
087:                    log
088:                            .error("The class "
089:                                    + clazz
090:                                    + " specified for "
091:                                    + errorId
092:                                    + " is not a CharTransformer or a ByteToCharTransformer");
093:                    return null;
094:                }
095:
096:                if (config == null)
097:                    config = "";
098:                if (ct instanceof  ConfigurableTransformer) {
099:                    log.debug("Trying to configure with '" + config + "'");
100:                    if (!config.equals("")) {
101:                        int conf;
102:                        try {
103:                            log.debug("With int");
104:                            conf = Integer.parseInt(config);
105:                        } catch (NumberFormatException nfe) {
106:                            try {
107:                                log.debug("With static field");
108:                                conf = clazz.getField(config).getInt(null);
109:                            } catch (Exception nsfe) {
110:                                log.error("Type " + errorId
111:                                        + " is not well configured : "
112:                                        + nfe.toString() + " and "
113:                                        + nsfe.toString());
114:                                return null;
115:                            }
116:                        }
117:                        ((ConfigurableTransformer) ct).configure(conf);
118:                    }
119:                } else {
120:                    if (!config.equals("")) {
121:                        log
122:                                .warn("Tried to configure non-configurable transformer "
123:                                        + name);
124:                    }
125:                }
126:                if (back) {
127:                    ct = new InverseCharTransformer(ct);
128:                }
129:                return ct;
130:            }
131:
132:            /**
133:             * @since MMBase-1.8
134:             */
135:
136:            public static ParameterizedTransformerFactory getTransformerFactory(
137:                    String name, String errorId) {
138:                Class<?> clazz;
139:                try {
140:                    clazz = Class.forName(name);
141:                } catch (ClassNotFoundException ex) {
142:                    log.error("Class " + name + " specified for " + errorId
143:                            + " could not be found");
144:                    return null;
145:                }
146:                if (!ParameterizedTransformerFactory.class
147:                        .isAssignableFrom(clazz)) {
148:                    log.error("The class " + clazz + " specified for "
149:                            + errorId
150:                            + " is not a ParamerizedTransformerFactory");
151:                    return null;
152:                }
153:                ParameterizedTransformerFactory fact;
154:                try {
155:                    fact = (ParameterizedTransformerFactory) clazz
156:                            .newInstance();
157:                } catch (Exception ex) {
158:                    log.error("Error instantiating a " + clazz + ": "
159:                            + ex.toString());
160:                    return null;
161:                }
162:
163:                return fact;
164:            }
165:
166:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.